#include "evo/biology/cellular_motion.hpp" #include "evo/accounting/rates.hpp" #include namespace evo::biology { namespace { constexpr std::uint64_t rate_period_microseconds = 1'000'000; 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::uint64_t integer_sqrt_ceil(std::uint64_t value) noexcept { const auto floor = integer_sqrt(value); return floor * floor == value ? floor : floor + 1; } bool valid(CellularIntent intent) noexcept { if (intent.x < -cellular_intent_component_limit || intent.x > cellular_intent_component_limit || intent.y < -cellular_intent_component_limit || intent.y > cellular_intent_component_limit) return false; const auto x = static_cast(intent.x); const auto y = static_cast(intent.y); return x * x + y * y <= std::int64_t{cellular_intent_component_limit} * cellular_intent_component_limit; } std::expected movement_cost(std::uint64_t full_cost, std::uint64_t distance, std::uint64_t full_distance) noexcept { if (distance == 0 || full_cost == 0) return std::uint64_t{}; if (full_distance == 0 || distance > full_distance) return std::unexpected{CellularMotionError::overflow}; const auto whole = full_cost / full_distance; const auto remainder = full_cost % full_distance; const auto base = whole * distance; // <= full_cost by construction. const auto product = remainder * distance; // Both are bounded by uint32 speed per tick. const auto fractional = product / full_distance + (product % full_distance != 0 ? 1 : 0); if (fractional > std::numeric_limits::max() - base) return std::unexpected{CellularMotionError::overflow}; return base + fractional; } } // namespace std::expected propose_cellular_motion(CellularArea area, CellularPosition position, std::uint32_t extent_um, CellularIntent intent, const MetabolicBudget& budget) noexcept { if (area.width_um == 0 || area.height_um == 0 || area.width_um > cellular_motion_axis_limit_um || area.height_um > cellular_motion_axis_limit_um) return std::unexpected{CellularMotionError::invalid_area}; if (extent_um == 0 || extent_um > area.width_um || extent_um > area.height_um) return std::unexpected{CellularMotionError::invalid_extent}; if (position.x_um > area.width_um - extent_um || position.y_um > area.height_um - extent_um) return std::unexpected{CellularMotionError::outside_area}; if (!valid(intent)) return std::unexpected{CellularMotionError::invalid_intent}; if (budget.rate_microseconds != rate_period_microseconds) return std::unexpected{CellularMotionError::unsupported_rate_period}; const auto max_step = accounting::per_tick(budget.phenotype.movement_speed, cellular_motion_tick_microseconds, accounting::Rounding::down); const auto full_cost = accounting::per_tick( budget.movement.value, cellular_motion_tick_microseconds, accounting::Rounding::up); if (!max_step || !full_cost) return std::unexpected{CellularMotionError::overflow}; if (*max_step > static_cast(std::numeric_limits::max()) / cellular_intent_component_limit) return std::unexpected{CellularMotionError::overflow}; const auto dx = static_cast(intent.x) * static_cast(*max_step) / cellular_intent_component_limit; const auto dy = static_cast(intent.y) * static_cast(*max_step) / cellular_intent_component_limit; const auto max_x = area.width_um - extent_um; const auto max_y = area.height_um - extent_um; const auto clamp_axis = [](std::uint64_t coordinate, std::int64_t delta, std::uint64_t maximum) noexcept { if (delta < 0) { const auto distance = static_cast(-delta); if (distance > coordinate) return std::uint64_t{0}; return coordinate - distance; } const auto distance = static_cast(delta); if (distance > maximum - coordinate) return maximum; return coordinate + distance; }; const auto next_x = clamp_axis(position.x_um, dx, max_x); const auto next_y = clamp_axis(position.y_um, dy, max_y); const auto moved_x = next_x >= position.x_um ? next_x - position.x_um : position.x_um - next_x; const auto moved_y = next_y >= position.y_um ? next_y - position.y_um : position.y_um - next_y; const auto distance = integer_sqrt_ceil(moved_x * moved_x + moved_y * moved_y); const auto cost = movement_cost(*full_cost, distance, *max_step); if (!cost) return std::unexpected{cost.error()}; return CellularMotionProposal{{next_x, next_y}, distance, {*cost}}; } std::expected propose_funded_cellular_motion(CellularArea area, CellularPosition position, std::uint32_t extent_um, CellularIntent intent, const MetabolicBudget& budget, accounting::Inventory reserve, accounting::Inventory heat) noexcept { const auto motion = propose_cellular_motion(area, position, extent_um, intent, budget); if (!motion) return std::unexpected{motion.error()}; const auto cost = motion->movement_cost.value; if (reserve.energy.value < cost) return std::unexpected{CellularMotionError::insufficient_reserve}; if (heat.energy.value > std::numeric_limits::max() - cost) return std::unexpected{CellularMotionError::overflow}; reserve.energy.value -= cost; heat.energy.value += cost; return CellularMotionCandidate{motion->position, reserve, heat, motion->movement_cost}; } bool cellular_footprints_contact(CellularPosition a, std::uint32_t extent_a_um, CellularPosition b, std::uint32_t extent_b_um) noexcept { if (extent_a_um == 0 || extent_b_um == 0) return false; const auto axis_touches = [](std::uint64_t a_start, std::uint32_t a_extent, std::uint64_t b_start, std::uint32_t b_extent) { return a_start <= b_start ? b_start - a_start <= a_extent : a_start - b_start <= b_extent; }; return axis_touches(a.x_um, extent_a_um, b.x_um, extent_b_um) && axis_touches(a.y_um, extent_a_um, b.y_um, extent_b_um); } } // namespace evo::biology