From 9f117c95453361a3e85611b567ed3bda50b2cf63 Mon Sep 17 00:00:00 2001 From: dingo Date: Thu, 22 Oct 2026 22:19:48 -0400 Subject: [PATCH] feat(lifecycle): rules 13 mutation splits, stay-or-become and litter Phase 4 of the cellular revamp build plan (ADR-0179), server half. - A chosen mutation now splits the cell (Cellular_reproduction_plan.md section 4): split_cell divides body and reserve per channel by the tuned share, each cell develops the largest body its own genome builds from its part, and the rest joins its reserve, conserving every channel. Both plans carry the mutation; only the offspring expresses it. A split is refused, spending nothing and keeping the offer, below the tuned growth, without room beside the cell or at the region's organism cap. - Stay or become: a choice (trace mutation byte 34) or a bud (step intent bit 2 of byte 13) can hand control to the offspring; the parent becomes wildlife. Ownership now follows the account under rules 13, so validation and plan history accept a founder that became wildlife (ADR-0163 succession). - Buds under rules 13: litter replaces the 25% twin (whole hundreds plus a chance, up to the tuned maximum, sharing one investment), the tuned bud fee is charged once and reclaimed, and births stop at the organism cap. - New results split_applied and split_refused; owners below rules 13 refuse a become. The revamp tuning file gains the reproduction keys. Tests cover the split's conservation and refusals, stay and become splits with exact restore, a refused second split, the cap, become validation and the litter count. Refreshes the DEV session window. Co-Authored-By: Claude Opus 5.5 --- Cellular_Revamp_Build_Plan.md | 2 +- Cellular_mutation_plan.md | 3 +- .../region/dev-session-route/listener.yaml | 2 +- .../region/dev/tuning/cellular-revamp.tuning | 11 + include/evo/biology/budding.hpp | 16 ++ .../evo/biology/cellular_revamp_tuning.hpp | 5 + include/evo/biology/cellular_step.hpp | 2 + .../evo/server/lifecycle/cellular_lives.hpp | 6 +- .../evo/server/lifecycle/cellular_revamp.hpp | 14 ++ src/biology/budding.cpp | 88 ++++++++ src/biology/cellular_revamp_tuning.cpp | 17 ++ src/server/cellular_region_host.cpp | 1 + src/server/lifecycle/cellular_batch.cpp | 17 +- src/server/lifecycle/cellular_lives.cpp | 210 ++++++++++++++---- src/server/lifecycle/cellular_revamp.cpp | 21 +- tests/budding_tests.cpp | 78 ++++++- tests/cellular_inheritance_replay_tests.cpp | 2 +- tests/cellular_revamp_owner_tests.cpp | 105 +++++++-- tests/cellular_revamp_tuning_tests.cpp | 11 +- 19 files changed, 540 insertions(+), 71 deletions(-) diff --git a/Cellular_Revamp_Build_Plan.md b/Cellular_Revamp_Build_Plan.md index b1c9dcc..2abfe0d 100644 --- a/Cellular_Revamp_Build_Plan.md +++ b/Cellular_Revamp_Build_Plan.md @@ -95,7 +95,7 @@ Verify: chances sum to the option count and respect floor and cap; the draw's marginal frequencies match the chances over a seed ensemble; triggers fire as specified; costs conserve. -### Phase 4: reproduction +### Phase 4: reproduction - server done Server: - On-demand bud with the control choice (stay or become the bud), the tunable fee and diff --git a/Cellular_mutation_plan.md b/Cellular_mutation_plan.md index d4929d6..262bd8b 100644 --- a/Cellular_mutation_plan.md +++ b/Cellular_mutation_plan.md @@ -337,7 +337,8 @@ mutation splits do not. is smaller. The bud costs the same. - The maximum offspring per bud is tunable (trial 10, so litter up to 900). - Each extra offspring needs a free spot touching the parent and room under the region - cap. If fewer can be placed, fewer are born and the investment is shared among them. + cap. If fewer can be placed, fewer are born; the parent keeps the share of any that + were not. - This replaces the current 25% twin chance: new founders start at litter 0. | Name | Effect | Pro | Con | Increase | Decrease | diff --git a/deploy/kubernetes/region/dev-session-route/listener.yaml b/deploy/kubernetes/region/dev-session-route/listener.yaml index 285c521..4b14c98 100644 --- a/deploy/kubernetes/region/dev-session-route/listener.yaml +++ b/deploy/kubernetes/region/dev-session-route/listener.yaml @@ -12,7 +12,7 @@ spec: env: - {name: REGION_SESSION, value: "dev"} - {name: REGION_SESSION_LISTEN, value: "0.0.0.0:7400"} - - {name: REGION_SESSION_WINDOW, value: "dev-20261022T225821-fb28cb29:1792724301"} + - {name: REGION_SESSION_WINDOW, value: "dev-20261023T021936-96b9c651:1792736376"} - {name: REGION_M10_MAX_GRANT_LIFETIME, value: "14400"} ports: # QUIC (ADR-0090): UDP only; nothing listens on TCP 7400. diff --git a/deploy/kubernetes/region/dev/tuning/cellular-revamp.tuning b/deploy/kubernetes/region/dev/tuning/cellular-revamp.tuning index fa6aed1..c2985ed 100644 --- a/deploy/kubernetes/region/dev/tuning/cellular-revamp.tuning +++ b/deploy/kubernetes/region/dev/tuning/cellular-revamp.tuning @@ -47,6 +47,17 @@ mutation.max_offspring = 10 # per bud, so litter at most 900 mutation.carbon_for_light_fg = 20_000_000 # carbon burned before photosynthesis is offered mutation.buds_for_adhesion = 5 # on-demand buds before Adhesion is offered +# Reproduction (reproduction plan). +reproduction.split_share_per_mille = 500 # the offspring's share of a mutation split +reproduction.split_min_growth_per_mille = 1_000 # a cell splits only fully grown +reproduction.bud_fee_e_pj = 0 # on-demand bud fee per channel, times the scale; reclaimed +reproduction.bud_fee_c_fg = 0 +reproduction.bud_fee_n_fg = 0 +reproduction.bud_fee_p_fg = 0 +reproduction.bud_fee_o_fg = 0 +reproduction.bud_fee_scale_percent = 0 +reproduction.organism_cap = 256 # births refused at this many lives (an organism counts once) + # Per-mutation overrides are optional: `mutation.. = value`, with fields enabled, # base_weight, increase_points, decrease_points, primary_step, paired_step, spread_step, # increase_ticks, decrease_ticks, cost_e_pj, cost_c_fg, cost_n_fg, cost_p_fg, cost_o_fg and diff --git a/include/evo/biology/budding.hpp b/include/evo/biology/budding.hpp index 4a7d4e8..f031b48 100644 --- a/include/evo/biology/budding.hpp +++ b/include/evo/biology/budding.hpp @@ -50,6 +50,22 @@ fund_offspring(const Genome& parent, const Genome& offspring, GrowthState parent [[nodiscard]] std::optional offspring_cost(const Genome& parent, const Genome& offspring, std::uint32_t temperature_millikelvin, std::uint32_t share = 1); +// Rules 13 (ADR-0179; Cellular_reproduction_plan.md section 4): a mutation split. The parent's +// body and reserve divide per channel, the offspring taking `share_per_thousand` (rounded +// down); each cell then develops the largest growth its own genome builds from its part of the +// body, and matter beyond that joins its reserve. Nothing is built, burned or lost: on every +// channel the two bodies and reserves sum to the parent's. Refused, changing nothing, when +// the parent is below `min_growth` or damaged, or either part builds no cell at all. +struct SplitTransition { + GrowthState parent_growth{}, child_growth{}; + accounting::Inventory parent_body{}, parent_reserve{}, child_body{}, child_reserve{}; + auto operator<=>(const SplitTransition&) const = default; +}; +[[nodiscard]] std::expected +split_cell(const Genome& parent, const Genome& child, GrowthState growth, + const accounting::Inventory& body, const accounting::Inventory& reserve, + std::uint32_t temperature_millikelvin, std::uint32_t share_per_thousand, + std::uint32_t min_growth, const BodyTuning& tuning = default_body_tuning); // What one bud takes from a fully grown, undamaged parent of this genome at this temperature // (ADR-0129 amendment): investment matter per channel, and construction energy plus the // bud's provision; exactly what `fund_bud` charges such a parent. Nullopt when the genome diff --git a/include/evo/biology/cellular_revamp_tuning.hpp b/include/evo/biology/cellular_revamp_tuning.hpp index 119c9b0..fd12914 100644 --- a/include/evo/biology/cellular_revamp_tuning.hpp +++ b/include/evo/biology/cellular_revamp_tuning.hpp @@ -45,6 +45,11 @@ struct CellularRevampTuning { std::int64_t mutation_min_points{}, mutation_max_points{}, mutation_recipe_min_share{}; std::int64_t mutation_max_offspring{}, mutation_carbon_for_light_fg{}, mutation_buds_for_adhesion{}; + // Reproduction (Cellular_reproduction_plan.md). + std::int64_t reproduction_split_share_per_mille{}, reproduction_split_min_growth_per_mille{}; + std::int64_t reproduction_bud_fee_e_pj{}, reproduction_bud_fee_c_fg{}, + reproduction_bud_fee_n_fg{}, reproduction_bud_fee_p_fg{}, reproduction_bud_fee_o_fg{}; + std::int64_t reproduction_bud_fee_scale_percent{}, reproduction_organism_cap{}; // Each catalog mutation's tunables, in catalog order. Optional keys // `mutation..` override the defaults from its spec. std::array mutations{}; diff --git a/include/evo/biology/cellular_step.hpp b/include/evo/biology/cellular_step.hpp index 53eee29..fa92964 100644 --- a/include/evo/biology/cellular_step.hpp +++ b/include/evo/biology/cellular_step.hpp @@ -25,6 +25,8 @@ struct CellularStepIntent { // Rules 13 (ADR-0179): matter channels the player keeps from being burned for anything but // upkeep, bit 0 carbon to bit 3 other matter. Wildlife keeps nothing. std::uint8_t exclude{}; + // Rules 13: with `bud`, the player continues as the (first) bud; the parent becomes wildlife. + bool become{}; auto operator<=>(const CellularStepIntent&) const = default; }; diff --git a/include/evo/server/lifecycle/cellular_lives.hpp b/include/evo/server/lifecycle/cellular_lives.hpp index 19cce4c..7deec4f 100644 --- a/include/evo/server/lifecycle/cellular_lives.hpp +++ b/include/evo/server/lifecycle/cellular_lives.hpp @@ -178,6 +178,8 @@ struct CellularLifeIntent { struct CellularMutationSelection { std::uint64_t life_id{}, event_index{}, offer_tick{}, successor_id{}; std::uint16_t operator_id{}; + // Rules 13: the split's offspring becomes the player's cell; the founder becomes wildlife. + bool become{}; auto operator<=>(const CellularMutationSelection&) const = default; }; // declined (ADR-0120 amendment): operator 0 consumed the offer without changing the genome. @@ -186,7 +188,9 @@ enum class CellularMutationResult : std::uint8_t { life_died, insufficient_funding, declined, - offspring_plan_applied // Rules 10: inherited plan changed, expressed body did not. + offspring_plan_applied, // Rules 10: inherited plan changed, expressed body did not. + split_applied, // Rules 13: the cell split; the offspring expresses the mutation. + split_refused // Rules 13: no split now (size, room or region cap); offer kept. }; struct CellularMutationExecution { std::uint64_t life_id{}, successor_id{}; diff --git a/include/evo/server/lifecycle/cellular_revamp.hpp b/include/evo/server/lifecycle/cellular_revamp.hpp index faaacae..be7715e 100644 --- a/include/evo/server/lifecycle/cellular_revamp.hpp +++ b/include/evo/server/lifecycle/cellular_revamp.hpp @@ -39,6 +39,12 @@ struct CellularRevampRules { biology::CellularCatalogBounds bounds{}; std::uint32_t offer_interval_ticks{500}; std::array mutations{}; + // Reproduction: the offspring's share of a split and the growth a cell needs to split; the + // on-demand bud's fee (amounts times scale, reclaimed); the region's organism cap. + std::uint32_t split_share_per_thousand{500}, split_min_growth{1'000}; + accounting::Inventory bud_fee{}; + std::uint32_t bud_fee_scale_percent{}; + std::uint32_t organism_cap{256}; auto operator<=>(const CellularRevampRules&) const = default; }; @@ -75,6 +81,14 @@ cellular_draw_offer(const biology::Genome& plan, const CellularRevampLife& life, const CellularRevampRules& rules, std::uint64_t world, std::uint64_t region, std::uint64_t life_id, std::uint64_t tick); +// How many offspring one bud makes from `litter` (hundredths of an extra offspring) and a roll: +// one, plus every whole hundred, plus one more when roll % 100 falls below the remainder; at most +// max_litter / 100 + 1. +[[nodiscard]] constexpr std::uint32_t +cellular_litter_count(std::uint32_t litter, std::uint64_t roll, std::uint32_t max_litter) noexcept { + const auto count = 1 + litter / 100 + (roll % 100 < litter % 100 ? 1U : 0U); + return count < max_litter / 100 + 1 ? count : max_litter / 100 + 1; +} // What choosing a mutation costs: its amounts times its scale. [[nodiscard]] accounting::Inventory cellular_mutation_cost(const biology::CellularMutationTuning& tuning) noexcept; diff --git a/src/biology/budding.cpp b/src/biology/budding.cpp index 1cda19e..42121e2 100644 --- a/src/biology/budding.cpp +++ b/src/biology/budding.cpp @@ -1,6 +1,7 @@ #include "evo/biology/budding.hpp" #include +#include namespace evo::biology { namespace { @@ -98,6 +99,93 @@ fund_offspring(const Genome& genome, const Genome& offspring, GrowthState parent return std::unexpected{BuddingError::insufficient_funding}; // Too little to build a cell. } +namespace { +// The largest growth whose body `genome` builds within `matter` on every channel, its body, +// or nothing when even the smallest does not fit. Bodies grow with growth, so a binary search. +std::expected>, BuddingError> +largest_fit(const Genome& genome, const accounting::Inventory& matter, std::uint32_t temperature, + const BodyTuning& tuning) { + const auto body_at = [&](std::uint32_t realized) + -> std::expected, BuddingError> { + const auto budget = + metabolic_budget(genome, {realized, 0}, {temperature, {}}, 1000, tuning); + if (!budget) + return std::unexpected{BuddingError::development}; + auto body = budget->body; + body.energy = {}; + auto only = matter; + only.energy = {}; + if (!fits(body, only)) + return std::optional{}; + return std::optional{body}; + }; + std::uint32_t low = 1, high = 1000; + std::optional> best; + while (low <= high) { + const auto mid = low + (high - low) / 2; + const auto body = body_at(mid); + if (!body) + return std::unexpected{body.error()}; + const auto& fitted = *body; + if (fitted) { + best = std::pair{GrowthState{mid, 0}, *fitted}; + low = mid + 1; + } else { + high = mid - 1; + } + } + return best; +} +} // namespace + +std::expected +split_cell(const Genome& parent, const Genome& child, GrowthState growth, + const accounting::Inventory& body, const accounting::Inventory& reserve, + std::uint32_t temperature_millikelvin, std::uint32_t share_per_thousand, + std::uint32_t min_growth, const BodyTuning& tuning) { + if (parent.schema_version() != child.schema_version() || + parent.development_version() != child.development_version()) + return std::unexpected{BuddingError::incompatible_genome}; + if (share_per_thousand == 0 || share_per_thousand >= 1000 || body.energy.value != 0) + return std::unexpected{BuddingError::no_capability}; + if (growth.realized_per_thousand < min_growth || growth.damage_per_thousand != 0) + return std::unexpected{BuddingError::not_grown}; + const auto part = [&](std::uint64_t value) { + return value / 1000 * share_per_thousand + value % 1000 * share_per_thousand / 1000; + }; + accounting::Inventory child_matter{}, parent_matter{}; + SplitTransition split{}; + split.child_reserve.energy.value = part(reserve.energy.value); + split.parent_reserve.energy.value = reserve.energy.value - split.child_reserve.energy.value; + for (std::size_t c = 0; c < body.matter.size(); ++c) { + child_matter.matter[c].value = part(body.matter[c].value); + parent_matter.matter[c].value = body.matter[c].value - child_matter.matter[c].value; + split.child_reserve.matter[c].value = part(reserve.matter[c].value); + split.parent_reserve.matter[c].value = + reserve.matter[c].value - split.child_reserve.matter[c].value; + } + const auto parent_fit = largest_fit(parent, parent_matter, temperature_millikelvin, tuning); + const auto child_fit = largest_fit(child, child_matter, temperature_millikelvin, tuning); + if (!parent_fit || !child_fit) + return std::unexpected{BuddingError::development}; + const auto& parent_part = *parent_fit; + const auto& child_part = *child_fit; + if (!parent_part || !child_part) + return std::unexpected{BuddingError::insufficient_funding}; // Too little for a cell. + split.parent_growth = parent_part->first; + split.parent_body = parent_part->second; + split.child_growth = child_part->first; + split.child_body = child_part->second; + // What each body's part does not build stays with that cell as reserve. + for (std::size_t c = 0; c < body.matter.size(); ++c) { + split.parent_reserve.matter[c].value += + parent_matter.matter[c].value - split.parent_body.matter[c].value; + split.child_reserve.matter[c].value += + child_matter.matter[c].value - split.child_body.matter[c].value; + } + return split; +} + std::optional bud_cost(const Genome& genome, std::uint32_t temperature_millikelvin) { return offspring_cost(genome, genome, temperature_millikelvin); diff --git a/src/biology/cellular_revamp_tuning.cpp b/src/biology/cellular_revamp_tuning.cpp index ad47491..993b04f 100644 --- a/src/biology/cellular_revamp_tuning.cpp +++ b/src/biology/cellular_revamp_tuning.cpp @@ -62,6 +62,23 @@ constexpr std::array fields{ amount_limit}, Field{"mutation.buds_for_adhesion", &CellularRevampTuning::mutation_buds_for_adhesion, 0, 1'000}, + Field{"reproduction.split_share_per_mille", + &CellularRevampTuning::reproduction_split_share_per_mille, 1, 999}, + Field{"reproduction.split_min_growth_per_mille", + &CellularRevampTuning::reproduction_split_min_growth_per_mille, 1, 1'000}, + Field{"reproduction.bud_fee_e_pj", &CellularRevampTuning::reproduction_bud_fee_e_pj, 0, + amount_limit}, + Field{"reproduction.bud_fee_c_fg", &CellularRevampTuning::reproduction_bud_fee_c_fg, 0, + amount_limit}, + Field{"reproduction.bud_fee_n_fg", &CellularRevampTuning::reproduction_bud_fee_n_fg, 0, + amount_limit}, + Field{"reproduction.bud_fee_p_fg", &CellularRevampTuning::reproduction_bud_fee_p_fg, 0, + amount_limit}, + Field{"reproduction.bud_fee_o_fg", &CellularRevampTuning::reproduction_bud_fee_o_fg, 0, + amount_limit}, + Field{"reproduction.bud_fee_scale_percent", + &CellularRevampTuning::reproduction_bud_fee_scale_percent, 0, 100}, + Field{"reproduction.organism_cap", &CellularRevampTuning::reproduction_organism_cap, 1, 256}, }; // A per-mutation field's bounds, in cellular_mutation_tuning_fields order. diff --git a/src/server/cellular_region_host.cpp b/src/server/cellular_region_host.cpp index c1fc0cf..1708064 100644 --- a/src/server/cellular_region_host.cpp +++ b/src/server/cellular_region_host.cpp @@ -118,6 +118,7 @@ network::ActionOutcome action_outcome(const FinalCellularMutation& mutation) noe return {mutation.action_id, mutation.result == CellularMutationResult::applied || mutation.result == CellularMutationResult::offspring_plan_applied || + mutation.result == CellularMutationResult::split_applied || mutation.result == CellularMutationResult::declined ? network::ActionResult::committed : network::ActionResult::committed_no_effect, diff --git a/src/server/lifecycle/cellular_batch.cpp b/src/server/lifecycle/cellular_batch.cpp index b785068..af6d5e2 100644 --- a/src/server/lifecycle/cellular_batch.cpp +++ b/src/server/lifecycle/cellular_batch.cpp @@ -58,7 +58,7 @@ bool valid_intent(const CellularLifeIntent& life) { !(step.bud && (step.intake_target || step.engulf)) && !(step.frozen && (step.movement != biology::CellularIntent{} || step.intake_target || step.engulf || step.bud || step.brake || step.exclude != 0)) && - step.exclude <= 0x0F; + step.exclude <= 0x0F && !(step.become && !step.bud); } bool valid_ticks(std::span ticks) { std::set identities; @@ -166,8 +166,10 @@ void put_intent(std::vector& out, const CellularLifeIntent& life) : 0); // Flags: bit 0 frozen (ADR-0128), bit 1 brake (rules 12). SQL never parses this byte, and // player action records keep it zero: the trace alone carries a brake. - out.push_back( - static_cast((life.intent.frozen ? 1U : 0U) | (life.intent.brake ? 2U : 0U))); + // Bit 2 (rules 13): continue as the bud. + out.push_back(static_cast((life.intent.frozen ? 1U : 0U) | + (life.intent.brake ? 2U : 0U) | + (life.intent.become ? 4U : 0U))); // Byte 14 (rules 13): kept matter channels, bits 0-3; byte 15 stays zero. out.push_back(life.intent.exclude); out.push_back(0); @@ -239,7 +241,8 @@ encode_cellular_batch(const CellularBatch& batch) { put(out, mutation.offer_tick, 8); put(out, mutation.successor_id, 8); put(out, mutation.operator_id, 2); - put(out, 0, 6); + put(out, mutation.become ? 1 : 0, 1); // Byte 34 (rules 13). + put(out, 0, 5); } } if (out.size() != size) @@ -308,7 +311,7 @@ decode_cellular_batch(std::span bytes) { step.lives.reserve(static_cast(count)); for (std::uint64_t i = 0; i < count; ++i) { const auto record = bytes.subspan(offset, cellular_batch_intent_bytes); - if (record[12] > 3 || record[13] > 3 || record[14] > 0x0F || record[15] != 0) + if (record[12] > 3 || record[13] > 7 || record[14] > 0x0F || record[15] != 0) return std::unexpected{CellularBatchError::format}; CellularLifeIntent life; life.life_id = get(record, 0, 8); @@ -328,6 +331,7 @@ decode_cellular_batch(std::span bytes) { life.intent.frozen = (record[13] & 1U) != 0; // ADR-0128. life.intent.brake = (record[13] & 2U) != 0; // Rules 12; owners below refuse it. life.intent.exclude = record[14]; // Rules 13; owners below refuse it. + life.intent.become = (record[13] & 4U) != 0; // Rules 13; owners below refuse it. if (!valid_intent(life) || (!step.lives.empty() && step.lives.back().life_id >= life.life_id)) return std::unexpected{CellularBatchError::format}; @@ -364,7 +368,8 @@ decode_cellular_batch(std::span bytes) { mutation.offer_tick = get(record, 16, 8); mutation.successor_id = get(record, 24, 8); mutation.operator_id = static_cast(get(record, 32, 2)); - if (!all_zero(record.subspan(34, 6)) || mutation.life_id == 0 || + mutation.become = record[34] == 1; + if (record[34] > 1 || !all_zero(record.subspan(35, 5)) || mutation.life_id == 0 || mutation.event_index == 0 || mutation.offer_tick == 0 || mutation.successor_id == 0 || (!step.mutations.empty() && step.mutations.back().life_id >= mutation.life_id)) diff --git a/src/server/lifecycle/cellular_lives.cpp b/src/server/lifecycle/cellular_lives.cpp index a5f8304..8d0ff9d 100644 --- a/src/server/lifecycle/cellular_lives.cpp +++ b/src/server/lifecycle/cellular_lives.cpp @@ -62,9 +62,11 @@ constexpr std::size_t cellular_life_motion_bytes = 20; // reserved zero, 32), the catalog bounds (five 4-byte fields, a reserved zero and the carbon // gate, 32), the offer interval and a reserved zero (8), and 49 mutation tunables of 80 bytes. constexpr std::size_t cellular_revamp_mutation_bytes = 80; +// Then the reproduction tunables: split share, split growth, organism cap, fee scale (16) and +// the bud fee (40). constexpr std::size_t cellular_lives_v11_header_bytes = cellular_lives_v10_header_bytes + 120 + 8 + 32 + 32 + 8 + - biology::cellular_catalog_size * cellular_revamp_mutation_bytes; + biology::cellular_catalog_size * cellular_revamp_mutation_bytes + 56; // Each format-11 life adds its mutation state: 49 scores and a reserved zero (100), 98 trigger // states of 24 bytes (2,352), lineage progress and the cadence counter (16), the last brake, // three zeros and the growth carry (8), the offers made (8), and the pending offer (88). @@ -243,6 +245,10 @@ void write_revamp_offers(Writer& out, const CellularRevampRules& r) { out.put(value, 4); out.inventory(m.cost); } + for (const auto value : + {r.split_share_per_thousand, r.split_min_growth, r.organism_cap, r.bud_fee_scale_percent}) + out.put(value, 4); + out.inventory(r.bud_fee); } bool read_revamp_offers(Reader& in, CellularRevampRules& r) { const auto u32 = [&in] { return static_cast(in.get(4)); }; @@ -286,6 +292,11 @@ bool read_revamp_offers(Reader& in, CellularRevampRules& r) { m.cost_scale_percent = u32(); m.cost = in.inventory(); } + r.split_share_per_thousand = u32(); + r.split_min_growth = u32(); + r.organism_cap = u32(); + r.bud_fee_scale_percent = u32(); + r.bud_fee = in.inventory(); return zero; } void write_revamp_life(Writer& out, const CellularRevampLife& life) { @@ -505,7 +516,8 @@ sum_state(accounting::Inventory nursery, accounting::Inventory heat, bool validate_plans(std::span lives, std::span corpses, const biology::LineageArchive& archive, - std::span history, std::uint64_t tick) { + std::span history, std::uint64_t tick, + bool transfers = false) { if (history.size() > biology::max_archived_genomes) return false; struct Plan { @@ -528,7 +540,7 @@ bool validate_plans(std::span lives, plans.push_back( {life.identity.life, life.identity.genome, life.identity.genome, life.offspring_genome, std::max(life.identity.born.value, last_expression[life.identity.organism]), 0, - life.account != LifeAccount{}}); + transfers || life.account != LifeAccount{}}); observe(life.identity.genome, life.identity.born.value); } for (const auto& corpse : corpses) { @@ -538,7 +550,7 @@ bool validate_plans(std::span lives, return false; plans.push_back({corpse.life_id, *expressed, *expressed, corpse.offspring_genome, std::max(record->born.value, last_expression[corpse.organism]), - corpse.died_tick, corpse.parent_life == 0}); + corpse.died_tick, transfers || corpse.parent_life == 0}); observe(record->genome, record->born.value); observe(*expressed, record->born.value); } @@ -590,7 +602,7 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame, bool bud_rules = false, std::uint64_t next_bud_sequence = 1, bool presence_rules = false, const CellularBoundaryLedger* boundary = nullptr, const std::vector* plans = nullptr, - const accounting::Inventory& spent = {}) { + const accounting::Inventory& spent = {}, bool transfers = false) { // A bud's identity lies in this region's namespace below the next sequence; anything // else is an allocated identity, below the namespace (ADR-0125). const auto bud_identity = [&](std::uint64_t identity) noexcept { @@ -658,30 +670,34 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame, const auto& life = lives[i]; if (!plans && life.offspring_genome != 0) return std::unexpected{CellularLivesError::version}; - // Founders are owned; buds are unowned wildlife (rules 4) and never earn offers. + // Founders are owned; buds are unowned wildlife (rules 4) and never earn offers. Rules 13 + // (ADR-0163 succession) moves control between parent and offspring, so ownership is the + // account's alone; bud identities still follow parentage. const bool wildlife = life.parent_life != 0; + const bool owned = transfers ? life.account != LifeAccount{} : !wildlife; // ADR-0128: only owned lives leave; a lingering life is within its linger time. const bool presence_valid = life.presence == CellularPresence::active ? life.frozen_since == 0 - : presence_rules && !wildlife && life.frozen_since != 0 && + : presence_rules && owned && life.frozen_since != 0 && life.frozen_since <= config.tick && (life.presence == CellularPresence::suspended || (life.presence == CellularPresence::lingering && config.tick - life.frozen_since < cellular_linger_ticks)); if (!presence_valid) return std::unexpected{CellularLivesError::invalid_life}; - if ((life.account == LifeAccount{}) != wildlife || life.identity.life == 0 || - life.identity.organism == 0 || life.identity.genome == 0 || life.identity.event == 0 || - life.identity.born.value == 0 || life.identity.born.value > config.tick || - life.source == 0 || (i != 0 && lives[i - 1].identity.life >= life.identity.life) || + if ((!transfers && (life.account == LifeAccount{}) != wildlife) || + life.identity.life == 0 || life.identity.organism == 0 || life.identity.genome == 0 || + life.identity.event == 0 || life.identity.born.value == 0 || + life.identity.born.value > config.tick || life.source == 0 || + (i != 0 && lives[i - 1].identity.life >= life.identity.life) || !lineage_valid(life.identity.life, life.parent_life) || (wildlife && (life.identity.organism != life.identity.life || life.identity.event != life.identity.life || life.mutation != CellularMutationProgress{}))) return std::unexpected{CellularLivesError::invalid_life}; for (std::size_t j = 0; j < i; ++j) - if ((!wildlife && lives[j].account == life.account) || + if ((owned && lives[j].account == life.account) || lives[j].identity.organism == life.identity.organism) return std::unexpected{CellularLivesError::duplicate_life}; const auto record = archive.organism(life.identity.organism); @@ -761,7 +777,7 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame, if (std::ranges::adjacent_find(life_ids) != life_ids.end()) return std::unexpected{CellularLivesError::duplicate_life}; - if (plans && !validate_plans(lives, corpses, archive, *plans, config.tick)) + if (plans && !validate_plans(lives, corpses, archive, *plans, config.tick, transfers)) return std::unexpected{CellularLivesError::invalid_life}; auto current = sum_state(nursery, heat, habitat, lives, corpses); if (!current) @@ -1011,11 +1027,11 @@ CellularLives::migration_replacement(std::vector living, state_->region, state_->frame); if (!habitat) return std::unexpected(CellularLivesError::habitat); - const auto total = - validate_state(state_->world, state_->region, state_->frame, state_->config, - state_->nursery_config, **habitat, living, state_->corpses, archive, - state_->nursery, state_->heat, state_->opening_total, true, true, - state_->next_bud_sequence, true, &boundary, nullptr, state_->spent); + const auto total = validate_state( + state_->world, state_->region, state_->frame, state_->config, state_->nursery_config, + **habitat, living, state_->corpses, archive, state_->nursery, state_->heat, + state_->opening_total, true, true, state_->next_bud_sequence, true, &boundary, nullptr, + state_->spent, state_->rules_version >= cellular_lives_revamp_rules); if (!total) return std::unexpected(total.error()); auto replacement = std::unique_ptr{new CellularLives{std::make_unique( @@ -1077,13 +1093,14 @@ CellularLives::exchange_boundary(std::span deposit habitat = std::move(*created); } const auto rules = state_->rules_version; - const auto total = validate_state( - state_->world, state_->region, state_->frame, state_->config, state_->nursery_config, - *habitat, state_->lives, state_->corpses, state_->archive, state_->nursery, heat, - state_->opening_total, rules >= cellular_lives_engulf_rules, - rules >= cellular_lives_bud_rules, state_->next_bud_sequence, - rules >= cellular_lives_presence_rules, &boundary, - cellular_retained_plans(rules) ? &state_->offspring_revisions : nullptr, state_->spent); + const auto total = + validate_state(state_->world, state_->region, state_->frame, state_->config, + state_->nursery_config, *habitat, state_->lives, state_->corpses, + state_->archive, state_->nursery, heat, state_->opening_total, + rules >= cellular_lives_engulf_rules, rules >= cellular_lives_bud_rules, + state_->next_bud_sequence, rules >= cellular_lives_presence_rules, &boundary, + cellular_retained_plans(rules) ? &state_->offspring_revisions : nullptr, + state_->spent, state_->rules_version >= cellular_lives_revamp_rules); if (!total) return std::unexpected(total.error()); BoundarySavepoint savepoint{std::move(state_->habitat), state_->heat, state_->current_total, @@ -1687,7 +1704,7 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c state_->format_version >= cellular_lives_migration_format ? &state_->boundary : nullptr, state_->format_version >= cellular_lives_inheritance_format ? &state_->offspring_revisions : nullptr, - state_->spent); + state_->spent, state_->rules_version >= cellular_lives_revamp_rules); if (!current) return std::unexpected{current.error()}; @@ -2128,7 +2145,7 @@ CellularLives::restore(std::span bytes, std::uint64_t world, saved_world, saved_region, saved_frame, config, nursery_config, **habitat, lives, corpses, *archive, nursery, heat, opening_total, engulf_version, bud_version, next_bud_sequence, presence_version, migration_version ? &boundary : nullptr, - inheritance_version ? &plan_history : nullptr, spent); + inheritance_version ? &plan_history : nullptr, spent, revamp_version); if (!current) return std::unexpected{current.error()}; @@ -2197,6 +2214,9 @@ CellularLives::advance_tick_with_mutations(std::span i return std::unexpected{CellularLivesError::invalid_intents}; if (intent.brake && !liquid_rules) return std::unexpected{CellularLivesError::invalid_intents}; + if (intent.become && + (!revamp_rules || !intent.bud || state_->lives[i].account == LifeAccount{})) + return std::unexpected{CellularLivesError::invalid_intents}; // Rules 13: exclusions are an owned life's setting, never wildlife's or a frozen cell's. if (intent.exclude != 0 && (!revamp_rules || intent.exclude > 0x0F || intent.frozen || state_->lives[i].account == LifeAccount{})) @@ -2205,7 +2225,8 @@ CellularLives::advance_tick_with_mutations(std::span i for (std::size_t i = 0; i < selections.size(); ++i) { const auto& selection = selections[i]; if (selection.life_id == 0 || selection.event_index == 0 || selection.offer_tick == 0 || - selection.successor_id == 0 || // Operator 0 declines (ADR-0120 amendment). + selection.successor_id == 0 || // Operator 0 declines (ADR-0120 amendment). + (selection.become && !revamp_rules) || // Rules 13 only (ADR-0179). (i != 0 && selections[i - 1].life_id >= selection.life_id)) return std::unexpected{CellularLivesError::invalid_intents}; const auto life = std::ranges::lower_bound( @@ -2501,6 +2522,8 @@ CellularLives::advance_tick_with_mutations(std::span i next_lives.erase(found); } std::vector mutation_results; + // Bud identities: rules-13 splits draw from the same sequence as buds. + auto next_bud_sequence = state_->next_bud_sequence; mutation_results.reserve(selections.size()); for (const auto& selection : selections) { const auto found = std::ranges::lower_bound( @@ -2560,21 +2583,81 @@ CellularLives::advance_tick_with_mutations(std::span i continue; } const auto successor = biology::Genome::create(*draft); - if (!successor || next_offspring_revisions.size() >= biology::max_archived_genomes || - !next_archive.admit(*successor)) + if (!successor) + return std::unexpected{CellularLivesError::mutation}; + // The split (Cellular_reproduction_plan.md section 4): body and reserve divide, the + // offspring expresses the mutation, both plans carry it. Refused, spending nothing, + // when the cell is too small, has no room beside it, or the region is full. + const auto temperature = state_->config.ambient_temperature_millikelvin; + const auto split = + biology::split_cell(*expressed, *successor, found->growth, found->body, reserve, + temperature, state_->revamp.split_share_per_thousand, + state_->revamp.split_min_growth, state_->revamp.body); + if (!split && split.error() == biology::BuddingError::development) + return std::unexpected{CellularLivesError::biology}; + const auto identity = cellular_bud_identity(state_->region, next_bud_sequence); + std::optional spot; + if (split) { + const auto shrunk = biology::metabolic_budget( + *expressed, split->parent_growth, {temperature, {}}, 1000, state_->revamp.body); + const auto born = biology::metabolic_budget( + *successor, split->child_growth, {temperature, {}}, 1000, state_->revamp.body); + if (!shrunk || !born) + return std::unexpected{CellularLivesError::biology}; + const auto position = + bud_position(*found, shrunk->phenotype.extent, born->phenotype.extent, + next_lives, next_archive, state_->config); + if (!position) + return std::unexpected{position.error()}; + spot = *position; + } + if (!split || !spot || identity == 0 || + next_lives.size() >= state_->revamp.organism_cap || + next_archive.organism(identity)) { + mutation_results.push_back({selection.life_id, selection.successor_id, + CellularMutationResult::split_refused}); + continue; + } + if (next_offspring_revisions.size() >= biology::max_archived_genomes || + !next_archive.admit(*successor) || + !next_archive.record_birth(identity, successor->id(), {proposal->end_tick})) return std::unexpected{CellularLivesError::archive}; next_offspring_revisions.push_back({selection.life_id, found->offspring_genome, selection.successor_id, proposal->end_tick}); - found->offspring_genome = selection.successor_id; - found->reserve = reserve; if (!add_inventory(next_spent, payment->spent)) return std::unexpected{CellularLivesError::overflow}; auto returned = cost; returned.energy.value = payment->energy; reclaims.emplace_back(returned, found->position); + CellularOwnedLife child{}; + child.identity = {identity, identity, successor->id(), identity, {proposal->end_tick}}; + child.source = found->source; + child.growth = split->child_growth; + child.position = *spot; + child.body = split->child_body; + child.reserve = split->child_reserve; + child.parent_life = found->identity.life; + child.offspring_genome = successor->id(); + child.revamp.affinity.score = found->revamp.affinity.score; + child.revamp.progress = found->revamp.progress; + found->growth = split->parent_growth; + found->body = split->parent_body; + found->reserve = split->parent_reserve; + found->offspring_genome = successor->id(); pending = {}; - mutation_results.push_back({selection.life_id, selection.successor_id, - CellularMutationResult::offspring_plan_applied}); + if (selection.become) { + // One-way: the player continues as the offspring; the founder is wildlife. + child.account = found->account; + found->account = {}; + found->revamp.active_ticks = 0; + } + ++next_bud_sequence; + next_lives.insert(std::ranges::lower_bound( + next_lives, identity, {}, + [](const CellularOwnedLife& life) { return life.identity.life; }), + child); + mutation_results.push_back( + {selection.life_id, selection.successor_id, CellularMutationResult::split_applied}); continue; } const auto parent = next_archive.find(cellular_offspring_genome(*found)); @@ -2639,7 +2722,6 @@ CellularLives::advance_tick_with_mutations(std::span i // Rules 4 budding (ADR-0125), after mutations and in parent life order: a parent that // died this tick cannot bud, and a bud born this tick asked for nothing. std::vector bud_results; - auto next_bud_sequence = state_->next_bud_sequence; for (const auto& request : ordered) { if (!bud_rules || !request.intent.bud) continue; @@ -2657,8 +2739,35 @@ CellularLives::advance_tick_with_mutations(std::span i // ADR-0137 (rules 7): on a twin roll the bud investment is split between two buds // (each a quarter of the parent's body). The parent's reported result is the // first bud's; a twin that finds no room is simply not made. + // Rules 13 (mutation plan section 9): litter gives extra offspring sharing one investment, + // whole hundreds guaranteed and the rest a chance, up to the declared maximum. + std::uint32_t litter_count = 1; + accounting::Inventory fee{}; + if (revamp_rules) { + const auto expressed_genome = next_archive.find(parent->identity.genome); + const auto& traits = + expressed_genome ? expressed_genome->cellular_traits() : std::nullopt; + const auto litter = traits ? std::uint32_t{traits->litter} : 0U; + Random roll{state_->world * 0x9e3779b97f4a7c15ULL ^ request.life_id ^ + proposal->end_tick * 0xbf58476d1ce4e5b9ULL}; + litter_count = + cellular_litter_count(litter, roll.next(), state_->revamp.bounds.max_litter); + fee = cellular_mutation_cost( + {.cost = state_->revamp.bud_fee, + .cost_scale_percent = state_->revamp.bud_fee_scale_percent}); + bool affordable = parent->reserve.energy.value >= fee.energy.value; + for (std::size_t c = 0; c < fee.matter.size(); ++c) + affordable = affordable && parent->reserve.matter[c].value >= fee.matter[c].value; + if (!affordable) { + bud_results.push_back( + {request.life_id, 0, CellularBudResult::insufficient_funding}); + if (parent->account != LifeAccount{}) + activity_of(request.life_id)[biology::CellularSignal::bud_refused] += 1; + continue; + } + } const bool twins = [&] { - if (state_->rules_version < cellular_lives_reproduction_rules) + if (state_->rules_version < cellular_lives_reproduction_rules || revamp_rules) return false; const auto parent_genome = next_archive.find(parent->identity.genome); const auto offspring_genome = next_archive.find(cellular_offspring_genome(*parent)); @@ -2674,7 +2783,8 @@ CellularLives::advance_tick_with_mutations(std::span i state_->config.ambient_temperature_millikelvin) .has_value(); }(); - for (int copy = 0; copy < (twins ? 2 : 1); ++copy) { + const auto copies = revamp_rules ? litter_count : (twins ? 2U : 1U); + for (std::uint32_t copy = 0; copy < copies; ++copy) { if (copy == 1) // Inserting the first bud may have moved the parent; find it again. parent = std::ranges::lower_bound( @@ -2697,8 +2807,8 @@ CellularLives::advance_tick_with_mutations(std::span i for (std::size_t c = 0; surplus && c < cost->matter.size(); ++c) surplus = parent->reserve.matter[c].value / 2 >= cost->matter[c].value; } - const auto transition = biology::fund_offspring( - *expressed, *genome, parent->growth, parent->reserve, temperature, twins ? 2U : 1U); + const auto transition = biology::fund_offspring(*expressed, *genome, parent->growth, + parent->reserve, temperature, copies); if (revamp_rules && !twin && parent->account != LifeAccount{}) { using S = biology::CellularSignal; auto& a = activity_of(request.life_id); @@ -2733,6 +2843,7 @@ CellularLives::advance_tick_with_mutations(std::span i return std::unexpected{position.error()}; if (identity == 0 || !*position || next_lives.size() >= biology::max_cellular_tick_lives || + (revamp_rules && next_lives.size() >= state_->revamp.organism_cap) || next_archive.organism(identity)) { if (!twin) bud_results.push_back({request.life_id, 0, CellularBudResult::no_placement}); @@ -2742,6 +2853,15 @@ CellularLives::advance_tick_with_mutations(std::span i !next_archive.record_birth(identity, genome->id(), {proposal->end_tick})) return std::unexpected{CellularLivesError::archive}; parent->reserve = transition->parent_reserve; + if (revamp_rules && !twin) { + // The fee, once per bud action, returns to the region; then control, if asked. + for (std::size_t c = 0; c < fee.matter.size(); ++c) + parent->reserve.matter[c].value -= + std::min(parent->reserve.matter[c].value, fee.matter[c].value); + parent->reserve.energy.value -= + std::min(parent->reserve.energy.value, fee.energy.value); + reclaims.emplace_back(fee, parent->position); + } CellularOwnedLife bud{}; bud.identity = {identity, identity, genome->id(), identity, {proposal->end_tick}}; bud.source = parent->source; @@ -2756,6 +2876,16 @@ CellularLives::advance_tick_with_mutations(std::span i // Offspring start with the parent's scores and lineage progress. bud.revamp.affinity.score = parent->revamp.affinity.score; bud.revamp.progress = parent->revamp.progress; + const auto asked = std::ranges::lower_bound(ordered, request.life_id, {}, + &CellularLifeIntent::life_id); + if (!twin && asked != ordered.end() && asked->life_id == request.life_id && + asked->intent.become) { + // One-way: the player continues as the first bud; the parent is wildlife. + bud.account = parent->account; + parent->account = {}; + parent->revamp.offer = {}; + parent->revamp.active_ticks = 0; + } } // Foreign residents can have a higher origin-region namespace. Keep // the sequence sorted even before the next parent's lookup/twin. @@ -3024,7 +3154,7 @@ CellularLives::advance_tick_with_mutations(std::span i state_->nursery, next_heat, state_->opening_total, engulf_rules, bud_rules, next_bud_sequence, presence_rules, state_->format_version >= cellular_lives_migration_format ? &state_->boundary : nullptr, - inheritance_rules ? &next_offspring_revisions : nullptr, next_spent); + inheritance_rules ? &next_offspring_revisions : nullptr, next_spent, revamp_rules); if (!total) return std::unexpected{total.error()}; diff --git a/src/server/lifecycle/cellular_revamp.cpp b/src/server/lifecycle/cellular_revamp.cpp index 48077c9..dac1153 100644 --- a/src/server/lifecycle/cellular_revamp.cpp +++ b/src/server/lifecycle/cellular_revamp.cpp @@ -47,7 +47,15 @@ bool cellular_revamp_valid(const CellularRevampRules& rules) noexcept { (rules.drip_interval_ticks == 0 || (rules.drip_bubble_pj != 0 && rules.drip_bubble_fg != 0)) && rules.reserve_load_per_thousand <= 1'000 && - rules.growth_per_tick_centi_mille <= 100'000 && cellular_revamp_offer_rules_valid(rules); + rules.growth_per_tick_centi_mille <= 100'000 && rules.split_share_per_thousand != 0 && + rules.split_share_per_thousand < 1'000 && rules.split_min_growth != 0 && + rules.split_min_growth <= 1'000 && rules.bud_fee_scale_percent <= 100 && + rules.bud_fee.energy.value <= most && + std::ranges::all_of( + rules.bud_fee.matter, + [](const accounting::Femtograms& f) { return f.value <= most; }) && + rules.organism_cap != 0 && rules.organism_cap <= biology::max_cellular_tick_lives && + cellular_revamp_offer_rules_valid(rules); } biology::CellularHandling cellular_loaded_handling(const biology::CellularHandling& handling, @@ -258,6 +266,15 @@ CellularRevampRules cellular_revamp_rules(const biology::CellularRevampTuning& t .carbon_for_light = u64(t.mutation_carbon_for_light_fg), .buds_for_adhesion = u32(t.mutation_buds_for_adhesion)}, .offer_interval_ticks = u32(t.mutation_offer_interval_ticks), - .mutations = t.mutations}; + .mutations = t.mutations, + .split_share_per_thousand = u32(t.reproduction_split_share_per_mille), + .split_min_growth = u32(t.reproduction_split_min_growth_per_mille), + .bud_fee = {{u64(t.reproduction_bud_fee_e_pj)}, + {accounting::Femtograms{u64(t.reproduction_bud_fee_c_fg)}, + accounting::Femtograms{u64(t.reproduction_bud_fee_n_fg)}, + accounting::Femtograms{u64(t.reproduction_bud_fee_p_fg)}, + accounting::Femtograms{u64(t.reproduction_bud_fee_o_fg)}}}, + .bud_fee_scale_percent = u32(t.reproduction_bud_fee_scale_percent), + .organism_cap = u32(t.reproduction_organism_cap)}; } } // namespace evo::server diff --git a/tests/budding_tests.cpp b/tests/budding_tests.cpp index 8686257..1e94ef5 100644 --- a/tests/budding_tests.cpp +++ b/tests/budding_tests.cpp @@ -14,7 +14,7 @@ void check(bool condition, std::source_location location = std::source_location: if (!condition) throw std::runtime_error("budding line " + std::to_string(location.line())); } -#define CHECK(expression) check(static_cast(expression)) +#define CHECK(...) check(static_cast(__VA_ARGS__)) constexpr std::uint32_t temperate = 293'150; Genome founder() { @@ -145,8 +145,84 @@ void the_shown_bud_cost_is_exactly_what_funding_takes() { } } // namespace +// Rules 13 (ADR-0179): a split divides body and reserve, each cell builds the largest body its +// part covers under its own genome, and every channel is conserved. +Genome founder6(std::uint64_t id = 9) { + const auto genome = primitive_founder(id, id, primitive_founder_v6); + CHECK(genome); + return *genome; +} +bool split_conserves(const Inventory& body, const Inventory& reserve, const SplitTransition& s) { + for (std::size_t c = 0; c < body.matter.size(); ++c) + if (body.matter[c].value + reserve.matter[c].value != + s.parent_body.matter[c].value + s.parent_reserve.matter[c].value + + s.child_body.matter[c].value + s.child_reserve.matter[c].value) + return false; + return reserve.energy.value == s.parent_reserve.energy.value + s.child_reserve.energy.value && + s.parent_body.energy.value == 0 && s.child_body.energy.value == 0; +} +void a_split_halves_and_conserves() { + const auto parent = founder6(); + const auto grown = metabolic_budget(parent, {1000, 0}, {temperate, {}}); + CHECK(grown); + const Inventory reserve{{2'000'001}, + {Femtograms{301}, Femtograms{7}, Femtograms{0}, Femtograms{1'000}}}; + const auto split = + split_cell(parent, parent, {1000, 0}, grown->body, reserve, temperate, 500, 1000); + CHECK(split && split_conserves(grown->body, reserve, *split)); + // Half the body builds a cell under half full size (membrane follows surface, not volume); + // the two halves are equal but for rounding. + CHECK(split->parent_growth.realized_per_thousand >= 300 && + split->parent_growth.realized_per_thousand < 500 && + split->child_growth == split->parent_growth); + CHECK(split->child_reserve.energy.value == 1'000'000 && + split->parent_reserve.energy.value == 1'000'001); + // A different child plan develops its own body from its half. + GenomeDraft larger; + larger.schema_version = genome_schema_v7; + larger.id = 10; + larger.origin_event = 10; + larger.development_version = 7; + larger.parents = {parent.id()}; + for (const auto& record : parent.capabilities()) + larger.capabilities.push_back(record); + for (auto& record : larger.capabilities) + if (record.kind == Capability::target_size) + record.parameter = 8'400'000; + larger.movement_arrangement = parent.movement_arrangement(); + larger.cellular_traits = parent.cellular_traits(); + const auto child = Genome::create(larger); + CHECK(child); + const auto mutated = + split_cell(parent, *child, {1000, 0}, grown->body, reserve, temperate, 500, 1000); + CHECK(mutated && split_conserves(grown->body, reserve, *mutated) && + mutated->child_growth.realized_per_thousand < + mutated->parent_growth.realized_per_thousand); +} +void a_split_refuses_without_changing_anything() { + const auto parent = founder6(); + const auto grown = metabolic_budget(parent, {1000, 0}, {temperate, {}}); + CHECK(grown); + CHECK(split_cell(parent, parent, {999, 0}, grown->body, {}, temperate, 500, 1000).error() == + BuddingError::not_grown); + CHECK(split_cell(parent, parent, {1000, 1}, grown->body, {}, temperate, 500, 1000).error() == + BuddingError::not_grown); + CHECK(split_cell(parent, parent, {1000, 0}, grown->body, {}, temperate, 0, 1000).error() == + BuddingError::no_capability); + // A sliver of the body builds no cell. + CHECK(split_cell(parent, parent, {1000, 0}, grown->body, {}, temperate, 1, 1000).error() == + BuddingError::insufficient_funding); + // A lower minimum lets a half-grown cell split. + const auto half = metabolic_budget(parent, {600, 0}, {temperate, {}}); + CHECK(half && split_cell(parent, parent, {600, 0}, half->body, {}, temperate, 500, 500)); + CHECK(split_cell(parent, founder(), {1000, 0}, grown->body, {}, temperate, 500, 1000).error() == + BuddingError::incompatible_genome); +} + int main() { try { + a_split_halves_and_conserves(); + a_split_refuses_without_changing_anything(); a_grown_parent_funds_the_largest_bud_its_investment_builds(); every_short_channel_refuses_and_spends_nothing(); only_a_grown_undamaged_reproducing_parent_buds(); diff --git a/tests/cellular_inheritance_replay_tests.cpp b/tests/cellular_inheritance_replay_tests.cpp index 4ef4fb7..6965967 100644 --- a/tests/cellular_inheritance_replay_tests.cpp +++ b/tests/cellular_inheritance_replay_tests.cpp @@ -233,7 +233,7 @@ void codec_checks(const CellularBatch& batch) { corrupt(112, 0xFFFF'FFFFULL); corrupt(124, 1); const auto selection = 112 + 16 + batch.ticks.front().lives.size() * 24; - corrupt(selection + 34, 1, 1); + corrupt(selection + 34, 2, 1); // Byte 34 is the rules-13 become flag: 0 or 1. for (const auto length : {std::size_t{0}, std::size_t{111}, std::size_t{127}, bytes.size() - 1}) require(!decode_cellular_batch(std::span{bytes}.first(length)), "truncated trace accepted"); auto trailing = bytes; diff --git a/tests/cellular_revamp_owner_tests.cpp b/tests/cellular_revamp_owner_tests.cpp index d704355..017fbea 100644 --- a/tests/cellular_revamp_owner_tests.cpp +++ b/tests/cellular_revamp_owner_tests.cpp @@ -298,29 +298,96 @@ std::vector choose(const CellularLives& state, std::u return {{life_id, offer.event_index, offer.offer_tick, successor, id}}; } -void a_choice_edits_the_plan_and_pays() { +// Rules 13 (reproduction plan section 4): a choice splits the cell. Both plans carry the mutation, +// only the offspring expresses it, body and reserve divide, and the player stays or becomes. +void a_choice_splits_the_cell(bool become) { auto state = offering_region(1'000); for (int tick = 0; tick < 40; ++tick) checked(state->advance_tick(swim(*state, {1'000, 0})), "swim tick"); const auto before = life_of(*state, 101); const auto id = before.revamp.offer.ids.at(0); const auto deposits = state->habitat().patches().size(); - const auto execution = checked( - state->advance_tick_with_mutations(swim(*state, {}), choose(*state, 101, id, 9'000)), - "choice tick"); - require(execution.mutations.size() == 1 && execution.mutations.front().result == - CellularMutationResult::offspring_plan_applied, - "the choice was not applied to the plan"); - const auto& after = life_of(*state, 101); - require(after.offspring_genome == 9'000 && after.identity.genome == before.identity.genome && - after.revamp.offer.count == 0, - "the choice changed the expressed body or left the offer"); - const auto plan = state->archive().find(9'000); - require(plan && plan->parents().size() == 1 && plan->parents()[0] == before.identity.genome, - "the successor plan is not the founder's child"); - // 1,000 pJ paid (upkeep and swimming aside) returned as one new bubble. + auto choice = choose(*state, 101, id, 9'000); + choice.front().become = become; + const auto execution = + checked(state->advance_tick_with_mutations(swim(*state, {}), choice), "choice tick"); + require(execution.mutations.size() == 1 && + execution.mutations.front().result == CellularMutationResult::split_applied, + "the choice did not split the cell"); + const auto identity = cellular_bud_identity(state->region(), 1); + const auto& founder = life_of(*state, 101); + const auto& child = life_of(*state, identity); + require(founder.identity.genome == before.identity.genome && + founder.offspring_genome == 9'000 && child.identity.genome == 9'000 && + child.offspring_genome == 9'000 && child.parent_life == 101 && + founder.revamp.offer.count == 0, + "the split did not give both plans the mutation and only the offspring its body"); + require(founder.growth.realized_per_thousand < 1'000 && + child.growth.realized_per_thousand < 1'000 && + child.revamp.affinity.score == founder.revamp.affinity.score, + "the split did not halve both cells or pass on the affinity"); + require(become ? child.account == account(2) && founder.account == LifeAccount{} + : child.account == LifeAccount{} && founder.account == account(2), + "control did not follow the choice"); require(state->habitat().patches().size() >= deposits + 1, "the paid cost was not reclaimed"); closed(*state); + // A half-grown founder cannot split again: the choice is refused and the offer kept. + if (!become) { + for (int tick = 0; tick < 40; ++tick) + checked(state->advance_tick(swim(*state, {1'000, 0})), "swim tick"); + const auto offer = life_of(*state, 101).revamp.offer; + require(offer.count == 3, "no second offer"); + const auto refused = + checked(state->advance_tick_with_mutations(swim(*state, {}), + choose(*state, 101, offer.ids.at(1), 9'010)), + "refused split tick"); + require(refused.mutations.front().result == CellularMutationResult::split_refused && + life_of(*state, 101).revamp.offer == offer && !state->archive().find(9'010), + "a split below full growth was not refused with the offer kept"); + closed(*state); + } + const auto copy = restore(*state); + require(copy->snapshot() == state->snapshot(), "a split region did not restore exactly"); +} + +void the_region_cap_refuses_splits() { + auto rules = revamp(); + rules.offer_interval_ticks = 40; + rules.organism_cap = 2; + auto fresh = opening(7, true, 2, false); + checked(fresh->advance_tick({}), "genesis tick"); + fresh = checked(fresh->migration_upgrade_candidate(), "boundary upgrade"); + fresh = checked(fresh->maturation_upgrade_candidate(), "maturation upgrade"); + fresh = checked(fresh->inheritance_upgrade_candidate(), "inheritance upgrade"); + fresh = checked(fresh->photosynthesis_upgrade_candidate({0, 1, {0}}), "light upgrade"); + fresh = checked(fresh->liquid_upgrade_candidate(liquid()), "liquid upgrade"); + fresh = checked(fresh->birth_candidate({account(1), 1}, {100, 100, 100, 100, {fresh->tick()}}), + "founder 5 birth"); + fresh = checked(fresh->revamp_upgrade_candidate(rules), "revamp upgrade"); + fresh = checked(fresh->birth_candidate({account(2), 1}, {101, 101, 101, 101, {fresh->tick()}}), + "founder 6 birth"); + for (int tick = 0; tick < 40; ++tick) + checked(fresh->advance_tick(swim(*fresh, {1'000, 0})), "swim tick"); + const auto offer = life_of(*fresh, 101).revamp.offer; + const auto refused = checked(fresh->advance_tick_with_mutations( + swim(*fresh, {}), choose(*fresh, 101, offer.ids.at(0), 9'020)), + "capped split tick"); + require(refused.mutations.front().result == CellularMutationResult::split_refused && + fresh->lives().size() == 2, + "a full region allowed a split"); +} + +// Become needs a bud, an owned life and rules 13; litter counts whole and chance offspring. +void become_and_litter() { + auto state = revamp_region(); + auto lone = swim(*state, {}); + lone.back().intent.become = true; + require(!state->advance_tick(lone), "become without a bud was accepted"); + require(cellular_litter_count(0, 0, 900) == 1 && cellular_litter_count(100, 99, 900) == 2 && + cellular_litter_count(150, 49, 900) == 3 && + cellular_litter_count(150, 50, 900) == 2 && + cellular_litter_count(9'900, 0, 900) == 10, + "litter counted the wrong offspring"); } void unaffordable_choices_wait_and_declines_clear() { @@ -398,12 +465,18 @@ int main() try { exclusions_are_checked_and_replayed(); a_long_run_keeps_closure_and_restores(); offers_come_from_active_play(); - a_choice_edits_the_plan_and_pays(); + a_choice_splits_the_cell(false); + a_choice_splits_the_cell(true); + the_region_cap_refuses_splits(); + become_and_litter(); unaffordable_choices_wait_and_declines_clear(); thrust_moves_affinity(); trace_replays(); std::puts("PASS rules 13 owner: upgrade, founder 6, restore and replay"); return 0; +} catch (const std::exception& error) { + std::printf("FAIL rules 13 owner: %s\n", error.what()); + return 1; } catch (...) { std::puts("FAIL rules 13 owner"); return 1; diff --git a/tests/cellular_revamp_tuning_tests.cpp b/tests/cellular_revamp_tuning_tests.cpp index 93a75d8..6be1531 100644 --- a/tests/cellular_revamp_tuning_tests.cpp +++ b/tests/cellular_revamp_tuning_tests.cpp @@ -46,7 +46,16 @@ CellularRevampTuning trial() { .mutation_recipe_min_share = 20, .mutation_max_offspring = 10, .mutation_carbon_for_light_fg = 20'000'000, - .mutation_buds_for_adhesion = 5}; + .mutation_buds_for_adhesion = 5, + .reproduction_split_share_per_mille = 500, + .reproduction_split_min_growth_per_mille = 1'000, + .reproduction_bud_fee_e_pj = 0, + .reproduction_bud_fee_c_fg = 0, + .reproduction_bud_fee_n_fg = 0, + .reproduction_bud_fee_p_fg = 0, + .reproduction_bud_fee_o_fg = 0, + .reproduction_bud_fee_scale_percent = 0, + .reproduction_organism_cap = 256}; const auto catalog = cellular_catalog(); for (std::size_t i = 0; i < catalog.size(); ++i) t.mutations.at(i) = cellular_default_tuning(catalog[i]);