Settlements were static -- every town grew with one global rate to the same
habitability cap (frozen in Live World), so they grew "the same amount
everywhere". Now stepCivilization(dtHours, liveTime) is environment-driven and
time-varying. All drivers are deterministic functions of (region, year, seed),
so step-back replays them with no new saved/snapshot state (population is
already snapshotted).
- Differentiated growth: rate scales with habitability (fertile boom, marginal
crawl); capacity K = maxPop * habitability * siteQuality * conditions.
- Site quality: max city size varies by an order of magnitude with location --
a great river (log-scaled discharge) or coast hosts a metropolis, a dry inland
cell a town (civSiteVariety). This spreads final sizes ~8-9x instead of all
saturating equally.
- Conditions (per ~20deg region, per year): multi-year droughts (worse in arid
regions), year-to-year harvests (bigger swings inland), rare cold years, river
floods, and the existing volcano ash. Regional, so different parts of a
continent have different fortunes.
- Storms kill directly: a system within its radius of a town deals deaths scaled
by strength (hurricanes x civHurricaneDeathMult) -- a parked hurricane can gut
a coastal city. Reads the already-snapshotted storms().
- Famine: accelerated loss when food < population; sustained drought / acute
disaster can collapse a settlement to ruins (revives when conditions return).
- Viewer: markers withered-tinted by hardship (3D + 2D), cell-info "drought/
conditions" line, and cause-attributed kind=3 events ("Hurricane X devastates
Y", "Famine shrinks Y to a Town", tier up/down/abandon).
New civ* knobs (self-describing config -> no save bump). test_civ extended:
sizes vary widely (not uniform), bad-year declines, a hurricane over a town
kills its population. All 10 suites pass; GUI build clean. Docs updated.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
192 lines
10 KiB
C++
192 lines
10 KiB
C++
// Headless test for civilization Step 2 (habitability + settlements). No display needed.
|
|
//
|
|
// g++ -std=c++17 -O2 -Isrc/sim test_civ.cpp src/sim/IcoSphere.cpp src/sim/Planet.cpp \
|
|
// src/sim/PlanetTectonics.cpp src/sim/PlanetDrift.cpp src/sim/PlanetErosion.cpp \
|
|
// src/sim/PlanetHydrology.cpp src/sim/PlanetBiomes.cpp src/sim/PlanetClimate.cpp \
|
|
// src/sim/PlanetLive.cpp src/sim/PlanetOcean.cpp src/sim/PlanetWeather.cpp \
|
|
// src/sim/PlanetVolcano.cpp src/sim/PlanetBiota.cpp src/sim/PlanetFloraGen.cpp \
|
|
// src/sim/PlanetFaunaGen.cpp src/sim/PlanetFungiGen.cpp src/sim/NameGen.cpp \
|
|
// src/sim/PlanetGeography.cpp src/sim/PlanetEcoregions.cpp src/sim/PlanetCiv.cpp \
|
|
// src/sim/PlanetIO.cpp -o /tmp/tc && /tmp/tc
|
|
//
|
|
// Verifies: habitability range/zeros; placement spacing/cap/land + unique names; food-driven growth
|
|
// and decline; tiers; determinism + RNG isolation; population snapshot round-trip; v20 save; reseed clear.
|
|
|
|
#include "Planet.hpp"
|
|
#include <cstdio>
|
|
#include <cmath>
|
|
#include <algorithm>
|
|
#include <set>
|
|
#include <sstream>
|
|
|
|
static int failures = 0;
|
|
static void check(bool cond, const char* what) {
|
|
std::printf(" [%s] %s\n", cond ? "PASS" : "FAIL", what);
|
|
if (!cond) ++failures;
|
|
}
|
|
static void settle(Planet& p, int maxSteps = 800) {
|
|
int run = 0;
|
|
for (int s = 0; s < maxSteps; ++s) { double mc = p.step(); if (mc < 2.0) { if (++run >= 3) break; } else run = 0; }
|
|
p.computeClimate(); p.classifyBiomes();
|
|
}
|
|
static void drift(Planet& p, int iters) {
|
|
p.drifting = true;
|
|
for (int k = 0; k < iters; ++k) { double dt = p.cflDtMy(); p.advect(dt); p.step(); p.erode(dt); if (k >= iters/2) p.hydrology(dt*0.2); }
|
|
p.computeClimate(); p.classifyBiomes();
|
|
}
|
|
|
|
int main() {
|
|
PlanetConfig cfg; cfg.subdivisions = 5; cfg.seed = 4242;
|
|
Planet p; p.generate(cfg); settle(p); drift(p, 400);
|
|
const int n = (int)p.cells.size();
|
|
const double sea = p.cfg.seaLevel;
|
|
const double yearH = p.cfg.dayLengthHours * p.cfg.yearLengthDays;
|
|
|
|
std::printf("Civ: habitability field\n");
|
|
p.computeHabitability();
|
|
const auto& H = p.habitability();
|
|
check((int)H.size() == n, "habitability sized n");
|
|
bool ranged = true, zeroWaterIce = true, anyHabitable = false;
|
|
for (int i = 0; i < n; ++i) {
|
|
if (!(std::isfinite(H[i]) && H[i] >= 0.0 && H[i] <= 1.0)) ranged = false;
|
|
if ((p.cells[i].elevation <= sea || p.cells[i].biome == Biome::Ice) && H[i] != 0.0) zeroWaterIce = false;
|
|
if (H[i] > 0.3) anyHabitable = true;
|
|
}
|
|
check(ranged, "habitability in [0,1]");
|
|
check(zeroWaterIce, "habitability 0 on ocean/ice");
|
|
check(anyHabitable, "some land is habitable");
|
|
|
|
std::printf("Civ: placement\n");
|
|
p.placeSettlements();
|
|
const auto& S = p.settlements;
|
|
check(!S.empty(), "settlements placed");
|
|
bool onLand = true, aboveMin = true, capOk = (int)S.size() <= p.cfg.civMaxSettlements;
|
|
std::set<std::string> names; bool uniqueNames = true;
|
|
const double sepCos = std::cos(p.cfg.civMinSpacingRadians);
|
|
bool spaced = true;
|
|
for (size_t a = 0; a < S.size(); ++a) {
|
|
if (p.cells[S[a].cell].elevation <= sea) onLand = false;
|
|
if (p.habitability()[S[a].cell] < p.cfg.civMinHabitability - 1e-9) aboveMin = false;
|
|
if (!names.insert(S[a].name).second || S[a].name.empty()) uniqueNames = false;
|
|
for (size_t b = a + 1; b < S.size(); ++b)
|
|
if (p.cells[S[a].cell].unit.dot(p.cells[S[b].cell].unit) > sepCos + 1e-9) spaced = false;
|
|
if (p.cellSettlement()[S[a].cell] != (int)a) onLand = false; // index consistency
|
|
}
|
|
std::printf(" %d settlements\n", (int)S.size());
|
|
check(onLand, "settlements sit on land + cellSettlement index is consistent");
|
|
check(aboveMin, "settlements only on cells >= civMinHabitability");
|
|
check(spaced, "settlements respect the minimum spacing");
|
|
check(capOk, "settlement count within the cap");
|
|
check(uniqueNames, "settlement names are unique + non-empty");
|
|
|
|
std::printf("Civ: environment-driven growth (differentiated + dynamic)\n");
|
|
{
|
|
int gi = 0, lo = 0;
|
|
for (size_t k = 0; k < S.size(); ++k) {
|
|
if (p.habitability()[S[k].cell] > p.habitability()[S[gi].cell]) gi = (int)k;
|
|
if (p.habitability()[S[k].cell] < p.habitability()[S[lo].cell]) lo = (int)k;
|
|
}
|
|
double g0 = p.settlements[gi].population;
|
|
double lt = 0.0; bool anyDecline = false;
|
|
std::vector<double> prev(p.settlements.size());
|
|
for (int yr = 0; yr < 600; ++yr) {
|
|
for (size_t k = 0; k < p.settlements.size(); ++k) prev[k] = p.settlements[k].population;
|
|
lt += 2.0 * yearH;
|
|
p.stepCivilization(2.0 * yearH, lt); // advance the clock so harvests/droughts vary
|
|
for (size_t k = 0; k < p.settlements.size(); ++k)
|
|
if (p.settlements[k].population > p.cfg.civAbandonPop && p.settlements[k].population < prev[k] * 0.999) anyDecline = true;
|
|
}
|
|
check(p.settlements[gi].population > g0 * 2.0, "a fertile settlement grows strongly");
|
|
// The user's complaint was "they grow the same amount everywhere": now sizes must vary widely.
|
|
std::vector<double> pops;
|
|
for (const auto& s : p.settlements) if (s.population >= p.cfg.civAbandonPop) pops.push_back(s.population);
|
|
std::sort(pops.begin(), pops.end());
|
|
double med = pops.empty() ? 0.0 : pops[pops.size() / 2];
|
|
double mx = pops.empty() ? 0.0 : pops.back();
|
|
std::printf(" alive %d median %.0f max %.0f (max/median %.1f)\n",
|
|
(int)pops.size(), med, mx, med > 0 ? mx / med : 0.0);
|
|
check(!pops.empty() && mx > med * 3.0, "settlement sizes vary widely (env-driven, not uniform growth)");
|
|
check(p.settlements[gi].population > p.settlements[lo].population, "fertile ends larger than marginal");
|
|
check(anyDecline, "settlements decline in bad years (harvest/drought dynamics, not monotonic)");
|
|
// Over-capacity settlement declines toward its food limit.
|
|
p.settlements[gi].population = 5.0e7;
|
|
double over = p.settlements[gi].population;
|
|
for (int yr = 0; yr < 200; ++yr) { lt += 2.0 * yearH; p.stepCivilization(2.0 * yearH, lt); }
|
|
check(p.settlements[gi].population < over, "an over-capacity settlement declines toward its food limit");
|
|
}
|
|
|
|
std::printf("Civ: a hurricane over a town kills people\n");
|
|
{
|
|
Planet w; w.generate(cfg); settle(w); drift(w, 400); w.placeSettlements();
|
|
if (!w.settlements.empty()) {
|
|
int si = 0; for (size_t k = 0; k < w.settlements.size(); ++k) if (w.settlements[k].population > w.settlements[si].population) si = (int)k;
|
|
w.settlements[si].population = 1.0e5;
|
|
WeatherSnapshot snap = w.captureWeather();
|
|
WeatherSystem ws; ws.id = 999; ws.pos = w.cells[w.settlements[si].cell].unit;
|
|
ws.radius = 0.3; ws.strength = 1.0; ws.tropical = true; ws.life = 1e9;
|
|
snap.storms.push_back(ws);
|
|
w.restoreWeather(snap); // inject a stationary hurricane over the town
|
|
double before = w.settlements[si].population;
|
|
double lt = 0.0; for (int yr = 0; yr < 3; ++yr) { lt += yearH; w.stepCivilization(yearH, lt); }
|
|
check(w.settlements[si].population < before * 0.9, "a hurricane parked over a town kills its population");
|
|
}
|
|
}
|
|
|
|
std::printf("Civ: tiers\n");
|
|
check(settleTierOf(100.0, p.cfg.civTownPop, p.cfg.civCityPop) == SettleTier::Village
|
|
&& settleTierOf(p.cfg.civTownPop, p.cfg.civTownPop, p.cfg.civCityPop) == SettleTier::Town
|
|
&& settleTierOf(p.cfg.civCityPop, p.cfg.civTownPop, p.cfg.civCityPop) == SettleTier::City,
|
|
"tier thresholds (village/town/city)");
|
|
|
|
std::printf("Civ: determinism\n");
|
|
Planet q; q.generate(cfg); settle(q); drift(q, 400); q.placeSettlements();
|
|
bool same = (q.settlements.size() == S.size());
|
|
if (same) for (size_t k = 0; k < S.size(); ++k)
|
|
if (q.settlements[k].cell != p.settlements[k].cell || q.settlements[k].name != p.settlements[k].name) { same = false; break; }
|
|
check(same, "placeSettlements is deterministic");
|
|
|
|
std::printf("Civ: RNG isolation from tectonics\n");
|
|
Planet x; x.generate(cfg); settle(x);
|
|
Planet y; y.generate(cfg); settle(y);
|
|
for (int k = 0; k < 40; ++k) {
|
|
double dx = x.cflDtMy(); x.advect(dx); x.step(); x.erode(dx);
|
|
double dy = y.cflDtMy(); y.advect(dy); y.step(); y.erode(dy);
|
|
if (k == 20) { y.placeSettlements(); y.stepCivilization(yearH, yearH); }
|
|
}
|
|
bool terrainSame = true;
|
|
for (int i = 0; i < n; ++i) if (std::fabs(x.cells[i].elevation - y.cells[i].elevation) > 1e-9) terrainSame = false;
|
|
check(terrainSame, "placeSettlements/stepCivilization never perturb tectonic evolution");
|
|
|
|
std::printf("Civ: population snapshot round-trip\n");
|
|
{
|
|
WeatherSnapshot snap = p.captureWeather();
|
|
for (auto& st : p.settlements) st.population = 12345.0;
|
|
p.restoreWeather(snap);
|
|
bool restored = true;
|
|
for (size_t k = 0; k < p.settlements.size(); ++k) if (std::fabs(p.settlements[k].population - snap.settlementPop[k]) > 1e-9) restored = false;
|
|
check(snap.settlementPop.size() == p.settlements.size() && restored, "captureWeather/restoreWeather round-trips populations");
|
|
}
|
|
|
|
std::printf("Civ: save v20 round-trip\n");
|
|
{
|
|
std::stringstream ss(std::ios::in | std::ios::out | std::ios::binary);
|
|
p.writeState(ss);
|
|
Planet r;
|
|
bool ok = r.readState(ss, true, true, true, true, true, true, true, true, true, true, true);
|
|
check(ok, "readState accepts a v20 stream");
|
|
bool match = (r.settlements.size() == p.settlements.size());
|
|
if (match) for (size_t k = 0; k < p.settlements.size(); ++k)
|
|
if (r.settlements[k].cell != p.settlements[k].cell || r.settlements[k].name != p.settlements[k].name
|
|
|| std::fabs(r.settlements[k].population - p.settlements[k].population) > 1e-6) { match = false; break; }
|
|
check(match, "settlements round-trip through save");
|
|
check(r.cellSettlement() == p.cellSettlement(), "cellSettlement index rebuilt on load");
|
|
}
|
|
|
|
std::printf("Civ: reseed clears settlements\n");
|
|
p.generate(cfg);
|
|
check(p.settlements.empty() && (p.cellSettlement().empty() || p.cellSettlement()[0] == -1), "reseed clears the settlement set");
|
|
|
|
std::printf(failures ? "\nFAILURES: %d\n" : "\nALL CIV CHECKS PASSED\n", failures);
|
|
return failures ? 1 : 0;
|
|
}
|