feat(lifecycle): rules 13 mutation splits, stay-or-become and litter
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m22s
Foundation / postgres-tools (push) Successful in 22s
Foundation / server-image (push) Successful in 4m21s
Foundation / image (push) Successful in 5m6s
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m22s
Foundation / postgres-tools (push) Successful in 22s
Foundation / server-image (push) Successful in 4m21s
Foundation / image (push) Successful in 5m6s
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 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "evo/biology/budding.hpp"
|
||||
|
||||
#include <limits>
|
||||
#include <utility>
|
||||
|
||||
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<std::optional<std::pair<GrowthState, accounting::Inventory>>, 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<std::optional<accounting::Inventory>, 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<accounting::Inventory>{};
|
||||
return std::optional{body};
|
||||
};
|
||||
std::uint32_t low = 1, high = 1000;
|
||||
std::optional<std::pair<GrowthState, accounting::Inventory>> 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<SplitTransition, BuddingError>
|
||||
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<accounting::Inventory> bud_cost(const Genome& genome,
|
||||
std::uint32_t temperature_millikelvin) {
|
||||
return offspring_cost(genome, genome, temperature_millikelvin);
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<const CellularTickIntents> ticks) {
|
||||
std::set<std::uint64_t> identities;
|
||||
@@ -166,8 +166,10 @@ void put_intent(std::vector<std::uint8_t>& 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<std::uint8_t>((life.intent.frozen ? 1U : 0U) | (life.intent.brake ? 2U : 0U)));
|
||||
// Bit 2 (rules 13): continue as the bud.
|
||||
out.push_back(static_cast<std::uint8_t>((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<const std::uint8_t> bytes) {
|
||||
step.lives.reserve(static_cast<std::size_t>(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<const std::uint8_t> 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<const std::uint8_t> bytes) {
|
||||
mutation.offer_tick = get(record, 16, 8);
|
||||
mutation.successor_id = get(record, 24, 8);
|
||||
mutation.operator_id = static_cast<std::uint16_t>(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))
|
||||
|
||||
@@ -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<std::uint32_t>(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<const CellularOwnedLife> lives,
|
||||
std::span<const CellularOwnedCorpse> corpses,
|
||||
const biology::LineageArchive& archive,
|
||||
std::span<const CellularOffspringRevision> history, std::uint64_t tick) {
|
||||
std::span<const CellularOffspringRevision> 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<const CellularOwnedLife> 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<const CellularOwnedLife> 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<CellularOffspringRevision>* 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<CellularOwnedLife> 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<CellularLives>{new CellularLives{std::make_unique<State>(
|
||||
@@ -1077,13 +1093,14 @@ CellularLives::exchange_boundary(std::span<const biology::CellularPatch> 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<const std::uint8_t> 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<const CellularLifeIntent> 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<const CellularLifeIntent> 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<const CellularLifeIntent> i
|
||||
next_lives.erase(found);
|
||||
}
|
||||
std::vector<CellularMutationExecution> 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<const CellularLifeIntent> 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<biology::CellularPosition> 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<const CellularLifeIntent> 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<CellularBudExecution> 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<const CellularLifeIntent> 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<const CellularLifeIntent> 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<const CellularLifeIntent> 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<const CellularLifeIntent> 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<const CellularLifeIntent> 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<const CellularLifeIntent> 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<const CellularLifeIntent> 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()};
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user