Files
aevolution/src/server/lifecycle/primitive_lives.cpp
T
dingo 69101a74c2
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m44s
Foundation / checks (postgres) (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (network) (push) Canceled after 13s
Foundation / checks (checks) (push) Canceled after 3m9s
Foundation / checks (client) (push) Canceled after 3m8s
M11.2a: fund equal primitive lives from finite spawn pools
2026-10-09 18:27:55 -04:00

118 lines
5.4 KiB
C++

#include "evo/server/lifecycle/primitive_lives.hpp"
#include <algorithm>
#include <cstdlib>
#include <limits>
#include <utility>
namespace evo::server {
struct PrimitiveLives::State {
NurseryConfig config;
accounting::Ledger ledger;
biology::LineageArchive archive;
std::vector<PrimitiveLife> lives;
};
PrimitiveLives::PrimitiveLives(std::unique_ptr<State> state) : state_{std::move(state)} {}
PrimitiveLives::~PrimitiveLives() = default;
std::expected<std::unique_ptr<PrimitiveLives>, LifeError>
PrimitiveLives::create(NurseryConfig config) {
if (config.world == 0 || config.region == 0 || config.frame == 0 || config.source == 0 ||
config.sites.empty() || config.sites.size() > max_primitive_lives ||
!std::ranges::is_sorted(config.sites))
return std::unexpected{LifeError::invalid};
const auto founder = biology::primitive_founder(1, 1);
if (!founder)
return std::unexpected{LifeError::biology};
const auto phenotype = biology::derive_phenotype(*founder, {1000, 0}, {});
if (!phenotype)
return std::unexpected{LifeError::biology};
for (std::size_t i = 0; i < config.sites.size(); ++i) {
const auto site = config.sites[i];
if (site.x_um < -spawn_coordinate_limit_um || site.x_um > spawn_coordinate_limit_um ||
site.y_um < -spawn_coordinate_limit_um || site.y_um > spawn_coordinate_limit_um)
return std::unexpected{LifeError::invalid};
for (std::size_t j = 0; j < i; ++j) {
const auto dx = std::abs(std::int64_t{site.x_um} - config.sites[j].x_um);
const auto dy = std::abs(std::int64_t{site.y_um} - config.sites[j].y_um);
if (dx < phenotype->extent && dy < phenotype->extent)
return std::unexpected{LifeError::occupied};
}
}
std::vector<accounting::Inventory> opening(2 + 2 * config.sites.size());
opening[0] = config.funding; // Source 1, heat 2; body/reserve pairs follow.
auto ledger = accounting::Ledger::create(opening);
if (!ledger)
return std::unexpected{LifeError::overflow};
auto state = std::make_unique<State>(State{std::move(config), std::move(*ledger), {}, {}});
return std::unique_ptr<PrimitiveLives>{new PrimitiveLives{std::move(state)}};
}
std::expected<PrimitiveLife, LifeError> PrimitiveLives::spawn(LifeRequest request,
LifeAllocation allocation) {
if (request.action == 0 || std::ranges::all_of(request.account, [](auto b) { return b == 0; }))
return std::unexpected{LifeError::invalid};
for (const auto& life : state_->lives) {
if (life.request == request)
return life;
if (life.request.account == request.account)
return std::unexpected{LifeError::existing_life};
}
if (allocation.life == 0 || allocation.organism == 0 || allocation.genome == 0 ||
allocation.event == 0)
return std::unexpected{LifeError::invalid};
for (const auto& life : state_->lives) {
const auto& old = life.identity;
if (old.life == allocation.life || old.organism == allocation.organism ||
old.genome == allocation.genome || old.event == allocation.event)
return std::unexpected{LifeError::identity_conflict};
}
const auto slot = state_->lives.size();
if (slot == state_->config.sites.size())
return std::unexpected{LifeError::capacity};
const auto endowed = biology::primitive_endowment();
const auto genome = biology::primitive_founder(allocation.genome, allocation.event);
if (!endowed || !genome)
return std::unexpected{LifeError::biology};
const auto body = accounting::ReservoirId{static_cast<std::uint32_t>(3 + 2 * slot)};
const auto reserve = accounting::ReservoirId{body.value + 1};
const PrimitiveLife life{
request, allocation, state_->config.source, state_->config.sites[slot], endowed->growth,
body, reserve};
// Detached candidate: all fallible work, including allocations and archive
// admission, precedes the single noexcept pointer swap.
auto next = std::make_unique<State>(*state_);
const std::array legs{accounting::Transfer{{1}, body, endowed->body},
accounting::Transfer{{1}, reserve, endowed->reserve},
accounting::Transfer{{1}, {2}, {endowed->construction, {}}}};
const auto paid = next->ledger.transfer(slot + 1, legs);
if (!paid)
return std::unexpected{paid.error() == accounting::LedgerError::insufficient_inventory
? LifeError::insufficient_funding
: LifeError::overflow};
if (!next->archive.admit(*genome) ||
!next->archive.record_birth(allocation.organism, allocation.genome, allocation.born))
return std::unexpected{LifeError::archive};
next->lives.push_back(life);
state_.swap(next);
return life;
}
const NurseryConfig& PrimitiveLives::config() const noexcept {
return state_->config;
}
std::span<const PrimitiveLife> PrimitiveLives::lives() const noexcept {
return state_->lives;
}
std::span<const accounting::Inventory> PrimitiveLives::balances() const noexcept {
return state_->ledger.balances();
}
accounting::Inventory PrimitiveLives::totals() const noexcept {
return state_->ledger.conserved_totals();
}
const biology::LineageArchive& PrimitiveLives::archive() const noexcept {
return state_->archive;
}
} // namespace evo::server