Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m27s
Foundation / postgres-tools (push) Successful in 33s
Foundation / server-image (push) Successful in 4m14s
Foundation / image (push) Successful in 4m41s
Phase 1 of the cellular revamp build plan (ADR-0179). Genome schema 7 carries twenty 16-bit cellular traits after the movement arrangement: density, upkeep efficiency, growth speed, collection per channel (E/C/N/P/O), burn efficiency per matter channel, engulf capacity, kill absorption, litter, the body recipe (C/N/P/O per thousand, summing to 1000) and adhesion. Its membrane is held in nanometres so it can change in 5% steps of the founder's 1 um. The largest record still fits the 256-byte bound. Development 7 and founder 6 serve rules 13: density sets how large a body is for its mass, the recipe sets the body's composition (intake keeps the founder shares), upkeep is an essential share no trait reduces plus the rest over upkeep efficiency (region-declared BodyTuning, trial 25%), the producer speed penalty is dropped, and the reproduction value is the bud's share of the body with the construction discount fixed at the founder's level. Founder 6 builds exactly founder 5's cell. Earlier schemas and development versions are unchanged. The development and metabolism test mains now print the failing line. Refreshes the DEV session window. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
233 lines
11 KiB
C++
233 lines
11 KiB
C++
#include "evo/biology/development.hpp"
|
|
|
|
#include "evo/accounting/rates.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <limits>
|
|
|
|
namespace evo::biology {
|
|
namespace {
|
|
constexpr std::uint32_t thousand = 1000;
|
|
|
|
bool multiply(std::uint64_t& result, std::uint64_t left, std::uint64_t right) noexcept {
|
|
if (left != 0 && right > std::numeric_limits<std::uint64_t>::max() / left) {
|
|
return false;
|
|
}
|
|
result = left * right;
|
|
return true;
|
|
}
|
|
bool add(std::uint64_t& result, std::uint64_t left, std::uint64_t right) noexcept {
|
|
if (right > std::numeric_limits<std::uint64_t>::max() - left) {
|
|
return false;
|
|
}
|
|
result = left + right;
|
|
return true;
|
|
}
|
|
// Exact: the largest r with r*r*r <= value. No floating point is involved.
|
|
std::uint64_t integer_cube_root(std::uint64_t value) noexcept {
|
|
std::uint64_t root = 0;
|
|
// 2^21 cubed already exceeds u64, so 21 bits are enough.
|
|
for (std::uint64_t bit = 1ULL << 21U; bit != 0; bit >>= 1U) {
|
|
const auto candidate = root | bit;
|
|
std::uint64_t cube{};
|
|
if (multiply(cube, candidate, candidate) && multiply(cube, cube, candidate) &&
|
|
cube <= value) {
|
|
root = candidate;
|
|
}
|
|
}
|
|
return root;
|
|
}
|
|
std::uint64_t scaled(std::uint64_t value, std::uint64_t numerator) noexcept {
|
|
// Declared flooring; the remainder is discarded rather than carried.
|
|
return value / thousand * numerator + value % thousand * numerator / thousand;
|
|
}
|
|
// Saturating on purpose. Availability is caller-supplied and unrestricted, and a
|
|
// wrapped total would read as no material at all, refusing growth that is plainly
|
|
// funded. Saturation is also the right answer: no step can need more than u64.
|
|
std::uint64_t matter_total(const accounting::Inventory& inventory) noexcept {
|
|
constexpr auto maximum = std::numeric_limits<std::uint64_t>::max();
|
|
std::uint64_t sum = 0;
|
|
for (const auto& mass : inventory.matter) {
|
|
sum = mass.value > maximum - sum ? maximum : sum + mass.value;
|
|
}
|
|
return sum;
|
|
}
|
|
} // namespace
|
|
|
|
std::expected<DevelopmentRules, DevelopmentError> rules_for(std::uint32_t development_version) {
|
|
if (development_version == 1) {
|
|
return DevelopmentRules{1000, 1200, 2, 3, 40, 60'000, 293'150, 50};
|
|
}
|
|
// Version 2 (ADR-0099; tunable, ADR-0098 decision 7): rates per second. Maintenance
|
|
// and membrane upkeep are a quarter of version 1's per-second equivalent (40, 60), so
|
|
// a primitive cell can fund its first bud within a session of feeding.
|
|
if (development_version == 2) {
|
|
return DevelopmentRules{1000, 1200, 10, 15, 40, 60'000, 293'150, 50};
|
|
}
|
|
// Version 3 (rules 6, 2026-10-15 balance): version 2 with a third of the construction
|
|
// energy (13 pJ per 1000 fg built instead of 40). Construction is overhead paid into
|
|
// heat, so this changes no matter balance.
|
|
// Version 4 (rules 7, ADR-0137) keeps version 3's rules; only what reproduction
|
|
// means changes (metabolism.cpp).
|
|
// Version 5 (ADR-0175) keeps them too; it adds photosynthetic organelles below.
|
|
// Version 6 (rules 12) keeps version 5's body; handling comes from the genome's movement
|
|
// arrangement (derive_cellular_handling).
|
|
// Version 7 (rules 13, ADR-0179) keeps the constants; the genome's cellular traits set
|
|
// density, recipe and upkeep efficiency, and the membrane is in nanometres.
|
|
if (development_version >= 3 && development_version <= 7) {
|
|
return DevelopmentRules{1000, 1200, 10, 15, 13, 60'000, 293'150, 50};
|
|
}
|
|
return std::unexpected{DevelopmentError::unsupported_rules};
|
|
}
|
|
|
|
std::expected<Phenotype, DevelopmentError> derive_phenotype(const Genome& genome,
|
|
GrowthState growth,
|
|
EnvironmentState environment,
|
|
const BodyTuning& tuning) {
|
|
if (growth.realized_per_thousand > thousand || growth.damage_per_thousand > thousand) {
|
|
return std::unexpected{DevelopmentError::invalid_growth};
|
|
}
|
|
const auto rules = rules_for(genome.development_version());
|
|
if (!rules) {
|
|
return std::unexpected{rules.error()};
|
|
}
|
|
// Version 7 reads the schema-7 traits; a version-7 genome without them is unsupported.
|
|
const bool v7 = genome.development_version() >= 7;
|
|
const auto& traits = genome.cellular_traits();
|
|
if (v7 != traits.has_value() || tuning.essential_upkeep_per_thousand > thousand) {
|
|
return std::unexpected{DevelopmentError::unsupported_rules};
|
|
}
|
|
Phenotype phenotype;
|
|
// target_size, membrane and energy_conversion are required by schema 1.
|
|
const auto target = *genome.parameter(Capability::target_size);
|
|
const auto built = scaled(target, growth.realized_per_thousand);
|
|
phenotype.structural_mass = scaled(built, thousand - growth.damage_per_thousand);
|
|
// Version 7: density scales how tightly that mass packs, so a denser body is smaller.
|
|
const auto density =
|
|
v7 ? std::max<std::uint64_t>(1, rules->structure_density * traits->density / 100)
|
|
: rules->structure_density;
|
|
phenotype.extent =
|
|
static_cast<std::uint32_t>(integer_cube_root(phenotype.structural_mass / density));
|
|
std::uint64_t face{};
|
|
if (!multiply(face, phenotype.extent, phenotype.extent) ||
|
|
!multiply(phenotype.surface, face, 6)) {
|
|
return std::unexpected{DevelopmentError::overflow};
|
|
}
|
|
std::uint64_t shell{};
|
|
if (!multiply(shell, phenotype.surface, *genome.parameter(Capability::membrane)) ||
|
|
!multiply(phenotype.membrane_mass, shell, rules->membrane_density)) {
|
|
return std::unexpected{DevelopmentError::overflow};
|
|
}
|
|
// Version 7 holds the membrane in nanometres: the same mass at 1,000 nm as at 1 um.
|
|
if (v7) {
|
|
phenotype.membrane_mass /= thousand;
|
|
}
|
|
const auto photosynthesis =
|
|
genome.development_version() >= 5 && genome.carries(Capability::photosynthesis)
|
|
? *genome.parameter(Capability::photosynthesis)
|
|
: 0U;
|
|
if (photosynthesis != 0 &&
|
|
!add(
|
|
phenotype.membrane_mass, phenotype.membrane_mass,
|
|
scaled(phenotype.structural_mass / photosynthesis_organelle_quarter, photosynthesis))) {
|
|
return std::unexpected{DevelopmentError::overflow};
|
|
}
|
|
if (!add(phenotype.total_mass, phenotype.structural_mass, phenotype.membrane_mass)) {
|
|
return std::unexpected{DevelopmentError::overflow};
|
|
}
|
|
// A thicker membrane is held as well as built, so upkeep is charged twice over.
|
|
if (!add(phenotype.maintenance, scaled(phenotype.total_mass, rules->maintenance_per_kilogram),
|
|
scaled(phenotype.membrane_mass, rules->membrane_upkeep_per_kilo))) {
|
|
return std::unexpected{DevelopmentError::overflow};
|
|
}
|
|
// Version 7: an essential share no trait reduces, plus the rest over upkeep efficiency.
|
|
if (v7) {
|
|
const auto essential = scaled(phenotype.maintenance, tuning.essential_upkeep_per_thousand);
|
|
std::uint64_t adjustable{};
|
|
if (!multiply(adjustable, phenotype.maintenance - essential, 100)) {
|
|
return std::unexpected{DevelopmentError::overflow};
|
|
}
|
|
phenotype.maintenance = essential + adjustable / traits->upkeep_efficiency;
|
|
}
|
|
phenotype.conversion_per_thousand =
|
|
static_cast<std::uint32_t>(*genome.parameter(Capability::energy_conversion));
|
|
// A capability the genome does not carry yields nothing, whatever the size.
|
|
// Version 1 is defined in 50 ms ticks: genome rates (per second) enter it at that
|
|
// quantum, rounded down as capacities (ADR-0099; exact for schema 1 records).
|
|
// Version 2 is defined per second and reads them as they are.
|
|
const auto period = development_rate_microseconds(genome.development_version());
|
|
const auto rate_per_tick = [period](std::uint64_t per_second) {
|
|
return accounting::per_tick(per_second, period, accounting::Rounding::down);
|
|
};
|
|
if (genome.carries(Capability::movement)) {
|
|
const auto speed = rate_per_tick(*genome.per_second(Capability::movement));
|
|
if (!speed || *speed > std::numeric_limits<std::uint32_t>::max()) {
|
|
return std::unexpected{DevelopmentError::overflow};
|
|
}
|
|
// Organelles slow the cell: p/2 per thousand off its speed (versions 5 and 6). Version 7
|
|
// drops this: organelle mass slows the cell through its handling instead.
|
|
const auto kept = v7 ? thousand : thousand - photosynthesis / photosynthesis_speed_halving;
|
|
phenotype.movement_speed = static_cast<std::uint32_t>(scaled(*speed, kept));
|
|
}
|
|
if (genome.carries(Capability::ingestion)) {
|
|
const auto intake = rate_per_tick(*genome.per_second(Capability::ingestion));
|
|
std::uint64_t uptake{};
|
|
if (!intake || !multiply(uptake, *intake, phenotype.surface)) {
|
|
return std::unexpected{DevelopmentError::overflow};
|
|
}
|
|
phenotype.ingestion_rate = uptake / rules->reference_surface;
|
|
}
|
|
const auto tolerance = genome.carries(Capability::thermal_tolerance)
|
|
? *genome.parameter(Capability::thermal_tolerance)
|
|
: 0;
|
|
const auto deviation = environment.temperature_millikelvin > rules->reference_temperature
|
|
? environment.temperature_millikelvin - rules->reference_temperature
|
|
: rules->reference_temperature - environment.temperature_millikelvin;
|
|
phenotype.thermal_shortfall = deviation > tolerance ? deviation - tolerance : 0;
|
|
return phenotype;
|
|
}
|
|
|
|
std::expected<DevelopmentPlan, DevelopmentError> plan_development(const Genome& genome,
|
|
GrowthState growth,
|
|
EnvironmentState environment,
|
|
const BodyTuning& tuning) {
|
|
const auto current = derive_phenotype(genome, growth, environment, tuning);
|
|
if (!current) {
|
|
return std::unexpected{current.error()};
|
|
}
|
|
const auto rules = rules_for(genome.development_version());
|
|
if (!rules) {
|
|
return std::unexpected{rules.error()};
|
|
}
|
|
DevelopmentPlan plan;
|
|
plan.after = growth;
|
|
const auto remaining = thousand - growth.realized_per_thousand;
|
|
const auto step = std::min(remaining, rules->growth_step_per_thousand);
|
|
// Take the largest step the available mass and energy admit, never a
|
|
// partially funded one and never a grant. This bounds by total mass only;
|
|
// which elements the step consumes is settled by the composition layer.
|
|
for (std::uint32_t attempt = step; attempt > 0; --attempt) {
|
|
auto proposed = growth;
|
|
proposed.realized_per_thousand += attempt;
|
|
const auto grown = derive_phenotype(genome, proposed, environment, tuning);
|
|
if (!grown) {
|
|
return std::unexpected{grown.error()};
|
|
}
|
|
if (grown->total_mass < current->total_mass) {
|
|
return std::unexpected{DevelopmentError::overflow};
|
|
}
|
|
const auto material = grown->total_mass - current->total_mass;
|
|
const auto energy = scaled(material, rules->construction_per_kilogram);
|
|
if (material > matter_total(environment.available) ||
|
|
energy > environment.available.energy.value) {
|
|
continue;
|
|
}
|
|
plan.after = proposed;
|
|
plan.material_required = material;
|
|
plan.energy_required = energy;
|
|
break;
|
|
}
|
|
return plan;
|
|
}
|
|
} // namespace evo::biology
|