319 lines
15 KiB
C++
319 lines
15 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,
|
|
static_cast<std::uint32_t>(area.width_um),
|
|
static_cast<std::uint32_t>(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);
|
|
// Food was left on every channel, so both limits held collection back.
|
|
CHECK(result->lives[0].candidate.matter_limited && result->lives[0].candidate.energy_limited);
|
|
CHECK(total(*result, *map, lives) == before(*map, lives));
|
|
}
|
|
|
|
// The even split: a source's matter limit (here 200 a tick) is shared equally by the
|
|
// nutrients it holds, each share scaled by the cell's collection trait; a collection
|
|
// percentage scales every limit.
|
|
void collection_split_evenly_between_what_a_source_holds() {
|
|
CellularRevampStepRules even{};
|
|
even.split_evenly = true;
|
|
const CellularRegionRules rules{true, true, even};
|
|
const auto collected = [&](const Inventory& stock, const CellularRegionRules& with,
|
|
CellularRevampLifeInput input = founder_input()) {
|
|
const auto map = liquid_map(stock);
|
|
const std::array lives{cell(1, {95, 95}, 1'000, {}, false, input)};
|
|
const auto result = propose_cellular_region_step(4, area, lives, *map, with);
|
|
CHECK(result && total(*result, *map, lives) == before(*map, lives));
|
|
return result->lives[0].candidate.reserve_after.matter;
|
|
};
|
|
const auto only_carbon = collected(inv(0, 1'000, 0, 0, 0), rules);
|
|
CHECK(only_carbon[0].value == 200 && only_carbon[2].value == 0);
|
|
const auto carbon_and_phosphorus = collected(inv(0, 1'000, 0, 1'000, 0), rules);
|
|
CHECK(carbon_and_phosphorus[0].value == 100 && carbon_and_phosphorus[2].value == 100);
|
|
const auto all_four = collected(inv(0, 1'000, 1'000, 1'000, 1'000), rules);
|
|
CHECK(all_four[0].value == 50 && all_four[1].value == 50 && all_four[2].value == 50 &&
|
|
all_four[3].value == 50);
|
|
// A share is capped by what the source holds; the trait scales its own share only.
|
|
const auto scarce = collected(inv(0, 1'000, 0, 30, 0), rules);
|
|
CHECK(scarce[0].value == 100 && scarce[2].value == 30);
|
|
auto poor = founder_input();
|
|
poor.collection = {100, 50, 100, 100, 100}; // Carbon collection halved.
|
|
const auto poor_take = collected(inv(0, 1'000, 0, 1'000, 0), rules, poor);
|
|
CHECK(poor_take[0].value == 50 && poor_take[2].value == 100);
|
|
// 130%: every limit 65 a tick, the shared matter limit 260.
|
|
auto more = even;
|
|
more.collection_percent = 130;
|
|
const auto boosted = collected(inv(0, 1'000, 0, 0, 0), {true, true, more});
|
|
CHECK(boosted[0].value == 260);
|
|
// Off, as before: per-nutrient limits (50 a tick each).
|
|
const auto per_nutrient = collected(inv(0, 1'000, 0, 0, 0), plain);
|
|
CHECK(per_nutrient[0].value == 50);
|
|
}
|
|
|
|
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();
|
|
collection_split_evenly_between_what_a_source_holds();
|
|
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;
|
|
}
|