Files
aevolution/tests/cellular_currents_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

203 lines
8.2 KiB
C++

#include "evo/biology/cellular_currents.hpp"
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <source_location>
#include <stdexcept>
#include <string>
#include <utility>
namespace {
using namespace evo::biology;
void check(bool value, std::source_location at = std::source_location::current()) {
if (!value)
throw std::runtime_error("cellular currents line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
constexpr std::int64_t um_per_s = cellular_velocity_q10_per_um_s;
// The v0 tuning: 60 x 60 mm, 2-4 routes, 3-6 mm wide, 60-300 um/s, 250 um grid.
CellularCurrentConfig v0(std::uint64_t seed) {
return {seed, 60'000, 60'000, 250, 2, 4, 3'000, 6'000, 60, 300};
}
CellularFlowField generate(const CellularCurrentConfig& config) {
auto field = CellularFlowField::generate(config);
check(field.has_value());
return std::move(*field);
}
std::int64_t speed(CellularFlowSample sample) {
const auto x = static_cast<double>(sample.x_q10), y = static_cast<double>(sample.y_q10);
return std::llround(std::sqrt(x * x + y * y));
}
void deterministic_generation() {
const auto first = generate(v0(7));
const auto second = generate(v0(7));
CHECK(first.digest() == second.digest() && first.routes().size() == second.routes().size());
CHECK(generate(v0(8)).digest() != first.digest());
CHECK(first.columns() == 242 && first.rows() == 242);
std::printf("current field digest %llu\n", static_cast<unsigned long long>(first.digest()));
CHECK(first.digest() == 6'763'241'105'898'603'476ULL);
}
void routes_within_configuration() {
for (std::uint64_t seed = 1; seed <= 40; ++seed) {
const auto field = generate(v0(seed));
CHECK(field.routes().size() >= 2 && field.routes().size() <= 4);
for (const auto& route : field.routes()) {
CHECK(route.width_um >= 3'000 && route.width_um <= 6'000);
CHECK(route.speed_um_per_s >= 60 && route.speed_um_per_s <= 300);
CHECK(route.points.size() == cellular_current_route_points);
const std::int64_t clear = route.width_um / 2;
for (const auto& point : route.points)
CHECK(point.x_um > clear && point.x_um < 60'000 - clear && point.y_um > clear &&
point.y_um < 60'000 - clear);
}
}
}
void bounded_with_quiet_water_and_still_walls() {
for (std::uint64_t seed = 1; seed <= 20; ++seed) {
const auto field = generate(v0(seed));
std::size_t still{}, flowing{};
for (const auto& node : field.nodes()) {
CHECK(speed(node) <= 300 * um_per_s);
(node.x_q10 == 0 && node.y_q10 == 0 ? still : flowing) += 1;
}
// Both quiet water and currents exist in every region.
CHECK(still > field.nodes().size() / 5 && flowing > field.nodes().size() / 20);
// Walls are still, so nothing is carried into them.
for (std::int64_t along = 0; along <= 60'000; along += 250) {
CHECK(field.sample(0, along) == CellularFlowSample{});
CHECK(field.sample(60'000, along) == CellularFlowSample{});
CHECK(field.sample(along, 0) == CellularFlowSample{});
CHECK(field.sample(along, 60'000) == CellularFlowSample{});
}
}
}
void route_centreline_runs_at_route_speed() {
const auto field = generate(v0(3));
const auto& route = field.routes().front();
const auto& a = route.points[4];
const auto& b = route.points[5];
const auto middle = field.sample((a.x_um + b.x_um) / 2, (a.y_um + b.y_um) / 2);
const auto expected = std::int64_t{route.speed_um_per_s} * um_per_s;
// Near full speed at the centreline, allowing for overlap and the grid.
CHECK(speed(middle) > expected * 8 / 10 && speed(middle) <= expected);
// Flow follows the route direction.
const auto along = (b.x_um - a.x_um) * middle.x_q10 + (b.y_um - a.y_um) * middle.y_q10;
CHECK(along > 0);
}
void interpolation_is_continuous() {
const auto field = generate(v0(5));
for (std::int64_t y = 1'000; y < 59'000; y += 1'777)
for (std::int64_t x = 1'000; x < 59'000; x += 13) {
const auto here = field.sample(x, y), next = field.sample(x + 1, y);
CHECK(std::abs(std::int64_t{here.x_q10} - next.x_q10) < 4 * um_per_s);
CHECK(std::abs(std::int64_t{here.y_q10} - next.y_q10) < 4 * um_per_s);
}
}
// A passive cell placed in a stream circulates for minutes without reaching a wall.
void passive_drift_circulates() {
const auto field = generate(v0(11));
const auto& start = field.routes().front().points.front();
constexpr std::uint32_t extent = 20;
CellularPosition position{static_cast<std::uint64_t>(start.x_um - extent / 2),
static_cast<std::uint64_t>(start.y_um - extent / 2)};
CellularMotionState state{};
const CellularHandling passive{.forward_drag_ppm = 95'163, .lateral_drag_ppm = 95'163};
std::uint64_t travelled{};
for (int tick = 0; tick < 20 * 60 * 5; ++tick) {
const auto next =
step_cellular_kinematics({60'000, 60'000}, position, extent, state, {}, passive,
field.at_footprint(position, extent), {});
CHECK(next.has_value());
travelled += next->path_um;
position = next->position;
state = next->state;
CHECK(position.x_um > 0 && position.y_um > 0 && position.x_um < 60'000 - extent &&
position.y_um < 60'000 - extent);
}
// Five minutes in a 60-300 um/s stream covers several centimetres.
CHECK(travelled > 10'000);
}
void invalid_configurations() {
auto bad = v0(1);
bad.pitch_um = 0;
CHECK(!CellularFlowField::generate(bad));
bad = v0(1);
bad.pitch_um = max_cellular_current_pitch_um + 1;
CHECK(!CellularFlowField::generate(bad));
bad = v0(1);
bad.min_count = 5;
CHECK(!CellularFlowField::generate(bad));
bad = v0(1);
bad.max_speed_um_per_s = 2'000'000;
CHECK(!CellularFlowField::generate(bad));
bad = v0(1);
bad.width_um = 3'000;
const auto tiny = CellularFlowField::generate(bad);
CHECK(!tiny && tiny.error() == CellularCurrentError::region_too_small);
auto none = v0(1);
none.min_count = none.max_count = 0;
const auto still = generate(none);
CHECK(still.routes().empty() && still.sample(30'000, 30'000) == CellularFlowSample{});
}
// A pool in a current moves to the nearest centre whose whole disc is still; one in still
// water stays.
void pools_rest_in_still_water() {
const auto field = generate(v0(7));
const auto disc_still = [&](CellularPatchAddress at, std::int64_t r) {
for (std::int64_t dx = -r; dx <= r; dx += r / 2)
for (std::int64_t dy = -r; dy <= r; dy += r / 2)
if (dx * dx + dy * dy <= r * r &&
field.sample(at.x_um + dx, at.y_um + dy) != CellularFlowSample{})
return false;
return true;
};
std::size_t moved{};
for (std::uint32_t x = 5'000; x <= 55'000; x += 5'000)
for (std::uint32_t y = 5'000; y <= 55'000; y += 5'000) {
const CellularPatchAddress at{x, y};
const auto rested = cellular_still_centre(field, at, 500);
if (field.sample(x, y) != CellularFlowSample{}) {
CHECK(rested != at && disc_still(rested, 500));
CHECK(rested.x_um >= 500 && rested.y_um >= 500 && rested.x_um <= 59'500 &&
rested.y_um <= 59'500);
++moved;
} else {
// Still at its centre: it stays, or moves only to clear a current at its rim.
CHECK(disc_still(rested, 500));
}
}
CHECK(moved != 0);
}
} // namespace
int main() {
try {
pools_rest_in_still_water();
routes_within_configuration();
bounded_with_quiet_water_and_still_walls();
route_centreline_runs_at_route_speed();
interpolation_is_continuous();
passive_drift_circulates();
invalid_configurations();
deterministic_generation();
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
return 1;
}
std::puts("cellular currents contracts passed");
return 0;
}