feat(cellular): rules 13 feeding, matter as energy, spent pool and predation
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m29s
Foundation / postgres-tools (push) Successful in 31s
Foundation / server-image (push) Successful in 4m24s
Foundation / image (push) Successful in 5m3s
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m29s
Foundation / postgres-tools (push) Successful in 31s
Foundation / server-image (push) Successful in 4m24s
Foundation / image (push) Successful in 5m3s
Phase 2 of the cellular revamp build plan (ADR-0179), server half. - Matter as energy: when an energy cost exceeds the reserve's energy, reserve matter stands in for the rest at each channel's burn rate (pJ per 1,000 fg, scaled by the genome's burn efficiency), largest channel first. Upkeep may burn any channel; thrust only channels the player has not excluded. Burned matter adds no heat; it goes to the region's spent pool. A cell dies only when nothing can pay its upkeep. - Collection: per-channel limits scaled by the genome's collection indices, a shared limit on all matter, a tunable cost per membrane point, and only the genome's kept share (energy conversion) of bubble energy reaching the reserve. - Predation: engulf mass carries a tunable share of reserve matter, the predator's engulf capacity scales its side and the prey's membrane (now in nanometres) its own; kill absorption scales the predator's intake; the kill's remainder is split between bubbles at the prey and the spent pool, and a full habitat sends overflow to the pool. - The spent pool is owner format 11 state, counted in conservation, and drips back as seeded random bubbles every interval tick. - Reserve matter is carried as load: slower thrust and turning, costlier thrust. - Channel exclusions are a 4-bit step intent field, trace byte 14; only owned lives under rules 13 may send them. All values come from the revamp tuning file. Rules up to 12 are unchanged. Tests cover payments and their conservation over 400 seeds, the rules-13 region step (collection, membrane, kept energy, burning, engulfment and kill splits with closure), the drip, load, exclusions, a 700-tick run with closure and exact restore, and trace replay with exclusions. Session v14 and the client follow the server phases (build plan). Refreshes the DEV session window. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -55,6 +55,60 @@ struct Deposit {
|
||||
std::uint64_t& channel_of(accounting::Inventory& value, std::size_t channel) noexcept {
|
||||
return channel == 0 ? value.energy.value : value.matter[channel - 1].value;
|
||||
}
|
||||
std::uint64_t value_of(const accounting::Inventory& value, std::size_t channel) noexcept {
|
||||
return channel == 0 ? value.energy.value : value.matter[channel - 1].value;
|
||||
}
|
||||
std::uint64_t matter_of(const accounting::Inventory& value) noexcept {
|
||||
std::uint64_t total{};
|
||||
for (const auto& channel : value.matter)
|
||||
total = channel.value > u64_max - total ? u64_max : total + channel.value;
|
||||
return total;
|
||||
}
|
||||
// Rules 13: a life's per-tick collection limits: the budget's founder-composed limits scaled by
|
||||
// its collection indices and its membrane, and the shared limit on all matter together.
|
||||
struct Limits {
|
||||
std::array<std::uint64_t, 5> channel{};
|
||||
std::uint64_t shared{};
|
||||
};
|
||||
std::optional<Limits> revamp_limits(const CellularLifeStepInput& life,
|
||||
const CellularRevampStepRules& rules) {
|
||||
if (!life.revamp)
|
||||
return std::nullopt;
|
||||
const auto& own = *life.revamp;
|
||||
const auto points = static_cast<std::int64_t>(own.membrane_nm / 10);
|
||||
const auto mult = std::clamp<std::int64_t>(
|
||||
1'000 - static_cast<std::int64_t>(rules.membrane_collection_per_mille_per_point) *
|
||||
(points - 100),
|
||||
100, 2'000);
|
||||
Limits limits{};
|
||||
std::uint64_t shared{};
|
||||
for (std::size_t channel = 0; channel < limits.channel.size(); ++channel) {
|
||||
const auto per_second = channel == 0 ? life.budget.intake_limit.energy.value
|
||||
: life.budget.intake_limit.matter[channel - 1].value;
|
||||
const auto base = accounting::per_tick(per_second, cellular_motion_tick_microseconds,
|
||||
accounting::Rounding::down);
|
||||
if (!base || *base > u64_max / 2'000)
|
||||
return std::nullopt;
|
||||
limits.channel.at(channel) = *base / 100 * own.collection.at(channel) +
|
||||
*base % 100 * own.collection.at(channel) / 100;
|
||||
if (limits.channel.at(channel) > u64_max / 2'000)
|
||||
return std::nullopt;
|
||||
limits.channel.at(channel) =
|
||||
limits.channel.at(channel) * static_cast<std::uint64_t>(mult) / 1'000;
|
||||
if (channel != 0)
|
||||
shared += *base;
|
||||
}
|
||||
limits.shared = shared * static_cast<std::uint64_t>(mult) / 1'000;
|
||||
return limits;
|
||||
}
|
||||
// Rules 13: engulf mass is the body plus the declared share of reserve matter.
|
||||
std::uint64_t engulf_mass(const CellularLifeStepInput& life, const accounting::Inventory& reserve,
|
||||
const CellularRevampStepRules& rules) noexcept {
|
||||
const auto carried = matter_of(reserve) / 100 * rules.engulf_reserve_percent +
|
||||
matter_of(reserve) % 100 * rules.engulf_reserve_percent / 100;
|
||||
const auto body = life.budget.phenotype.total_mass;
|
||||
return carried > u64_max - body ? u64_max : body + carried;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<CellularRegionStepCandidate, CellularRegionStepError>
|
||||
@@ -84,6 +138,11 @@ propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
|
||||
if (rules.engulf_and_detritus && (life.membrane_um == 0 || life.source == 0 ||
|
||||
(life.intent.engulf && life.intent.intake_target)))
|
||||
return std::unexpected{CellularRegionStepError::invalid_intent};
|
||||
// Rules 13: a revamp input for every life, only in a liquid contact-feeding region.
|
||||
if (life.revamp.has_value() != rules.revamp.has_value() ||
|
||||
(rules.revamp && (!liquid || !rules.contact_feeding || life.revamp->membrane_nm == 0 ||
|
||||
life.revamp->energy_kept_per_thousand > 1'000)))
|
||||
return std::unexpected{CellularRegionStepError::invalid_intent};
|
||||
ordered.push_back(&life);
|
||||
}
|
||||
std::ranges::sort(ordered, {}, [](const auto* life) { return life->life_id; });
|
||||
@@ -132,7 +191,8 @@ propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
|
||||
life->liquid
|
||||
? propose_liquid_cellular_step(begin_tick, area, life->position, life->extent_um,
|
||||
intent.movement, *life->liquid, life->reserve,
|
||||
life->heat, life->budget)
|
||||
life->heat, life->budget,
|
||||
life->revamp ? &life->revamp->burn : nullptr)
|
||||
: propose_cellular_step(begin_tick, area, life->position, life->extent_um, intent,
|
||||
life->reserve, life->heat, life->budget, habitat);
|
||||
if (!step && step.error() == CellularStepError::insufficient_maintenance) {
|
||||
@@ -159,6 +219,8 @@ propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
|
||||
if (!step)
|
||||
return std::unexpected{CellularRegionStepError::life_step};
|
||||
auto result = *step;
|
||||
if (!add(candidate.spent, result.spent))
|
||||
return std::unexpected{CellularRegionStepError::overflow};
|
||||
if (target) {
|
||||
const auto index = habitat.find(*target);
|
||||
if (!index) {
|
||||
@@ -181,7 +243,60 @@ propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
|
||||
}
|
||||
}
|
||||
}
|
||||
if (rules.contact_feeding) {
|
||||
if (rules.contact_feeding && rules.revamp) {
|
||||
// Rules 13: per-channel collection scaled by the genome and membrane, a shared limit
|
||||
// on all matter, and only the kept share of bubble energy reaching the reserve.
|
||||
const auto limits = revamp_limits(*life, *rules.revamp);
|
||||
if (!limits)
|
||||
return std::unexpected{CellularRegionStepError::overflow};
|
||||
accounting::Inventory drained{}, acquired{};
|
||||
std::optional<std::size_t> first;
|
||||
for (std::size_t index = 0; index < patch_stock.size(); ++index) {
|
||||
const auto& deposit = habitat.patches()[index];
|
||||
if (!cellular_path_contact(life->position, result.position, life->extent_um,
|
||||
deposit))
|
||||
continue;
|
||||
bool took = false;
|
||||
for (std::size_t channel = 0; channel < limits->channel.size(); ++channel) {
|
||||
auto& stock = channel_of(patch_stock[index], channel);
|
||||
auto& taken = channel_of(drained, channel);
|
||||
auto allowance = limits->channel.at(channel) - taken;
|
||||
if (channel != 0) {
|
||||
const auto matter = matter_of(drained);
|
||||
allowance = std::min(allowance,
|
||||
limits->shared > matter ? limits->shared - matter : 0);
|
||||
}
|
||||
if (deposit.kind == CellularDepositKind::pool)
|
||||
allowance =
|
||||
std::min(allowance, limits->channel.at(channel) *
|
||||
cellular_pool_yield_per_thousand / 1000);
|
||||
auto& reserve = channel_of(result.reserve_after, channel);
|
||||
const auto take = std::min({stock, allowance, u64_max - reserve});
|
||||
if (take == 0)
|
||||
continue;
|
||||
stock -= take;
|
||||
taken += take;
|
||||
auto kept = take;
|
||||
if (channel == 0) {
|
||||
kept = take / 1'000 * life->revamp->energy_kept_per_thousand +
|
||||
take % 1'000 * life->revamp->energy_kept_per_thousand / 1'000;
|
||||
if (!add(result.heat_after.energy.value, take - kept))
|
||||
return std::unexpected{CellularRegionStepError::overflow};
|
||||
}
|
||||
reserve += kept;
|
||||
channel_of(acquired, channel) += kept;
|
||||
took = true;
|
||||
}
|
||||
if (took && !first)
|
||||
first = index;
|
||||
}
|
||||
if (first) {
|
||||
result.intake_status = CellularIntakeStatus::acquired;
|
||||
result.intake = CellularPatchUptakeProposal{
|
||||
habitat.patches()[*first].address,
|
||||
{patch_stock[*first], result.reserve_after, acquired}};
|
||||
}
|
||||
} else if (rules.contact_feeding) {
|
||||
// ADR-0136: drain every touched deposit (ascending address) up to the per-tick
|
||||
// intake limit; a pool gives at most a quarter of that limit.
|
||||
std::array<std::uint64_t, 5> limit{};
|
||||
@@ -260,15 +375,26 @@ propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
|
||||
if (!cellular_footprints_contact(predator.candidate.position, hunter->extent_um,
|
||||
prey_step.position, prey->extent_um))
|
||||
continue;
|
||||
if (!product_at_least(hunter->budget.phenotype.total_mass, 1'000,
|
||||
prey->budget.phenotype.total_mass,
|
||||
cellular_engulf_ratio_milli_per_membrane_um *
|
||||
prey->membrane_um)) {
|
||||
// Rules 13: engulf masses carry reserve matter, the predator's capacity scales
|
||||
// its side and the prey's membrane (nanometres; 1,000 is the founder) its own.
|
||||
const auto& revamp = rules.revamp;
|
||||
const auto hunter_mass =
|
||||
revamp ? engulf_mass(*hunter, predator.candidate.reserve_after, *revamp)
|
||||
: hunter->budget.phenotype.total_mass;
|
||||
const auto prey_mass = revamp ? engulf_mass(*prey, prey_step.reserve_after, *revamp)
|
||||
: prey->budget.phenotype.total_mass;
|
||||
const auto hunter_scale =
|
||||
revamp ? std::uint64_t{hunter->revamp->engulf_capacity} * 10'000 : 1'000;
|
||||
const auto prey_scale =
|
||||
revamp ? std::uint64_t{revamp->engulf_ratio_milli} * prey->revamp->membrane_nm
|
||||
: cellular_engulf_ratio_milli_per_membrane_um * prey->membrane_um;
|
||||
const auto founder_scale = revamp
|
||||
? std::uint64_t{revamp->engulf_ratio_milli} * 1'000
|
||||
: cellular_engulf_ratio_milli_per_membrane_um;
|
||||
if (!product_at_least(hunter_mass, hunter_scale, prey_mass, prey_scale)) {
|
||||
membrane_blocked =
|
||||
membrane_blocked ||
|
||||
product_at_least(hunter->budget.phenotype.total_mass, 1'000,
|
||||
prey->budget.phenotype.total_mass,
|
||||
cellular_engulf_ratio_milli_per_membrane_um);
|
||||
product_at_least(hunter_mass, hunter_scale, prey_mass, founder_scale);
|
||||
predator.engulf = membrane_blocked ? CellularEngulfStatus::membrane_too_thick
|
||||
: CellularEngulfStatus::prey_too_large;
|
||||
continue;
|
||||
@@ -292,6 +418,52 @@ propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
|
||||
? predator.candidate.intake->transfer.acquired
|
||||
: accounting::Inventory{};
|
||||
accounting::Inventory taken_in{};
|
||||
if (revamp) {
|
||||
// Rules 13: the collection limits scaled by kill absorption; only the kept
|
||||
// share of the prey's energy reaches the reserve, the rest is heat.
|
||||
const auto limits = revamp_limits(*hunter, *revamp);
|
||||
if (!limits)
|
||||
return std::unexpected{CellularRegionStepError::overflow};
|
||||
for (std::size_t channel = 0; channel < 5; ++channel) {
|
||||
const auto limit =
|
||||
limits->channel.at(channel) / 100 * hunter->revamp->kill_absorption +
|
||||
limits->channel.at(channel) % 100 * hunter->revamp->kill_absorption /
|
||||
100;
|
||||
const auto already = value_of(acquired, channel);
|
||||
channel_of(taken_in, channel) = std::min(
|
||||
value_of(whole, channel), limit > already ? limit - already : 0);
|
||||
}
|
||||
auto kept_in = taken_in;
|
||||
kept_in.energy.value =
|
||||
taken_in.energy.value / 1'000 * hunter->revamp->energy_kept_per_thousand +
|
||||
taken_in.energy.value % 1'000 * hunter->revamp->energy_kept_per_thousand /
|
||||
1'000;
|
||||
if (!add(predator.candidate.heat_after.energy.value,
|
||||
taken_in.energy.value - kept_in.energy.value) ||
|
||||
!add(predator.candidate.reserve_after, kept_in))
|
||||
return std::unexpected{CellularRegionStepError::overflow};
|
||||
accounting::Inventory bubbles = whole;
|
||||
accounting::Inventory spent{};
|
||||
for (std::size_t channel = 0; channel < 5; ++channel) {
|
||||
const auto rest = value_of(bubbles, channel) - value_of(taken_in, channel);
|
||||
const auto left = rest / 1'000 * revamp->kill_bubble_per_thousand +
|
||||
rest % 1'000 * revamp->kill_bubble_per_thousand / 1'000;
|
||||
channel_of(bubbles, channel) = left;
|
||||
channel_of(spent, channel) = rest - left;
|
||||
}
|
||||
if (!add(candidate.spent, spent))
|
||||
return std::unexpected{CellularRegionStepError::overflow};
|
||||
deposits.push_back({prey->life_id, prey->source, prey_step.position, bubbles,
|
||||
prey->extent_um});
|
||||
CellularCorpseCandidate eaten{prey->life_id, prey_step.position, {}};
|
||||
eaten.cause = CellularDeathCause::engulfed;
|
||||
eaten.predator_id = hunter->life_id;
|
||||
candidate.starved.push_back(eaten);
|
||||
predator.engulf = CellularEngulfStatus::engulfed;
|
||||
predator.prey_id = prey->life_id;
|
||||
taken[q] = true;
|
||||
break;
|
||||
}
|
||||
const auto energy_room =
|
||||
remaining(hunter->budget.intake_limit.energy.value, acquired.energy.value);
|
||||
if (!energy_room)
|
||||
@@ -380,6 +552,13 @@ propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
|
||||
}
|
||||
}
|
||||
deposits = std::move(overflow); // A full habitat: the nearest deposit takes it.
|
||||
if (rules.revamp) {
|
||||
// Rules 13: the spent pool takes it instead, so it seeps back elsewhere.
|
||||
for (const auto& deposit : deposits)
|
||||
if (!add(candidate.spent, deposit.inventory))
|
||||
return std::unexpected{CellularRegionStepError::overflow};
|
||||
deposits.clear();
|
||||
}
|
||||
}
|
||||
for (const auto& deposit : deposits) {
|
||||
const auto address = cell_of(deposit.position);
|
||||
|
||||
Reference in New Issue
Block a user