#include "evo/biology/founder.hpp" #include "evo/biology/mutation.hpp" #include "evo/server/lifecycle/cellular_light.hpp" #include "evo/server/lifecycle/wildlife_controller.hpp" #include #include #include #include #include namespace { using namespace evo; using namespace evo::biology; using namespace evo::server; void require(bool condition, const char* message) { if (!condition) throw std::runtime_error(message); } template T checked(std::expected value, const char* message) { if (!value) throw std::runtime_error(std::string{message} + ": " + std::to_string(static_cast(value.error()))); return std::move(*value); } Genome with(const Genome& base, std::uint32_t efficiency) { GenomeDraft draft{base.id() + 1, 2, base.development_version(), {base.id()}, {}, {}}; draft.schema_version = base.schema_version(); draft.capabilities.assign(base.capabilities().begin(), base.capabilities().end()); draft.capabilities.push_back({Capability::photosynthesis, efficiency, 0}); return checked(Genome::create(draft), "photosynthetic genome"); } constexpr EnvironmentState reference{293'150, {}}; // Founder 4 is founder 3 with a schema that can express photosynthesis; it starts without. void founders() { const auto three = checked(primitive_founder(5, 5, primitive_founder_v3), "founder 3"); const auto four = checked(primitive_founder(5, 5, primitive_founder_v4), "founder 4"); require(!four.carries(Capability::photosynthesis) && four.schema_version() == 5 && four.development_version() == 5, "founder 4 carries photosynthesis or wrong versions"); const auto a = checked(metabolic_budget(three, {1000, 0}, reference), "founder 3 budget"); const auto b = checked(metabolic_budget(four, {1000, 0}, reference), "founder 4 budget"); require(a.phenotype == b.phenotype && a.body == b.body && a.maintenance == b.maintenance && a.reproduction_investment == b.reproduction_investment, "founder 4 differs from founder 3 without photosynthesis"); require(checked(primitive_endowment(primitive_founder_v4), "endowment 4") == checked(primitive_endowment(primitive_founder_v3), "endowment 3"), "founder 4 endowment changed"); // Earlier schemas cannot carry it at any efficiency. GenomeDraft old{9, 9, 4, {}, {}, genome_schema_v4}; old.capabilities.assign(three.capabilities().begin(), three.capabilities().end()); old.capabilities.push_back({Capability::photosynthesis, 100, 0}); require(Genome::create(old).error() == GenomeError::parameter_out_of_range, "schema 4 accepted photosynthesis"); } // Organelles: p/4 of structure as membrane mass (upkept twice), speed down by p/2. void costs() { const auto four = checked(primitive_founder(5, 5, primitive_founder_v4), "founder 4"); const auto base = checked(metabolic_budget(four, {1000, 0}, reference), "base budget"); for (const auto efficiency : {100U, 500U, 1000U}) { const auto producer = with(four, efficiency); const auto budget = checked(metabolic_budget(producer, {1000, 0}, reference), "producer"); const auto organelles = base.phenotype.structural_mass / 4 * efficiency / 1000; require(budget.phenotype.membrane_mass == base.phenotype.membrane_mass + organelles && budget.phenotype.structural_mass == base.phenotype.structural_mass && budget.phenotype.extent == base.phenotype.extent && budget.maintenance.value > base.maintenance.value && budget.phenotype.movement_speed == base.phenotype.movement_speed * (1000 - efficiency / 2) / 1000, "photosynthesis costs differ from the declared trial rule"); const CellularLight light{cellular_light_trial_irradiance, 1, {1ULL << 50}}; const auto captured_still = cellular_light_capture(producer, budget.phenotype.extent, light, false); const auto captured_moving = cellular_light_capture(producer, budget.phenotype.extent, light, true); require(captured_still.has_value() && captured_moving.has_value(), "capture overflowed"); const auto still = captured_still.value_or(0), moving = captured_moving.value_or(0); const auto section = std::uint64_t{budget.phenotype.extent} * budget.phenotype.extent; require(still == section * cellular_light_trial_irradiance * efficiency / 1000 && moving == still / 4, "light capture differs from cross-section x irradiance x efficiency"); std::cout << "photosynthesis " << efficiency << ": maintenance " << budget.maintenance.value / 20 << " pJ/tick (founder " << base.maintenance.value / 20 << "), capture still " << still << ", moving " << moving << ", speed " << budget.phenotype.movement_speed << " um/s\n"; } require(cellular_light_capture(four, 20, {cellular_light_trial_irradiance, 1, {1}}, false) == 0, "a genome without the capability captured light"); // Dark light captures nothing; capture scales exactly with irradiance when it divides. const auto producer = with(four, 100); require(cellular_light_capture(producer, 20, {0, 1, {0}}, false) == 0, "dark light captured energy"); const auto trial = cellular_light_capture(producer, 20, {cellular_light_trial_irradiance, 1, {1}}, false); const auto bright = cellular_light_capture(producer, 20, {cellular_light_trial_irradiance * 20, 1, {1}}, false); require(trial == 1'600 && bright == 32'000, "capture did not scale with irradiance"); } // Schema 5 offers photosynthesis through the appended operators; earlier schemas never do. void offers() { const auto catalog = mutation_catalog(); require(catalog.size() == 32, "catalog size"); for (std::uint16_t id = 29; id <= 32; ++id) require(catalog[id - 1].target == Capability::photosynthesis, "photosynthesis operators are not IDs 29-32"); const auto four = checked(primitive_founder(5, 5, primitive_founder_v4), "founder 4"); const auto three = checked(primitive_founder(5, 5, primitive_founder_v3), "founder 3"); bool offered = false; for (std::uint64_t event = 1; event <= 400 && !offered; ++event) { const MutationContext context{0x5EED, 1, event, 8, {1000, 0}, reference}; const auto old = checked(mutation_candidates(three, context), "schema 4 offer"); require(std::ranges::none_of( old.candidates, [](const auto& c) { return c.target == Capability::photosynthesis; }), "schema 4 lineage offered photosynthesis"); const auto offer = checked(mutation_candidates(four, context), "schema 5 offer"); const auto found = std::ranges::find_if(offer.candidates, [](const auto& c) { return c.target == Capability::photosynthesis && c.effect == MutationEffect::acquire; }); if (found == offer.candidates.end()) continue; offered = true; require(found->parameter == photosynthesis_acquired_start, "acquired photosynthesis does not start at the trial value"); const auto successor = checked(select_mutation(four, context, offer, found->operator_id, 77, 77), "select"); require(*successor.parameter(Capability::photosynthesis) == photosynthesis_acquired_start, "selected successor lost photosynthesis"); } require(offered, "schema 5 never offered photosynthesis"); } // Rules 11 foraging: a hungry cell passes a nearer carbon mote for energy, a fed cell takes // the nearest food, and with no energy in view the hungry cell still takes the nearest. void foraging() { const auto mote = [](std::uint32_t x, bool energy) { CellularPatchPerception p{}; p.address = {x, 0}; p.radius_um = cellular_mote_radius_um; p.kind = CellularDepositKind::mote; if (energy) p.available.energy.value = 1'000; else p.available.matter[0].value = 1'000; // Carbon only. return p; }; const std::array view{mote(40, false), mote(400, true)}; const auto hungry = wildlife_food(view, 0, 0, true, true); const auto fed = wildlife_food(view, 0, 0, true, false); require(hungry && hungry->address.x_um == 400, "a hungry cell parked on carbon"); require(fed && fed->address.x_um == 40, "a fed cell skipped the nearest food"); const std::array carbon{mote(40, false)}; const auto only = wildlife_food(carbon, 0, 0, true, true); require(only && only->address.x_um == 40, "a hungry cell ignored the only food in view"); } } // namespace int main() { try { founders(); costs(); offers(); foraging(); std::cout << "photosynthesis contracts passed\n"; return 0; } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; } }