#include "evo/server/lifecycle/cellular_population.hpp" #include "evo/server/lifecycle/wildlife_controller.hpp" #include #include #include #include namespace evo::server { CellularPopulation::CellularPopulation(std::unique_ptr state, CellularTierPolicy policy) : state_(std::move(state)), policy_(policy) {} std::expected, CellularPopulationError> CellularPopulation::create(std::unique_ptr state, CellularTierPolicy policy) { const bool supported = state && ((state->rules_version() == cellular_lives_foraging_rules && (state->format_version() == cellular_lives_presence_format || state->format_version() == cellular_lives_migration_format)) || (state->rules_version() == cellular_lives_maturation_rules && state->format_version() == cellular_lives_migration_format) || (state->rules_version() == cellular_lives_inheritance_rules && state->format_version() == cellular_lives_inheritance_format)); if (!supported || policy.materialize_radius_um == 0 || policy.collapse_radius_um <= policy.materialize_radius_um || policy.collapse_radius_um > biology::cellular_motion_axis_limit_um) return std::unexpected(CellularPopulationError::unsupported); return std::unique_ptr(new CellularPopulation(std::move(state), policy)); } const CellularLives& CellularPopulation::state() const noexcept { return *state_; } std::span CellularPopulation::members() const noexcept { return members_; } bool CellularPopulation::in_cohort(std::uint64_t life) const noexcept { return std::ranges::binary_search(members_, life, {}, &CellularCohortMember::life); } CellularTierPolicy CellularPopulation::policy() const noexcept { return policy_; } std::uint64_t CellularPopulation::conversion_sequence() const noexcept { return conversion_sequence_; } std::uint32_t CellularPopulation::format_version() const noexcept { if (state_->format_version() == cellular_lives_inheritance_format) return cellular_population_inheritance_format; return state_->format_version() == cellular_lives_migration_format ? 2U : 1U; } std::expected, CellularPopulationError> CellularPopulation::migration_upgrade_candidate() const { auto biological = state_->migration_upgrade_candidate(); if (!biological) return std::unexpected(CellularPopulationError::unsupported); auto candidate = create(std::move(*biological), policy_); if (!candidate) return std::unexpected(candidate.error()); (*candidate)->members_ = members_; (*candidate)->conversion_sequence_ = conversion_sequence_; return candidate; } std::expected, CellularPopulationError> CellularPopulation::maturation_upgrade_candidate() const { auto biological = state_->maturation_upgrade_candidate(); if (!biological) return std::unexpected(CellularPopulationError::unsupported); auto candidate = create(std::move(*biological), policy_); if (!candidate) return std::unexpected(candidate.error()); (*candidate)->members_ = members_; (*candidate)->conversion_sequence_ = conversion_sequence_; return candidate; } std::expected, CellularPopulationError> CellularPopulation::inheritance_upgrade_candidate() const { auto biological = state_->inheritance_upgrade_candidate(); if (!biological) return std::unexpected(CellularPopulationError::unsupported); auto candidate = create(std::move(*biological), policy_); if (!candidate) return std::unexpected(candidate.error()); (*candidate)->members_ = members_; (*candidate)->conversion_sequence_ = conversion_sequence_; return candidate; } std::expected CellularPopulation::collapse(std::uint64_t genome) { auto candidate = members_; std::size_t converted = 0; bool found = false; for (const auto& life : state_->lives()) { if (life.identity.genome != genome || life.account != LifeAccount{}) continue; found = true; if (life.presence != CellularPresence::active) return std::unexpected(CellularPopulationError::ineligible); if (!in_cohort(life.identity.life)) { candidate.push_back({life.identity.life, genome}); ++converted; } } if (!found) return std::unexpected(CellularPopulationError::absent); if (converted > 0 && conversion_sequence_ == std::numeric_limits::max()) return std::unexpected(CellularPopulationError::overflow); std::ranges::sort(candidate); members_.swap(candidate); if (converted > 0) ++conversion_sequence_; return converted; } std::expected CellularPopulation::materialize(std::uint64_t genome) { if (std::ranges::none_of(members_, [genome](const auto& m) { return m.genome == genome; })) return std::unexpected(CellularPopulationError::absent); if (conversion_sequence_ == std::numeric_limits::max()) return std::unexpected(CellularPopulationError::overflow); const auto removed = std::erase_if(members_, [genome](const auto& m) { return m.genome == genome; }); ++conversion_sequence_; return removed; } std::expected CellularPopulation::advance_tick(std::span controlled, std::span selections) { if (policy_.automatic && conversion_sequence_ == std::numeric_limits::max()) return std::unexpected(CellularLivesError::overflow); auto wildlife = wildlife_intents(*state_, state_->tick() + 1); if (!wildlife) return std::unexpected(wildlife.error()); std::vector intents; intents.reserve(state_->lives().size()); std::size_t player = 0, autonomous = 0; for (const auto& life : state_->lives()) { if (life.account == LifeAccount{}) { intents.push_back((*wildlife)[autonomous++]); } else { if (player == controlled.size() || controlled[player].life_id != life.identity.life) return std::unexpected(CellularLivesError::invalid_intents); intents.push_back(controlled[player++]); } } if (player != controlled.size()) return std::unexpected(CellularLivesError::invalid_intents); for (const auto& selection : selections) { if (std::ranges::find(controlled, selection.life_id, &CellularLifeIntent::life_id) == controlled.end()) return std::unexpected(CellularLivesError::invalid_intents); } auto executed = state_->advance_tick_with_mutations(intents, selections); if (!executed) return std::unexpected(executed.error()); std::erase_if(members_, [this](const auto& member) { return std::ranges::find(state_->lives(), member.life, [](const auto& life) { return life.identity.life; }) == state_->lives().end(); }); if (policy_.automatic) apply_policy(); return executed; } } // namespace evo::server