#include "evo/server/lifecycle/cellular_batch.hpp" #include "fixtures/cellular_ecology.hpp" #include #include #include // Rules 12 owner (Cellular_Region_Movement_Plan.md phase 2): the format 9 -> 10 upgrade, liquid // motion inside the owner's tick, moving food and exact restore. namespace { using namespace evo; using namespace evo::server; using namespace evo::fixture; constexpr biology::CellularArea area{48'000, 48'000}; CellularLiquidRules rules(std::uint64_t seed = 5) { return {.currents = {seed, 48'000, 48'000, 250, 2, 4, 3'000, 6'000, 60, 300}, .founder = {.thrust_q10 = 20'480, .reverse_thrust_q10 = 20'480, .forward_drag_ppm = 95'163, .lateral_drag_ppm = 95'163, .turn = 3'276}, .deposits = {.drift_radius_um = 200, .max_drift_q10 = 100 * 1'024, .drift_decay_ppm = 95'163, .merged_cap = 8, .bubble_radius_um = biology::cellular_mote_radius_um}}; } // A rules-12 region whose founder is born after the upgrade (founder 5, schema 6). Owner liquid_region() { auto state = opening(7, true, 1, false); checked(state->advance_tick({}), "genesis tick"); state = checked(state->migration_upgrade_candidate(), "boundary upgrade"); state = checked(state->maturation_upgrade_candidate(), "maturation upgrade"); state = checked(state->inheritance_upgrade_candidate(), "inheritance upgrade"); state = checked(state->photosynthesis_upgrade_candidate({0, 1, {0}}), "light upgrade"); require(!state->liquid_upgrade_candidate(CellularLiquidRules{}), "empty rules accepted"); auto wrong = rules(); wrong.currents.width_um = 30'000; require(!state->liquid_upgrade_candidate(wrong), "rules for another area accepted"); state = checked(state->liquid_upgrade_candidate(rules()), "liquid upgrade"); require(state->format_version() == cellular_lives_liquid_format && state->rules_version() == cellular_lives_liquid_rules && state->liquid_rules() == rules() && state->flow_field() != nullptr && state->habitat().config().format == biology::cellular_habitat_liquid_format && !state->liquid_upgrade_candidate(rules()), "liquid upgrade repeated, wrong versions or lost rules"); for (const auto& patch : state->habitat().patches()) require(patch.merged == 1, "upgraded deposit without a merged count"); LifeAccount account{}; account.back() = 1; state = checked(state->birth_candidate({account, 1}, {100, 100, 100, 100, {state->tick()}}), "founder 5 birth"); const auto genome = state->archive().find(100); require(genome && genome->schema_version() == biology::genome_schema_v6 && genome->movement_arrangement() == biology::founder_movement_arrangement, "founder 5 is not schema 6 with the founder arrangement"); return state; } std::vector swim(const CellularLives& state, biology::CellularIntent movement, bool brake = false) { std::vector result; for (const auto& life : state.lives()) { CellularLifeIntent input{life.identity.life, {}}; input.intent.movement = movement; input.intent.brake = brake; result.push_back(input); } return result; } void closed(const CellularLives& state) { const auto& ledger = state.boundary_ledger(); auto expected = state.opening_total(); expected.energy.value += ledger.incoming.energy.value - ledger.outgoing.energy.value; for (std::size_t channel = 0; channel < expected.matter.size(); ++channel) expected.matter[channel].value += ledger.incoming.matter[channel].value - ledger.outgoing.matter[channel].value; require(state.current_total() == expected, "liquid region broke closure"); } void upgrade_and_restore() { auto state = liquid_region(); const auto copy = restore(*state); require(copy->snapshot() == state->snapshot() && copy->liquid_rules() == rules() && copy->flow_field()->digest() == state->flow_field()->digest(), "format 10 did not restore exactly"); } void swimming_moves_and_pays() { auto state = liquid_region(); const auto start = state->lives().front(); for (int tick = 0; tick < 40; ++tick) checked(state->advance_tick(swim(*state, {1'000, 0})), "swim tick"); const auto& life = state->lives().front(); require(life.motion.velocity_x_q10 > 150 * 1'024, "the founder did not reach swimming speed"); require(life.position.x_um > start.position.x_um + 100, "the founder did not move east"); closed(*state); // Braking brings the relative velocity to rest. for (int tick = 0; tick < 40; ++tick) checked(state->advance_tick(swim(*state, {}, true)), "brake tick"); require(state->lives().front().motion.velocity_x_q10 == 0 && state->lives().front().motion.velocity_y_q10 == 0, "braking did not stop the founder relative to the liquid"); closed(*state); } void food_moves_and_merges_without_loss() { auto state = liquid_region(); const auto before = state->habitat().config().patches; for (int tick = 0; tick < 400; ++tick) checked(state->advance_tick(swim(*state, {})), "drift tick"); const auto after = state->habitat().config().patches; const auto moved = std::ranges::count_if(after, [&before](const biology::CellularPatch& p) { const auto old = std::ranges::find(before, p.id, &biology::CellularPatch::id); return old != before.end() && old->address != p.address; }); require(moved > 0, "no bubble moved with the current"); for (const auto& patch : after) if (patch.kind == biology::CellularDepositKind::pool) require(std::ranges::any_of(before, [&patch](const biology::CellularPatch& old) { return old.id == patch.id && old.address == patch.address; }), "a pool moved"); closed(*state); } void restore_continues_exactly() { auto continuous = liquid_region(); for (int tick = 0; tick < 30; ++tick) checked(continuous->advance_tick(swim(*continuous, {600, 800})), "continuous tick"); auto resumed = restore(*continuous); for (int tick = 0; tick < 30; ++tick) { checked(continuous->advance_tick(swim(*continuous, {-700, 300})), "continuous tick"); checked(resumed->advance_tick(swim(*resumed, {-700, 300})), "resumed tick"); } require(continuous->snapshot() == resumed->snapshot(), "restore diverged from a live run"); } // A trace of swimming and braking replays to the exact live result (brake is flag bit 1). void trace_replays_brakes() { auto live = liquid_region(); const auto base = restore(*live); CellularBatch batch; batch.format_version = cellular_batch_inheritance_format; batch.header = {live->world(), live->region(), live->frame(), {}, {}, live->tick(), 0}; batch.header.predecessor.fill(1); // Any bound commit; this test replays portably. batch.header.predecessor_digest.fill(2); for (int tick = 0; tick < 24; ++tick) { const auto intents = swim(*live, {static_cast(tick < 12 ? 1'000 : 0), 0}, tick >= 12); checked(live->advance_tick(intents), "recorded tick"); batch.ticks.push_back({intents, {}, {}}); } batch.header.end_tick = live->tick(); const auto bytes = checked(encode_cellular_batch(batch), "brake trace encodes"); const auto decoded = checked(decode_cellular_batch(bytes), "brake trace decodes"); require(decoded == batch && decoded.ticks.back().lives.front().intent.brake, "the brake did not survive the trace"); const auto replayed = checked(replay_cellular_batch(*base, decoded), "brake trace replays"); require(replayed->snapshot() == live->snapshot(), "replay diverged from the live run"); } void older_rules_refuse_brake() { auto state = opening(7, true, 1); require(!state->advance_tick(swim(*state, {}, true)), "rules 8 accepted a brake"); } } // namespace int main() { try { upgrade_and_restore(); swimming_moves_and_pays(); food_moves_and_merges_without_loss(); restore_continues_exactly(); trace_replays_brakes(); older_rules_refuse_brake(); } catch (const std::exception& error) { std::fprintf(stderr, "%s\n", error.what()); return 1; } std::puts("cellular liquid owner contracts passed"); return 0; }