feat(biology): procedural currents, habitat format 3 and moving food
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>
This commit is contained in:
@@ -0,0 +1,227 @@
|
||||
#include "evo/biology/cellular_currents.hpp"
|
||||
|
||||
#include "evo/core/random.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
|
||||
namespace evo::biology {
|
||||
namespace {
|
||||
constexpr std::int64_t per_mille = 1'000;
|
||||
constexpr std::int64_t trig = cellular_trig_one;
|
||||
// Low-frequency radius wobble, at most this share of the loop radius per harmonic.
|
||||
constexpr std::int64_t max_wobble_per_mille = 75;
|
||||
|
||||
std::uint64_t integer_sqrt(std::uint64_t value) noexcept {
|
||||
std::uint64_t result{}, bit = std::uint64_t{1} << 62U;
|
||||
while (bit > value)
|
||||
bit >>= 2U;
|
||||
while (bit != 0) {
|
||||
if (value >= result + bit) {
|
||||
value -= result + bit;
|
||||
result = (result >> 1U) + bit;
|
||||
} else {
|
||||
result >>= 1U;
|
||||
}
|
||||
bit >>= 2U;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Inclusive uniform draw; the modulo bias is far below anything gameplay can see and the
|
||||
// draw is deterministic either way.
|
||||
std::int64_t uniform(Random& random, std::int64_t low, std::int64_t high) noexcept {
|
||||
if (high <= low)
|
||||
return low;
|
||||
return low +
|
||||
static_cast<std::int64_t>(random.next() % static_cast<std::uint64_t>(high - low + 1));
|
||||
}
|
||||
|
||||
bool valid(const CellularCurrentConfig& c) noexcept {
|
||||
return c.width_um != 0 && c.height_um != 0 && c.width_um <= cellular_motion_axis_limit_um &&
|
||||
c.height_um <= cellular_motion_axis_limit_um && c.pitch_um != 0 &&
|
||||
c.pitch_um <= max_cellular_current_pitch_um && c.min_count <= c.max_count &&
|
||||
c.max_count <= 16 && c.min_route_width_um != 0 &&
|
||||
c.min_route_width_um <= c.max_route_width_um &&
|
||||
c.min_speed_um_per_s <= c.max_speed_um_per_s &&
|
||||
std::int64_t{c.max_speed_um_per_s} * cellular_velocity_q10_per_um_s <=
|
||||
cellular_velocity_limit_q10;
|
||||
}
|
||||
|
||||
std::expected<CellularCurrentRoute, CellularCurrentError>
|
||||
generate_route(Random& random, const CellularCurrentConfig& c) {
|
||||
CellularCurrentRoute route;
|
||||
route.width_um =
|
||||
static_cast<std::uint32_t>(uniform(random, c.min_route_width_um, c.max_route_width_um));
|
||||
route.speed_um_per_s =
|
||||
static_cast<std::uint32_t>(uniform(random, c.min_speed_um_per_s, c.max_speed_um_per_s));
|
||||
route.clockwise = (random.next() & 1U) != 0;
|
||||
// Keep the whole stream, wobble included, a grid cell clear of every wall.
|
||||
const std::int64_t margin = std::int64_t{route.width_um} / 2 + c.pitch_um;
|
||||
const std::int64_t span_x = std::int64_t{c.width_um} - 2 * margin;
|
||||
const std::int64_t span_y = std::int64_t{c.height_um} - 2 * margin;
|
||||
if (span_x < 8 || span_y < 8)
|
||||
return std::unexpected{CellularCurrentError::region_too_small};
|
||||
const auto radius_x = uniform(random, span_x / 8, span_x * 2 / 5);
|
||||
const auto radius_y = uniform(random, span_y / 8, span_y * 2 / 5);
|
||||
const std::int64_t wobble_limit = per_mille + 2 * max_wobble_per_mille;
|
||||
const auto reach_x = radius_x * wobble_limit / per_mille;
|
||||
const auto reach_y = radius_y * wobble_limit / per_mille;
|
||||
const auto centre_x = uniform(random, margin + reach_x, c.width_um - margin - reach_x);
|
||||
const auto centre_y = uniform(random, margin + reach_y, c.height_um - margin - reach_y);
|
||||
const auto wobble_2 = uniform(random, 0, max_wobble_per_mille);
|
||||
const auto wobble_3 = uniform(random, 0, max_wobble_per_mille);
|
||||
const auto phase_2 = static_cast<std::uint16_t>(random.next());
|
||||
const auto phase_3 = static_cast<std::uint16_t>(random.next());
|
||||
constexpr auto step = cellular_heading_turn / cellular_current_route_points;
|
||||
for (std::size_t k = 0; k < cellular_current_route_points; ++k) {
|
||||
const auto theta = static_cast<std::uint16_t>(k * step);
|
||||
const auto radius =
|
||||
per_mille +
|
||||
wobble_2 * cellular_cos_q14(static_cast<std::uint16_t>(2 * theta + phase_2)) / trig +
|
||||
wobble_3 * cellular_cos_q14(static_cast<std::uint16_t>(3 * theta + phase_3)) / trig;
|
||||
route.points.push_back(
|
||||
{centre_x + radius_x * radius * cellular_cos_q14(theta) / (per_mille * trig),
|
||||
centre_y + radius_y * radius * cellular_sin_q14(theta) / (per_mille * trig)});
|
||||
}
|
||||
if (route.clockwise)
|
||||
std::ranges::reverse(route.points);
|
||||
return route;
|
||||
}
|
||||
|
||||
struct Contribution {
|
||||
std::int64_t x{}, y{};
|
||||
bool inside{};
|
||||
};
|
||||
|
||||
// Velocity of one route at a point: along the nearest segment, full at the centreline and
|
||||
// falling to zero at the stream's edge.
|
||||
Contribution route_velocity(const CellularCurrentRoute& route, std::int64_t px,
|
||||
std::int64_t py) noexcept {
|
||||
std::int64_t best = std::numeric_limits<std::int64_t>::max();
|
||||
std::int64_t tangent_x{}, tangent_y{};
|
||||
const auto count = route.points.size();
|
||||
for (std::size_t index = 0; index < count; ++index) {
|
||||
const auto& a = route.points[index];
|
||||
const auto& b = route.points[(index + 1) % count];
|
||||
const auto abx = b.x_um - a.x_um, aby = b.y_um - a.y_um;
|
||||
const auto length_squared = abx * abx + aby * aby;
|
||||
if (length_squared == 0)
|
||||
continue;
|
||||
const auto along =
|
||||
std::clamp<std::int64_t>((px - a.x_um) * abx + (py - a.y_um) * aby, 0, length_squared);
|
||||
const auto dx = px - (a.x_um + abx * along / length_squared);
|
||||
const auto dy = py - (a.y_um + aby * along / length_squared);
|
||||
const auto distance_squared = dx * dx + dy * dy;
|
||||
if (distance_squared < best) {
|
||||
best = distance_squared;
|
||||
tangent_x = abx;
|
||||
tangent_y = aby;
|
||||
}
|
||||
}
|
||||
const std::int64_t half = route.width_um / 2;
|
||||
if (best >= half * half)
|
||||
return {};
|
||||
const auto profile = per_mille - best * per_mille / (half * half);
|
||||
const auto length = static_cast<std::int64_t>(
|
||||
integer_sqrt(static_cast<std::uint64_t>(tangent_x * tangent_x + tangent_y * tangent_y)));
|
||||
const auto speed = std::int64_t{route.speed_um_per_s} * cellular_velocity_q10_per_um_s;
|
||||
return {speed * profile / per_mille * tangent_x / length,
|
||||
speed * profile / per_mille * tangent_y / length, true};
|
||||
}
|
||||
|
||||
// Overlapping streams add, capped at the fastest contributing stream's speed.
|
||||
CellularFlowSample node_velocity(std::span<const CellularCurrentRoute> routes, std::int64_t x,
|
||||
std::int64_t y) noexcept {
|
||||
std::int64_t vx{}, vy{}, cap{};
|
||||
for (const auto& route : routes) {
|
||||
const auto part = route_velocity(route, x, y);
|
||||
if (!part.inside)
|
||||
continue;
|
||||
vx += part.x;
|
||||
vy += part.y;
|
||||
cap = std::max(cap, std::int64_t{route.speed_um_per_s} * cellular_velocity_q10_per_um_s);
|
||||
}
|
||||
const auto speed =
|
||||
static_cast<std::int64_t>(integer_sqrt(static_cast<std::uint64_t>(vx * vx + vy * vy)));
|
||||
if (speed > cap && speed > 0) {
|
||||
vx = vx * cap / speed;
|
||||
vy = vy * cap / speed;
|
||||
}
|
||||
return {static_cast<std::int32_t>(vx), static_cast<std::int32_t>(vy)};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<CellularFlowField, CellularCurrentError>
|
||||
CellularFlowField::generate(const CellularCurrentConfig& config) {
|
||||
if (!valid(config))
|
||||
return std::unexpected{CellularCurrentError::invalid_config};
|
||||
const auto columns = std::size_t{config.width_um} / config.pitch_um + 2;
|
||||
const auto rows = std::size_t{config.height_um} / config.pitch_um + 2;
|
||||
if (columns * rows > max_cellular_current_nodes)
|
||||
return std::unexpected{CellularCurrentError::invalid_config};
|
||||
CellularFlowField field;
|
||||
field.config_ = config;
|
||||
field.columns_ = columns;
|
||||
field.rows_ = rows;
|
||||
Random random{config.seed};
|
||||
const auto count = uniform(random, config.min_count, config.max_count);
|
||||
for (std::int64_t index = 0; index < count; ++index) {
|
||||
auto route = generate_route(random, config);
|
||||
if (!route)
|
||||
return std::unexpected{route.error()};
|
||||
field.routes_.push_back(std::move(*route));
|
||||
}
|
||||
field.nodes_.reserve(columns * rows);
|
||||
for (std::size_t row = 0; row < rows; ++row)
|
||||
for (std::size_t column = 0; column < columns; ++column)
|
||||
field.nodes_.push_back(
|
||||
node_velocity(field.routes_, static_cast<std::int64_t>(column * config.pitch_um),
|
||||
static_cast<std::int64_t>(row * config.pitch_um)));
|
||||
return field;
|
||||
}
|
||||
|
||||
CellularFlowSample CellularFlowField::sample(std::int64_t x_um, std::int64_t y_um) const noexcept {
|
||||
const std::int64_t pitch = config_.pitch_um;
|
||||
const auto x = std::clamp<std::int64_t>(x_um, 0, config_.width_um);
|
||||
const auto y = std::clamp<std::int64_t>(y_um, 0, config_.height_um);
|
||||
const auto column = static_cast<std::size_t>(x / pitch);
|
||||
const auto row = static_cast<std::size_t>(y / pitch);
|
||||
const auto fx = x % pitch, fy = y % pitch;
|
||||
const auto at = [this](std::size_t c, std::size_t r) -> const CellularFlowSample& {
|
||||
return nodes_[r * columns_ + c];
|
||||
};
|
||||
const auto& n00 = at(column, row);
|
||||
const auto& n10 = at(column + 1, row);
|
||||
const auto& n01 = at(column, row + 1);
|
||||
const auto& n11 = at(column + 1, row + 1);
|
||||
const auto blend = [&](std::int64_t v00, std::int64_t v10, std::int64_t v01, std::int64_t v11) {
|
||||
return (v00 * (pitch - fx) * (pitch - fy) + v10 * fx * (pitch - fy) +
|
||||
v01 * (pitch - fx) * fy + v11 * fx * fy) /
|
||||
(pitch * pitch);
|
||||
};
|
||||
return {static_cast<std::int32_t>(blend(n00.x_q10, n10.x_q10, n01.x_q10, n11.x_q10)),
|
||||
static_cast<std::int32_t>(blend(n00.y_q10, n10.y_q10, n01.y_q10, n11.y_q10))};
|
||||
}
|
||||
|
||||
CellularFlowSample CellularFlowField::at_footprint(CellularPosition position,
|
||||
std::uint32_t extent_um) const noexcept {
|
||||
return sample(static_cast<std::int64_t>(position.x_um + extent_um / 2),
|
||||
static_cast<std::int64_t>(position.y_um + extent_um / 2));
|
||||
}
|
||||
|
||||
std::uint64_t CellularFlowField::digest() const noexcept {
|
||||
std::uint64_t hash = 14695981039346656037ULL;
|
||||
const auto mix = [&hash](std::uint64_t value) {
|
||||
for (int byte = 0; byte < 8; ++byte) {
|
||||
hash = (hash ^ (value & 0xffU)) * 1099511628211ULL;
|
||||
value >>= 8U;
|
||||
}
|
||||
};
|
||||
for (const auto& node : nodes_) {
|
||||
mix(static_cast<std::uint32_t>(node.x_q10));
|
||||
mix(static_cast<std::uint32_t>(node.y_q10));
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
} // namespace evo::biology
|
||||
@@ -0,0 +1,279 @@
|
||||
#include "evo/biology/cellular_deposit_motion.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace evo::biology {
|
||||
namespace {
|
||||
constexpr std::int64_t carry_per_um = cellular_velocity_q10_per_um_s * 1'000'000;
|
||||
constexpr std::int64_t tick_microseconds = cellular_motion_tick_microseconds;
|
||||
// Induced drift closes this share of the gap to the pushing cells' velocity each tick.
|
||||
constexpr std::int64_t drift_blend_divisor = 4;
|
||||
|
||||
std::uint64_t integer_sqrt(std::uint64_t value) noexcept {
|
||||
std::uint64_t result{}, bit = std::uint64_t{1} << 62U;
|
||||
while (bit > value)
|
||||
bit >>= 2U;
|
||||
while (bit != 0) {
|
||||
if (value >= result + bit) {
|
||||
value -= result + bit;
|
||||
result = (result >> 1U) + bit;
|
||||
} else {
|
||||
result >>= 1U;
|
||||
}
|
||||
bit >>= 2U;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::int64_t floor_div(std::int64_t value, std::int64_t divisor) noexcept {
|
||||
const auto quotient = value / divisor;
|
||||
return value % divisor != 0 && value < 0 ? quotient - 1 : quotient;
|
||||
}
|
||||
|
||||
std::int64_t decay(std::int64_t v, std::uint32_t ppm) noexcept {
|
||||
if (v == 0 || ppm == 0)
|
||||
return v;
|
||||
const auto size = v < 0 ? -v : v;
|
||||
const auto loss = std::min<std::int64_t>(size, (size * ppm + cellular_drag_ppm_limit - 1) /
|
||||
cellular_drag_ppm_limit);
|
||||
return v < 0 ? v + loss : v - loss;
|
||||
}
|
||||
|
||||
bool add(accounting::Inventory& to, const accounting::Inventory& from) noexcept {
|
||||
constexpr auto top = std::numeric_limits<std::uint64_t>::max();
|
||||
if (from.energy.value > top - to.energy.value)
|
||||
return false;
|
||||
for (std::size_t channel = 0; channel < to.matter.size(); ++channel)
|
||||
if (from.matter[channel].value > top - to.matter[channel].value)
|
||||
return false;
|
||||
to.energy.value += from.energy.value;
|
||||
for (std::size_t channel = 0; channel < to.matter.size(); ++channel)
|
||||
to.matter[channel].value += from.matter[channel].value;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Fixed-size square buckets over the region; keys are row-major.
|
||||
struct Buckets {
|
||||
std::int64_t size{}, columns{};
|
||||
[[nodiscard]] std::int64_t key(std::int64_t x, std::int64_t y) const noexcept {
|
||||
return (y / size) * columns + x / size;
|
||||
}
|
||||
};
|
||||
|
||||
// (bucket, item) pairs sorted by bucket then item, so a bucket's items keep their order.
|
||||
std::vector<std::pair<std::int64_t, std::size_t>>
|
||||
bucketed(const Buckets& buckets, std::size_t count, const auto& position_of) {
|
||||
std::vector<std::pair<std::int64_t, std::size_t>> entries;
|
||||
entries.reserve(count);
|
||||
for (std::size_t index = 0; index < count; ++index) {
|
||||
const auto [x, y] = position_of(index);
|
||||
entries.emplace_back(buckets.key(x, y), index);
|
||||
}
|
||||
std::ranges::sort(entries);
|
||||
return entries;
|
||||
}
|
||||
|
||||
// Items in the 3 x 3 buckets around a point.
|
||||
void neighbours(const Buckets& buckets,
|
||||
const std::vector<std::pair<std::int64_t, std::size_t>>& entries, std::int64_t x,
|
||||
std::int64_t y, std::vector<std::size_t>& out) {
|
||||
out.clear();
|
||||
const auto column = x / buckets.size, row = y / buckets.size;
|
||||
for (std::int64_t r = row - 1; r <= row + 1; ++r) {
|
||||
for (std::int64_t c = column - 1; c <= column + 1; ++c) {
|
||||
if (r < 0 || c < 0 || c >= buckets.columns)
|
||||
continue;
|
||||
const auto key = r * buckets.columns + c;
|
||||
auto it = std::ranges::lower_bound(entries, std::pair{key, std::size_t{0}});
|
||||
for (; it != entries.end() && it->first == key; ++it)
|
||||
out.push_back(it->second);
|
||||
}
|
||||
}
|
||||
std::ranges::sort(out);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::uint32_t cellular_merged_radius(std::uint32_t bubble_radius_um,
|
||||
std::uint16_t merged) noexcept {
|
||||
const auto scale = integer_sqrt(std::uint64_t{merged} * 1'000'000);
|
||||
return static_cast<std::uint32_t>(std::min<std::uint64_t>(
|
||||
max_cellular_deposit_radius_um, std::uint64_t{bubble_radius_um} * scale / 1'000));
|
||||
}
|
||||
|
||||
std::expected<CellularDepositMotion, CellularHabitatError>
|
||||
advect_cellular_deposits(const CellularHabitatConfig& config, const CellularFlowField& field,
|
||||
std::span<const CellularWake> wakes,
|
||||
const CellularDepositMotionRules& rules) {
|
||||
if (config.format != cellular_habitat_liquid_format ||
|
||||
field.config().width_um != config.width_um ||
|
||||
field.config().height_um != config.height_um || rules.merged_cap == 0 ||
|
||||
rules.bubble_radius_um == 0 || rules.bubble_radius_um > max_cellular_deposit_radius_um ||
|
||||
rules.max_drift_q10 < 0 || rules.max_drift_q10 > cellular_velocity_limit_q10 ||
|
||||
rules.drift_decay_ppm >= cellular_drag_ppm_limit)
|
||||
return std::unexpected{CellularHabitatError::invalid_config};
|
||||
|
||||
CellularDepositMotion result{config, {}};
|
||||
auto& patches = result.config.patches;
|
||||
const std::int64_t width = config.width_um, height = config.height_um;
|
||||
|
||||
// Wakes indexed by drift radius, so each bubble checks only nearby cells.
|
||||
const std::int64_t radius = rules.drift_radius_um;
|
||||
const Buckets wake_buckets{std::max<std::int64_t>(radius, 1),
|
||||
width / std::max<std::int64_t>(radius, 1) + 1};
|
||||
const auto wake_centre = [&wakes](std::size_t index) {
|
||||
const auto& wake = wakes[index];
|
||||
return std::pair{static_cast<std::int64_t>(wake.position.x_um + wake.extent_um / 2),
|
||||
static_cast<std::int64_t>(wake.position.y_um + wake.extent_um / 2)};
|
||||
};
|
||||
const auto wake_index = radius > 0 ? bucketed(wake_buckets, wakes.size(), wake_centre)
|
||||
: std::vector<std::pair<std::int64_t, std::size_t>>{};
|
||||
std::vector<std::size_t> nearby;
|
||||
|
||||
for (auto& patch : patches) {
|
||||
if (patch.kind != CellularDepositKind::mote)
|
||||
continue;
|
||||
const std::int64_t x = patch.address.x_um, y = patch.address.y_um;
|
||||
std::int64_t drift_x = patch.drift_x_q10, drift_y = patch.drift_y_q10;
|
||||
if (radius > 0) {
|
||||
neighbours(wake_buckets, wake_index, x, y, nearby);
|
||||
std::int64_t target_x{}, target_y{};
|
||||
bool pushed = false;
|
||||
for (const auto index : nearby) {
|
||||
const auto [cx, cy] = wake_centre(index);
|
||||
const auto dx = x - cx, dy = y - cy;
|
||||
const auto distance_squared = dx * dx + dy * dy;
|
||||
if (distance_squared >= radius * radius)
|
||||
continue;
|
||||
const auto distance = static_cast<std::int64_t>(
|
||||
integer_sqrt(static_cast<std::uint64_t>(distance_squared)));
|
||||
target_x +=
|
||||
std::int64_t{wakes[index].velocity_x_q10} * (radius - distance) / radius;
|
||||
target_y +=
|
||||
std::int64_t{wakes[index].velocity_y_q10} * (radius - distance) / radius;
|
||||
pushed = true;
|
||||
}
|
||||
if (pushed) {
|
||||
drift_x += (target_x - drift_x) / drift_blend_divisor;
|
||||
drift_y += (target_y - drift_y) / drift_blend_divisor;
|
||||
}
|
||||
const auto speed = static_cast<std::int64_t>(
|
||||
integer_sqrt(static_cast<std::uint64_t>(drift_x * drift_x + drift_y * drift_y)));
|
||||
if (speed > rules.max_drift_q10 && speed > 0) {
|
||||
drift_x = drift_x * rules.max_drift_q10 / speed;
|
||||
drift_y = drift_y * rules.max_drift_q10 / speed;
|
||||
}
|
||||
}
|
||||
const auto flow = field.sample(x, y);
|
||||
auto carry_x = std::int64_t{patch.carry_x} + (flow.x_q10 + drift_x) * tick_microseconds;
|
||||
auto carry_y = std::int64_t{patch.carry_y} + (flow.y_q10 + drift_y) * tick_microseconds;
|
||||
const auto step_x = floor_div(carry_x, carry_per_um);
|
||||
const auto step_y = floor_div(carry_y, carry_per_um);
|
||||
carry_x -= step_x * carry_per_um;
|
||||
carry_y -= step_y * carry_per_um;
|
||||
auto next_x = x + step_x, next_y = y + step_y;
|
||||
// Region edges stop a bubble; its carry and drift into the edge are dropped.
|
||||
if (next_x < 0 || next_x > width - 1) {
|
||||
next_x = std::clamp<std::int64_t>(next_x, 0, width - 1);
|
||||
carry_x = 0;
|
||||
drift_x = 0;
|
||||
}
|
||||
if (next_y < 0 || next_y > height - 1) {
|
||||
next_y = std::clamp<std::int64_t>(next_y, 0, height - 1);
|
||||
carry_y = 0;
|
||||
drift_y = 0;
|
||||
}
|
||||
if (next_x != x || next_y != y)
|
||||
++result.report.moved;
|
||||
patch.address = {static_cast<std::uint32_t>(next_x), static_cast<std::uint32_t>(next_y)};
|
||||
patch.carry_x = static_cast<std::uint32_t>(carry_x);
|
||||
patch.carry_y = static_cast<std::uint32_t>(carry_y);
|
||||
patch.drift_x_q10 = static_cast<std::int32_t>(decay(drift_x, rules.drift_decay_ppm));
|
||||
patch.drift_y_q10 = static_cast<std::int32_t>(decay(drift_y, rules.drift_decay_ppm));
|
||||
}
|
||||
|
||||
// Merge touching bubbles in ascending id: the lower id absorbs the higher one.
|
||||
std::vector<std::size_t> motes;
|
||||
for (std::size_t index = 0; index < patches.size(); ++index)
|
||||
if (patches[index].kind == CellularDepositKind::mote)
|
||||
motes.push_back(index);
|
||||
std::ranges::sort(motes, {}, [&patches](std::size_t index) { return patches[index].id; });
|
||||
const auto largest = static_cast<std::int64_t>(
|
||||
std::max(cellular_merged_radius(rules.bubble_radius_um, rules.merged_cap),
|
||||
max_cellular_deposit_radius_um / 8));
|
||||
const Buckets merge_buckets{2 * largest, width / (2 * largest) + 1};
|
||||
const auto mote_index = bucketed(merge_buckets, motes.size(), [&](std::size_t order) {
|
||||
const auto& patch = patches[motes[order]];
|
||||
return std::pair{static_cast<std::int64_t>(patch.address.x_um),
|
||||
static_cast<std::int64_t>(patch.address.y_um)};
|
||||
});
|
||||
std::vector<bool> absorbed(motes.size(), false);
|
||||
for (std::size_t order = 0; order < motes.size(); ++order) {
|
||||
if (absorbed[order])
|
||||
continue;
|
||||
auto& keeper = patches[motes[order]];
|
||||
neighbours(merge_buckets, mote_index, keeper.address.x_um, keeper.address.y_um, nearby);
|
||||
for (const auto other : nearby) {
|
||||
if (other <= order || absorbed[other] || keeper.merged >= rules.merged_cap)
|
||||
continue;
|
||||
const auto& taken = patches[motes[other]];
|
||||
if (std::uint32_t{keeper.merged} + taken.merged > rules.merged_cap)
|
||||
continue;
|
||||
const auto dx = std::int64_t{keeper.address.x_um} - taken.address.x_um;
|
||||
const auto dy = std::int64_t{keeper.address.y_um} - taken.address.y_um;
|
||||
const auto reach = std::int64_t{keeper.radius_um} + taken.radius_um;
|
||||
if (dx * dx + dy * dy > reach * reach)
|
||||
continue;
|
||||
if (!add(keeper.stock, taken.stock))
|
||||
return std::unexpected{CellularHabitatError::overflow};
|
||||
const std::int64_t mass = keeper.merged, more = taken.merged;
|
||||
keeper.drift_x_q10 = static_cast<std::int32_t>(
|
||||
(keeper.drift_x_q10 * mass + taken.drift_x_q10 * more) / (mass + more));
|
||||
keeper.drift_y_q10 = static_cast<std::int32_t>(
|
||||
(keeper.drift_y_q10 * mass + taken.drift_y_q10 * more) / (mass + more));
|
||||
keeper.merged = static_cast<std::uint16_t>(mass + more);
|
||||
keeper.radius_um =
|
||||
std::max({keeper.radius_um, taken.radius_um,
|
||||
cellular_merged_radius(rules.bubble_radius_um, keeper.merged)});
|
||||
absorbed[other] = true;
|
||||
++result.report.merges;
|
||||
}
|
||||
}
|
||||
if (result.report.merges != 0) {
|
||||
std::vector<bool> removed(patches.size(), false);
|
||||
for (std::size_t order = 0; order < motes.size(); ++order)
|
||||
if (absorbed[order])
|
||||
removed[motes[order]] = true;
|
||||
std::vector<CellularPatch> kept;
|
||||
kept.reserve(patches.size() - result.report.merges);
|
||||
for (std::size_t index = 0; index < patches.size(); ++index)
|
||||
if (!removed[index])
|
||||
kept.push_back(patches[index]);
|
||||
patches = std::move(kept);
|
||||
}
|
||||
|
||||
// Addresses are unique: a later id sharing an address steps one micrometre along x.
|
||||
const auto order = [](const CellularPatch& a, const CellularPatch& b) {
|
||||
return std::pair{a.address, a.id} < std::pair{b.address, b.id};
|
||||
};
|
||||
std::ranges::sort(patches, order);
|
||||
for (bool clash = true; clash;) {
|
||||
clash = false;
|
||||
for (std::size_t index = 1; index < patches.size(); ++index) {
|
||||
if (patches[index].address != patches[index - 1].address)
|
||||
continue;
|
||||
auto& moved = patches[index].kind == CellularDepositKind::pool ? patches[index - 1]
|
||||
: patches[index];
|
||||
moved.address.x_um = moved.address.x_um + 1 < config.width_um ? moved.address.x_um + 1
|
||||
: moved.address.x_um - 1;
|
||||
++result.report.nudged;
|
||||
clash = true;
|
||||
}
|
||||
if (clash)
|
||||
std::ranges::sort(patches, order);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
} // namespace evo::biology
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "evo/biology/cellular_habitat.hpp"
|
||||
|
||||
#include "evo/biology/cellular_kinematics.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <limits>
|
||||
@@ -96,12 +98,16 @@ CellularHabitat::create(CellularHabitatConfig config) {
|
||||
config.height_um > cellular_motion_axis_limit_um ||
|
||||
config.width_um % cellular_patch_pitch_um != 0 ||
|
||||
config.height_um % cellular_patch_pitch_um != 0 ||
|
||||
config.patches.size() > max_cellular_patches ||
|
||||
(config.format != cellular_habitat_format &&
|
||||
config.format != cellular_habitat_deposit_format) ||
|
||||
config.format != cellular_habitat_deposit_format &&
|
||||
config.format != cellular_habitat_liquid_format) ||
|
||||
config.patches.size() > (config.format == cellular_habitat_liquid_format
|
||||
? max_cellular_liquid_deposits
|
||||
: max_cellular_patches) ||
|
||||
(config.format == cellular_habitat_format && config.next_deposit != 0))
|
||||
return std::unexpected{CellularHabitatError::invalid_config};
|
||||
const bool deposits = config.format == cellular_habitat_deposit_format;
|
||||
const bool liquid = config.format == cellular_habitat_liquid_format;
|
||||
const bool deposits = config.format == cellular_habitat_deposit_format || liquid;
|
||||
|
||||
accounting::Inventory totals{};
|
||||
std::vector<std::uint32_t> ids;
|
||||
@@ -121,6 +127,19 @@ CellularHabitat::create(CellularHabitatConfig config) {
|
||||
patch.radius_um > max_cellular_deposit_radius_um ||
|
||||
(patch.kind != CellularDepositKind::pool && patch.kind != CellularDepositKind::mote)))
|
||||
return std::unexpected{CellularHabitatError::invalid_patch};
|
||||
const bool moving = patch.drift_x_q10 != 0 || patch.drift_y_q10 != 0 ||
|
||||
patch.carry_x != 0 || patch.carry_y != 0;
|
||||
// Format 3: bounded drift and carry on bubbles only; every deposit counts at least one
|
||||
// merged bubble. Earlier formats carry none of these fields.
|
||||
if (liquid ? (patch.merged == 0 || patch.carry_x >= cellular_deposit_carry_limit ||
|
||||
patch.carry_y >= cellular_deposit_carry_limit ||
|
||||
patch.drift_x_q10 < -cellular_velocity_limit_q10 ||
|
||||
patch.drift_x_q10 > cellular_velocity_limit_q10 ||
|
||||
patch.drift_y_q10 < -cellular_velocity_limit_q10 ||
|
||||
patch.drift_y_q10 > cellular_velocity_limit_q10 ||
|
||||
(patch.kind == CellularDepositKind::pool && (moving || patch.merged != 1)))
|
||||
: (moving || patch.merged != 0))
|
||||
return std::unexpected{CellularHabitatError::invalid_patch};
|
||||
if (deposits)
|
||||
ids.push_back(patch.id);
|
||||
if (index != 0 && !(config.patches[index - 1].address < patch.address))
|
||||
@@ -237,7 +256,8 @@ std::vector<std::uint8_t> CellularHabitat::snapshot() const {
|
||||
cellular_patch_record_bytes * config_.patches.size() + 8);
|
||||
Writer out{bytes};
|
||||
out.put(habitat_magic);
|
||||
const bool deposits = config_.format == cellular_habitat_deposit_format;
|
||||
const bool liquid = config_.format == cellular_habitat_liquid_format;
|
||||
const bool deposits = config_.format == cellular_habitat_deposit_format || liquid;
|
||||
out.put(config_.format, 4);
|
||||
out.put(habitat_rules, 4);
|
||||
out.put(config_.world);
|
||||
@@ -258,6 +278,14 @@ std::vector<std::uint8_t> CellularHabitat::snapshot() const {
|
||||
out.put(static_cast<std::uint8_t>(patch.kind), 1);
|
||||
out.put(0, 1);
|
||||
}
|
||||
if (liquid) {
|
||||
out.put(static_cast<std::uint32_t>(patch.drift_x_q10), 4);
|
||||
out.put(static_cast<std::uint32_t>(patch.drift_y_q10), 4);
|
||||
out.put(patch.carry_x, 4);
|
||||
out.put(patch.carry_y, 4);
|
||||
out.put(patch.merged, 2);
|
||||
out.put(0, 2);
|
||||
}
|
||||
out.put(patch.source);
|
||||
out.inventory(patch.stock);
|
||||
}
|
||||
@@ -278,10 +306,12 @@ CellularHabitat::restore(std::span<const std::uint8_t> bytes, std::uint64_t worl
|
||||
if (in.get() != habitat_magic)
|
||||
return std::unexpected{CellularHabitatError::format};
|
||||
const auto format = in.get(4);
|
||||
if ((format != cellular_habitat_format && format != cellular_habitat_deposit_format) ||
|
||||
if ((format != cellular_habitat_format && format != cellular_habitat_deposit_format &&
|
||||
format != cellular_habitat_liquid_format) ||
|
||||
in.get(4) != habitat_rules)
|
||||
return std::unexpected{CellularHabitatError::version};
|
||||
const bool deposits = format == cellular_habitat_deposit_format;
|
||||
const bool liquid = format == cellular_habitat_liquid_format;
|
||||
const bool deposits = format == cellular_habitat_deposit_format || liquid;
|
||||
CellularHabitatConfig config;
|
||||
config.format = static_cast<std::uint32_t>(format);
|
||||
config.world = in.get();
|
||||
@@ -295,8 +325,10 @@ CellularHabitat::restore(std::span<const std::uint8_t> bytes, std::uint64_t worl
|
||||
return std::unexpected{CellularHabitatError::version};
|
||||
const auto count = in.get(4);
|
||||
const auto next_deposit = in.get();
|
||||
const auto record = deposits ? cellular_deposit_record_bytes : cellular_patch_record_bytes;
|
||||
if (in.get() != 0 || count > max_cellular_patches ||
|
||||
const auto record = liquid ? cellular_liquid_record_bytes
|
||||
: deposits ? cellular_deposit_record_bytes
|
||||
: cellular_patch_record_bytes;
|
||||
if (in.get() != 0 || count > (liquid ? max_cellular_liquid_deposits : max_cellular_patches) ||
|
||||
next_deposit > std::numeric_limits<std::uint32_t>::max() ||
|
||||
bytes.size() != cellular_habitat_header_bytes + record * count + 8)
|
||||
return std::unexpected{CellularHabitatError::format};
|
||||
@@ -314,6 +346,15 @@ CellularHabitat::restore(std::span<const std::uint8_t> bytes, std::uint64_t worl
|
||||
return std::unexpected{CellularHabitatError::format};
|
||||
patch.kind = static_cast<CellularDepositKind>(kind);
|
||||
}
|
||||
if (liquid) {
|
||||
patch.drift_x_q10 = static_cast<std::int32_t>(static_cast<std::uint32_t>(in.get(4)));
|
||||
patch.drift_y_q10 = static_cast<std::int32_t>(static_cast<std::uint32_t>(in.get(4)));
|
||||
patch.carry_x = static_cast<std::uint32_t>(in.get(4));
|
||||
patch.carry_y = static_cast<std::uint32_t>(in.get(4));
|
||||
patch.merged = static_cast<std::uint16_t>(in.get(2));
|
||||
if (in.get(2) != 0)
|
||||
return std::unexpected{CellularHabitatError::format};
|
||||
}
|
||||
patch.source = in.get();
|
||||
patch.stock = in.inventory();
|
||||
config.patches.push_back(patch);
|
||||
|
||||
@@ -43,7 +43,7 @@ constexpr std::array fields{
|
||||
speed_limit},
|
||||
Field{"currents.max_speed_um_per_s", &CellularTuning::currents_max_speed_um_per_s, 0,
|
||||
speed_limit},
|
||||
Field{"currents.grid_pitch_um", &CellularTuning::currents_grid_pitch_um, 10, 100'000},
|
||||
Field{"currents.grid_pitch_um", &CellularTuning::currents_grid_pitch_um, 10, 10'000},
|
||||
Field{"food.merged_bubble_cap_multiple", &CellularTuning::food_merged_bubble_cap_multiple, 1,
|
||||
1'000},
|
||||
Field{"food.visible_moving_bubbles", &CellularTuning::food_visible_moving_bubbles, 0, 512},
|
||||
|
||||
Reference in New Issue
Block a user