Files
aevolution/tests/cellular_deposit_motion_tests.cpp
dingoandClaude Opus 5.5 131ed5f254
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
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 5s
Foundation / checks (deploy) (push) Successful in 4m29s
Foundation / postgres-tools (push) Successful in 29s
Foundation / server-image (push) Successful in 4m24s
Foundation / image (push) Successful in 4m58s
feat(cellular): playtest fixes - edge, cell count, mixed bubbles, still pools
From the first rules-13 playtest:
- An earned offer waits until a single cell can split (keeping its count),
  so choices are no longer refused one after another.
- The client draws the region's walls (dimming outside), names the edge
  when the cell is against it, shows the region's cell count, and says when
  upkeep is burning kept matter because no energy is left.
- Mixed bubbles hold every nutrient at once (--mixed-motes; none leaves the
  scatter stream unchanged) and are drawn as small pools of their nutrients.
  With intake split by the body's recipe, single-nutrient phosphorus
  bubbles fed slowly; mixed ones carry it with everything else.
- Liquid openings move each pool out of the currents to the nearest still
  water (cellular_still_centre).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 09:02:43 -04:00

263 lines
11 KiB
C++

#include "evo/biology/cellular_deposit_motion.hpp"
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <source_location>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
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 deposit motion line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
constexpr std::int32_t um_per_s = 1'024;
constexpr CellularDepositMotionRules rules{.drift_radius_um = 200,
.max_drift_q10 = 100 * um_per_s,
.drift_decay_ppm = 95'163,
.merged_cap = 8,
.bubble_radius_um = cellular_mote_radius_um};
Inventory energy(std::uint64_t amount) {
Inventory value{};
value.energy.value = amount;
return value;
}
CellularPatch bubble(std::uint32_t id, std::uint32_t x, std::uint32_t y,
std::uint64_t amount = 10) {
CellularPatch patch{
{x, y}, 1, energy(amount), id, cellular_mote_radius_um, CellularDepositKind::mote};
patch.merged = 1;
return patch;
}
CellularHabitatConfig liquid(std::vector<CellularPatch> patches, std::uint32_t size = 60'000) {
std::ranges::sort(patches, {}, &CellularPatch::address);
std::uint32_t next{};
for (const auto& patch : patches)
next = std::max(next, patch.id + 1);
return {100, 1, 9, size, size, std::move(patches), cellular_habitat_liquid_format, next};
}
CellularFlowField still(std::uint32_t size = 60'000) {
auto field = CellularFlowField::generate({1, size, size, 250, 0, 0, 3'000, 6'000, 60, 300});
check(field.has_value());
return std::move(*field);
}
CellularFlowField currents() {
auto field =
CellularFlowField::generate({11, 60'000, 60'000, 250, 2, 4, 3'000, 6'000, 60, 300});
check(field.has_value());
return std::move(*field);
}
CellularDepositMotion advect(const CellularHabitatConfig& config, const CellularFlowField& field,
std::span<const CellularWake> wakes = {}) {
auto moved = advect_cellular_deposits(config, field, wakes, rules);
check(moved.has_value());
check(CellularHabitat::create(moved->config).has_value());
return std::move(*moved);
}
void format_three_round_trip() {
auto moving = bubble(1, 500, 600);
moving.drift_x_q10 = -3'000;
moving.carry_y = 1'000;
moving.merged = 3;
CellularPatch pool{{9'000, 9'000}, 2, energy(500), 2, 500, CellularDepositKind::pool};
pool.merged = 1;
const auto habitat = CellularHabitat::create(liquid({moving, pool}));
CHECK(habitat);
const auto bytes = (*habitat)->snapshot();
CHECK(bytes.size() == cellular_habitat_header_bytes + 2 * cellular_liquid_record_bytes + 8);
const auto restored = CellularHabitat::restore(bytes, 100, 1, 9);
CHECK(restored && (*restored)->config() == (*habitat)->config());
auto unmerged = liquid({bubble(1, 500, 600)});
unmerged.patches[0].merged = 0;
CHECK(!CellularHabitat::create(unmerged));
auto drifting_pool = liquid({pool});
drifting_pool.patches[0].drift_x_q10 = 1;
CHECK(!CellularHabitat::create(drifting_pool));
auto old = liquid({bubble(1, 500, 600)});
old.format = cellular_habitat_deposit_format;
CHECK(!CellularHabitat::create(old)); // Format 2 carries no motion fields.
old.patches[0].merged = 0;
CHECK(CellularHabitat::create(old));
}
// A format-3 habitat is perceived as pools and bubbles, exactly like format 2.
void liquid_deposits_are_perceived() {
CellularPatch pool{{8'000, 8'000}, 2, energy(500), 2, 500, CellularDepositKind::pool};
pool.merged = 1;
const auto habitat = CellularHabitat::create(liquid({bubble(1, 5'100, 5'000), pool}));
CHECK(habitat);
const auto seen = (*habitat)->perceive({5'000, 5'000}, 20);
CHECK(seen && seen->size() == 2);
CHECK(std::ranges::any_of(*seen, [](const auto& p) {
return p.id == 1 && p.kind == CellularDepositKind::mote && p.radius_um == 30;
}));
CHECK(std::ranges::any_of(
*seen, [](const auto& p) { return p.id == 2 && p.kind == CellularDepositKind::pool; }));
}
void still_water_keeps_bubbles() {
const auto config = liquid({bubble(1, 5'000, 5'000), bubble(2, 9'000, 9'000)});
const auto moved = advect(config, still());
CHECK(moved.config == config && moved.report == CellularDepositMotionReport{});
}
void passing_cells_push_and_drift_decays() {
const auto field = still();
auto config = liquid({bubble(1, 30'000, 30'000)});
// A cell passing at 190 um/s right beside the bubble.
const CellularWake wake{{29'950, 29'990}, 20, 190 * um_per_s, 0};
for (int tick = 0; tick < 10; ++tick)
config = advect(config, field, std::span{&wake, 1}).config;
const auto pushed = config.patches[0];
CHECK(pushed.address.x_um > 30'000 && pushed.address.y_um == 30'000);
CHECK(pushed.drift_x_q10 > 0 && pushed.drift_x_q10 <= 100 * um_per_s);
// A distant cell does nothing.
const CellularWake far{{20'000, 20'000}, 20, 190 * um_per_s, 0};
const auto untouched = advect(liquid({bubble(1, 30'000, 30'000)}), field, std::span{&far, 1});
CHECK(untouched.config.patches[0].address == CellularPatchAddress{30'000, 30'000});
// Without cells the induced drift decays and the bubble stops.
int ticks{};
while (config.patches[0].drift_x_q10 != 0) {
config = advect(config, field).config;
CHECK(++ticks < 400);
}
const auto resting = config.patches[0].address;
CHECK(advect(config, field).config.patches[0].address == resting);
}
void touching_bubbles_merge_up_to_the_cap() {
const auto field = still();
const auto merged =
advect(liquid({bubble(4, 1'000, 1'000, 10), bubble(9, 1'050, 1'000, 7)}), field);
CHECK(merged.report.merges == 1 && merged.config.patches.size() == 1);
const auto& keeper = merged.config.patches[0];
CHECK(keeper.id == 4 && keeper.stock.energy.value == 17 && keeper.merged == 2);
CHECK(keeper.address == CellularPatchAddress{1'000, 1'000});
CHECK(keeper.radius_um == cellular_merged_radius(cellular_mote_radius_um, 2) &&
keeper.radius_um == 42);
// Apart, nothing merges.
CHECK(advect(liquid({bubble(1, 1'000, 1'000), bubble(2, 1'061, 1'000)}), field).report.merges ==
0);
// At the cap, bubbles stay separate.
auto full = bubble(1, 1'000, 1'000);
full.merged = 8;
full.radius_um = cellular_merged_radius(cellular_mote_radius_um, 8);
CHECK(advect(liquid({full, bubble(2, 1'010, 1'000)}), field).report.merges == 0);
// Pools never merge or move.
CellularPatch pool{{2'000, 2'000}, 2, energy(500), 3, 500, CellularDepositKind::pool};
pool.merged = 1;
const auto beside = advect(liquid({pool, bubble(5, 2'010, 2'000)}), field);
CHECK(beside.report.merges == 0 && beside.config.patches.size() == 2);
}
void shared_addresses_are_separated() {
// Two bubbles at the cap landing on one address stay distinct.
auto a = bubble(1, 3'000, 3'000), b = bubble(2, 3'000, 3'000);
a.merged = b.merged = 8;
const auto separated = advect_cellular_deposits(
{100, 1, 9, 60'000, 60'000, {a, b}, cellular_habitat_liquid_format, 3}, still(), {}, rules);
CHECK(separated && separated->report.nudged == 1);
CHECK(CellularHabitat::create(separated->config));
}
void currents_carry_food_without_loss_or_piles() {
const auto field = currents();
CellularHabitatConfig base{100, 1, 9, 60'000, 60'000, {}, cellular_habitat_deposit_format, 1};
const auto scattered = scatter_deposits(base, {}, {1'800, 7, energy(70), 99});
CHECK(scattered);
auto config = *scattered;
config.format = cellular_habitat_liquid_format;
for (auto& patch : config.patches)
patch.merged = 1;
const auto total = [](const CellularHabitatConfig& c) {
std::uint64_t sum{};
for (const auto& patch : c.patches)
sum += patch.stock.energy.value;
return sum;
};
const auto opening = total(config);
std::uint32_t merges{}, moved{};
for (int tick = 0; tick < 2'000; ++tick) {
const auto next = advect(config, field);
merges += next.report.merges;
moved += next.report.moved;
config = next.config;
}
CHECK(total(config) == opening);
CHECK(config.patches.size() + merges == 1'800);
CHECK(moved > 10'000 && merges > 0);
// Nothing collects against the walls: the edges are still water.
const auto at_edge = std::ranges::count_if(config.patches, [](const CellularPatch& p) {
return p.address.x_um == 0 || p.address.y_um == 0 || p.address.x_um == 59'999 ||
p.address.y_um == 59'999;
});
CHECK(at_edge < 20);
// Deterministic.
CHECK(advect(config, field).config == advect(config, field).config);
}
void refusals() {
auto two = liquid({bubble(1, 1'000, 1'000)});
two.format = cellular_habitat_deposit_format;
CHECK(!advect_cellular_deposits(two, still(), {}, rules));
CHECK(!advect_cellular_deposits(liquid({bubble(1, 1'000, 1'000)}, 30'000), still(), {}, rules));
auto bad = rules;
bad.merged_cap = 0;
CHECK(!advect_cellular_deposits(liquid({bubble(1, 1'000, 1'000)}), still(), {}, bad));
}
} // namespace
// Mixed bubbles hold every nutrient at once and follow the single-nutrient ones from the same
// stream: without them the scatter is unchanged.
void mixed_bubbles_hold_every_nutrient() {
CellularHabitatConfig base{100, 1, 9, 60'000, 60'000, {}, cellular_habitat_deposit_format, 1};
const auto plain = scatter_deposits(base, {}, {300, 7, energy(70), 99});
CHECK(plain);
Inventory mixed{{1'000}, {{{500}, {150}, {50}, {300}}}};
CellularMoteField field{300, 7, energy(70), 99, 200, mixed};
const auto both = scatter_deposits(base, {}, field);
CHECK(both && both->patches.size() == 500);
CHECK(std::ranges::count(both->patches, mixed, &CellularPatch::stock) == 200);
field.mixed_count = 0;
const auto none = scatter_deposits(base, {}, field);
CHECK(none && none->patches == plain->patches);
// Mixed bubbles alone, and a mixed count without an amount.
const auto only = scatter_deposits(base, {}, {0, 7, {}, 99, 50, mixed});
CHECK(only && only->patches.size() == 50);
CHECK(!scatter_deposits(base, {}, {0, 7, {}, 99, 50, {}}));
}
int main() {
try {
mixed_bubbles_hold_every_nutrient();
format_three_round_trip();
liquid_deposits_are_perceived();
still_water_keeps_bubbles();
passing_cells_push_and_drift_decays();
touching_bubbles_merge_up_to_the_cap();
shared_addresses_are_separated();
currents_carry_food_without_loss_or_piles();
refusals();
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
return 1;
}
std::puts("cellular deposit motion contracts passed");
return 0;
}