// ADR-0162: bounded real player-origin generations, not a game rule. #include "../../tests/fixtures/cellular_ecology.hpp" #include "evo/server/lifecycle/cellular_population.hpp" #include #include namespace { using namespace evo; using namespace evo::server; using namespace evo::fixture; std::uint64_t number(std::string_view text) { std::uint64_t value{}; const auto parsed = std::from_chars(text.data(), text.data() + text.size(), value); require(parsed.ec == std::errc{} && parsed.ptr == text.data() + text.size(), "invalid number"); return value; } struct Result { std::uint64_t births{}, late_births{}, living{}, growth{}, conversions{}, restores{}; unsigned deepest{}; }; Result run(std::uint64_t seed, std::uint64_t ticks, std::uint32_t founders, bool maturing, bool rich, bool paired) { auto state = opening(seed, rich, founders); if (maturing) { state = checked(state->migration_upgrade_candidate(), "generation boundary upgrade"); state = checked(state->maturation_upgrade_candidate(), "generation maturation upgrade"); } auto population = paired ? checked(CellularPopulation::create(restore(*state)), "generation population") : nullptr; Result result; std::map generations; std::map roots, root_births; for (const auto& life : state->lives()) { generations.emplace(life.identity.life, 0); roots.emplace(life.identity.life, life.identity.life); } std::uint64_t first_bud = 0, blocked_buds = 0, peak = 0, cohort_ticks = 0; std::uint64_t engulfed = 0, starved = 0, twin_events = 0; std::vector frozen_players; // Once the first real funded bud succeeds, all controlled cells stay frozen. // No wildlife intention, mutation, birth or resource is injected by this driver. for (std::uint64_t index = 0; index < ticks; ++index) { if (population && index % 257 == 0) { std::set genomes; for (const auto& life : state->lives()) if (life.account == LifeAccount{}) genomes.insert(life.identity.genome); for (const auto genome : genomes) { if ((index / 257) % 2 == 0) result.conversions += checked(population->collapse(genome), "generation collapse"); else if (std::ranges::any_of(population->members(), [genome](const auto& member) { return member.genome == genome; })) result.conversions += checked(population->materialize(genome), "generation materialize"); } require(population->state().snapshot() == state->snapshot(), "generation conversion changed biology"); } auto intents = checked(wildlife_intents(*state, state->tick() + 1), "generation wildlife"); std::vector players; for (const auto& life : state->lives()) { if (life.account == LifeAccount{}) continue; CellularLifeIntent controlled{life.identity.life, {}}; controlled.intent.frozen = first_bud != 0; controlled.intent.bud = first_bud == 0 && (state->tick() + 1) % 100 == 0; intents.push_back(controlled); players.push_back(controlled); if (population) require(!population->in_cohort(life.identity.life), "player entered cohort"); } std::ranges::sort(intents, {}, &CellularLifeIntent::life_id); const auto executed = checked(state->advance_tick_with_mutations(intents, {}), "generation biology"); if (population) { const auto candidate = checked(population->advance_tick(players), "generation paired tick"); require(candidate.step == executed.step && candidate.buds == executed.buds && candidate.mutations == executed.mutations && candidate.growth == executed.growth, "generation paired events diverged"); require(population->state().snapshot() == state->snapshot(), "generation paired biology diverged"); require(population->state().current_total() == population->state().opening_total(), "generation paired stock did not close"); cohort_ticks += population->members().size(); } result.growth += executed.growth.size(); for (const auto& bud : executed.buds) blocked_buds += bud.result == CellularBudResult::no_placement; for (const auto& corpse : executed.step.starved) { engulfed += corpse.cause == biology::CellularDeathCause::engulfed; starved += corpse.cause == biology::CellularDeathCause::starved; } std::map parent_births; std::uint64_t living = 0; for (const auto& life : state->lives()) { if (life.account != LifeAccount{}) continue; ++living; if (generations.contains(life.identity.life)) continue; const auto parent = generations.find(life.parent_life); require(parent != generations.end(), "wildlife lacks retained player-origin parent"); const auto generation = parent->second + 1; generations.emplace(life.identity.life, generation); const auto root = roots.at(life.parent_life); roots.emplace(life.identity.life, root); ++root_births[root]; result.deepest = std::max(result.deepest, generation); ++result.births; ++parent_births[life.parent_life]; result.late_births += index >= ticks * 3 / 4; if (first_bud == 0) first_bud = state->tick(); } for (const auto& [parent, count] : parent_births) { static_cast(parent); twin_events += count == 2; } peak = std::max(peak, living); require(state->current_total() == state->opening_total(), "generation stock did not close"); // Presence may change during disconnection. Frozen biology must not. for (const auto& before : frozen_players) { const auto after = std::ranges::find(state->lives(), before.identity.life, [](const auto& life) { return life.identity.life; }); require(after != state->lives().end() && after->identity == before.identity && after->growth == before.growth && after->position == before.position && after->body == before.body && after->reserve == before.reserve && after->mutation == before.mutation, "frozen player biology changed"); } if (first_bud != 0 && frozen_players.empty()) for (const auto& life : state->lives()) if (life.account != LifeAccount{}) frozen_players.push_back(life); if (index % 4096 == 4095) { const auto bytes = state->snapshot(); const auto input = checked(wildlife_intents(*state, state->tick() + 1), "before restore"); state = restore(*state); require(state->snapshot() == bytes && checked(wildlife_intents(*state, state->tick() + 1), "after restore") == input, "generation raw restore changed bytes or controller"); if (population) { const auto envelope = population->snapshot(); population = checked(CellularPopulation::restore(envelope, state->world(), state->region(), state->frame(), state->tick()), "generation population restore"); require(population->snapshot() == envelope && population->state().snapshot() == bytes && checked(wildlife_intents(population->state(), state->tick() + 1), "population restored controller") == input, "generation population restore diverged"); } ++result.restores; } } std::uint64_t adults = 0; for (const auto& life : state->lives()) if (life.account == LifeAccount{}) { ++result.living; adults += life.growth.realized_per_thousand == 1000; } std::cout << "{\"kind\":\"generations\",\"seed\":" << seed << ",\"rules\":" << state->rules_version() << ",\"founders\":" << founders << ",\"ticks\":" << ticks << ",\"first_bud\":" << first_bud << ",\"births\":" << result.births << ",\"deepest\":" << result.deepest << ",\"peak\":" << peak << ",\"living\":" << result.living << ",\"adults\":" << adults << ",\"growth\":" << result.growth << ",\"blocked_buds\":" << blocked_buds << ",\"engulfed\":" << engulfed << ",\"starved\":" << starved << ",\"twin_events\":" << twin_events << ",\"rich\":" << (rich ? "true" : "false") << ",\"paired\":" << (paired ? "true" : "false") << ",\"late_births\":" << result.late_births << ",\"conversions\":" << result.conversions << ",\"cohort_member_ticks\":" << cohort_ticks << ",\"restores\":" << result.restores << "}\n"; for (const auto& [root, count] : root_births) std::cout << "{\"kind\":\"ancestry\",\"root\":" << root << ",\"births\":" << count << "}\n"; std::cout.flush(); return result; } } // namespace int main(int argc, char** argv) try { if (argc == 2 && std::string_view{argv[1]} == "--qualify") { bool passed = true; for (std::uint64_t seed = 0; seed < 4; ++seed) { const auto result = run(seed, 80'000, 4, true, true, true); passed = passed && result.deepest >= 3 && result.living > 0 && result.late_births > 0 && result.conversions > 0 && result.restores > 0; } const auto legacy = run(7, 80'000, 1, false, true, true); require(legacy.births > 0 && legacy.deepest == 1 && legacy.growth == 0, "legacy generation control changed"); const auto scarce = run(0, 80'000, 4, true, false, true); require(scarce.births == 0 && scarce.living == 0, "scarce opening unexpectedly funded wildlife"); require(passed, "declared rich generation qualification failed; retain all results"); return 0; } if (argc == 2 && std::string_view{argv[1]} == "--verify") { for (const bool maturing : {false, true}) { const auto result = run(7, 12'000, 1, maturing, true, true); require(result.births > 0 && result.conversions > 0 && result.restores > 0, "generation contract missed funded births/conversions/restores"); require(maturing ? result.growth > 0 : (result.growth == 0 && result.deepest == 1), "generation rule control failed"); } return 0; } std::uint64_t seed = 7, ticks = 40'000, founders = 1, rules = 9; bool rich = true, paired = false; require(argc % 2 == 1, "expected option/value pairs"); for (int index = 1; index < argc; index += 2) { const std::string_view option{argv[index]}; const auto value = number(argv[index + 1]); if (option == "--seed") seed = value; else if (option == "--ticks") ticks = value; else if (option == "--founders") founders = value; else if (option == "--rules") rules = value; else if (option == "--rich" || option == "--paired") { require(value <= 1, "expected boolean zero or one"); (option == "--rich" ? rich : paired) = value != 0; } else throw std::runtime_error("unknown generation option"); } require(seed <= 1'000'000 && ticks > 0 && ticks <= 1'000'000 && founders > 0 && founders <= 16 && (rules == 8 || rules == 9), "generation work bound"); run(seed, ticks, static_cast(founders), rules == 9, rich, paired); return 0; } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; }