Files
aevolution/src/server/lifecycle/cellular_population.cpp
T
dingo a898a35e1b
Foundation / checks (ecs) (push) Successful in 2m26s
Foundation / checks (deploy) (push) Successful in 4m57s
Foundation / checks (network) (push) Successful in 20m53s
Foundation / checks (client) (push) Successful in 22m12s
Foundation / checks (tsan) (push) Successful in 39m45s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 45m41s
Foundation / checks (checks) (push) Canceled after 45m43s
feat(lifecycle): preserve inherited plans in exact populations
2026-10-19 15:19:43 -04:00

164 lines
7.3 KiB
C++

#include "evo/server/lifecycle/cellular_population.hpp"
#include "evo/server/lifecycle/wildlife_controller.hpp"
#include <algorithm>
#include <limits>
#include <set>
#include <utility>
namespace evo::server {
CellularPopulation::CellularPopulation(std::unique_ptr<CellularLives> state,
CellularTierPolicy policy)
: state_(std::move(state)), policy_(policy) {}
std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
CellularPopulation::create(std::unique_ptr<CellularLives> 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<CellularPopulation>(new CellularPopulation(std::move(state), policy));
}
const CellularLives& CellularPopulation::state() const noexcept {
return *state_;
}
std::span<const CellularCohortMember> 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<std::unique_ptr<CellularPopulation>, 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<std::unique_ptr<CellularPopulation>, 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<std::unique_ptr<CellularPopulation>, 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<std::size_t, CellularPopulationError>
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<std::uint64_t>::max())
return std::unexpected(CellularPopulationError::overflow);
std::ranges::sort(candidate);
members_.swap(candidate);
if (converted > 0)
++conversion_sequence_;
return converted;
}
std::expected<std::size_t, CellularPopulationError>
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<std::uint64_t>::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<CellularTickExecution, CellularLivesError>
CellularPopulation::advance_tick(std::span<const CellularLifeIntent> controlled,
std::span<const CellularMutationSelection> selections) {
if (policy_.automatic && conversion_sequence_ == std::numeric_limits<std::uint64_t>::max())
return std::unexpected(CellularLivesError::overflow);
auto wildlife = wildlife_intents(*state_, state_->tick() + 1);
if (!wildlife)
return std::unexpected(wildlife.error());
std::vector<CellularLifeIntent> 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