Add reproduction safety trial and resizable cellular panels

This commit is contained in:
dingo
2026-10-27 09:13:28 -04:00
parent 7ca5f74f03
commit 5416590f9e
26 changed files with 805 additions and 52 deletions
+123 -21
View File
@@ -61,6 +61,8 @@ constexpr std::size_t cellular_life_motion_bytes = 20;
// Then the growth rate and a reserved zero (8), the offer rules (seven 4-byte fields and a
// 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.
// ADR-0181 uses the growth/offer/bounds reserved words for safety revision,
// growth fuel seconds and split launch speed; historical zeros retain their law.
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).
@@ -217,7 +219,7 @@ std::optional<biology::CellularTriggerState> read_trigger(Reader& in) {
}
void write_revamp_offers(Writer& out, const CellularRevampRules& r) {
out.put(r.growth_per_tick_centi_mille, 4);
out.put(0, 4);
out.put(r.safety_version, 4);
const auto& o = r.offers;
for (const auto value : {o.options, o.floor_ppm, o.cap_ppm})
out.put(value, 4);
@@ -225,12 +227,12 @@ void write_revamp_offers(Writer& out, const CellularRevampRules& r) {
out.put(static_cast<std::uint32_t>(o.score_max), 4);
out.put(o.max_multiplier_per_mille, 4);
out.put(o.min_multiplier_per_mille, 4);
out.put(0, 4);
out.put(r.growth_reserve_seconds, 4);
const auto& b = r.bounds;
for (const auto value :
{b.min_points, b.max_points, b.recipe_min_share, b.max_litter, b.buds_for_adhesion})
out.put(value, 4);
out.put(0, 4);
out.put(r.split_launch_um_per_s, 4);
out.put(b.carbon_for_light);
out.put(r.offer_interval_ticks, 4);
out.put(0, 4);
@@ -261,7 +263,7 @@ bool read_revamp_offers(Reader& in, CellularRevampRules& r) {
};
bool zero = true;
r.growth_per_tick_centi_mille = u32();
zero = zero && in.get(4) == 0;
r.safety_version = u32();
auto& o = r.offers;
o.options = u32();
o.floor_ppm = u32();
@@ -270,14 +272,14 @@ bool read_revamp_offers(Reader& in, CellularRevampRules& r) {
o.score_max = i32();
o.max_multiplier_per_mille = u32();
o.min_multiplier_per_mille = u32();
zero = zero && in.get(4) == 0;
r.growth_reserve_seconds = u32();
auto& b = r.bounds;
b.min_points = u32();
b.max_points = u32();
b.recipe_min_share = u32();
b.max_litter = u32();
b.buds_for_adhesion = u32();
zero = zero && in.get(4) == 0;
r.split_launch_um_per_s = u32();
b.carbon_for_light = in.get();
r.offer_interval_ticks = u32();
zero = zero && in.get(4) == 0;
@@ -479,7 +481,7 @@ bool separated(std::uint64_t left, std::uint64_t right, std::uint32_t left_exten
std::expected<std::optional<biology::CellularPosition>, CellularLivesError>
bud_position(const CellularOwnedLife& parent, std::uint32_t parent_extent, std::uint32_t bud_extent,
std::span<const CellularOwnedLife> lives, const biology::LineageArchive& archive,
const CellularLivesConfig& config) {
const CellularLivesConfig& config, std::uint32_t launch_gap = 0) {
constexpr std::array<std::array<std::int64_t, 2>, 8> directions{
{{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}};
if (bud_extent == 0 || bud_extent > config.area.width_um || bud_extent > config.area.height_um)
@@ -490,10 +492,12 @@ bud_position(const CellularOwnedLife& parent, std::uint32_t parent_extent, std::
const auto budget = budget_for(archive, living, config.ambient_temperature_millikelvin);
if (!budget)
return std::unexpected{budget.error()};
extents.push_back(budget->phenotype.extent);
extents.push_back(launch_gap != 0 && living.identity.life == parent.identity.life
? parent_extent
: budget->phenotype.extent);
}
// Doubled centre coordinates keep half extents exact.
const auto step = std::int64_t{parent_extent} + bud_extent + 2;
const auto step = std::int64_t{parent_extent} + bud_extent + 2 + 2 * std::int64_t{launch_gap};
// Twice the bud's corner coordinate on one axis; negative is outside the area.
const auto doubled_corner = [&](std::uint64_t parent_corner, std::int64_t direction) {
return 2 * static_cast<std::int64_t>(parent_corner) + parent_extent + direction * step -
@@ -511,10 +515,21 @@ bud_position(const CellularOwnedLife& parent, std::uint32_t parent_extent, std::
const biology::CellularPosition candidate{static_cast<std::uint64_t>(x),
static_cast<std::uint64_t>(y)};
bool clear = true;
for (std::size_t i = 0; i < lives.size() && clear; ++i)
for (std::size_t i = 0; i < lives.size() && clear; ++i) {
clear = lives[i].presence == CellularPresence::suspended ||
separated(candidate.x_um, lives[i].position.x_um, bud_extent, extents[i]) ||
separated(candidate.y_um, lives[i].position.y_um, bud_extent, extents[i]);
if (clear && launch_gap != 0 && lives[i].presence != CellularPresence::suspended &&
lives[i].identity.life != parent.identity.life) {
// The short launch corridor must be clear too; no jumping through a cell.
const auto begin_x = static_cast<std::uint64_t>(x - dx * launch_gap);
const auto begin_y = static_cast<std::uint64_t>(y - dy * launch_gap);
clear = separated(std::min(begin_x, candidate.x_um), lives[i].position.x_um,
bud_extent + (dx == 0 ? 0 : launch_gap), extents[i]) ||
separated(std::min(begin_y, candidate.y_um), lives[i].position.y_um,
bud_extent + (dy == 0 ? 0 : launch_gap), extents[i]);
}
}
if (clear)
return std::optional{candidate};
}
@@ -2990,14 +3005,17 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
// 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);
const auto child_share = state_->revamp.safety_version == 1 && !selection.become
? 1'000 - state_->revamp.split_share_per_thousand
: state_->revamp.split_share_per_thousand;
const auto split = biology::split_cell(
*expressed, *successor, found->growth, found->body, reserve, temperature,
child_share, 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;
std::uint16_t launch_heading{};
if (split) {
const auto shrunk = biology::metabolic_budget(
*expressed, split->parent_growth, {temperature, {}}, 1000, state_->revamp.body);
@@ -3005,12 +3023,21 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
*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);
const auto position = bud_position(
*found, shrunk->phenotype.extent, born->phenotype.extent, next_lives,
next_archive, state_->config, state_->revamp.split_launch_um_per_s / 20);
if (!position)
return std::unexpected{position.error()};
spot = *position;
if (spot && state_->revamp.safety_version == 1) {
const auto dx = 2 * (static_cast<std::int64_t>(spot->x_um) -
static_cast<std::int64_t>(found->position.x_um)) +
std::int64_t{born->phenotype.extent} - shrunk->phenotype.extent;
const auto dy = 2 * (static_cast<std::int64_t>(spot->y_um) -
static_cast<std::int64_t>(found->position.y_um)) +
std::int64_t{born->phenotype.extent} - shrunk->phenotype.extent;
launch_heading = biology::cellular_heading_of(dx, dy);
}
}
if (!split || !spot || identity == 0 ||
next_lives.size() >= state_->revamp.organism_cap ||
@@ -3037,6 +3064,24 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
child.position = *spot;
child.body = split->child_body;
child.reserve = split->child_reserve;
if (state_->revamp.safety_version == 1) {
// A liquid-relative launch, inherited by either control choice and persisted
// as ordinary motion. Drag, braking and currents apply on the next tick.
child.motion = found->motion;
child.motion.heading = launch_heading;
const auto impulse = std::int64_t{state_->revamp.split_launch_um_per_s} *
biology::cellular_velocity_q10_per_um_s;
const auto launched = [&](std::int32_t velocity, std::int32_t direction) {
return static_cast<std::int32_t>(
std::clamp(velocity + impulse * direction / biology::cellular_trig_one,
-std::int64_t{biology::cellular_velocity_limit_q10},
std::int64_t{biology::cellular_velocity_limit_q10}));
};
child.motion.velocity_x_q10 = launched(
child.motion.velocity_x_q10, biology::cellular_cos_q14(child.motion.heading));
child.motion.velocity_y_q10 = launched(
child.motion.velocity_y_q10, biology::cellular_sin_q14(child.motion.heading));
}
child.parent_life = found->identity.life;
child.offspring_genome = successor->id();
child.revamp.affinity.score = found->revamp.affinity.score;
@@ -3248,12 +3293,13 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
}();
const auto copies = revamp_rules ? litter_count : (twins ? 2U : 1U);
for (std::uint32_t copy = 0; copy < copies; ++copy) {
if (copy == 1)
if (copy != 0)
// Inserting the first bud may have moved the parent; find it again.
parent = std::ranges::lower_bound(
next_lives, request.life_id, {},
[](const CellularOwnedLife& value) { return value.identity.life; });
const bool twin = copy == 1;
const bool twin =
revamp_rules && state_->revamp.safety_version == 1 ? copy != 0 : copy == 1;
const auto temperature = state_->config.ambient_temperature_millikelvin;
const auto genome = next_archive.find(cellular_offspring_genome(*parent));
const auto expressed = next_archive.find(parent->identity.genome);
@@ -3331,7 +3377,20 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
// Funding checked that the reserve still covers it.
if (!subtract_inventory(parent->reserve, fee))
return std::unexpected{CellularLivesError::conservation};
reclaims.emplace_back(fee, parent->position);
const auto asked = std::ranges::lower_bound(ordered, request.life_id, {},
&CellularLifeIntent::life_id);
if (state_->revamp.safety_version == 1 && parent->account != LifeAccount{}) {
// The provision was funded in full above, then returned exactly once to
// the selected cell. Construction and the ordinary bud provision remain.
auto& recipient = asked != ordered.end() && asked->life_id == request.life_id &&
asked->intent.become
? transition->reserve
: parent->reserve;
if (!add_inventory(recipient, fee))
return std::unexpected{CellularLivesError::overflow};
} else {
reclaims.emplace_back(fee, parent->position);
}
}
CellularOwnedLife bud{};
bud.identity = {identity, identity, genome->id(), identity, {proposal->end_tick}};
@@ -3482,12 +3541,55 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
if (step == 0)
continue;
}
const auto budget = biology::metabolic_budget(
auto budget = biology::metabolic_budget(
*genome, life.growth,
{state_->config.ambient_temperature_millikelvin, life.reserve}, step,
state_->revamp.body);
if (!budget || budget->body != life.body)
return std::unexpected(CellularLivesError::biology);
if (revamp_rules && state_->revamp.safety_version == 1) {
const auto burn = biology::cellular_burn(
state_->revamp.burn_pj_per_kfg,
traits ? traits->burn : std::array<std::uint16_t, 4>{100, 100, 100, 100},
input->intent.exclude);
// Narrow to the largest construction step that leaves the NEW body's
// full-demand fuel buffer. This is an affordability check, never a payment
// or resource grant. Matter consumed by construction no longer counts as fuel.
bool protected_fuel = false;
while (budget->construction.after.realized_per_thousand >
life.growth.realized_per_thousand) {
auto remaining = life.reserve;
const auto& construction = budget->construction;
if (!subtract_inventory(remaining, construction.intake) ||
!add_inventory(remaining, construction.release))
return std::unexpected{CellularLivesError::conservation};
const auto target = biology::metabolic_budget(
*genome, construction.after,
{state_->config.ambient_temperature_millikelvin, {}}, 1000,
state_->revamp.body);
if (!target || target->rate_microseconds == 0)
return std::unexpected{CellularLivesError::biology};
const auto periods = std::uint64_t{state_->revamp.growth_reserve_seconds} *
1'000'000 / target->rate_microseconds;
const bool bounded =
periods == 0 ||
target->demand.value <= std::numeric_limits<std::uint64_t>::max() / periods;
if (bounded && biology::cellular_payable_energy(remaining, burn, false) >=
target->demand.value * periods)
break;
protected_fuel = true;
const auto smaller = construction.after.realized_per_thousand -
life.growth.realized_per_thousand - 1;
budget = biology::metabolic_budget(
*genome, life.growth,
{state_->config.ambient_temperature_millikelvin, life.reserve}, smaller,
state_->revamp.body);
if (!budget)
return std::unexpected{CellularLivesError::biology};
}
if (protected_fuel && life.account != LifeAccount{})
growth_limits.emplace_back(life.identity.life, std::uint8_t{1});
}
const auto& move = budget->construction;
// Rules 13: an owned life learns which nutrient (or energy) narrowed its step: the
// one its reserve covers least of what an ample reserve's step needs.