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>
272 lines
12 KiB
C++
272 lines
12 KiB
C++
#include "evo/biology/cellular_region_step.hpp"
|
|
|
|
#include <array>
|
|
#include <cstdio>
|
|
#include <memory>
|
|
#include <source_location>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
// Rules 13 region step (ADR-0179; feeding and energy, predation and defense plans): per-channel
|
|
// collection with a shared limit, the membrane's collection cost, the kept share of bubble
|
|
// energy, matter standing in for energy, engulf eligibility with reserve mass and capacity, and
|
|
// the kill remainder split between bubbles and the spent pool. Everything conserves.
|
|
namespace {
|
|
using namespace evo::biology;
|
|
using evo::accounting::Inventory;
|
|
|
|
void check(bool value, std::source_location at = std::source_location::current()) {
|
|
if (!value)
|
|
throw std::runtime_error("cellular revamp step line " + std::to_string(at.line()));
|
|
}
|
|
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
|
|
|
constexpr CellularArea area{3'000, 2'000};
|
|
constexpr CellularRegionRules plain{true, true, CellularRevampStepRules{}};
|
|
|
|
Inventory inv(std::uint64_t e, std::uint64_t c, std::uint64_t n, std::uint64_t p, std::uint64_t o) {
|
|
return {{e}, {{{c}, {n}, {p}, {o}}}};
|
|
}
|
|
void add(Inventory& to, const Inventory& from) {
|
|
to.energy.value += from.energy.value;
|
|
for (std::size_t i = 0; i < to.matter.size(); ++i)
|
|
to.matter[i].value += from.matter[i].value;
|
|
}
|
|
|
|
// One bubble at (100, 100), or none.
|
|
std::unique_ptr<CellularHabitat> liquid_map(const Inventory& stock, bool bubble = true) {
|
|
CellularHabitatConfig config{7, 9, 11, area.width_um, area.height_um, {}};
|
|
config.format = cellular_habitat_liquid_format;
|
|
config.next_deposit = 2;
|
|
if (bubble) {
|
|
CellularPatch mote{
|
|
{100, 100}, 51, stock, 1, cellular_mote_radius_um, CellularDepositKind::mote};
|
|
mote.merged = 1;
|
|
config.patches.push_back(mote);
|
|
}
|
|
auto made = CellularHabitat::create(std::move(config));
|
|
CHECK(made);
|
|
return std::move(*made);
|
|
}
|
|
|
|
// Intake limits of 1,000 per second on every channel: 50 per 50 ms tick.
|
|
MetabolicBudget budget(std::uint64_t mass, std::uint64_t maintenance = 0) {
|
|
MetabolicBudget value{};
|
|
value.rate_microseconds = 1'000'000;
|
|
value.intake_limit = inv(1'000, 1'000, 1'000, 1'000, 1'000);
|
|
value.phenotype.total_mass = mass;
|
|
value.maintenance.value = maintenance;
|
|
return value;
|
|
}
|
|
|
|
CellularRevampLifeInput founder_input() {
|
|
CellularRevampLifeInput input{};
|
|
input.burn = cellular_burn({10, 10, 10, 10}, {100, 100, 100, 100}, 0);
|
|
return input;
|
|
}
|
|
|
|
// A still cell; no thrust, no current.
|
|
CellularLifeStepInput cell(std::uint64_t id, CellularPosition at, std::uint64_t mass,
|
|
const Inventory& reserve, bool engulf = false,
|
|
CellularRevampLifeInput revamp = founder_input(),
|
|
std::uint64_t maintenance = 0) {
|
|
CellularLifeStepInput life{};
|
|
life.life_id = id;
|
|
life.position = at;
|
|
life.extent_um = 20;
|
|
life.intent.engulf = engulf;
|
|
life.body = inv(0, 10, 10, 10, 10);
|
|
life.reserve = reserve;
|
|
life.budget = budget(mass, maintenance);
|
|
life.membrane_um = 1;
|
|
life.source = 51;
|
|
life.liquid = CellularLiquidInput{};
|
|
life.revamp = revamp;
|
|
return life;
|
|
}
|
|
|
|
// Everything the region holds after a candidate: lives (body, reserve, heat), deposits, corpses,
|
|
// heat released and the spent pool.
|
|
Inventory total(const CellularRegionStepCandidate& c, const CellularHabitat& map,
|
|
std::span<const CellularLifeStepInput> lives) {
|
|
Inventory sum{};
|
|
for (std::size_t i = 0; i < map.patches().size(); ++i) {
|
|
const auto update = std::ranges::find(c.patches, map.patches()[i].address,
|
|
&CellularPatchStockUpdate::address);
|
|
add(sum, update == c.patches.end() ? map.patches()[i].stock : update->stock);
|
|
}
|
|
for (const auto& patch : c.new_patches)
|
|
add(sum, patch.stock);
|
|
for (const auto& corpse : c.starved)
|
|
add(sum, corpse.inventory);
|
|
for (const auto& life : c.lives) {
|
|
add(sum, life.candidate.reserve_after);
|
|
add(sum, life.candidate.heat_after);
|
|
for (const auto& input : lives)
|
|
if (input.life_id == life.life_id)
|
|
add(sum, input.body);
|
|
}
|
|
sum.energy.value += c.heat_released.value;
|
|
add(sum, c.spent);
|
|
return sum;
|
|
}
|
|
Inventory before(const CellularHabitat& map, std::span<const CellularLifeStepInput> lives) {
|
|
Inventory sum{};
|
|
for (const auto& patch : map.patches())
|
|
add(sum, patch.stock);
|
|
for (const auto& life : lives) {
|
|
add(sum, life.reserve);
|
|
add(sum, life.body);
|
|
add(sum, life.heat);
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
void collection_per_channel_with_a_shared_limit() {
|
|
const auto map = liquid_map(inv(1'000, 1'000, 1'000, 1'000, 1'000));
|
|
auto input = founder_input();
|
|
input.collection = {100, 200, 100, 100, 100};
|
|
input.energy_kept_per_thousand = 400;
|
|
const std::array lives{cell(1, {95, 95}, 1'000, {}, false, input)};
|
|
const auto result = propose_cellular_region_step(4, area, lives, *map, plain);
|
|
CHECK(result && result->lives.size() == 1);
|
|
const auto& reserve = result->lives[0].candidate.reserve_after;
|
|
// Carbon at 200 takes 100 of the shared 200; N and P take their 50; other matter gets none.
|
|
CHECK(reserve.matter[0].value == 100 && reserve.matter[1].value == 50 &&
|
|
reserve.matter[2].value == 50 && reserve.matter[3].value == 0);
|
|
// 50 pJ drained, 40% kept, the rest heat.
|
|
CHECK(reserve.energy.value == 20 && result->lives[0].candidate.heat_after.energy.value == 30);
|
|
CHECK(total(*result, *map, lives) == before(*map, lives));
|
|
}
|
|
|
|
void a_thicker_membrane_collects_slower() {
|
|
const auto map = liquid_map(inv(1'000, 1'000, 1'000, 1'000, 1'000));
|
|
auto input = founder_input();
|
|
input.membrane_nm = 1'100; // 110 points.
|
|
CellularRevampStepRules costly{};
|
|
costly.membrane_collection_per_mille_per_point = 20; // 10 points: -20%.
|
|
const CellularRegionRules rules{true, true, costly};
|
|
const std::array lives{cell(1, {95, 95}, 1'000, {}, false, input)};
|
|
const auto result = propose_cellular_region_step(4, area, lives, *map, rules);
|
|
CHECK(result);
|
|
const auto& reserve = result->lives[0].candidate.reserve_after;
|
|
CHECK(reserve.matter[0].value == 40 && reserve.energy.value == 40);
|
|
CHECK(total(*result, *map, lives) == before(*map, lives));
|
|
}
|
|
|
|
void matter_pays_upkeep_and_nothing_left_starves() {
|
|
const auto map = liquid_map({}, false);
|
|
// 20 pJ per second is 1 pJ per tick; 10 pJ per 1,000 fg makes that 100 fg of carbon.
|
|
const std::array lives{
|
|
cell(1, {500, 500}, 1'000, inv(0, 1'000, 0, 0, 0), false, founder_input(), 20),
|
|
cell(2, {900, 900}, 1'000, {}, false, founder_input(), 20)};
|
|
const auto result = propose_cellular_region_step(4, area, lives, *map, plain);
|
|
CHECK(result && result->lives.size() == 1 && result->lives[0].life_id == 1);
|
|
CHECK(result->lives[0].candidate.reserve_after == inv(0, 900, 0, 0, 0));
|
|
CHECK(result->lives[0].candidate.spent == inv(0, 100, 0, 0, 0));
|
|
CHECK(result->lives[0].candidate.heat_after.energy.value == 0); // Matter adds no heat.
|
|
CHECK(result->spent == inv(0, 100, 0, 0, 0));
|
|
CHECK(result->starved.size() == 1 && result->starved[0].life_id == 2);
|
|
CHECK(total(*result, *map, lives) == before(*map, lives));
|
|
}
|
|
|
|
void reserve_mass_and_capacity_decide_engulfment() {
|
|
const auto map = liquid_map({}, false);
|
|
// Predator 3,000; prey body 1,000 plus 1,000 fg of reserve matter: exactly 1.5 times.
|
|
const auto prey_reserve = inv(100, 250, 250, 250, 250);
|
|
{
|
|
const std::array lives{cell(1, {500, 500}, 3'000, {}, true),
|
|
cell(2, {510, 500}, 1'000, prey_reserve)};
|
|
const auto result = propose_cellular_region_step(4, area, lives, *map, plain);
|
|
CHECK(result && result->lives.size() == 1 &&
|
|
result->lives[0].engulf == CellularEngulfStatus::engulfed);
|
|
CHECK(total(*result, *map, lives) == before(*map, lives));
|
|
}
|
|
{
|
|
// One more femtogram of reserve makes the prey too large.
|
|
const std::array lives{cell(1, {500, 500}, 3'000, {}, true),
|
|
cell(2, {510, 500}, 1'000, inv(100, 251, 250, 250, 250))};
|
|
const auto result = propose_cellular_region_step(4, area, lives, *map, plain);
|
|
CHECK(result && result->lives.size() == 2 &&
|
|
result->lives[0].engulf == CellularEngulfStatus::prey_too_large);
|
|
// Counting no reserve, or a bigger chamber, makes it eligible again.
|
|
CellularRevampStepRules unladen{};
|
|
unladen.engulf_reserve_percent = 0;
|
|
const CellularRegionRules light{true, true, unladen};
|
|
const auto again = propose_cellular_region_step(4, area, lives, *map, light);
|
|
CHECK(again && again->lives.size() == 1);
|
|
auto chamber = founder_input();
|
|
chamber.engulf_capacity = 105;
|
|
const std::array bigger{cell(1, {500, 500}, 3'000, {}, true, chamber), lives[1]};
|
|
const auto wider = propose_cellular_region_step(4, area, bigger, *map, plain);
|
|
CHECK(wider && wider->lives.size() == 1);
|
|
}
|
|
{
|
|
// A thicker membrane defends: reported as the membrane, not the size.
|
|
auto tough = founder_input();
|
|
tough.membrane_nm = 1'200;
|
|
const std::array lives{cell(1, {500, 500}, 3'000, {}, true),
|
|
cell(2, {510, 500}, 1'000, prey_reserve, false, tough)};
|
|
const auto result = propose_cellular_region_step(4, area, lives, *map, plain);
|
|
CHECK(result && result->lives.size() == 2 &&
|
|
result->lives[0].engulf == CellularEngulfStatus::membrane_too_thick);
|
|
}
|
|
}
|
|
|
|
void a_kill_splits_its_remainder() {
|
|
const auto map = liquid_map({}, false);
|
|
auto hunter = founder_input();
|
|
hunter.energy_kept_per_thousand = 500;
|
|
hunter.kill_absorption = 200; // Twice the 50-per-tick limit: 100 per channel.
|
|
const std::array lives{cell(1, {500, 500}, 4'000, {}, true, hunter),
|
|
cell(2, {510, 500}, 1'000, inv(1'000, 290, 290, 290, 290))};
|
|
const auto result = propose_cellular_region_step(4, area, lives, *map, plain);
|
|
CHECK(result && result->lives.size() == 1);
|
|
const auto& predator = result->lives[0].candidate;
|
|
// Prey: 1,000 pJ and 300 fg per channel (290 reserve + 10 body). Absorbed: 100 each;
|
|
// half of the 100 pJ kept, half heat.
|
|
CHECK(predator.reserve_after == inv(50, 100, 100, 100, 100));
|
|
CHECK(predator.heat_after.energy.value == 50);
|
|
// Remainder 900 pJ and 200 fg per channel: half as bubbles, half to the spent pool.
|
|
CHECK(result->spent == inv(450, 100, 100, 100, 100));
|
|
Inventory bubbles{};
|
|
for (const auto& patch : result->new_patches)
|
|
add(bubbles, patch.stock);
|
|
CHECK(bubbles == inv(450, 100, 100, 100, 100));
|
|
CHECK(total(*result, *map, lives) == before(*map, lives));
|
|
}
|
|
|
|
void every_life_needs_a_revamp_input() {
|
|
const auto map = liquid_map({}, false);
|
|
auto bare = cell(1, {500, 500}, 1'000, {});
|
|
bare.revamp.reset();
|
|
const std::array lives{bare};
|
|
CHECK(!propose_cellular_region_step(4, area, lives, *map, plain));
|
|
auto greedy = founder_input();
|
|
greedy.energy_kept_per_thousand = 1'001;
|
|
const auto kept = cell(1, {500, 500}, 1'000, {}, false, greedy);
|
|
const std::array bad{kept};
|
|
CHECK(!propose_cellular_region_step(4, area, bad, *map, plain));
|
|
// And none without rules 13.
|
|
const std::array revamped{cell(1, {500, 500}, 1'000, {})};
|
|
CHECK(!propose_cellular_region_step(4, area, revamped, *map, CellularRegionRules{true, true}));
|
|
}
|
|
} // namespace
|
|
|
|
int main() {
|
|
try {
|
|
collection_per_channel_with_a_shared_limit();
|
|
a_thicker_membrane_collects_slower();
|
|
matter_pays_upkeep_and_nothing_left_starves();
|
|
reserve_mass_and_capacity_decide_engulfment();
|
|
a_kill_splits_its_remainder();
|
|
every_life_needs_a_revamp_input();
|
|
} catch (const std::exception& error) {
|
|
std::fprintf(stderr, "%s\n", error.what());
|
|
return 1;
|
|
}
|
|
std::puts("PASS rules 13 region step: collection, energy, matter as energy and engulfment");
|
|
return 0;
|
|
}
|