Phase 2 biology. CellularFlowField generates 2-n seeded closed looping currents inside the region, each with its own width, speed and direction, strongest at the centreline and still at the stream edges and walls, rasterized to a Q10 grid with integer bilinear sampling. The field digest matches across g++-14, clang-19 and MSVC, so clients regenerate it from the configuration. Habitat format 3 adds per-deposit induced drift, sub-micrometre carry and a merged count, with up to 8,192 deposits. advect_cellular_deposits moves bubbles with the flow plus drift from passing cells (falloff and decay), merges touching bubbles into the lower id up to the merged cap, keeps pools fixed and addresses unique. A 2,000-tick run of 1,800 bubbles conserves stock exactly and leaves nothing piled at the walls; it costs under 0.7 ms per tick optimized. The tuned grid pitch is capped at 10 mm. Refreshes the DEV session window. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
226 lines
9.1 KiB
C++
226 lines
9.1 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));
|
|
}
|
|
|
|
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();
|
|
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;
|
|
}
|