#include "evo/server/lifecycle/cellular_census.hpp" #include #include #include namespace evo::server { namespace { bool add(std::uint64_t& target, std::uint64_t value) { if (value > std::numeric_limits::max() - target) return false; target += value; return true; } bool add(accounting::Inventory& target, const accounting::Inventory& value) { if (!add(target.energy.value, value.energy.value)) return false; for (std::size_t channel = 0; channel < target.matter.size(); ++channel) if (!add(target.matter[channel].value, value.matter[channel].value)) return false; return true; } bool add(CellularCensusTotals& target, const CellularCensusTotals& value) { if (!add(target.retained_nonresident_records, value.retained_nonresident_records)) return false; for (std::size_t kind = 0; kind < target.resident.size(); ++kind) { auto& stock = target.resident[kind]; const auto& source = value.resident[kind]; if (!add(stock.count, source.count) || !add(stock.body, source.body) || !add(stock.reserve, source.reserve)) return false; } return true; } } // namespace std::size_t CellularCensus::locate(std::uint64_t genome) const { const auto found = std::ranges::lower_bound(rows_, genome, {}, &CellularCensusRow::genome); return found != rows_.end() && found->genome == genome ? static_cast(found - rows_.begin()) : rows_.size(); } std::expected CellularCensus::observe(const CellularLives& state) { return observe(state, {}, 0); } std::expected CellularCensus::observe(const CellularPopulation& population) { return observe(population.state(), population.members(), population.format_version()); } std::expected CellularCensus::observe(const CellularLives& state, std::span members, std::uint32_t population) { const bool maturing = state.rules_version() == cellular_lives_maturation_rules && state.format_version() == cellular_lives_migration_format && (population == 0 || population == 2); if ((!maturing && state.rules_version() != cellular_lives_foraging_rules) || (state.format_version() != cellular_lives_presence_format && state.format_version() != cellular_lives_migration_format) || (population != 0 && population != 1 && population != 2)) return std::unexpected(CellularCensusError::unsupported); CellularCensus result; result.boundary_ = {state.world(), state.region(), state.frame(), state.tick(), state.format_version(), state.rules_version(), population}; const auto& archive = state.archive(); result.genomes_.assign(archive.genomes().begin(), archive.genomes().end()); result.rows_.reserve(archive.archived()); for (const auto& genome : archive.genomes()) result.rows_.push_back({genome.id(), genome.schema_version(), genome.development_version(), {genome.parents().begin(), genome.parents().end()}, {}}); result.children_.resize(result.rows_.size()); for (std::size_t child = 0; child < result.rows_.size(); ++child) for (const auto parent : result.rows_[child].parents) { const auto index = result.locate(parent); if (index == result.rows_.size()) return std::unexpected(CellularCensusError::history); result.children_[index].push_back(child); } for (const auto& life : state.lives()) { result.residents_.push_back(life.identity.organism); const auto row = result.locate(life.identity.genome); if (row == result.rows_.size()) return std::unexpected(CellularCensusError::history); CellularCensusKind kind{}; if (life.account == LifeAccount{}) { const bool cohort = std::ranges::binary_search(members, life.identity.life, {}, &CellularCohortMember::life); kind = cohort ? CellularCensusKind::cohort_wildlife : CellularCensusKind::individual_wildlife; } else { switch (life.presence) { case CellularPresence::active: kind = CellularCensusKind::active_controlled; break; case CellularPresence::lingering: kind = CellularCensusKind::lingering_controlled; break; case CellularPresence::suspended: kind = CellularCensusKind::suspended_controlled; break; } } auto& stock = result.rows_[row].totals.resident[static_cast(kind)]; if (!add(stock.count, 1) || !add(stock.body, life.body) || !add(stock.reserve, life.reserve)) return std::unexpected(CellularCensusError::overflow); } const auto& nonresident = state.boundary_ledger().nonresident_organisms; std::vector nonresident_genomes; nonresident_genomes.reserve(nonresident.size()); for (const auto organism : nonresident) { const auto birth = archive.organism(organism); if (!birth) return std::unexpected(CellularCensusError::history); nonresident_genomes.push_back(birth->genome); } // One bounded revision pass, rather than scanning every revision for every // departed record. Archive admission guarantees canonical revision order. for (const auto& revision : archive.revisions()) { const auto found = std::ranges::lower_bound(nonresident, revision.organism); if (found == nonresident.end() || *found != revision.organism) continue; auto& genome = nonresident_genomes[static_cast(found - nonresident.begin())]; if (genome != revision.previous_genome) return std::unexpected(CellularCensusError::history); genome = revision.successor_genome; } for (const auto genome : nonresident_genomes) { const auto row = result.locate(genome); if (row == result.rows_.size()) return std::unexpected(CellularCensusError::history); if (!add(result.rows_[row].totals.retained_nonresident_records, 1)) return std::unexpected(CellularCensusError::overflow); } return result; } std::expected CellularCensus::query(std::uint64_t anchor, CellularCensusScope scope) const { if (scope != CellularCensusScope::exact_genome && scope != CellularCensusScope::descendants) return std::unexpected(CellularCensusError::invalid_scope); const auto root = locate(anchor); if (root == rows_.size()) return std::unexpected(CellularCensusError::unknown_genome); if (scope == CellularCensusScope::exact_genome) return rows_[root].totals; CellularCensusTotals result; std::vector visited(rows_.size(), false); std::vector pending{root}; visited[root] = true; for (std::size_t next = 0; next < pending.size(); ++next) { const auto row = pending[next]; if (!add(result, rows_[row].totals)) return std::unexpected(CellularCensusError::overflow); for (const auto child : children_[row]) if (!visited[child]) { visited[child] = true; pending.push_back(child); } } return result; } } // namespace evo::server