#include "evo/biology/cellular_region_step.hpp" #include #include #include #include #include #include #include #include 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 liquid step line " + std::to_string(at.line())); } #define CHECK(...) check(static_cast(__VA_ARGS__)) constexpr CellularArea area{60'000, 60'000}; constexpr std::int32_t um_per_s = 1'024; constexpr CellularRegionRules rules{.engulf_and_detritus = true, .contact_feeding = true}; MetabolicBudget budget() { MetabolicBudget value{}; value.rate_microseconds = 1'000'000; value.maintenance = {20}; // One picojoule per tick. value.intake_limit = {{1'000'000}, {{{1'000'000}, {1'000'000}, {1'000'000}, {1'000'000}}}}; value.phenotype.total_mass = 1'000; return value; } CellularHandling handling() { return {.thrust_q10 = 20'480, .reverse_thrust_q10 = 20'480, .forward_drag_ppm = 95'163, .lateral_drag_ppm = 95'163, .turn = 3'277, .thrust_cost = {10}, .reverse_cost = {10}}; } CellularPatch bubble(std::uint32_t id, std::uint32_t x, std::uint32_t y) { CellularPatch patch{ {x, y}, 1, {{50}, {}}, id, cellular_mote_radius_um, CellularDepositKind::mote}; patch.merged = 1; return patch; } std::unique_ptr liquid_habitat(std::vector patches = {}) { std::uint32_t next = 1; for (const auto& patch : patches) next = std::max(next, patch.id + 1); auto made = CellularHabitat::create( {100, 1, 9, 60'000, 60'000, std::move(patches), cellular_habitat_liquid_format, next}); check(made.has_value()); return std::move(*made); } CellularLifeStepInput life(std::uint64_t id, CellularPosition position, CellularIntent movement, Inventory reserve = {{1'000}, {}}, CellularLiquidInput liquid = {{}, handling(), {}, false}) { CellularLifeStepInput input{}; input.life_id = id; input.position = position; input.extent_um = 20; input.intent.movement = movement; input.body = {{0}, {{{5}, {5}, {5}, {5}}}}; input.reserve = reserve; input.budget = budget(); input.membrane_um = 1; input.source = 7; input.liquid = liquid; return input; } void propulsion_moves_and_pays() { const auto map = liquid_habitat(); const std::array lives{life(1, {30'000, 30'000}, {1'000, 0})}; const auto tick = propose_cellular_region_step(4, area, lives, *map, rules); CHECK(tick && tick->lives.size() == 1); const auto& step = tick->lives[0].candidate; CHECK(step.movement_status == CellularMovementStatus::moved); CHECK(step.motion.velocity_x_q10 > 0 && step.position.x_um == 30'000); // First tick: 20 um/s * 50 ms leaves sub-micrometre carry; maintenance 1 + thrust 10. CHECK(step.motion.remainder_x > 0); CHECK(step.maintenance_cost.value == 1 && step.movement_cost.value == 10); CHECK(step.reserve_after.energy.value == 989 && step.heat_after.energy.value == 11); } void unfunded_thrust_coasts_with_the_flow() { const auto map = liquid_habitat(); // Enough for maintenance, not for thrust; a 100 um/s current still carries the cell. const auto broke = life(1, {30'000, 30'000}, {1'000, 0}, {{5}, {}}, {{}, handling(), {100 * um_per_s, 0}, false}); const std::array lives{broke}; const auto tick = propose_cellular_region_step(4, area, lives, *map, rules); CHECK(tick && tick->lives.size() == 1); const auto& step = tick->lives[0].candidate; CHECK(step.movement_status == CellularMovementStatus::insufficient_reserve); CHECK(step.movement_cost.value == 0 && step.reserve_after.energy.value == 4); CHECK(step.position.x_um == 30'005); } void fast_cells_feed_along_their_path() { // A bubble lies between where a very fast cell starts and where it ends this tick. const auto map = liquid_habitat({bubble(1, 30'040, 30'010)}); const auto moving = life(1, {30'000, 30'000}, {}, {{1'000}, {}}, {{.velocity_x_q10 = 2'000 * um_per_s}, handling(), {1'000 * um_per_s, 0}, false}); const std::array lives{moving}; const auto tick = propose_cellular_region_step(4, area, lives, *map, rules); CHECK(tick && tick->lives.size() == 1); const auto& step = tick->lives[0].candidate; // It ends well past the bubble's reach, yet it fed on the way. CHECK(step.position.x_um > 30'040 + cellular_mote_radius_um + 20); CHECK(step.intake_status == CellularIntakeStatus::acquired); CHECK(tick->patches.size() == 1 && tick->patches[0].stock == Inventory{}); } void starved_cells_leave_merged_bubbles() { const auto map = liquid_habitat(); auto starving = life(1, {30'000, 30'000}, {}, {{0}, {}}); starving.body = {{0}, {{{3}, {0}, {2}, {0}}}}; const std::array lives{starving}; const auto tick = propose_cellular_region_step(4, area, lives, *map, rules); CHECK(tick && tick->lives.empty() && tick->starved.size() == 1); CHECK(tick->new_patches.size() == 2); for (const auto& patch : tick->new_patches) CHECK(patch.kind == CellularDepositKind::mote && patch.merged == 1); } void frozen_cells_keep_their_motion() { const auto map = liquid_habitat(); const CellularMotionState held{.velocity_x_q10 = 50 * um_per_s, .heading = 99}; auto frozen = life(1, {30'000, 30'000}, {}, {{1'000}, {}}, {held, handling(), {200 * um_per_s, 0}, false}); frozen.intent.frozen = true; const std::array lives{frozen}; const auto tick = propose_cellular_region_step(4, area, lives, *map, rules); CHECK(tick && tick->lives.size() == 1); const auto& step = tick->lives[0].candidate; CHECK(step.position == CellularPosition{30'000, 30'000}); CHECK(step.motion == held && step.reserve_after.energy.value == 1'000); } void liquid_and_format_must_match() { const std::array liquid_life{life(1, {1'000, 1'000}, {})}; auto old = CellularHabitat::create( {100, 1, 9, 60'000, 60'000, {}, cellular_habitat_deposit_format, 1}); CHECK(old); const auto mismatched = propose_cellular_region_step(4, area, liquid_life, **old, rules); CHECK(!mismatched && mismatched.error() == CellularRegionStepError::invalid_intent); auto plain = life(1, {1'000, 1'000}, {}); plain.liquid.reset(); const std::array plain_life{plain}; const auto missing = propose_cellular_region_step(4, area, plain_life, *liquid_habitat(), rules); CHECK(!missing && missing.error() == CellularRegionStepError::invalid_intent); } } // namespace int main() { try { propulsion_moves_and_pays(); unfunded_thrust_coasts_with_the_flow(); fast_cells_feed_along_their_path(); starved_cells_leave_merged_bubbles(); frozen_cells_keep_their_motion(); liquid_and_format_must_match(); } catch (const std::exception& error) { std::fprintf(stderr, "%s\n", error.what()); return 1; } std::puts("cellular liquid step contracts passed"); return 0; }