#pragma once // Current rules-8 funded reference fixture, shared by population qualification. #include "evo/server/lifecycle/cellular_batch.hpp" #include "evo/server/lifecycle/cellular_play_summary.hpp" #include "evo/server/lifecycle/wildlife_controller.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace evo::fixture { using namespace evo; using namespace evo::server; using accounting::Inventory; using Owner = std::unique_ptr; inline void require(bool condition, const char* message) { if (!condition) throw std::runtime_error(message); } template T checked(std::expected result, const char* message) { if (!result) throw std::runtime_error(std::string{message} + ": " + std::to_string(static_cast(result.error()))); return std::move(*result); } inline Owner restore(const CellularLives& state) { return checked(CellularLives::restore(state.snapshot(), state.world(), state.region(), state.frame(), state.tick()), "reference restore"); } inline Owner opening(std::uint64_t seed, bool rich, std::uint32_t count = 4, bool populated = true, std::uint64_t region = 1) { require(count > 0 && count <= biology::max_cellular_tick_lives, "fixture population bound"); const std::uint32_t side = count == 4 ? 6'000 : 48'000; const auto world = 200 + seed; const auto endowment = checked(biology::primitive_endowment(biology::primitive_founder_v3), "reference founder endowment"); auto funding = endowment.body; funding.energy.value = count * (endowment.reserve.energy.value + endowment.construction.value); for (auto& channel : funding.matter) channel.value *= count; std::vector sites{{1'000, 1'000}, {1'000, 4'000}, {4'000, 1'000}, {4'000, 4'000}}; if (count != 4) { sites.clear(); for (std::int32_t index = 0; index < static_cast(count); ++index) sites.push_back({1'000 + (index / 16) * 3'000, 1'000 + (index % 16) * 3'000}); } auto primitive = checked(PrimitiveLives::create({world, region, 1, 17, funding, sites}), "reference nursery"); std::vector pools; for (const auto& site : sites) { biology::CellularPatch pool; pool.address = {static_cast(site.x_um), static_cast(site.y_um)}; pool.radius_um = 500; pool.source = 18; pool.stock.energy.value = rich ? 1'000'000'000'000ULL : 0; for (auto& channel : pool.stock.matter) channel.value = rich ? 1'000'000'000'000ULL : 1'000; pools.push_back(pool); } Inventory amount{{rich ? 1'000'000ULL : 10'000ULL}, {}}; for (auto& channel : amount.matter) channel.value = rich ? 100'000 : 10; auto config = checked(biology::scatter_deposits({world, region, 1, side, side, {}}, pools, {64, 19, amount, seed}), "reference deposits"); if (rich) { // Explicit finite contact food on each founding site. It is not a reserve grant // or replenishment; the owner must acquire and spend it through real biology. for (const auto& site : sites) for (std::uint32_t channel = 0; channel < 5; ++channel) { biology::CellularPatch mote; mote.address = {static_cast(site.x_um) + 10 + channel * 2, static_cast(site.y_um) + 10}; mote.source = 20; mote.id = config.next_deposit++; mote.radius_um = biology::cellular_mote_radius_um; mote.kind = biology::CellularDepositKind::mote; if (channel == 0) mote.stock.energy.value = 1'000'000'000'000ULL; else mote.stock.matter[channel - 1].value = 1'000'000'000'000ULL; config.patches.push_back(mote); } std::ranges::sort(config.patches, {}, &biology::CellularPatch::address); } auto habitat = checked(biology::CellularHabitat::create(std::move(config)), "reference habitat"); auto state = checked(CellularLives::create(*primitive, std::move(habitat), {{side, side}, 0, 293'150}, CellularLivesProfile::foraging), "reference cellular owner"); if (!populated) return state; require(state->advance_tick(std::span{}).has_value(), "reference genesis tick"); for (std::uint64_t index = 0; index < count; ++index) { LifeRequest request{}; request.account.back() = static_cast(index + 1); request.account[request.account.size() - 2] = static_cast((index + 1) >> 8U); request.action = 1; const auto identity = 100 + index; state = checked(state->birth_candidate( request, {identity, identity, identity, identity, {state->tick()}}), "reference funded birth"); } require(state->rules_version() == cellular_lives_foraging_rules && state->habitat().config().format == biology::cellular_habitat_deposit_format, "reference version bundle"); return state; } inline CellularTickIntents inputs(const CellularLives& state, std::uint64_t seed, const std::set& mutated) { CellularTickIntents tick; const auto wildlife = checked(wildlife_intents(state, state.tick() + 1), "reference wildlife"); for (const auto& life : state.lives()) { CellularLifeIntent intent{life.identity.life, {}}; if (life.account == LifeAccount{}) { const auto found = std::ranges::find(wildlife, life.identity.life, &CellularLifeIntent::life_id); require(found != wildlife.end(), "reference missing wildlife intention"); intent = *found; } else { const auto phase = (state.tick() + 1) % 1024; intent.intent.frozen = life.identity.life == 100 && phase >= 128 && phase < 384; if (!intent.intent.frozen) { // Fixture players request real actions; the owner alone admits/funds them. if ((state.tick() + 1 + life.identity.life) % 100 == 0) intent.intent.bud = true; else if ((state.tick() + 1) % 197 == 0) intent.intent.engulf = true; else if (state.tick() >= 4096) { const auto direction = (state.tick() / 64 + life.identity.life + seed) % 4; constexpr std::array directions{ {{1'000, 0}, {0, 1'000}, {-1'000, 0}, {0, -1'000}}}; intent.intent.movement = directions[direction]; } } if (!intent.intent.frozen && life.presence == CellularPresence::active && !mutated.contains(life.identity.life) && life.mutation.offer) { const auto genome = checked(state.archive().find(life.identity.genome), "reference offered genome"); const auto& offer = *life.mutation.offer; for (std::size_t offset = 0; offset < offer.candidates.size(); ++offset) { const auto& choice = offer.candidates[(offset + seed) % offer.candidates.size()]; const auto successor = 1'000'000 + state.tick() * 4 + life.identity.life - 100; const auto funded = biology::fund_mutation(genome, offer.context, offer, choice.operator_id, successor, successor, life.reserve); if (funded) { tick.mutations.push_back({life.identity.life, life.mutation.event_index, life.mutation.offer_tick, successor, choice.operator_id}); break; } } } } tick.lives.push_back(intent); } return tick; } inline Owner cellular_descendants() { auto state = opening(1, true); std::set mutated; for (unsigned step = 0; step < 8192; ++step) { const auto input = inputs(*state, 1, mutated); const auto executed = checked(state->advance_tick_with_mutations(input.lives, input.mutations), "descendant fixture tick"); for (const auto& mutation : executed.mutations) if (mutation.result == CellularMutationResult::applied) mutated.insert(mutation.life_id); } require(std::ranges::any_of(state->lives(), [](const auto& life) { return life.account == LifeAccount{}; }), "fixture has no funded descendant"); return state; } } // namespace evo::fixture