Files
aevolution/tests/cellular_deposit_motion_tests.cpp
T
dingoandClaude Opus 5.5 556d5164fb
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 5s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m32s
Foundation / postgres-tools (push) Successful in 27s
Foundation / server-image (push) Successful in 4m19s
Foundation / image (push) Successful in 4m53s
fix(cellular): perceive liquid deposits and commit every flush interval
Region 10 showed no food: the habitat's perception treated only format 2 as pools
and bubbles, so a format-3 habitat fell into the square-patch path, whose entries
the server bridge skips. Format 3 is now perceived exactly like format 2.

It also ran at 3-4 ticks per second: the cellular loop published every tick, and
each publication writes the whole region payload (about 300 KB for 60 x 60 mm with
3,500 deposits) to the object store and commits it to PostgreSQL. The loop now
keeps one session batch open and commits every flush interval (ADR-0089: 1 s,
20 ticks). Players see the batch's working state immediately; mutation offers and
life endings still come from committed state, so choices stay durable. Births and
mutation choices commit promptly in a fresh batch, a batch commits when demand
ends, and it is dropped when the host loses authority or changes mode. Pacing
follows the simulated tick, never backwards within an epoch. Refreshes the DEV
session window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 21:45:54 -04:00

242 lines
9.8 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
int main() {
try {
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;
}