Add funded cellular birth candidate
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 39s
Foundation / checks (ecs) (push) Successful in 1m54s
Foundation / checks (postgres) (push) Failing after 51s
Foundation / checks (tsan) (push) Failing after 32s
Foundation / checks (network) (push) Failing after 3m4s
Foundation / checks (client) (push) Failing after 4m35s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped

This commit is contained in:
dingo
2026-10-11 00:19:47 -04:00
parent c06ea9023d
commit 81866a6897
6 changed files with 254 additions and 6 deletions
+6 -6
View File
@@ -3,6 +3,7 @@
#include <algorithm>
#include <array>
#include <bit>
#include <optional>
#include <ranges>
#include <tuple>
#include <utility>
@@ -280,12 +281,11 @@ match_cellular_actions_to_trace(const CellularLives& predecessor, const Cellular
if (intent == intents.end() || intent->life_id != life->identity.life)
return std::unexpected{CellularActionBlockError::mismatch};
const auto input = std::span<const std::uint8_t>{action.input};
const auto target = input[12] == 0
? std::optional<biology::CellularPatchAddress>{}
: std::optional<biology::CellularPatchAddress>{
biology::CellularPatchAddress{
static_cast<std::uint32_t>(get(input, 16, 4)),
static_cast<std::uint32_t>(get(input, 20, 4))}};
std::optional<biology::CellularPatchAddress> target;
if (input[12] != 0)
target = biology::CellularPatchAddress{
static_cast<std::uint32_t>(get(input, 16, 4)),
static_cast<std::uint32_t>(get(input, 20, 4))};
if (intent->intent.movement.x != std::bit_cast<std::int16_t>(
static_cast<std::uint16_t>(get(input, 8, 2))) ||
intent->intent.movement.y != std::bit_cast<std::int16_t>(
+127
View File
@@ -1,4 +1,5 @@
#include "evo/server/lifecycle/cellular_lives.hpp"
#include "evo/biology/founder.hpp"
#include <algorithm>
#include <bit>
@@ -100,6 +101,19 @@ bool add_inventory(accounting::Inventory& total, const accounting::Inventory& va
return true;
}
bool subtract_inventory(accounting::Inventory& total,
const accounting::Inventory& value) noexcept {
if (value.energy.value > total.energy.value)
return false;
for (std::size_t channel = 0; channel < total.matter.size(); ++channel)
if (value.matter[channel].value > total.matter[channel].value)
return false;
total.energy.value -= value.energy.value;
for (std::size_t channel = 0; channel < total.matter.size(); ++channel)
total.matter[channel].value -= value.matter[channel].value;
return true;
}
std::expected<biology::MetabolicBudget, CellularLivesError>
budget_for(const biology::LineageArchive& archive, const CellularOwnedLife& life,
std::uint32_t temperature) {
@@ -474,6 +488,119 @@ CellularLives::perceive(std::uint64_t life_id) const {
return std::move(*view);
}
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) const {
if (request.action == 0 ||
std::ranges::all_of(request.account, [](std::uint8_t value) { return value == 0; }))
return std::unexpected{CellularLivesError::invalid_life};
if (allocation.life == 0 || allocation.life != allocation.organism ||
allocation.life != allocation.genome || allocation.life != allocation.event)
return std::unexpected{CellularLivesError::identity};
if (allocation.born.value == 0 || allocation.born.value != state_->config.tick)
return std::unexpected{CellularLivesError::invalid_tick};
if (state_->lives.size() >= biology::max_cellular_tick_lives)
return std::unexpected{CellularLivesError::no_placement};
if (std::ranges::any_of(state_->lives, [&](const CellularOwnedLife& life) {
return life.account == request.account;
}))
return std::unexpected{CellularLivesError::life_exists};
if (std::ranges::any_of(state_->lives, [&](const CellularOwnedLife& life) {
return life.identity.life == allocation.life ||
life.identity.organism == allocation.organism ||
life.identity.genome == allocation.genome ||
life.identity.event == allocation.event;
}) ||
std::ranges::any_of(state_->corpses, [&](const CellularOwnedCorpse& corpse) {
return corpse.life_id == allocation.life || corpse.organism == allocation.organism;
}) || state_->archive.find(allocation.genome) || state_->archive.organism(allocation.organism))
return std::unexpected{CellularLivesError::duplicate_life};
const auto endowed = biology::primitive_endowment();
const auto genome = biology::primitive_founder(allocation.genome, allocation.event);
if (!endowed || !genome)
return std::unexpected{CellularLivesError::biology};
const auto budget = biology::metabolic_budget(
*genome, endowed->growth,
{state_->config.ambient_temperature_millikelvin, {}});
if (!budget || budget->body != endowed->body)
return std::unexpected{CellularLivesError::biology};
std::optional<biology::CellularPosition> position;
for (const auto site : state_->nursery_config.sites) {
if (site.x_um < 0 || site.y_um < 0)
return std::unexpected{CellularLivesError::invalid_config};
const biology::CellularPosition candidate{static_cast<std::uint64_t>(site.x_um),
static_cast<std::uint64_t>(site.y_um)};
bool occupied = false;
for (const auto& living : state_->lives) {
const auto living_budget = budget_for(
state_->archive, living, state_->config.ambient_temperature_millikelvin);
if (!living_budget)
return std::unexpected{living_budget.error()};
const auto separated = [](std::uint64_t left, std::uint64_t right,
std::uint32_t left_extent,
std::uint32_t right_extent) noexcept {
return left < right ? right - left >= left_extent : left - right >= right_extent;
};
if (!separated(candidate.x_um, living.position.x_um, budget->phenotype.extent,
living_budget->phenotype.extent) &&
!separated(candidate.y_um, living.position.y_um, budget->phenotype.extent,
living_budget->phenotype.extent)) {
occupied = true;
break;
}
}
if (!occupied) {
position = candidate;
break;
}
}
if (!position)
return std::unexpected{CellularLivesError::no_placement};
auto nursery = state_->nursery;
if (!subtract_inventory(nursery, endowed->body) ||
!subtract_inventory(nursery, endowed->reserve) ||
endowed->construction.value > nursery.energy.value)
return std::unexpected{CellularLivesError::insufficient_funding};
nursery.energy.value -= endowed->construction.value;
auto heat = state_->heat;
if (endowed->construction.value >
std::numeric_limits<std::uint64_t>::max() - heat.energy.value)
return std::unexpected{CellularLivesError::overflow};
heat.energy.value += endowed->construction.value;
auto archive = state_->archive;
if (!archive.admit(*genome) ||
!archive.record_birth(allocation.organism, allocation.genome, allocation.born))
return std::unexpected{CellularLivesError::archive};
auto lives = state_->lives;
lives.push_back({request.account, allocation, state_->nursery_config.source, endowed->growth,
*position, endowed->body, endowed->reserve});
std::ranges::sort(lives, {}, [](const CellularOwnedLife& life) {
return life.identity.life;
});
auto nursery_config = state_->nursery_config;
nursery_config.funding = nursery;
auto habitat = biology::CellularHabitat::restore(state_->habitat->snapshot(), state_->world,
state_->region, state_->frame);
if (!habitat)
return std::unexpected{CellularLivesError::habitat};
const auto current = validate_state(state_->world, state_->region, state_->frame,
state_->config, nursery_config, **habitat, lives,
state_->corpses, archive, nursery, heat,
state_->opening_total);
if (!current)
return std::unexpected{current.error()};
auto candidate = std::unique_ptr<CellularLives>{new CellularLives{std::make_unique<State>(
state_->world, state_->region, state_->frame, state_->config,
std::move(nursery_config), std::move(*habitat), std::move(lives), state_->corpses,
archive, nursery, heat, state_->opening_total, *current)}};
return candidate;
}
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
std::uint64_t region, std::uint64_t frame, std::uint64_t tick) {