Files
aevolution/tests/cellular_burn_tests.cpp
dingoandClaude Opus 5.5 76d21b4815
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
feat(cellular): rules 13 feeding, matter as energy, spent pool and predation
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>
2026-10-22 18:35:31 -04:00

134 lines
5.7 KiB
C++

#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;
}