#include "evo/biology/development.hpp" #include "evo/accounting/rates.hpp" #include #include 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::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::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::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 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). if (development_version >= 3 && development_version <= 6) { return DevelopmentRules{1000, 1200, 10, 15, 13, 60'000, 293'150, 50}; } return std::unexpected{DevelopmentError::unsupported_rules}; } std::expected derive_phenotype(const Genome& genome, GrowthState growth, EnvironmentState environment) { 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()}; } 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); phenotype.extent = static_cast( integer_cube_root(phenotype.structural_mass / rules->structure_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}; } 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}; } phenotype.conversion_per_thousand = static_cast(*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::max()) { return std::unexpected{DevelopmentError::overflow}; } // Organelles slow the cell: p/2 per thousand off its speed (version 5). const auto kept = thousand - photosynthesis / photosynthesis_speed_halving; phenotype.movement_speed = static_cast(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 plan_development(const Genome& genome, GrowthState growth, EnvironmentState environment) { const auto current = derive_phenotype(genome, growth, environment); 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); 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