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
#![allow(dead_code)]
mod econ;
use crate::{
config::CONFIG,
sim::WorldSim,
site::{namegen::NameGen, Castle, Settlement, Site as WorldSite, Tree},
site2,
util::{attempt, seed_expan, DHashMap, NEIGHBORS},
Index, IndexRef, Land,
};
use common::{
astar::Astar,
calendar::Calendar,
path::Path,
spiral::Spiral2d,
store::{Id, Store},
terrain::{
uniform_idx_as_vec2, BiomeKind, CoordinateConversions, MapSizeLg, TerrainChunkSize,
TERRAIN_CHUNK_BLOCKS_LG,
},
vol::RectVolSize,
};
use common_base::prof_span;
use core::{fmt, hash::BuildHasherDefault, ops::Range};
use fxhash::FxHasher64;
use rand::prelude::*;
use rand_chacha::ChaChaRng;
use tracing::{debug, info, warn};
use vek::*;
fn initial_civ_count(map_size_lg: MapSizeLg) -> u32 {
// NOTE: since map_size_lg's dimensions must fit in a u16, we can safely add
// them here.
//
// NOTE: 48 at "default" scale of 10 × 10 chunk bits (1024 × 1024 chunks).
let cnt = (3 << (map_size_lg.vec().x + map_size_lg.vec().y)) >> 16;
cnt.max(1) // we need at least one civ in order to generate a starting site
}
pub struct CaveInfo {
pub location: (Vec2<i32>, Vec2<i32>),
pub name: String,
}
#[derive(Default)]
pub struct Civs {
pub civs: Store<Civ>,
pub places: Store<Place>,
pub pois: Store<PointOfInterest>,
pub tracks: Store<Track>,
/// We use this hasher (FxHasher64) because
/// (1) we don't care about DDOS attacks (ruling out SipHash);
/// (2) we care about determinism across computers (ruling out AAHash);
/// (3) we have 8-byte keys (for which FxHash is fastest).
pub track_map: DHashMap<Id<Site>, DHashMap<Id<Site>, Id<Track>>>,
pub bridges: DHashMap<Vec2<i32>, (Vec2<i32>, Id<Site>)>,
pub sites: Store<Site>,
pub caves: Store<CaveInfo>,
}
// Change this to get rid of particularly horrid seeds
const SEED_SKIP: u8 = 5;
const POI_THINNING_DIST_SQRD: i32 = 300;
pub struct GenCtx<'a, R: Rng> {
sim: &'a mut WorldSim,
rng: R,
}
struct ProximitySpec {
location: Vec2<i32>,
min_distance: Option<i32>,
max_distance: Option<i32>,
}
impl ProximitySpec {
pub fn satisfied_by(&self, site: Vec2<i32>) -> bool {
let distance_squared = site.distance_squared(self.location);
let min_ok = self
.min_distance
.map(|mind| distance_squared > (mind * mind))
.unwrap_or(true);
let max_ok = self
.max_distance
.map(|maxd| distance_squared < (maxd * maxd))
.unwrap_or(true);
min_ok && max_ok
}
pub fn avoid(location: Vec2<i32>, min_distance: i32) -> Self {
ProximitySpec {
location,
min_distance: Some(min_distance),
max_distance: None,
}
}
pub fn be_near(location: Vec2<i32>, max_distance: i32) -> Self {
ProximitySpec {
location,
min_distance: None,
max_distance: Some(max_distance),
}
}
}
struct ProximityRequirementsBuilder {
all_of: Vec<ProximitySpec>,
any_of: Vec<ProximitySpec>,
}
impl ProximityRequirementsBuilder {
pub fn finalize(self, world_dims: &Aabr<i32>) -> ProximityRequirements {
let location_hint = self.location_hint(world_dims);
ProximityRequirements {
all_of: self.all_of,
any_of: self.any_of,
location_hint,
}
}
fn location_hint(&self, world_dims: &Aabr<i32>) -> Aabr<i32> {
let bounding_box_of_point = |point: Vec2<i32>, max_distance: i32| Aabr {
min: Vec2 {
x: point.x - max_distance,
y: point.y - max_distance,
},
max: Vec2 {
x: point.x + max_distance,
y: point.y + max_distance,
},
};
let any_of_hint = self
.any_of
.iter()
.fold(None, |acc, spec| match spec.max_distance {
None => acc,
Some(max_distance) => {
let bounding_box_of_new_point =
bounding_box_of_point(spec.location, max_distance);
match acc {
None => Some(bounding_box_of_new_point),
Some(acc) => Some(acc.union(bounding_box_of_new_point)),
}
},
})
.map(|hint| hint.intersection(*world_dims))
.unwrap_or_else(|| world_dims.to_owned());
let hint = self
.all_of
.iter()
.fold(any_of_hint, |acc, spec| match spec.max_distance {
None => acc,
Some(max_distance) => {
let bounding_box_of_new_point =
bounding_box_of_point(spec.location, max_distance);
acc.intersection(bounding_box_of_new_point)
},
});
hint
}
pub fn new() -> Self {
Self {
all_of: Vec::new(),
any_of: Vec::new(),
}
}
pub fn avoid_all_of(
mut self,
locations: impl Iterator<Item = Vec2<i32>>,
distance: i32,
) -> Self {
let specs = locations.map(|loc| ProximitySpec::avoid(loc, distance));
self.all_of.extend(specs);
self
}
pub fn close_to_one_of(
mut self,
locations: impl Iterator<Item = Vec2<i32>>,
distance: i32,
) -> Self {
let specs = locations.map(|loc| ProximitySpec::be_near(loc, distance));
self.any_of.extend(specs);
self
}
}
struct ProximityRequirements {
all_of: Vec<ProximitySpec>,
any_of: Vec<ProximitySpec>,
location_hint: Aabr<i32>,
}
impl ProximityRequirements {
pub fn satisfied_by(&self, site: Vec2<i32>) -> bool {
if self.location_hint.contains_point(site) {
let all_of_compliance = self.all_of.iter().all(|spec| spec.satisfied_by(site));
let any_of_compliance =
self.any_of.is_empty() || self.any_of.iter().any(|spec| spec.satisfied_by(site));
all_of_compliance && any_of_compliance
} else {
false
}
}
}
impl<'a, R: Rng> GenCtx<'a, R> {
pub fn reseed(&mut self) -> GenCtx<'_, impl Rng> {
let mut entropy = self.rng.gen::<[u8; 32]>();
entropy[0] = entropy[0].wrapping_add(SEED_SKIP); // Skip bad seeds
GenCtx {
sim: self.sim,
rng: ChaChaRng::from_seed(entropy),
}
}
}
#[derive(Debug)]
pub enum WorldCivStage {
/// Civilization creation, how many out of how many civilizations have been
/// generated yet
CivCreation(u32, u32),
SiteGeneration,
}
impl Civs {
pub fn generate(
seed: u32,
sim: &mut WorldSim,
index: &mut Index,
calendar: Option<&Calendar>,
report_stage: &dyn Fn(WorldCivStage),
) -> Self {
prof_span!("Civs::generate");
let mut this = Self::default();
let rng = ChaChaRng::from_seed(seed_expan::rng_state(seed));
let name_rng = rng.clone();
let mut name_ctx = GenCtx { sim, rng: name_rng };
if index.features().peak_naming {
info!("starting peak naming");
this.name_peaks(&mut name_ctx);
}
if index.features().biome_naming {
info!("starting biome naming");
this.name_biomes(&mut name_ctx);
}
let initial_civ_count = initial_civ_count(sim.map_size_lg());
let mut ctx = GenCtx { sim, rng };
// info!("starting cave generation");
// this.generate_caves(&mut ctx);
info!("starting civilisation creation");
prof_span!(guard, "create civs");
for i in 0..initial_civ_count {
prof_span!("create civ");
debug!("Creating civilisation...");
if this.birth_civ(&mut ctx.reseed()).is_none() {
warn!("Failed to find starting site for civilisation.");
}
report_stage(WorldCivStage::CivCreation(i, initial_civ_count));
}
drop(guard);
info!(?initial_civ_count, "all civilisations created");
report_stage(WorldCivStage::SiteGeneration);
prof_span!(guard, "find locations and establish sites");
let world_dims = ctx.sim.get_aabr();
for _ in 0..initial_civ_count * 3 {
attempt(5, || {
let (loc, kind) = match ctx.rng.gen_range(0..116) {
0..=4 => {
if index.features().site2_giant_trees {
(
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
.avoid_all_of(this.tree_enemies(), 40)
.finalize(&world_dims),
&SiteKind::GiantTree,
)?,
SiteKind::GiantTree,
)
} else {
(
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
.avoid_all_of(this.tree_enemies(), 40)
.finalize(&world_dims),
&SiteKind::Tree,
)?,
SiteKind::Tree,
)
}
},
5..=15 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
.avoid_all_of(this.gnarling_enemies(), 40)
.finalize(&world_dims),
&SiteKind::Gnarling,
)?,
SiteKind::Gnarling,
),
16..=20 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
.avoid_all_of(this.chapel_site_enemies(), 40)
.finalize(&world_dims),
&SiteKind::ChapelSite,
)?,
SiteKind::ChapelSite,
),
21..=27 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
.avoid_all_of(this.gnarling_enemies(), 40)
.finalize(&world_dims),
&SiteKind::Adlet,
)?,
SiteKind::Adlet,
),
28..=38 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
.avoid_all_of(this.pirate_hideout_enemies(), 40)
.finalize(&world_dims),
&SiteKind::PirateHideout,
)?,
SiteKind::PirateHideout,
),
39..=45 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
.avoid_all_of(this.jungle_ruin_enemies(), 40)
.finalize(&world_dims),
&SiteKind::JungleRuin,
)?,
SiteKind::JungleRuin,
),
46..=55 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
.avoid_all_of(this.rock_circle_enemies(), 40)
.finalize(&world_dims),
&SiteKind::RockCircle,
)?,
SiteKind::RockCircle,
),
56..=66 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
.avoid_all_of(this.troll_cave_enemies(), 40)
.finalize(&world_dims),
&SiteKind::TrollCave,
)?,
SiteKind::TrollCave,
),
67..=72 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
.avoid_all_of(this.camp_enemies(), 40)
.finalize(&world_dims),
&SiteKind::Camp,
)?,
SiteKind::Camp,
),
73..=76 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
.avoid_all_of(this.mine_site_enemies(), 40)
.finalize(&world_dims),
&SiteKind::Haniwa,
)?,
SiteKind::Haniwa,
),
77..=81 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
.avoid_all_of(this.terracotta_enemies(), 40)
.finalize(&world_dims),
&SiteKind::Terracotta,
)?,
SiteKind::Terracotta,
),
82..=87 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
.avoid_all_of(this.mine_site_enemies(), 40)
.finalize(&world_dims),
&SiteKind::DwarvenMine,
)?,
SiteKind::DwarvenMine,
),
88..=91 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
.avoid_all_of(this.cultist_enemies(), 40)
.finalize(&world_dims),
&SiteKind::Cultist,
)?,
SiteKind::Cultist,
),
92..=96 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
.avoid_all_of(this.sahagin_enemies(), 40)
.finalize(&world_dims),
&SiteKind::Sahagin,
)?,
SiteKind::Sahagin,
),
97..=102 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
.avoid_all_of(this.vampire_castle_enemies(), 40)
.finalize(&world_dims),
&SiteKind::VampireCastle,
)?,
SiteKind::VampireCastle,
),
103..108 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new().finalize(&world_dims),
&SiteKind::GliderCourse,
)?,
SiteKind::GliderCourse,
),
/*103..=108 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
.avoid_all_of(this.castle_enemies(), 40)
.close_to_one_of(this.towns(), 20)
.finalize(&world_dims),
&SiteKind::Castle,
)?,
SiteKind::Castle,
),
109..=114 => (SiteKind::Citadel, (&castle_enemies, 20)),
*/
_ => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
.avoid_all_of(this.myrmidon_enemies(), 40)
.finalize(&world_dims),
&SiteKind::Myrmidon,
)?,
SiteKind::Myrmidon,
),
};
Some(this.establish_site(&mut ctx.reseed(), loc, |place| Site {
kind,
center: loc,
place,
site_tmp: None,
}))
});
}
drop(guard);
// Tick
//=== old economy is gone
// Flatten ground around sites
prof_span!(guard, "Flatten ground around sites");
for site in this.sites.values() {
let wpos = site.center * TerrainChunkSize::RECT_SIZE.map(|e: u32| e as i32);
let (radius, flatten_radius) = match &site.kind {
SiteKind::Settlement => (32i32, 10.0f32),
SiteKind::Castle => (16i32, 5.0),
SiteKind::Refactor => (32i32, 10.0),
SiteKind::CliffTown => (2i32, 1.0),
SiteKind::SavannahTown => (48i32, 25.0),
SiteKind::CoastalTown => (64i32, 35.0),
SiteKind::JungleRuin => (8i32, 3.0),
SiteKind::DesertCity => (64i32, 25.0),
SiteKind::ChapelSite => (36i32, 10.0),
SiteKind::Terracotta => (64i32, 35.0),
SiteKind::Tree => (12i32, 8.0),
SiteKind::GiantTree => (12i32, 8.0),
SiteKind::Gnarling => (16i32, 10.0),
SiteKind::Citadel => (16i32, 0.0),
SiteKind::Bridge(_, _) => (0, 0.0),
SiteKind::Adlet => (16i32, 0.0),
SiteKind::Haniwa => (32i32, 16.0),
SiteKind::PirateHideout => (8i32, 3.0),
SiteKind::RockCircle => (8i32, 3.0),
SiteKind::TrollCave => (4i32, 1.5),
SiteKind::Camp => (4i32, 1.5),
SiteKind::DwarvenMine => (8i32, 3.0),
SiteKind::Cultist => (24i32, 10.0),
SiteKind::Sahagin => (8i32, 3.0),
SiteKind::VampireCastle => (10i32, 16.0),
SiteKind::GliderCourse => (0, 0.0),
SiteKind::Myrmidon => (64i32, 35.0),
};
let (raise, raise_dist, make_waypoint): (f32, i32, bool) = match &site.kind {
SiteKind::Settlement => (10.0, 6, true),
SiteKind::Castle => (0.0, 6, true),
_ => (0.0, 0, false),
};
// Flatten ground
if let Some(center_alt) = ctx.sim.get_alt_approx(wpos) {
for offs in Spiral2d::new().take(radius.pow(2) as usize) {
let center_alt = center_alt
+ if offs.magnitude_squared() <= raise_dist.pow(2) {
raise
} else {
0.0
}; // Raise the town centre up a little
let pos = site.center + offs;
let factor = ((1.0
- (site.center - pos).map(|e| e as f32).magnitude()
/ flatten_radius.max(0.01))
* 1.25)
.min(1.0);
let rng = &mut ctx.rng;
ctx.sim
.get_mut(pos)
// Don't disrupt chunks that are near water
.filter(|chunk| !chunk.river.near_water())
.map(|chunk| {
let diff = Lerp::lerp_precise(chunk.alt, center_alt, factor) - chunk.alt;
// Make sure we don't fall below sea level (fortunately, we don't have
// to worry about the case where water_alt is already set to a correct
// value higher than alt, since this chunk should have been filtered
// out in that case).
chunk.water_alt = CONFIG.sea_level.max(chunk.water_alt + diff);
chunk.alt += diff;
chunk.basement += diff;
chunk.rockiness = 0.0;
chunk.surface_veg *= 1.0 - factor * rng.gen_range(0.25..0.9);
if make_waypoint && offs == Vec2::zero() {
chunk.contains_waypoint = true;
}
});
}
}
}
drop(guard);
// Place sites in world
prof_span!(guard, "Place sites in world");
let mut cnt = 0;
for sim_site in this.sites.values_mut() {
cnt += 1;
let wpos = sim_site
.center
.map2(TerrainChunkSize::RECT_SIZE, |e, sz: u32| {
e * sz as i32 + sz as i32 / 2
});
let mut rng = ctx.reseed().rng;
let site = index.sites.insert({
let index_ref = IndexRef {
colors: &index.colors(),
features: &index.features(),
index,
};
match &sim_site.kind {
SiteKind::Settlement => {
WorldSite::settlement(Settlement::generate(wpos, Some(ctx.sim), &mut rng))
},
SiteKind::Castle => {
WorldSite::castle(Castle::generate(wpos, Some(ctx.sim), &mut rng))
},
SiteKind::Refactor => {
let size = Lerp::lerp(0.03, 1.0, rng.gen_range(0.0..1f32).powi(5));
WorldSite::refactor(site2::Site::generate_city(
&Land::from_sim(ctx.sim),
index_ref,
&mut rng,
wpos,
size,
calendar,
))
},
SiteKind::GliderCourse => {
WorldSite::glider_course(site2::Site::generate_glider_course(
&Land::from_sim(ctx.sim),
index_ref,
&mut rng,
wpos,
))
},
SiteKind::CliffTown => WorldSite::cliff_town(site2::Site::generate_cliff_town(
&Land::from_sim(ctx.sim),
index_ref,
&mut rng,
wpos,
)),
SiteKind::SavannahTown => {
WorldSite::savannah_town(site2::Site::generate_savannah_town(
&Land::from_sim(ctx.sim),
&mut rng,
wpos,
))
},
SiteKind::CoastalTown => {
WorldSite::coastal_town(site2::Site::generate_coastal_town(
&Land::from_sim(ctx.sim),
&mut rng,
wpos,
))
},
SiteKind::PirateHideout => {
WorldSite::pirate_hideout(site2::Site::generate_pirate_hideout(
&Land::from_sim(ctx.sim),
&mut rng,
wpos,
))
},
SiteKind::JungleRuin => WorldSite::jungle_ruin(
site2::Site::generate_jungle_ruin(&Land::from_sim(ctx.sim), &mut rng, wpos),
),
SiteKind::RockCircle => WorldSite::rock_circle(
site2::Site::generate_rock_circle(&Land::from_sim(ctx.sim), &mut rng, wpos),
),
SiteKind::TrollCave => WorldSite::troll_cave(site2::Site::generate_troll_cave(
&Land::from_sim(ctx.sim),
&mut rng,
wpos,
)),
SiteKind::Camp => WorldSite::troll_cave(site2::Site::generate_camp(
&Land::from_sim(ctx.sim),
&mut rng,
wpos,
)),
SiteKind::DesertCity => WorldSite::desert_city(
site2::Site::generate_desert_city(&Land::from_sim(ctx.sim), &mut rng, wpos),
),
SiteKind::Tree => {
WorldSite::tree(Tree::generate(wpos, &Land::from_sim(ctx.sim), &mut rng))
},
SiteKind::GiantTree => WorldSite::giant_tree(site2::Site::generate_giant_tree(
&Land::from_sim(ctx.sim),
&mut rng,
wpos,
)),
SiteKind::Gnarling => WorldSite::gnarling(site2::Site::generate_gnarling(
&Land::from_sim(ctx.sim),
&mut rng,
wpos,
)),
SiteKind::DwarvenMine => WorldSite::dwarven_mine(site2::Site::generate_mine(
&Land::from_sim(ctx.sim),
&mut rng,
wpos,
)),
SiteKind::ChapelSite => WorldSite::chapel_site(
site2::Site::generate_chapel_site(&Land::from_sim(ctx.sim), &mut rng, wpos),
),
SiteKind::Terracotta => WorldSite::terracotta(
site2::Site::generate_terracotta(&Land::from_sim(ctx.sim), &mut rng, wpos),
),
SiteKind::Citadel => WorldSite::gnarling(site2::Site::generate_citadel(
&Land::from_sim(ctx.sim),
&mut rng,
wpos,
)),
SiteKind::Bridge(a, b) => WorldSite::bridge(site2::Site::generate_bridge(
&Land::from_sim(ctx.sim),
index_ref,
&mut rng,
*a,
*b,
)),
SiteKind::Adlet => WorldSite::adlet(site2::Site::generate_adlet(
&Land::from_sim(ctx.sim),
&mut rng,
wpos,
index_ref,
)),
SiteKind::Haniwa => WorldSite::haniwa(site2::Site::generate_haniwa(
&Land::from_sim(ctx.sim),
&mut rng,
wpos,
)),
SiteKind::Cultist => WorldSite::cultist(site2::Site::generate_cultist(
&Land::from_sim(ctx.sim),
&mut rng,
wpos,
)),
SiteKind::Myrmidon => WorldSite::myrmidon(site2::Site::generate_myrmidon(
&Land::from_sim(ctx.sim),
&mut rng,
wpos,
)),
SiteKind::Sahagin => WorldSite::sahagin(site2::Site::generate_sahagin(
&Land::from_sim(ctx.sim),
index_ref,
&mut rng,
wpos,
)),
SiteKind::VampireCastle => {
WorldSite::vampire_castle(site2::Site::generate_vampire_castle(
&Land::from_sim(ctx.sim),
&mut rng,
wpos,
))
},
}
});
sim_site.site_tmp = Some(site);
let site_ref = &index.sites[site];
let radius_chunks =
(site_ref.radius() / TerrainChunkSize::RECT_SIZE.x as f32).ceil() as usize;
for pos in Spiral2d::new()
.map(|offs| sim_site.center + offs)
.take((radius_chunks * 2).pow(2))
{
ctx.sim.get_mut(pos).map(|chunk| chunk.sites.push(site));
}
debug!(?sim_site.center, "Placed site at location");
}
drop(guard);
info!(?cnt, "all sites placed");
//this.display_info();
// remember neighbor information in economy
for (s1, val) in this.track_map.iter() {
if let Some(index1) = this.sites.get(*s1).site_tmp {
for (s2, t) in val.iter() {
if let Some(index2) = this.sites.get(*s2).site_tmp {
if index.sites.get(index1).do_economic_simulation()
&& index.sites.get(index2).do_economic_simulation()
{
let cost = this.tracks.get(*t).path.len();
index
.sites
.get_mut(index1)
.economy
.add_neighbor(index2, cost);
index
.sites
.get_mut(index2)
.economy
.add_neighbor(index1, cost);
}
}
}
}
}
// TODO: this looks optimizable
// collect natural resources
prof_span!(guard, "collect natural resources");
let sites = &mut index.sites;
(0..ctx.sim.map_size_lg().chunks_len()).for_each(|posi| {
let chpos = uniform_idx_as_vec2(ctx.sim.map_size_lg(), posi);
let wpos = chpos.map(|e| e as i64) * TerrainChunkSize::RECT_SIZE.map(|e| e as i64);
let closest_site = (*sites)
.iter_mut()
.filter(|s| !matches!(s.1.kind, crate::site::SiteKind::Myrmidon(_)))
.min_by_key(|(_id, s)| s.get_origin().map(|e| e as i64).distance_squared(wpos));
if let Some((_id, s)) = closest_site {
let distance_squared = s.get_origin().map(|e| e as i64).distance_squared(wpos);
s.economy
.add_chunk(ctx.sim.get(chpos).unwrap(), distance_squared);
}
});
drop(guard);
sites
.iter_mut()
.for_each(|(_, s)| s.economy.cache_economy());
this
}
fn generate_caves(&mut self, ctx: &mut GenCtx<impl Rng>) {
let mut water_caves = Vec::new();
for _ in 0..ctx.sim.get_size().product() / 10_000 {
self.generate_cave(ctx, &mut water_caves);
}
// Floodfills cave water.
while let Some(loc) = water_caves.pop() {
let cave = ctx.sim.get(loc).unwrap().cave.1;
for l in NEIGHBORS {
let l = loc + l;
if let Some(o_cave) = ctx.sim.get_mut(l).map(|c| &mut c.cave.1) {
// Contains cave
if o_cave.alt != 0.0 {
let should_fill = o_cave.water_alt < cave.water_alt
&& o_cave.alt - o_cave.width < cave.water_alt as f32;
if should_fill {
o_cave.water_alt = cave.water_alt;
o_cave.water_dist = 0.0;
water_caves.push(l);
}
// If we don't fill and the cave has no water, continue filling distance
else if o_cave.water_alt == i32::MIN
&& o_cave.water_dist > cave.water_dist + 1.0
{
o_cave.water_dist = cave.water_dist + 1.0;
water_caves.push(l);
}
}
}
}
}
}
// TODO: Move this
fn generate_cave(
&mut self,
ctx: &mut GenCtx<impl Rng>,
submerged_cave_chunks: &mut Vec<Vec2<i32>>,
) {
let mut pos = ctx
.sim
.get_size()
.map(|sz| ctx.rng.gen_range(0..sz as i32) as f32);
let mut vel = pos
.map2(ctx.sim.get_size(), |pos, sz| sz as f32 / 2.0 - pos)
.try_normalized()
.unwrap_or_else(Vec2::unit_y);
let path = (-100..100)
.filter_map(|i: i32| {
let depth = (i.abs() as f32 / 100.0 * std::f32::consts::PI / 2.0).cos();
vel = (vel
+ Vec2::new(
ctx.rng.gen_range(-0.35..0.35),
ctx.rng.gen_range(-0.35..0.35),
))
.try_normalized()
.unwrap_or_else(Vec2::unit_y);
let old_pos = pos.map(|e| e as i32);
pos = (pos + vel * 0.5)
.clamped(Vec2::zero(), ctx.sim.get_size().map(|e| e as f32 - 1.0));
Some((pos.map(|e| e as i32), depth)).filter(|(pos, _)| *pos != old_pos)
})
.collect::<Vec<_>>();
for locs in path.windows(3) {
let to_prev_idx = NEIGHBORS
.iter()
.enumerate()
.find(|(_, dir)| **dir == locs[0].0 - locs[1].0)
.expect("Track locations must be neighbors")
.0;
let to_next_idx = NEIGHBORS
.iter()
.enumerate()
.find(|(_, dir)| **dir == locs[2].0 - locs[1].0)
.expect("Track locations must be neighbors")
.0;
ctx.sim.get_mut(locs[0].0).unwrap().cave.0.neighbors |=
1 << ((to_prev_idx as u8 + 4) % 8);
ctx.sim.get_mut(locs[1].0).unwrap().cave.0.neighbors |=
(1 << (to_prev_idx as u8)) | (1 << (to_next_idx as u8));
ctx.sim.get_mut(locs[2].0).unwrap().cave.0.neighbors |=
1 << ((to_next_idx as u8 + 4) % 8);
}
for loc in path.iter() {
let chunk = ctx.sim.get_mut(loc.0).unwrap();
let depth = loc.1 * 250.0 - 20.0;
chunk.cave.1.alt =
chunk.alt - depth + ctx.rng.gen_range(-4.0..4.0) * (depth > 10.0) as i32 as f32;
chunk.cave.1.width = ctx.rng.gen_range(6.0..32.0);
chunk.cave.0.offset = Vec2::new(ctx.rng.gen_range(-16..17), ctx.rng.gen_range(-16..17));
if chunk.cave.1.alt + chunk.cave.1.width + 5.0 > chunk.alt {
chunk.spawn_rate = 0.0;
}
let cave_min_alt = chunk.cave.1.alt - chunk.cave.1.width;
let cave_max_alt = chunk.cave.1.alt + chunk.cave.1.width;
let submerged = chunk.alt - 2.0 < chunk.water_alt
&& chunk.alt < cave_max_alt
&& cave_min_alt < chunk.water_alt
&& chunk.river.near_water()
// Only do this for caves at the sea level for now.
// The reason being that floodfilling from a water alt to an alt lower than the water alt causes problems.
&& chunk.water_alt <= CONFIG.sea_level;
if submerged {
submerged_cave_chunks.push(loc.0);
chunk.cave.1.water_alt = chunk.water_alt as i32;
chunk.cave.1.water_dist = 0.0;
}
}
self.caves.insert(CaveInfo {
location: (
path.first().unwrap().0 * TerrainChunkSize::RECT_SIZE.map(|e: u32| e as i32),
path.last().unwrap().0 * TerrainChunkSize::RECT_SIZE.map(|e: u32| e as i32),
),
name: {
let name = NameGen::location(&mut ctx.rng).generate();
match ctx.rng.gen_range(0..7) {
0 => format!("{} Hole", name),
1 => format!("{} Cavern", name),
2 => format!("{} Hollow", name),
3 => format!("{} Tunnel", name),
4 => format!("{} Mouth", name),
5 => format!("{} Grotto", name),
_ => format!("{} Den", name),
}
},
});
}
pub fn place(&self, id: Id<Place>) -> &Place { self.places.get(id) }
pub fn sites(&self) -> impl Iterator<Item = &Site> + '_ { self.sites.values() }
#[allow(dead_code)]
fn display_info(&self) {
for (id, civ) in self.civs.iter() {
println!("# Civilisation {:?}", id);
println!("Name: <unnamed>");
println!("Homeland: {:#?}", self.places.get(civ.homeland));
}
for (id, site) in self.sites.iter() {
println!("# Site {:?}", id);
println!("{:#?}", site);
}
}
/// Return the direct track between two places, bool if the track should be
/// reversed or not
pub fn track_between(&self, a: Id<Site>, b: Id<Site>) -> Option<(Id<Track>, bool)> {
self.track_map
.get(&a)
.and_then(|dests| Some((*dests.get(&b)?, false)))
.or_else(|| {
self.track_map
.get(&b)
.and_then(|dests| Some((*dests.get(&a)?, true)))
})
}
/// Return an iterator over a site's neighbors
pub fn neighbors(&self, site: Id<Site>) -> impl Iterator<Item = Id<Site>> + '_ {
let to = self
.track_map
.get(&site)
.map(|dests| dests.keys())
.into_iter()
.flatten();
let fro = self
.track_map
.iter()
.filter(move |(_, dests)| dests.contains_key(&site))
.map(|(p, _)| p);
to.chain(fro).filter(move |p| **p != site).copied()
}
/// Find the cheapest route between two places
fn route_between(&self, a: Id<Site>, b: Id<Site>) -> Option<(Path<Id<Site>>, f32)> {
let heuristic = move |p: &Id<Site>, _: &Id<Site>| {
(self
.sites
.get(*p)
.center
.distance_squared(self.sites.get(b).center) as f32)
.sqrt()
};
let transition =
|a: Id<Site>, b: Id<Site>| self.tracks.get(self.track_between(a, b).unwrap().0).cost;
let neighbors = |p: &Id<Site>| {
let p = *p;
self.neighbors(p)
.map(move |neighbor| (neighbor, transition(p, neighbor)))
};
let satisfied = |p: &Id<Site>| *p == b;
// We use this hasher (FxHasher64) because
// (1) we don't care about DDOS attacks (ruling out SipHash);
// (2) we care about determinism across computers (ruling out AAHash);
// (3) we have 8-byte keys (for which FxHash is fastest).
let mut astar = Astar::new(100, a, BuildHasherDefault::<FxHasher64>::default());
astar.poll(100, heuristic, neighbors, satisfied).into_path()
}
fn birth_civ(&mut self, ctx: &mut GenCtx<impl Rng>) -> Option<Id<Civ>> {
// TODO: specify SiteKind based on where a suitable location is found
let kind = match ctx.rng.gen_range(0..64) {
0..=8 => SiteKind::CliffTown,
9..=17 => SiteKind::DesertCity,
18..=23 => SiteKind::SavannahTown,
24..=33 => SiteKind::CoastalTown,
_ => SiteKind::Refactor,
};
let world_dims = ctx.sim.get_aabr();
let avoid_town_enemies = ProximityRequirementsBuilder::new()
.avoid_all_of(self.town_enemies(), 60)
.finalize(&world_dims);
let loc = (0..100)
.flat_map(|_| {
find_site_loc(ctx, &avoid_town_enemies, &kind).and_then(|loc| {
town_attributes_of_site(loc, ctx.sim)
.map(|town_attrs| (loc, town_attrs.score()))
})
})
.reduce(|a, b| if a.1 > b.1 { a } else { b })?
.0;
let site = self.establish_site(ctx, loc, |place| Site {
kind,
site_tmp: None,
center: loc,
place,
/* most economic members have moved to site/Economy */
/* last_exports: Stocks::from_default(0.0),
* export_targets: Stocks::from_default(0.0),
* //trade_states: Stocks::default(), */
});
let civ = self.civs.insert(Civ {
capital: site,
homeland: self.sites.get(site).place,
});
Some(civ)
}
fn establish_place(
&mut self,
_ctx: &mut GenCtx<impl Rng>,
loc: Vec2<i32>,
_area: Range<usize>,
) -> Id<Place> {
self.places.insert(Place { center: loc })
}
/// Adds lake POIs and names them
fn name_biomes(&mut self, ctx: &mut GenCtx<impl Rng>) {
prof_span!("name_biomes");
let map_size_lg = ctx.sim.map_size_lg();
let world_size = map_size_lg.chunks();
let mut biomes: Vec<(common::terrain::BiomeKind, Vec<usize>)> = Vec::new();
let mut explored = vec![false; world_size.x as usize * world_size.y as usize];
let mut to_floodfill = Vec::new();
let mut to_explore = Vec::new();
// TODO: have start point in center and ignore ocean?
let start_point = 0;
to_explore.push(start_point);
while let Some(exploring) = to_explore.pop() {
if explored[exploring] {
continue;
}
to_floodfill.push(exploring);
// Should always be a chunk on the map
let biome = ctx.sim.chunks[exploring].get_biome();
let mut filled = Vec::new();
while let Some(filling) = to_floodfill.pop() {
explored[filling] = true;
filled.push(filling);
for neighbour in common::terrain::neighbors(map_size_lg, filling) {
if explored[neighbour] {
continue;
}
let n_biome = ctx.sim.chunks[neighbour].get_biome();
if n_biome == biome {
to_floodfill.push(neighbour);
} else {
to_explore.push(neighbour);
}
}
}
biomes.push((biome, filled));
}
prof_span!("after flood fill");
let mut biome_count = 0;
for biome in biomes {
let name = match biome.0 {
common::terrain::BiomeKind::Lake if biome.1.len() as u32 > 200 => Some(format!(
"{} {}",
["Lake", "Loch"].choose(&mut ctx.rng).unwrap(),
NameGen::location(&mut ctx.rng).generate_lake_custom()
)),
common::terrain::BiomeKind::Lake if biome.1.len() as u32 > 10 => Some(format!(
"{} {}",
NameGen::location(&mut ctx.rng).generate_lake_custom(),
["Pool", "Well", "Pond"].choose(&mut ctx.rng).unwrap()
)),
common::terrain::BiomeKind::Grassland if biome.1.len() as u32 > 750 => {
Some(format!(
"{} {}",
[
NameGen::location(&mut ctx.rng).generate_grassland_engl(),
NameGen::location(&mut ctx.rng).generate_grassland_custom()
]
.choose(&mut ctx.rng)
.unwrap(),
[
"Grasslands",
"Plains",
"Meadows",
"Fields",
"Heath",
"Hills",
"Prairie",
"Lowlands",
"Steppe",
"Downs",
"Greens",
]
.choose(&mut ctx.rng)
.unwrap()
))
},
common::terrain::BiomeKind::Ocean if biome.1.len() as u32 > 750 => Some(format!(
"{} {}",
[
NameGen::location(&mut ctx.rng).generate_ocean_engl(),
NameGen::location(&mut ctx.rng).generate_ocean_custom()
]
.choose(&mut ctx.rng)
.unwrap(),
["Sea", "Bay", "Gulf", "Deep", "Depths", "Ocean", "Blue",]
.choose(&mut ctx.rng)
.unwrap()
)),
common::terrain::BiomeKind::Mountain if biome.1.len() as u32 > 750 => {
Some(format!(
"{} {}",
[
NameGen::location(&mut ctx.rng).generate_mountain_engl(),
NameGen::location(&mut ctx.rng).generate_mountain_custom()
]
.choose(&mut ctx.rng)
.unwrap(),
[
"Mountains",
"Range",
"Reach",
"Massif",
"Rocks",
"Cliffs",
"Peaks",
"Heights",
"Bluffs",
"Ridge",
"Canyon",
"Plateau",
]
.choose(&mut ctx.rng)
.unwrap()
))
},
common::terrain::BiomeKind::Snowland if biome.1.len() as u32 > 750 => {
Some(format!(
"{} {}",
[
NameGen::location(&mut ctx.rng).generate_snowland_engl(),
NameGen::location(&mut ctx.rng).generate_snowland_custom()
]
.choose(&mut ctx.rng)
.unwrap(),
[
"Snowlands",
"Glacier",
"Tundra",
"Drifts",
"Snowfields",
"Hills",
"Downs",
"Uplands",
"Highlands",
]
.choose(&mut ctx.rng)
.unwrap()
))
},
common::terrain::BiomeKind::Desert if biome.1.len() as u32 > 750 => Some(format!(
"{} {}",
[
NameGen::location(&mut ctx.rng).generate_desert_engl(),
NameGen::location(&mut ctx.rng).generate_desert_custom()
]
.choose(&mut ctx.rng)
.unwrap(),
[
"Desert", "Sands", "Sandsea", "Drifts", "Dunes", "Droughts", "Flats",
]
.choose(&mut ctx.rng)
.unwrap()
)),
common::terrain::BiomeKind::Swamp if biome.1.len() as u32 > 200 => Some(format!(
"{} {}",
NameGen::location(&mut ctx.rng).generate_swamp_engl(),
[
"Swamp",
"Swamps",
"Swamplands",
"Marsh",
"Marshlands",
"Morass",
"Mire",
"Bog",
"Wetlands",
"Fen",
"Moors",
]
.choose(&mut ctx.rng)
.unwrap()
)),
common::terrain::BiomeKind::Jungle if biome.1.len() as u32 > 85 => Some(format!(
"{} {}",
[
NameGen::location(&mut ctx.rng).generate_jungle_engl(),
NameGen::location(&mut ctx.rng).generate_jungle_custom()
]
.choose(&mut ctx.rng)
.unwrap(),
[
"Jungle",
"Rainforest",
"Greatwood",
"Wilds",
"Wildwood",
"Tangle",
"Tanglewood",
"Bush",
]
.choose(&mut ctx.rng)
.unwrap()
)),
common::terrain::BiomeKind::Forest if biome.1.len() as u32 > 750 => Some(format!(
"{} {}",
[
NameGen::location(&mut ctx.rng).generate_forest_engl(),
NameGen::location(&mut ctx.rng).generate_forest_custom()
]
.choose(&mut ctx.rng)
.unwrap(),
["Forest", "Woodlands", "Woods", "Glades", "Grove", "Weald",]
.choose(&mut ctx.rng)
.unwrap()
)),
common::terrain::BiomeKind::Savannah if biome.1.len() as u32 > 750 => {
Some(format!(
"{} {}",
[
NameGen::location(&mut ctx.rng).generate_savannah_engl(),
NameGen::location(&mut ctx.rng).generate_savannah_custom()
]
.choose(&mut ctx.rng)
.unwrap(),
[
"Savannah",
"Shrublands",
"Sierra",
"Prairie",
"Lowlands",
"Flats",
]
.choose(&mut ctx.rng)
.unwrap()
))
},
common::terrain::BiomeKind::Taiga if biome.1.len() as u32 > 750 => Some(format!(
"{} {}",
[
NameGen::location(&mut ctx.rng).generate_taiga_engl(),
NameGen::location(&mut ctx.rng).generate_taiga_custom()
]
.choose(&mut ctx.rng)
.unwrap(),
[
"Forest",
"Woodlands",
"Woods",
"Timberlands",
"Highlands",
"Uplands",
]
.choose(&mut ctx.rng)
.unwrap()
)),
_ => None,
};
if let Some(name) = name {
// find average center of the biome
let center = biome
.1
.iter()
.map(|b| {
uniform_idx_as_vec2(map_size_lg, *b).as_::<f32>() / biome.1.len() as f32
})
.sum::<Vec2<f32>>()
.as_::<i32>();
// Select the point closest to the center
let idx = *biome
.1
.iter()
.min_by_key(|&b| center.distance_squared(uniform_idx_as_vec2(map_size_lg, *b)))
.unwrap();
let id = self.pois.insert(PointOfInterest {
name,
loc: uniform_idx_as_vec2(map_size_lg, idx),
kind: PoiKind::Biome(biome.1.len() as u32),
});
for chunk in biome.1 {
ctx.sim.chunks[chunk].poi = Some(id);
}
biome_count += 1;
}
}
info!(?biome_count, "all biomes named");
}
/// Adds mountain POIs and name them
fn name_peaks(&mut self, ctx: &mut GenCtx<impl Rng>) {
prof_span!("name_peaks");
let map_size_lg = ctx.sim.map_size_lg();
const MIN_MOUNTAIN_ALT: f32 = 600.0;
const MIN_MOUNTAIN_CHAOS: f32 = 0.35;
let rng = &mut ctx.rng;
let sim_chunks = &ctx.sim.chunks;
let peaks = sim_chunks
.iter()
.enumerate()
.filter(|(posi, chunk)| {
let neighbor_alts_max = common::terrain::neighbors(map_size_lg, *posi)
.map(|i| sim_chunks[i].alt as u32)
.max();
chunk.alt > MIN_MOUNTAIN_ALT
&& chunk.chaos > MIN_MOUNTAIN_CHAOS
&& neighbor_alts_max.map_or(false, |n_alt| chunk.alt as u32 > n_alt)
})
.map(|(posi, chunk)| {
(
posi,
uniform_idx_as_vec2(map_size_lg, posi),
(chunk.alt - CONFIG.sea_level) as u32,
)
})
.collect::<Vec<(usize, Vec2<i32>, u32)>>();
let mut num_peaks = 0;
let mut removals = vec![false; peaks.len()];
for (i, peak) in peaks.iter().enumerate() {
for (k, n_peak) in peaks.iter().enumerate() {
// If the difference in position of this peak and another is
// below a threshold and this peak's altitude is lower, remove the
// peak from the list
if i != k
&& (peak.1).distance_squared(n_peak.1) < POI_THINNING_DIST_SQRD
&& peak.2 <= n_peak.2
{
// Remove this peak
// This cannot panic as `removals` is the same length as `peaks`
// i is the index in `peaks`
removals[i] = true;
}
}
}
peaks
.iter()
.enumerate()
.filter(|&(i, _)| !removals[i])
.for_each(|(_, (_, loc, alt))| {
num_peaks += 1;
self.pois.insert(PointOfInterest {
name: {
let name = NameGen::location(rng).generate();
if *alt < 1000 {
match rng.gen_range(0..6) {
0 => format!("{} Bluff", name),
1 => format!("{} Crag", name),
_ => format!("{} Hill", name),
}
} else {
match rng.gen_range(0..8) {
0 => format!("{}'s Peak", name),
1 => format!("{} Peak", name),
2 => format!("{} Summit", name),
_ => format!("Mount {}", name),
}
}
},
kind: PoiKind::Peak(*alt),
loc: *loc,
});
});
info!(?num_peaks, "all peaks named");
}
fn establish_site(
&mut self,
ctx: &mut GenCtx<impl Rng>,
loc: Vec2<i32>,
site_fn: impl FnOnce(Id<Place>) -> Site,
) -> Id<Site> {
prof_span!("establish_site");
const SITE_AREA: Range<usize> = 1..4; //64..256;
fn establish_site(
civs: &mut Civs,
ctx: &mut GenCtx<impl Rng>,
loc: Vec2<i32>,
site_fn: impl FnOnce(Id<Place>) -> Site,
) -> Id<Site> {
let place = match ctx.sim.get(loc).and_then(|site| site.place) {
Some(place) => place,
None => civs.establish_place(ctx, loc, SITE_AREA),
};
civs.sites.insert(site_fn(place))
}
let site = establish_site(self, ctx, loc, site_fn);
// Find neighbors
// Note, the maximum distance that I have so far observed not hitting the
// iteration limit in `find_path` is 364. So I think this is a reasonable
// limit (although the relationship between distance and pathfinding iterations
// can be a bit variable). Note, I have seen paths reach the iteration limit
// with distances as small as 137, so this certainly doesn't catch all
// cases that would fail.
const MAX_NEIGHBOR_DISTANCE: f32 = 400.0;
let mut nearby = self
.sites
.iter()
.filter(|&(id, _)| id != site)
.filter(|(_, p)| {
matches!(
p.kind,
SiteKind::Refactor
| SiteKind::Settlement
| SiteKind::CliffTown
| SiteKind::SavannahTown
| SiteKind::CoastalTown
| SiteKind::DesertCity
| SiteKind::Castle
)
})
.map(|(id, p)| (id, (p.center.distance_squared(loc) as f32).sqrt()))
.filter(|(_, dist)| *dist < MAX_NEIGHBOR_DISTANCE)
.collect::<Vec<_>>();
nearby.sort_by_key(|(_, dist)| *dist as i32);
if let SiteKind::Refactor
| SiteKind::Settlement
| SiteKind::CliffTown
| SiteKind::SavannahTown
| SiteKind::CoastalTown
| SiteKind::DesertCity
| SiteKind::Castle = self.sites[site].kind
{
for (nearby, _) in nearby.into_iter().take(4) {
prof_span!("for nearby");
// Find a route using existing paths
//
// If the novel path isn't efficient compared to this, don't use it
let max_novel_cost = self
.route_between(site, nearby)
.map_or(f32::MAX, |(_, route_cost)| route_cost / 3.0);
let start = loc;
let end = self.sites.get(nearby).center;
// Find a novel path.
let get_bridge = |start| self.bridges.get(&start).map(|(end, _)| *end);
if let Some((path, cost)) = find_path(ctx, get_bridge, start, end, max_novel_cost) {
// Write the track to the world as a path
for locs in path.nodes().windows(3) {
let mut randomize_offset = false;
if let Some((i, _)) = NEIGHBORS
.iter()
.enumerate()
.find(|(_, dir)| **dir == locs[0] - locs[1])
{
ctx.sim.get_mut(locs[0]).unwrap().path.0.neighbors |=
1 << ((i as u8 + 4) % 8);
ctx.sim.get_mut(locs[1]).unwrap().path.0.neighbors |= 1 << (i as u8);
randomize_offset = true;
}
if let Some((i, _)) = NEIGHBORS
.iter()
.enumerate()
.find(|(_, dir)| **dir == locs[2] - locs[1])
{
ctx.sim.get_mut(locs[2]).unwrap().path.0.neighbors |=
1 << ((i as u8 + 4) % 8);
ctx.sim.get_mut(locs[1]).unwrap().path.0.neighbors |= 1 << (i as u8);
randomize_offset = true;
} else if !self.bridges.contains_key(&locs[1]) {
let center = (locs[1] + locs[2]) / 2;
let id =
establish_site(self, &mut ctx.reseed(), center, move |place| {
Site {
kind: SiteKind::Bridge(locs[1], locs[2]),
site_tmp: None,
center,
place,
}
});
self.bridges.insert(locs[1], (locs[2], id));
self.bridges.insert(locs[2], (locs[1], id));
}
/*
let to_prev_idx = NEIGHBORS
.iter()
.enumerate()
.find(|(_, dir)| **dir == (locs[0] - locs[1]).map(|e| e.signum()))
.expect("Track locations must be neighbors")
.0;
let to_next_idx = NEIGHBORS
.iter()
.enumerate()
.find(|(_, dir)| **dir == (locs[2] - locs[1]).map(|e| e.signum()))
.expect("Track locations must be neighbors")
.0;
ctx.sim.get_mut(locs[0]).unwrap().path.0.neighbors |=
1 << ((to_prev_idx as u8 + 4) % 8);
ctx.sim.get_mut(locs[2]).unwrap().path.0.neighbors |=
1 << ((to_next_idx as u8 + 4) % 8);
let mut chunk = ctx.sim.get_mut(locs[1]).unwrap();
chunk.path.0.neighbors |=
(1 << (to_prev_idx as u8)) | (1 << (to_next_idx as u8));
*/
if randomize_offset {
let chunk = ctx.sim.get_mut(locs[1]).unwrap();
chunk.path.0.offset =
Vec2::new(ctx.rng.gen_range(-16..17), ctx.rng.gen_range(-16..17));
}
}
// Take note of the track
let track = self.tracks.insert(Track { cost, path });
self.track_map
.entry(site)
.or_default()
.insert(nearby, track);
}
}
}
site
}
fn gnarling_enemies(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {
self.sites().filter_map(|s| match s.kind {
SiteKind::Tree | SiteKind::GiantTree => None,
_ => Some(s.center),
})
}
fn adlet_enemies(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {
self.sites().filter_map(|s| match s.kind {
SiteKind::Tree | SiteKind::GiantTree => None,
_ => Some(s.center),
})
}
fn haniwa_enemies(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {
self.sites().filter_map(|s| match s.kind {
SiteKind::Tree | SiteKind::GiantTree => None,
_ => Some(s.center),
})
}
fn chapel_site_enemies(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {
self.sites().filter_map(|s| match s.kind {
SiteKind::Tree | SiteKind::GiantTree => None,
_ => Some(s.center),
})
}
fn mine_site_enemies(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {
self.sites().filter_map(|s| match s.kind {
SiteKind::Tree | SiteKind::GiantTree => None,
_ => Some(s.center),
})
}
fn terracotta_enemies(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {
self.sites().filter_map(|s| match s.kind {
SiteKind::Tree | SiteKind::GiantTree => None,
_ => Some(s.center),
})
}
fn cultist_enemies(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {
self.sites().filter_map(|s| match s.kind {
SiteKind::Tree | SiteKind::GiantTree => None,
_ => Some(s.center),
})
}
fn myrmidon_enemies(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {
self.sites().filter_map(|s| match s.kind {
SiteKind::Tree | SiteKind::GiantTree => None,
_ => Some(s.center),
})
}
fn vampire_castle_enemies(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {
self.sites().filter_map(|s| match s.kind {
SiteKind::Tree | SiteKind::GiantTree => None,
_ => Some(s.center),
})
}
fn tree_enemies(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {
self.sites().map(|s| s.center)
}
fn castle_enemies(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {
self.sites().filter_map(|s| {
if s.is_settlement() {
None
} else {
Some(s.center)
}
})
}
fn jungle_ruin_enemies(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {
self.sites().filter_map(|s| match s.kind {
SiteKind::Tree | SiteKind::GiantTree => None,
_ => Some(s.center),
})
}
fn town_enemies(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {
self.sites().filter_map(|s| match s.kind {
SiteKind::Castle | SiteKind::Citadel => None,
_ => Some(s.center),
})
}
fn towns(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {
self.sites().filter_map(|s| {
if s.is_settlement() {
Some(s.center)
} else {
None
}
})
}
fn pirate_hideout_enemies(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {
self.sites().filter_map(|s| match s.kind {
SiteKind::Tree | SiteKind::GiantTree => None,
_ => Some(s.center),
})
}
fn sahagin_enemies(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {
self.sites().filter_map(|s| match s.kind {
SiteKind::Tree | SiteKind::GiantTree => None,
_ => Some(s.center),
})
}
fn rock_circle_enemies(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {
self.sites().filter_map(|s| match s.kind {
SiteKind::Tree | SiteKind::GiantTree => None,
_ => Some(s.center),
})
}
fn troll_cave_enemies(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {
self.sites().filter_map(|s| match s.kind {
SiteKind::Tree | SiteKind::GiantTree => None,
_ => Some(s.center),
})
}
fn camp_enemies(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {
self.sites().filter_map(|s| match s.kind {
SiteKind::Tree | SiteKind::GiantTree => None,
_ => Some(s.center),
})
}
}
/// Attempt to find a path between two locations
fn find_path(
ctx: &mut GenCtx<impl Rng>,
get_bridge: impl Fn(Vec2<i32>) -> Option<Vec2<i32>>,
a: Vec2<i32>,
b: Vec2<i32>,
max_path_cost: f32,
) -> Option<(Path<Vec2<i32>>, f32)> {
prof_span!("find_path");
const MAX_PATH_ITERS: usize = 100_000;
let sim = &ctx.sim;
// NOTE: If heuristic overestimates the actual cost, then A* is not guaranteed
// to produce the least-cost path (since it will explore partially based on
// the heuristic).
// TODO: heuristic can be larger than actual cost, since existing bridges cost
// 1.0 (after the 1.0 that is added to everthting), but they can cover
// multiple chunks.
let heuristic = move |l: &Vec2<i32>, _: &Vec2<i32>| (l.distance_squared(b) as f32).sqrt();
let neighbors = |l: &Vec2<i32>| {
let l = *l;
let bridge = get_bridge(l);
let potential = walk_in_all_dirs(sim, bridge, l);
potential
.into_iter()
.filter_map(|p| p.map(|(node, cost)| (node, cost + 1.0)))
};
let satisfied = |l: &Vec2<i32>| *l == b;
// We use this hasher (FxHasher64) because
// (1) we don't care about DDOS attacks (ruling out SipHash);
// (2) we care about determinism across computers (ruling out AAHash);
// (3) we have 8-byte keys (for which FxHash is fastest).
let mut astar = Astar::new(
MAX_PATH_ITERS,
a,
BuildHasherDefault::<FxHasher64>::default(),
)
.with_max_cost(max_path_cost);
astar
.poll(MAX_PATH_ITERS, heuristic, neighbors, satisfied)
.into_path()
}
/// Return Some if travel between a location and a chunk next to it is permitted
/// If permitted, the approximate relative const of traversal is given
// (TODO: by whom?)
/// Return tuple: (final location, cost)
///
/// For efficiency, this computes for all 8 directions at once.
fn walk_in_all_dirs(
sim: &WorldSim,
bridge: Option<Vec2<i32>>,
a: Vec2<i32>,
) -> [Option<(Vec2<i32>, f32)>; 8] {
let mut potential = [None; 8];
let adjacents = NEIGHBORS.map(|dir| a + dir);
let Some(a_chunk) = sim.get(a) else {
return potential;
};
let mut chunks = [None; 8];
for i in 0..8 {
if loc_suitable_for_walking(sim, adjacents[i]) {
chunks[i] = sim.get(adjacents[i]);
}
}
for i in 0..8 {
let Some(b_chunk) = chunks[i] else { continue };
let hill_cost = ((b_chunk.alt - a_chunk.alt).abs() / 5.0).powi(2);
let water_cost = (b_chunk.water_alt - b_chunk.alt + 8.0).clamped(0.0, 8.0) * 3.0; // Try not to path swamps / tidal areas
let wild_cost = if b_chunk.path.0.is_way() {
0.0 // Traversing existing paths has no additional cost!
} else {
3.0 // + (1.0 - b_chunk.tree_density) * 20.0 // Prefer going through forests, for aesthetics
};
let cost = 1.0 + hill_cost + water_cost + wild_cost;
potential[i] = Some((adjacents[i], cost));
}
// Look for potential bridge spots in the cardinal directions if
// `loc_suitable_for_wallking` was false for the adjacent chunk.
for (i, &dir) in NEIGHBORS.iter().enumerate() {
let is_cardinal_dir = dir.x == 0 || dir.y == 0;
if is_cardinal_dir && potential[i].is_none() {
// if we can skip over unsuitable area with a bridge
potential[i] = (4..=5).find_map(|i| {
loc_suitable_for_walking(sim, a + dir * i)
.then(|| (a + dir * i, 120.0 + (i - 4) as f32 * 10.0))
});
}
}
// If current position is a bridge, skip to its destination.
if let Some(p) = bridge {
let dir = (p - a).map(|e| e.signum());
if let Some((dir_index, _)) = NEIGHBORS
.iter()
.enumerate()
.find(|(_, n_dir)| **n_dir == dir)
{
potential[dir_index] = Some((p, 0.0));
}
}
potential
}
/// Return true if a position is suitable for walking on
fn loc_suitable_for_walking(sim: &WorldSim, loc: Vec2<i32>) -> bool {
if sim.get(loc).is_some() {
NEIGHBORS.iter().all(|n| {
sim.get(loc + *n)
.map_or(false, |chunk| !chunk.river.near_water())
})
} else {
false
}
}
/// Attempt to search for a location that's suitable for site construction
// FIXME when a `close_to_one_of` requirement is passed in, we should start with
// just the chunks around those locations instead of random sampling the entire
// map
fn find_site_loc(
ctx: &mut GenCtx<impl Rng>,
proximity_reqs: &ProximityRequirements,
site_kind: &SiteKind,
) -> Option<Vec2<i32>> {
prof_span!("find_site_loc");
const MAX_ATTEMPTS: usize = 10000;
let mut loc = None;
let location_hint = proximity_reqs.location_hint;
for _ in 0..MAX_ATTEMPTS {
let test_loc = loc.unwrap_or_else(|| {
Vec2::new(
ctx.rng.gen_range(location_hint.min.x..location_hint.max.x),
ctx.rng.gen_range(location_hint.min.y..location_hint.max.y),
)
});
let is_suitable_loc = site_kind.is_suitable_loc(test_loc, ctx.sim);
if is_suitable_loc && proximity_reqs.satisfied_by(test_loc) {
if site_kind.exclusion_radius_clear(ctx.sim, test_loc) {
return Some(test_loc);
}
// If the current location is suitable and meets proximity requirements,
// try nearby spot downhill.
loc = ctx.sim.get(test_loc).and_then(|c| c.downhill);
}
}
debug!("Failed to place site {:?}.", site_kind);
None
}
fn town_attributes_of_site(loc: Vec2<i32>, sim: &WorldSim) -> Option<TownSiteAttributes> {
sim.get(loc).map(|chunk| {
const RESOURCE_RADIUS: i32 = 1;
let mut river_chunks = 0;
let mut lake_chunks = 0;
let mut ocean_chunks = 0;
let mut rock_chunks = 0;
let mut tree_chunks = 0;
let mut farmable_chunks = 0;
let mut farmable_needs_irrigation_chunks = 0;
let mut land_chunks = 0;
for x in (-RESOURCE_RADIUS)..RESOURCE_RADIUS {
for y in (-RESOURCE_RADIUS)..RESOURCE_RADIUS {
let check_loc = loc + Vec2::new(x, y).cpos_to_wpos();
sim.get(check_loc).map(|c| {
if num::abs(chunk.alt - c.alt) < 200.0 {
if c.river.is_river() {
river_chunks += 1;
}
if c.river.is_lake() {
lake_chunks += 1;
}
if c.river.is_ocean() {
ocean_chunks += 1;
}
if c.tree_density > 0.7 {
tree_chunks += 1;
}
if c.rockiness < 0.3 && c.temp > CONFIG.snow_temp {
if c.surface_veg > 0.5 {
farmable_chunks += 1;
} else {
match c.get_biome() {
common::terrain::BiomeKind::Savannah => {
farmable_needs_irrigation_chunks += 1
},
common::terrain::BiomeKind::Desert => {
farmable_needs_irrigation_chunks += 1
},
_ => (),
}
}
}
if !c.river.is_river() && !c.river.is_lake() && !c.river.is_ocean() {
land_chunks += 1;
}
}
// Mining is different since presumably you dig into the hillside
if c.rockiness > 0.7 && c.alt - chunk.alt > -10.0 {
rock_chunks += 1;
}
});
}
}
let has_river = river_chunks > 1;
let has_lake = lake_chunks > 1;
let vegetation_implies_potable_water = chunk.tree_density > 0.4
&& !matches!(chunk.get_biome(), common::terrain::BiomeKind::Swamp);
let has_many_rocks = chunk.rockiness > 1.2;
let warm_or_firewood = chunk.temp > CONFIG.snow_temp || tree_chunks > 2;
let has_potable_water =
{ has_river || (has_lake && chunk.alt > 100.0) || vegetation_implies_potable_water };
let has_building_materials = tree_chunks > 0
|| rock_chunks > 0
|| chunk.temp > CONFIG.tropical_temp && (has_river || has_lake);
let water_rich = lake_chunks + river_chunks > 2;
let can_grow_rice = water_rich
&& chunk.humidity + 1.0 > CONFIG.jungle_hum
&& chunk.temp + 1.0 > CONFIG.tropical_temp;
let farming_score = if can_grow_rice {
farmable_chunks * 2
} else {
farmable_chunks
} + if water_rich {
farmable_needs_irrigation_chunks
} else {
0
};
let fish_score = lake_chunks + ocean_chunks;
let food_score = farming_score + fish_score;
let mining_score = if tree_chunks > 1 { rock_chunks } else { 0 };
let forestry_score = if has_river { tree_chunks } else { 0 };
let trading_score = std::cmp::min(std::cmp::min(land_chunks, ocean_chunks), river_chunks);
TownSiteAttributes {
food_score,
mining_score,
forestry_score,
trading_score,
heating: warm_or_firewood,
potable_water: has_potable_water,
building_materials: has_building_materials,
aquifer: has_many_rocks,
}
})
}
pub struct TownSiteAttributes {
food_score: i32,
mining_score: i32,
forestry_score: i32,
trading_score: i32,
heating: bool,
potable_water: bool,
building_materials: bool,
aquifer: bool,
}
impl TownSiteAttributes {
pub fn score(&self) -> f32 {
3.0 * (self.food_score as f32 + 1.0).log2()
+ 2.0 * (self.forestry_score as f32 + 1.0).log2()
+ (self.mining_score as f32 + 1.0).log2()
+ (self.trading_score as f32 + 1.0).log2()
}
}
#[derive(Debug)]
pub struct Civ {
capital: Id<Site>,
homeland: Id<Place>,
}
#[derive(Debug)]
pub struct Place {
pub center: Vec2<i32>,
/* act sort of like territory with sites belonging to it
* nat_res/NaturalResources was moved to Economy
* nat_res: NaturalResources, */
}
pub struct Track {
/// Cost of using this track relative to other paths. This cost is an
/// arbitrary unit and doesn't make sense unless compared to other track
/// costs.
pub cost: f32,
path: Path<Vec2<i32>>,
}
impl Track {
pub fn path(&self) -> &Path<Vec2<i32>> { &self.path }
}
#[derive(Debug)]
pub struct Site {
pub kind: SiteKind,
// TODO: Remove this field when overhauling
pub site_tmp: Option<Id<crate::site::Site>>,
pub center: Vec2<i32>,
pub place: Id<Place>,
}
impl fmt::Display for Site {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "{:?}", self.kind)?;
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SiteKind {
Settlement,
Castle,
Refactor,
CliffTown,
SavannahTown,
CoastalTown,
DesertCity,
ChapelSite,
Terracotta,
Tree,
GiantTree,
Gnarling,
Citadel,
Bridge(Vec2<i32>, Vec2<i32>),
Adlet,
Haniwa,
PirateHideout,
RockCircle,
TrollCave,
Camp,
DwarvenMine,
JungleRuin,
Cultist,
Sahagin,
VampireCastle,
GliderCourse,
Myrmidon,
}
impl SiteKind {
pub fn is_suitable_loc(&self, loc: Vec2<i32>, sim: &WorldSim) -> bool {
let on_land = || -> bool {
if let Some(chunk) = sim.get(loc) {
!chunk.river.is_ocean()
&& !chunk.river.is_lake()
&& !chunk.river.is_river()
&& !chunk.is_underwater()
&& !matches!(
chunk.get_biome(),
common::terrain::BiomeKind::Lake | common::terrain::BiomeKind::Ocean
)
} else {
false
}
};
let on_flat_terrain = || -> bool {
sim.get_gradient_approx(loc)
.map(|grad| grad < 1.0)
.unwrap_or(false)
};
sim.get(loc).map_or(false, |chunk| {
let suitable_for_town = || -> bool {
let attributes = town_attributes_of_site(loc, sim);
attributes.map_or(false, |attributes| {
// aquifer and has_many_rocks was added to make mesa clifftowns suitable for towns
(attributes.potable_water || (attributes.aquifer && matches!(self, SiteKind::CliffTown)))
&& attributes.building_materials
&& attributes.heating
// Because of how the algorithm for site2 towns work, they have to start on land.
&& on_land()
})
};
match self {
SiteKind::Gnarling => {
on_land()
&& on_flat_terrain()
&& (-0.3..0.4).contains(&chunk.temp)
&& chunk.tree_density > 0.75
},
SiteKind::Adlet => chunk.temp < -0.2 && chunk.cliff_height > 25.0,
SiteKind::DwarvenMine => {
matches!(chunk.get_biome(), BiomeKind::Forest | BiomeKind::Desert)
&& !chunk.near_cliffs()
&& !chunk.river.near_water()
&& on_flat_terrain()
},
SiteKind::Haniwa => {
on_land()
&& on_flat_terrain()
&& (-0.3..0.4).contains(&chunk.temp)
},
SiteKind::GiantTree | SiteKind::Tree => {
on_land()
&& on_flat_terrain()
&& chunk.tree_density > 0.4
&& (-0.3..0.4).contains(&chunk.temp)
},
SiteKind::Citadel => true,
SiteKind::CliffTown => {
chunk.temp >= CONFIG.desert_temp
&& chunk.cliff_height > 40.0
&& chunk.rockiness > 1.2
&& suitable_for_town()
},
SiteKind::GliderCourse => {
chunk.alt > 1400.0
},
SiteKind::SavannahTown => {
matches!(chunk.get_biome(), BiomeKind::Savannah)
&& !chunk.near_cliffs()
&& !chunk.river.near_water()
&& suitable_for_town()
},
SiteKind::CoastalTown => {
(2.0..3.5).contains(&(chunk.water_alt - CONFIG.sea_level))
&& suitable_for_town()
},
SiteKind::PirateHideout => {
(0.5..3.5).contains(&(chunk.water_alt - CONFIG.sea_level))
},
SiteKind::Sahagin => {
matches!(chunk.get_biome(), BiomeKind::Ocean)
&& (40.0..45.0).contains(&(CONFIG.sea_level - chunk.alt))
},
SiteKind::JungleRuin => {
matches!(chunk.get_biome(), BiomeKind::Jungle)
},
SiteKind::RockCircle => !chunk.near_cliffs() && !chunk.river.near_water(),
SiteKind::TrollCave => {
!chunk.near_cliffs()
&& on_flat_terrain()
&& !chunk.river.near_water()
&& chunk.temp < 0.6
},
SiteKind::Camp => {
!chunk.near_cliffs() && on_flat_terrain() && !chunk.river.near_water()
},
SiteKind::DesertCity => {
(0.9..1.0).contains(&chunk.temp) && !chunk.near_cliffs() && suitable_for_town()
&& on_land()
&& !chunk.river.near_water()
},
SiteKind::ChapelSite => {
matches!(chunk.get_biome(), BiomeKind::Ocean)
&& CONFIG.sea_level < chunk.alt + 1.0
},
SiteKind::Terracotta => {
(0.9..1.0).contains(&chunk.temp)
&& on_land()
&& (chunk.water_alt - CONFIG.sea_level) > 50.0
&& on_flat_terrain()
&& !chunk.river.near_water()
&& !chunk.near_cliffs()
},
SiteKind::Myrmidon => {
(0.9..1.0).contains(&chunk.temp)
&& on_land()
&& (chunk.water_alt - CONFIG.sea_level) > 50.0
&& on_flat_terrain()
&& !chunk.river.near_water()
&& !chunk.near_cliffs()
},
SiteKind::Cultist => on_land() && chunk.temp < 0.5 && chunk.near_cliffs(),
SiteKind::VampireCastle => on_land() && chunk.temp <= -0.8 && chunk.near_cliffs(),
SiteKind::Castle => {
if chunk.tree_density > 0.4 || chunk.river.near_water() || chunk.near_cliffs() {
return false;
}
const HILL_RADIUS: i32 = 3 * TERRAIN_CHUNK_BLOCKS_LG as i32;
for x in (-HILL_RADIUS)..HILL_RADIUS {
for y in (-HILL_RADIUS)..HILL_RADIUS {
let check_loc = loc + Vec2::new(x, y);
if let Some(true) = sim
.get_alt_approx(check_loc)
.map(|surrounding_alt| surrounding_alt > chunk.alt + 1.0)
{
return false;
}
// Castles are really big, so to avoid parts of them ending up
// underwater or in other awkward positions
// we have to do this
if sim
.get(check_loc)
.map_or(true, |c| c.is_underwater() || c.near_cliffs())
{
return false;
}
}
}
true
},
SiteKind::Refactor | SiteKind::Settlement => suitable_for_town(),
SiteKind::Bridge(_, _) => true,
}
})
}
pub fn exclusion_radius(&self) -> i32 {
// FIXME: Provide specific values for each individual SiteKind
match self {
SiteKind::Myrmidon => 7,
_ => 8, // This is just an arbitrary value
}
}
pub fn exclusion_radius_clear(&self, sim: &WorldSim, loc: Vec2<i32>) -> bool {
let radius = self.exclusion_radius();
for x in (-radius)..radius {
for y in (-radius)..radius {
let check_loc = loc + Vec2::new(x, y);
if sim.get(check_loc).map_or(false, |c| !c.sites.is_empty()) {
return false;
}
}
}
true
}
}
impl Site {
pub fn is_dungeon(&self) -> bool {
matches!(
self.kind,
SiteKind::Adlet
| SiteKind::Gnarling
| SiteKind::ChapelSite
| SiteKind::Terracotta
| SiteKind::Haniwa
| SiteKind::Myrmidon
| SiteKind::DwarvenMine
| SiteKind::Cultist
| SiteKind::Sahagin
| SiteKind::VampireCastle
)
}
pub fn is_settlement(&self) -> bool {
matches!(
self.kind,
SiteKind::Settlement
| SiteKind::Refactor
| SiteKind::CliffTown
| SiteKind::DesertCity
| SiteKind::SavannahTown
| SiteKind::CoastalTown
)
}
pub fn is_castle(&self) -> bool { matches!(self.kind, SiteKind::Castle) }
pub fn is_bridge(&self) -> bool { matches!(self.kind, SiteKind::Bridge(_, _)) }
}
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct PointOfInterest {
pub name: String,
pub kind: PoiKind,
pub loc: Vec2<i32>,
}
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum PoiKind {
/// Peak stores the altitude
Peak(u32),
/// Lake stores a metric relating to size
Biome(u32),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_proximity_requirements() {
let world_dims = Aabr {
min: Vec2 { x: 0, y: 0 },
max: Vec2 {
x: 200_i32,
y: 200_i32,
},
};
let reqs = ProximityRequirementsBuilder::new().finalize(&world_dims);
assert!(reqs.satisfied_by(Vec2 { x: 0, y: 0 }));
}
#[test]
fn avoid_proximity_requirements() {
let world_dims = Aabr {
min: Vec2 {
x: -200_i32,
y: -200_i32,
},
max: Vec2 {
x: 200_i32,
y: 200_i32,
},
};
let reqs = ProximityRequirementsBuilder::new()
.avoid_all_of(vec![Vec2 { x: 0, y: 0 }].into_iter(), 10)
.finalize(&world_dims);
assert!(reqs.satisfied_by(Vec2 { x: 8, y: -8 }));
assert!(!reqs.satisfied_by(Vec2 { x: -1, y: 1 }));
}
#[test]
fn near_proximity_requirements() {
let world_dims = Aabr {
min: Vec2 {
x: -200_i32,
y: -200_i32,
},
max: Vec2 {
x: 200_i32,
y: 200_i32,
},
};
let reqs = ProximityRequirementsBuilder::new()
.close_to_one_of(vec![Vec2 { x: 0, y: 0 }].into_iter(), 10)
.finalize(&world_dims);
assert!(reqs.satisfied_by(Vec2 { x: 1, y: -1 }));
assert!(!reqs.satisfied_by(Vec2 { x: -8, y: 8 }));
}
#[test]
fn complex_proximity_requirements() {
let a_site = Vec2 { x: 572, y: 724 };
let world_dims = Aabr {
min: Vec2 { x: 0, y: 0 },
max: Vec2 {
x: 1000_i32,
y: 1000_i32,
},
};
let reqs = ProximityRequirementsBuilder::new()
.close_to_one_of(vec![a_site].into_iter(), 60)
.avoid_all_of(vec![a_site].into_iter(), 40)
.finalize(&world_dims);
assert!(reqs.satisfied_by(Vec2 { x: 572, y: 774 }));
assert!(!reqs.satisfied_by(a_site));
}
#[test]
fn location_hint() {
let reqs = ProximityRequirementsBuilder::new().close_to_one_of(
vec![Vec2 { x: 1, y: 0 }, Vec2 { x: 13, y: 12 }].into_iter(),
10,
);
let expected = Aabr {
min: Vec2 { x: 0, y: 0 },
max: Vec2 { x: 23, y: 22 },
};
let map_dims = Aabr {
min: Vec2 { x: 0, y: 0 },
max: Vec2 { x: 200, y: 300 },
};
assert_eq!(expected, reqs.location_hint(&map_dims));
}
}