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:
@@ -0,0 +1,133 @@
|
||||
#include "evo/biology/cellular_burn.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <source_location>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
// Matter as energy (rules 13, ADR-0179): energy first, then the largest burnable channel at its
|
||||
// rate; exclusions hold except for upkeep; nothing changes when the cost cannot be paid.
|
||||
namespace {
|
||||
using namespace evo;
|
||||
using namespace evo::biology;
|
||||
void check(bool condition, std::source_location at = std::source_location::current()) {
|
||||
if (!condition)
|
||||
throw std::runtime_error("cellular burn line " + std::to_string(at.line()));
|
||||
}
|
||||
#define CHECK(expression) check(static_cast<bool>(expression))
|
||||
|
||||
accounting::Inventory reserve(std::uint64_t energy, std::uint64_t c, std::uint64_t n,
|
||||
std::uint64_t p, std::uint64_t o) {
|
||||
return {{energy},
|
||||
{accounting::Femtograms{c}, accounting::Femtograms{n}, accounting::Femtograms{p},
|
||||
accounting::Femtograms{o}}};
|
||||
}
|
||||
// 10 pJ per 1,000 fg on every channel unless stated.
|
||||
CellularBurn flat(std::uint8_t excluded = 0) {
|
||||
return cellular_burn({10, 10, 10, 10}, {100, 100, 100, 100}, excluded);
|
||||
}
|
||||
|
||||
void energy_first() {
|
||||
auto r = reserve(500, 1'000, 1'000, 1'000, 1'000);
|
||||
const auto paid = cellular_pay(r, 300, flat(), false);
|
||||
CHECK(paid && paid->energy == 300 && paid->spent == accounting::Inventory{});
|
||||
CHECK(r == reserve(200, 1'000, 1'000, 1'000, 1'000));
|
||||
}
|
||||
|
||||
void largest_channel_then_next() {
|
||||
// 100 pJ of energy, then 150 pJ from matter: carbon (largest) covers 100 pJ with 10,000 fg,
|
||||
// then nitrogen (now largest) covers the last 50 pJ with 5,000 fg.
|
||||
auto r = reserve(100, 10'000, 8'000, 1'000, 2'000);
|
||||
const auto paid = cellular_pay(r, 250, flat(), false);
|
||||
CHECK(paid && paid->energy == 100 && paid->spent == reserve(0, 10'000, 5'000, 0, 0));
|
||||
CHECK(r == reserve(0, 0, 3'000, 1'000, 2'000));
|
||||
// Ties go to the lower channel.
|
||||
auto tie = reserve(0, 3'000, 3'000, 0, 0);
|
||||
const auto first = cellular_pay(tie, 10, flat(), false);
|
||||
CHECK(first && first->spent == reserve(0, 1'000, 0, 0, 0));
|
||||
}
|
||||
|
||||
void whole_femtograms_round_up() {
|
||||
// 1 pJ at 10 pJ per 1,000 fg needs 100 fg; 1 pJ at 3 pJ per 1,000 fg needs 334 fg.
|
||||
auto r = reserve(0, 1'000, 0, 0, 0);
|
||||
const auto paid = cellular_pay(r, 1, flat(), false);
|
||||
CHECK(paid && paid->spent.matter[0].value == 100);
|
||||
auto odd = reserve(0, 1'000, 0, 0, 0);
|
||||
const auto thirds =
|
||||
cellular_pay(odd, 1, cellular_burn({3, 3, 3, 3}, {100, 100, 100, 100}, 0), false);
|
||||
CHECK(thirds && thirds->spent.matter[0].value == 334);
|
||||
}
|
||||
|
||||
void exclusions_and_upkeep_override() {
|
||||
// Carbon excluded: nitrogen pays, though carbon is larger.
|
||||
auto r = reserve(0, 10'000, 2'000, 0, 0);
|
||||
const auto paid = cellular_pay(r, 10, flat(0b0001), false);
|
||||
CHECK(paid && paid->spent == reserve(0, 0, 1'000, 0, 0));
|
||||
// Only excluded matter left: refused for thrust, paid for upkeep.
|
||||
auto kept = reserve(0, 10'000, 0, 0, 0);
|
||||
CHECK(cellular_payable_energy(kept, flat(0b0001), false) == 0);
|
||||
CHECK(!cellular_pay(kept, 10, flat(0b0001), false) && kept == reserve(0, 10'000, 0, 0, 0));
|
||||
const auto upkeep = cellular_pay(kept, 10, flat(0b0001), true);
|
||||
CHECK(upkeep && upkeep->spent == reserve(0, 1'000, 0, 0, 0));
|
||||
}
|
||||
|
||||
void refusal_changes_nothing() {
|
||||
// 5 pJ of energy plus 10 pJ of matter cannot pay 16 pJ.
|
||||
auto r = reserve(5, 1'000, 0, 0, 0);
|
||||
CHECK(cellular_payable_energy(r, flat(), false) == 15);
|
||||
CHECK(!cellular_pay(r, 16, flat(), false) && r == reserve(5, 1'000, 0, 0, 0));
|
||||
const auto exact = cellular_pay(r, 15, flat(), false);
|
||||
CHECK(exact && exact->energy == 5 && exact->spent == reserve(0, 1'000, 0, 0, 0) &&
|
||||
r == reserve(0, 0, 0, 0, 0));
|
||||
}
|
||||
|
||||
void efficiency_scales_rates() {
|
||||
const auto burn = cellular_burn({10, 10, 10, 10}, {105, 99, 100, 100}, 0xFF);
|
||||
CHECK(burn.pj_per_kfg[0] == 10 && burn.pj_per_kfg[1] == 9 && burn.excluded == 0x0F);
|
||||
const auto finer = cellular_burn({10'000, 10'000, 0, 10'000}, {105, 99, 100, 100}, 0);
|
||||
CHECK(finer.pj_per_kfg[0] == 10'500 && finer.pj_per_kfg[1] == 9'900);
|
||||
// A zero rate cannot be burned.
|
||||
auto r = reserve(0, 0, 0, 5'000, 0);
|
||||
CHECK(cellular_payable_energy(r, finer, true) == 0 && !cellular_pay(r, 1, finer, true));
|
||||
}
|
||||
|
||||
// Every femtogram and picojoule is accounted: reserve before = reserve after + paid.
|
||||
void conservation_property() {
|
||||
for (std::uint64_t seed = 1; seed < 400; ++seed) {
|
||||
const auto x = seed * 2'654'435'761ULL;
|
||||
const auto before =
|
||||
reserve(x % 997, x % 50'021, (x >> 8) % 40'009, (x >> 16) % 3'001, (x >> 24) % 70'001);
|
||||
const auto burn = cellular_burn({7 + seed % 13, 9, 11 + seed % 5, 13},
|
||||
{100, static_cast<std::uint16_t>(80 + seed % 40), 100, 120},
|
||||
static_cast<std::uint8_t>(seed % 16));
|
||||
auto after = before;
|
||||
const auto cost = (x >> 32) % 2'000;
|
||||
const auto paid = cellular_pay(after, cost, burn, seed % 3 == 0);
|
||||
if (!paid) {
|
||||
CHECK(after == before);
|
||||
continue;
|
||||
}
|
||||
CHECK(after.energy.value + paid->energy == before.energy.value);
|
||||
for (std::size_t c = 0; c < 4; ++c)
|
||||
CHECK(after.matter[c].value + paid->spent.matter[c].value == before.matter[c].value);
|
||||
CHECK(paid->spent.energy.value == 0);
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
try {
|
||||
energy_first();
|
||||
largest_channel_then_next();
|
||||
whole_femtograms_round_up();
|
||||
exclusions_and_upkeep_override();
|
||||
refusal_changes_nothing();
|
||||
efficiency_scales_rates();
|
||||
conservation_property();
|
||||
} catch (const std::exception& error) {
|
||||
std::fprintf(stderr, "%s\n", error.what());
|
||||
return 1;
|
||||
}
|
||||
std::puts("PASS cellular matter-as-energy payments");
|
||||
return 0;
|
||||
}
|
||||
@@ -25,8 +25,41 @@ CellularLiquidRules liquid() {
|
||||
.bubble_radius_um = biology::cellular_mote_radius_um}};
|
||||
}
|
||||
|
||||
// The trial revamp rules (deploy/kubernetes/region/dev/tuning/cellular-revamp.tuning).
|
||||
CellularRevampRules revamp(std::uint32_t essential = 250) {
|
||||
return {.body = {.essential_upkeep_per_thousand = essential}};
|
||||
return {.body = {.essential_upkeep_per_thousand = essential},
|
||||
.burn_pj_per_kfg = {2'000, 2'000, 2'000, 2'000},
|
||||
.step = {.membrane_collection_per_mille_per_point = 2,
|
||||
.engulf_ratio_milli = 1'500,
|
||||
.engulf_reserve_percent = 100,
|
||||
.kill_bubble_per_thousand = 500},
|
||||
.drip_interval_ticks = 20,
|
||||
.drip_bubble_pj = 20'000'000,
|
||||
.drip_bubble_fg = 1'000'000,
|
||||
.reserve_load_per_thousand = 1'000};
|
||||
}
|
||||
|
||||
// Reserve matter slows thrust and turning and raises thrust costs in proportion to the body.
|
||||
void carried_matter_is_load() {
|
||||
const biology::CellularHandling base{.thrust_q10 = 20'480,
|
||||
.reverse_thrust_q10 = 10'240,
|
||||
.forward_drag_ppm = 95'163,
|
||||
.lateral_drag_ppm = 95'163,
|
||||
.turn = 3'276,
|
||||
.thrust_cost = {1'000},
|
||||
.reverse_cost = {500}};
|
||||
require(cellular_loaded_handling(base, 8'000'000, 0, revamp()) == base,
|
||||
"an empty reserve changed the handling");
|
||||
const auto heavy = cellular_loaded_handling(base, 8'000'000, 8'000'000, revamp());
|
||||
require(heavy.thrust_q10 == 10'240 && heavy.reverse_thrust_q10 == 5'120 &&
|
||||
heavy.turn == 1'638 && heavy.thrust_cost.value == 2'000 &&
|
||||
heavy.reverse_cost.value == 1'000 &&
|
||||
heavy.forward_drag_ppm == base.forward_drag_ppm,
|
||||
"carrying the body's mass again did not halve thrust and double its cost");
|
||||
auto none = revamp();
|
||||
none.reserve_load_per_thousand = 0;
|
||||
require(cellular_loaded_handling(base, 8'000'000, 8'000'000, none) == base,
|
||||
"a zero load share still loaded the cell");
|
||||
}
|
||||
|
||||
LifeAccount account(std::uint8_t last) {
|
||||
@@ -67,11 +100,13 @@ Owner revamp_region(std::uint32_t essential = 250) {
|
||||
return state;
|
||||
}
|
||||
|
||||
std::vector<CellularLifeIntent> swim(const CellularLives& state, biology::CellularIntent movement) {
|
||||
std::vector<CellularLifeIntent> swim(const CellularLives& state, biology::CellularIntent movement,
|
||||
std::uint8_t exclude = 0) {
|
||||
std::vector<CellularLifeIntent> result;
|
||||
for (const auto& life : state.lives()) {
|
||||
CellularLifeIntent input{life.identity.life, {}};
|
||||
input.intent.movement = movement;
|
||||
input.intent.exclude = exclude;
|
||||
result.push_back(input);
|
||||
}
|
||||
return result;
|
||||
@@ -118,6 +153,93 @@ void mixed_generations_tick_and_continue() {
|
||||
closed(*continuous);
|
||||
}
|
||||
|
||||
// The spent pool drips back one bubble per non-empty channel on every interval tick, at
|
||||
// seeded spots, without losing or creating anything; a full habitat keeps it waiting.
|
||||
void the_spent_pool_drips_back() {
|
||||
const auto rules = revamp();
|
||||
biology::CellularHabitatConfig config{7, 9, 11, 48'000, 48'000, {}};
|
||||
config.format = biology::cellular_habitat_liquid_format;
|
||||
config.next_deposit = 1;
|
||||
accounting::Inventory spent{{50'000'000}, {{{2'500'000}, {0}, {300}, {1'000'000}}}};
|
||||
auto off = config;
|
||||
auto kept = spent;
|
||||
require(cellular_drip_spent(off, kept, rules, 1, 2, 19, 5) && off == config && kept == spent,
|
||||
"the pool dripped between intervals");
|
||||
auto once = config;
|
||||
auto left = spent;
|
||||
require(cellular_drip_spent(once, left, rules, 1, 2, 20, 5), "drip refused");
|
||||
require(once.patches.size() == 4 && once.next_deposit == 5,
|
||||
"a drip is one bubble per non-empty channel");
|
||||
accounting::Inventory dripped{};
|
||||
for (const auto& bubble : once.patches) {
|
||||
require(bubble.kind == biology::CellularDepositKind::mote && bubble.merged == 1 &&
|
||||
bubble.source == 5 && bubble.address.x_um < 48'000 &&
|
||||
bubble.address.y_um < 48'000,
|
||||
"a drip bubble is malformed");
|
||||
dripped.energy.value += bubble.stock.energy.value;
|
||||
for (std::size_t c = 0; c < 4; ++c)
|
||||
dripped.matter[c].value += bubble.stock.matter[c].value;
|
||||
}
|
||||
require(dripped ==
|
||||
accounting::Inventory{{20'000'000}, {{{1'000'000}, {0}, {300}, {1'000'000}}}} &&
|
||||
left == accounting::Inventory{{30'000'000}, {{{1'500'000}, {0}, {0}, {0}}}},
|
||||
"the drip did not move the capped amounts exactly");
|
||||
auto again = config;
|
||||
auto same = spent;
|
||||
require(cellular_drip_spent(again, same, rules, 1, 2, 20, 5) && again == once,
|
||||
"the drip is not deterministic");
|
||||
auto full = config;
|
||||
for (std::uint32_t i = 0; i < biology::max_cellular_liquid_deposits; ++i) {
|
||||
biology::CellularPatch filler{{i % 48'000, i / 48'000},
|
||||
5,
|
||||
{{1}, {}},
|
||||
++full.next_deposit,
|
||||
biology::cellular_mote_radius_um,
|
||||
biology::CellularDepositKind::mote};
|
||||
filler.merged = 1;
|
||||
full.patches.push_back(filler);
|
||||
}
|
||||
const auto packed = full;
|
||||
auto waiting = spent;
|
||||
require(cellular_drip_spent(full, waiting, rules, 1, 2, 20, 5) && full == packed &&
|
||||
waiting == spent,
|
||||
"a full habitat took a drip");
|
||||
}
|
||||
|
||||
// Exclusions belong to owned lives under rules 13 and travel in the trace.
|
||||
void exclusions_are_checked_and_replayed() {
|
||||
auto state = revamp_region();
|
||||
require(state->advance_tick(swim(*state, {300, 0}, 0b0101)).has_value(),
|
||||
"an owned life's exclusions were refused");
|
||||
require(!state->advance_tick(swim(*state, {}, 0x10)).has_value(),
|
||||
"an exclusion outside the four matter channels was accepted");
|
||||
auto older = opening(7, true, 2, false);
|
||||
checked(older->advance_tick({}), "genesis tick");
|
||||
older = checked(older->migration_upgrade_candidate(), "boundary upgrade");
|
||||
older = checked(older->maturation_upgrade_candidate(), "maturation upgrade");
|
||||
older = checked(older->inheritance_upgrade_candidate(), "inheritance upgrade");
|
||||
older = checked(older->photosynthesis_upgrade_candidate({0, 1, {0}}), "light upgrade");
|
||||
older = checked(older->liquid_upgrade_candidate(liquid()), "liquid upgrade");
|
||||
older = checked(older->birth_candidate({account(1), 1}, {100, 100, 100, 100, {older->tick()}}),
|
||||
"founder 5 birth");
|
||||
require(!older->advance_tick(swim(*older, {}, 0b0001)).has_value(),
|
||||
"a rules-12 owner accepted exclusions");
|
||||
}
|
||||
|
||||
// A rules-13 region runs long enough for founders to spend their endowment, keeping closure,
|
||||
// and restores with its rules and spent pool exactly.
|
||||
void a_long_run_keeps_closure_and_restores() {
|
||||
auto state = revamp_region();
|
||||
for (int tick = 0; tick < 700; ++tick) {
|
||||
const auto x = static_cast<std::int16_t>((tick / 50) % 2 == 0 ? 900 : -900);
|
||||
checked(state->advance_tick(swim(*state, {x, 400}, 0b0010)), "long-run tick");
|
||||
}
|
||||
closed(*state);
|
||||
const auto copy = restore(*state);
|
||||
require(copy->snapshot() == state->snapshot() && copy->revamp_rules() == revamp(),
|
||||
"format 11 did not restore the rules and spent pool exactly");
|
||||
}
|
||||
|
||||
// A recorded trace replays to the exact live result under rules 13.
|
||||
void trace_replays() {
|
||||
auto live = revamp_region();
|
||||
@@ -128,7 +250,8 @@ void trace_replays() {
|
||||
batch.header.predecessor.fill(1);
|
||||
batch.header.predecessor_digest.fill(2);
|
||||
for (int tick = 0; tick < 24; ++tick) {
|
||||
const auto intents = swim(*live, {static_cast<std::int16_t>(tick < 12 ? 1'000 : -400), 0});
|
||||
const auto intents = swim(*live, {static_cast<std::int16_t>(tick < 12 ? 1'000 : -400), 0},
|
||||
static_cast<std::uint8_t>(tick % 16));
|
||||
checked(live->advance_tick(intents), "recorded tick");
|
||||
batch.ticks.push_back({intents, {}, {}});
|
||||
}
|
||||
@@ -137,12 +260,18 @@ void trace_replays() {
|
||||
const auto decoded = checked(decode_cellular_batch(bytes), "trace decodes");
|
||||
const auto replayed = checked(replay_cellular_batch(*base, decoded), "trace replays");
|
||||
require(replayed->snapshot() == live->snapshot(), "replay diverged from the live run");
|
||||
require(decoded.ticks.back().lives.front().intent.exclude == 23 % 16,
|
||||
"exclusions did not survive the trace");
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main() try {
|
||||
upgrade_and_restore();
|
||||
mixed_generations_tick_and_continue();
|
||||
the_spent_pool_drips_back();
|
||||
carried_matter_is_load();
|
||||
exclusions_are_checked_and_replayed();
|
||||
a_long_run_keeps_closure_and_restores();
|
||||
trace_replays();
|
||||
std::puts("PASS rules 13 owner: upgrade, founder 6, restore and replay");
|
||||
return 0;
|
||||
|
||||
@@ -0,0 +1,271 @@
|
||||
#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;
|
||||
}
|
||||
@@ -18,7 +18,20 @@ void check(bool condition, std::source_location at = std::source_location::curre
|
||||
#define CHECK(expression) check(static_cast<bool>(expression))
|
||||
|
||||
CellularRevampTuning trial() {
|
||||
return {.version = 1, .body_essential_upkeep_per_mille = 250};
|
||||
return {.version = 1,
|
||||
.body_essential_upkeep_per_mille = 250,
|
||||
.body_reserve_load_per_mille = 1'000,
|
||||
.energy_burn_carbon_pj_per_kfg = 2'000,
|
||||
.energy_burn_nitrogen_pj_per_kfg = 2'000,
|
||||
.energy_burn_phosphorus_pj_per_kfg = 2'000,
|
||||
.energy_burn_other_pj_per_kfg = 2'000,
|
||||
.feeding_membrane_collection_per_mille_per_point = 2,
|
||||
.predation_engulf_ratio_milli = 1'500,
|
||||
.predation_engulf_reserve_percent = 100,
|
||||
.predation_kill_bubble_per_mille = 500,
|
||||
.spent_drip_interval_ticks = 20,
|
||||
.spent_drip_bubble_pj = 20'000'000,
|
||||
.spent_drip_bubble_fg = 1'000'000};
|
||||
}
|
||||
|
||||
std::string with(std::string text, const std::string& key, const std::string& value) {
|
||||
@@ -60,6 +73,10 @@ void refusals() {
|
||||
const auto parsed = parse_cellular_revamp_tuning(missing);
|
||||
CHECK(!parsed && parsed.error().kind == CellularTuningErrorKind::missing_key &&
|
||||
parsed.error().key == "body.essential_upkeep_per_mille");
|
||||
CHECK(error_of(with(base, "spent.drip_bubble_fg", "0")) ==
|
||||
CellularTuningErrorKind::inconsistent);
|
||||
CHECK(error_of(with(base, "predation.engulf_reserve_percent", "101")) ==
|
||||
CellularTuningErrorKind::out_of_range);
|
||||
// Rules-12 keys belong in the other file.
|
||||
CHECK(error_of(base + "region.width_um = 60000\n") == CellularTuningErrorKind::unknown_key);
|
||||
CHECK(error_of(std::string(max_cellular_revamp_tuning_bytes + 1, '#')) ==
|
||||
|
||||
Reference in New Issue
Block a user