Files
aevolution/src/server/lifecycle/cellular_revamp.cpp
T
dingoandClaude Opus 5.5 9f117c9545
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
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 <noreply@anthropic.com>
2026-10-22 22:19:48 -04:00

281 lines
16 KiB
C++

#include "evo/server/lifecycle/cellular_revamp.hpp"
#include "evo/core/random.hpp"
#include <algorithm>
#include <limits>
#include <vector>
namespace evo::server {
bool cellular_revamp_offer_rules_valid(const CellularRevampRules& rules) noexcept {
const auto& o = rules.offers;
const auto& b = rules.bounds;
constexpr std::uint64_t most = 1'000'000'000'000'000ULL;
if (o.options == 0 || o.options > max_cellular_offer_options || o.floor_ppm > o.cap_ppm ||
o.cap_ppm > biology::cellular_chance_whole || o.score_min > 0 || o.score_max < 0 ||
o.score_min < -30'000 || o.score_max > 30'000 || o.max_multiplier_per_mille > 100'000 ||
o.min_multiplier_per_mille > 1'000 || b.min_points == 0 || b.min_points >= b.max_points ||
b.max_points > biology::max_trait_points || b.recipe_min_share == 0 ||
b.recipe_min_share > 250 || b.max_litter > biology::max_litter ||
rules.offer_interval_ticks == 0 || rules.offer_interval_ticks > 72'000)
return false;
for (const auto& m : rules.mutations) {
if (m.base_weight > 10'000 || m.increase_points < -10'000 || m.increase_points > 10'000 ||
m.decrease_points < -10'000 || m.decrease_points > 10'000 || m.primary_step > 1'000 ||
m.paired_step > 1'000 || m.spread_step > 1'000 || m.cost_scale_percent > 100 ||
m.increase_ticks > 10'000'000 || m.decrease_ticks > 10'000'000 ||
m.cost.energy.value > most)
return false;
for (const auto& channel : m.cost.matter)
if (channel.value > most)
return false;
}
return true;
}
bool cellular_revamp_valid(const CellularRevampRules& rules) noexcept {
constexpr std::uint64_t most = 1'000'000'000'000'000ULL;
const auto& step = rules.step;
for (const auto rate : rules.burn_pj_per_kfg)
if (rate > 1'000'000'000'000ULL)
return false;
return rules.body.essential_upkeep_per_thousand <= 1'000 &&
step.membrane_collection_per_mille_per_point <= 1'000 && step.engulf_ratio_milli >= 1 &&
step.engulf_ratio_milli <= 1'000'000 && step.engulf_reserve_percent <= 100 &&
step.kill_bubble_per_thousand <= 1'000 && rules.drip_interval_ticks <= 72'000 &&
rules.drip_bubble_pj <= most && rules.drip_bubble_fg <= most &&
(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 && 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,
std::uint64_t body_mass, std::uint64_t carried,
const CellularRevampRules& rules) noexcept {
// Masses stay far below 2^40 fg, so these products fit in 64 bits.
const auto load = carried / 1'000 * rules.reserve_load_per_thousand +
carried % 1'000 * rules.reserve_load_per_thousand / 1'000;
if (load == 0 || body_mass == 0 || body_mass > (1ULL << 40U) || load > (1ULL << 40U))
return handling;
const auto total = body_mass + load;
const auto slower = [&](std::uint64_t value) { return value * body_mass / total; };
const auto dearer = [&](std::uint64_t value) {
return value > (1ULL << 23U) ? value / body_mass * total
: (value * total + body_mass - 1) / body_mass;
};
auto loaded = handling;
loaded.thrust_q10 = static_cast<std::uint32_t>(slower(handling.thrust_q10));
loaded.reverse_thrust_q10 = static_cast<std::uint32_t>(slower(handling.reverse_thrust_q10));
loaded.turn = static_cast<std::uint16_t>(slower(handling.turn));
loaded.thrust_cost.value = dearer(handling.thrust_cost.value);
loaded.reverse_cost.value = dearer(handling.reverse_cost.value);
return loaded;
}
bool cellular_drip_spent(biology::CellularHabitatConfig& config, accounting::Inventory& spent,
const CellularRevampRules& rules, std::uint64_t world,
std::uint64_t region, std::uint64_t tick, std::uint64_t source) {
if (rules.drip_interval_ticks == 0 || tick % rules.drip_interval_ticks != 0)
return true;
Random random{world * 0x9e3779b97f4a7c15ULL ^ region * 0xc2b2ae3d27d4eb4fULL ^ tick};
const auto by_address = [](const biology::CellularPatch& patch) { return patch.address; };
for (std::size_t channel = 0; channel < 5; ++channel) {
auto& pool = channel == 0 ? spent.energy.value : spent.matter[channel - 1].value;
const auto cap = channel == 0 ? rules.drip_bubble_pj : rules.drip_bubble_fg;
// Both draws happen for every channel, so a channel's spot never depends on another's.
const auto x = random.next();
const auto y = random.next();
if (pool == 0 || config.patches.size() >= biology::max_cellular_liquid_deposits)
continue;
biology::CellularPatchAddress address{static_cast<std::uint32_t>(x % config.width_um),
static_cast<std::uint32_t>(y % config.height_um)};
auto at = std::ranges::lower_bound(config.patches, address, {}, by_address);
while (at != config.patches.end() && at->address == address) {
address.x_um = address.x_um + 1 < config.width_um ? address.x_um + 1 : 0;
at = std::ranges::lower_bound(config.patches, address, {}, by_address);
}
if (config.next_deposit == std::numeric_limits<std::uint32_t>::max())
return false;
const auto amount = std::min(pool, cap);
biology::CellularPatch bubble{address,
source,
{},
config.next_deposit++,
biology::cellular_mote_radius_um,
biology::CellularDepositKind::mote};
bubble.merged = 1;
if (channel == 0)
bubble.stock.energy.value = amount;
else
bubble.stock.matter[channel - 1].value = amount;
pool -= amount;
config.patches.insert(at, bubble);
}
return true;
}
std::optional<CellularRevampOffer> 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) {
const auto catalog = biology::cellular_catalog();
std::vector<std::uint16_t> eligible;
std::vector<std::uint64_t> weights;
for (std::size_t i = 0; i < catalog.size(); ++i) {
const auto& tuning = rules.mutations.at(i);
// A dry run with placeholder identities decides eligibility.
if (!biology::apply_cellular_mutation(plan, catalog[i], tuning, rules.bounds, life.progress,
plan.id() + 1, 1))
continue;
eligible.push_back(catalog[i].id);
weights.push_back(biology::cellular_offer_weight(tuning.base_weight,
life.affinity.score.at(i), rules.offers));
}
const auto chances = biology::cellular_offer_chances(weights, rules.offers);
if (!chances)
return std::nullopt;
const auto event = life.events + 1;
Random random{world * 0x9e3779b97f4a7c15ULL ^ region * 0xc2b2ae3d27d4eb4fULL ^
life_id * 0x165667b19e3779f9ULL ^ event};
std::uint64_t draw = random.next();
constexpr auto whole = std::uint64_t{biology::cellular_chance_whole};
while (draw >= std::numeric_limits<std::uint64_t>::max() / whole * whole)
draw = random.next(); // Exactly uniform below one whole.
const auto picked = biology::cellular_systematic_draw(*chances, rules.offers.options,
static_cast<std::uint32_t>(draw % whole));
if (picked.size() != rules.offers.options)
return std::nullopt;
CellularRevampOffer offer{};
offer.event_index = event;
offer.offer_tick = tick;
offer.count = static_cast<std::uint8_t>(picked.size());
for (std::size_t k = 0; k < picked.size(); ++k) {
offer.ids.at(k) = eligible.at(picked[k]);
offer.chances.at(k) = chances->at(picked[k]);
}
return offer;
}
accounting::Inventory cellular_mutation_cost(const biology::CellularMutationTuning& t) noexcept {
const auto scaled = [&](std::uint64_t amount) {
return amount / 100 * t.cost_scale_percent + amount % 100 * t.cost_scale_percent / 100;
};
accounting::Inventory cost{};
cost.energy.value = scaled(t.cost.energy.value);
for (std::size_t c = 0; c < cost.matter.size(); ++c)
cost.matter[c].value = scaled(t.cost.matter[c].value);
return cost;
}
bool cellular_reclaim(biology::CellularHabitatConfig& config, const accounting::Inventory& amount,
std::uint64_t seed, biology::CellularPosition from, std::uint64_t source) {
Random random{seed};
const auto by_address = [](const biology::CellularPatch& patch) { return patch.address; };
for (std::size_t channel = 0; channel < 5; ++channel) {
const auto value = channel == 0 ? amount.energy.value : amount.matter[channel - 1].value;
const auto x = random.next();
const auto y = random.next();
if (value == 0)
continue;
if (config.patches.size() >= biology::max_cellular_liquid_deposits) {
// Full: the deposit farthest from the paying cell (lowest address on a tie).
const auto distance = [&](const biology::CellularPatch& patch) {
const auto dx = static_cast<std::int64_t>(patch.address.x_um) -
static_cast<std::int64_t>(from.x_um);
const auto dy = static_cast<std::int64_t>(patch.address.y_um) -
static_cast<std::int64_t>(from.y_um);
return static_cast<std::uint64_t>(dx * dx) + static_cast<std::uint64_t>(dy * dy);
};
auto farthest = config.patches.begin();
for (auto it = config.patches.begin(); it != config.patches.end(); ++it)
if (distance(*it) > distance(*farthest))
farthest = it;
if (farthest == config.patches.end())
return false;
auto& target = channel == 0 ? farthest->stock.energy.value
: farthest->stock.matter[channel - 1].value;
if (value > std::numeric_limits<std::uint64_t>::max() - target)
return false;
target += value;
continue;
}
biology::CellularPatchAddress address{static_cast<std::uint32_t>(x % config.width_um),
static_cast<std::uint32_t>(y % config.height_um)};
auto at = std::ranges::lower_bound(config.patches, address, {}, by_address);
while (at != config.patches.end() && at->address == address) {
address.x_um = address.x_um + 1 < config.width_um ? address.x_um + 1 : 0;
at = std::ranges::lower_bound(config.patches, address, {}, by_address);
}
if (config.next_deposit == std::numeric_limits<std::uint32_t>::max())
return false;
biology::CellularPatch bubble{address,
source,
{},
config.next_deposit++,
biology::cellular_mote_radius_um,
biology::CellularDepositKind::mote};
bubble.merged = 1;
if (channel == 0)
bubble.stock.energy.value = value;
else
bubble.stock.matter[channel - 1].value = value;
config.patches.insert(at, bubble);
}
return true;
}
CellularRevampRules cellular_revamp_rules(const biology::CellularRevampTuning& t) {
const auto u32 = [](std::int64_t value) { return static_cast<std::uint32_t>(value); };
const auto u64 = [](std::int64_t value) { return static_cast<std::uint64_t>(value); };
return {.body = {.essential_upkeep_per_thousand = u32(t.body_essential_upkeep_per_mille)},
.burn_pj_per_kfg = {u64(t.energy_burn_carbon_pj_per_kfg),
u64(t.energy_burn_nitrogen_pj_per_kfg),
u64(t.energy_burn_phosphorus_pj_per_kfg),
u64(t.energy_burn_other_pj_per_kfg)},
.step = {.membrane_collection_per_mille_per_point =
u32(t.feeding_membrane_collection_per_mille_per_point),
.engulf_ratio_milli = u32(t.predation_engulf_ratio_milli),
.engulf_reserve_percent = u32(t.predation_engulf_reserve_percent),
.kill_bubble_per_thousand = u32(t.predation_kill_bubble_per_mille)},
.drip_interval_ticks = u32(t.spent_drip_interval_ticks),
.drip_bubble_pj = u64(t.spent_drip_bubble_pj),
.drip_bubble_fg = u64(t.spent_drip_bubble_fg),
.reserve_load_per_thousand = u32(t.body_reserve_load_per_mille),
.growth_per_tick_centi_mille = u32(t.body_growth_per_tick_centi_mille),
.offers = {.options = u32(t.mutation_options),
.floor_ppm = u32(t.mutation_floor_ppm),
.cap_ppm = u32(t.mutation_cap_ppm),
.score_min = static_cast<std::int32_t>(t.mutation_score_min),
.score_max = static_cast<std::int32_t>(t.mutation_score_max),
.max_multiplier_per_mille = u32(t.mutation_max_multiplier_per_mille),
.min_multiplier_per_mille = u32(t.mutation_min_multiplier_per_mille)},
.bounds = {.min_points = u32(t.mutation_min_points),
.max_points = u32(t.mutation_max_points),
.recipe_min_share = u32(t.mutation_recipe_min_share),
.max_litter = (u32(t.mutation_max_offspring) - 1) * 100,
.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,
.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