#include "evo/biology/cellular_step.hpp" #include "evo/accounting/rates.hpp" #include "evo/biology/cellular_motion.hpp" #include namespace evo::biology { namespace { constexpr std::uint64_t rate_period_microseconds = 1'000'000; bool credit_heat(accounting::Inventory& heat, std::uint64_t amount) noexcept { if (heat.energy.value > std::numeric_limits::max() - amount) return false; heat.energy.value += amount; return true; } } // namespace std::expected propose_cellular_step(std::uint64_t begin_tick, CellularArea area, CellularPosition position, std::uint32_t extent_um, CellularStepIntent intent, accounting::Inventory reserve, accounting::Inventory heat, const MetabolicBudget& budget, const CellularHabitat& habitat) noexcept { if (begin_tick == std::numeric_limits::max()) return std::unexpected{CellularStepError::invalid_tick}; if (budget.rate_microseconds != rate_period_microseconds) return std::unexpected{CellularStepError::unsupported_rate_period}; if (area.width_um != habitat.config().width_um || area.height_um != habitat.config().height_um) return std::unexpected{CellularStepError::motion}; const auto maintenance = accounting::per_tick( budget.maintenance.value, cellular_motion_tick_microseconds, accounting::Rounding::up); if (!maintenance) return std::unexpected{CellularStepError::overflow}; if (reserve.energy.value < *maintenance) return std::unexpected{CellularStepError::insufficient_maintenance}; if (!credit_heat(heat, *maintenance)) return std::unexpected{CellularStepError::overflow}; reserve.energy.value -= *maintenance; const auto original_position = position; const bool movement_requested = intent.movement.x != 0 || intent.movement.y != 0; const auto movement = propose_funded_cellular_motion(area, position, extent_um, intent.movement, budget, reserve, heat); if (!movement && movement.error() != CellularMotionError::insufficient_reserve) return std::unexpected{CellularStepError::motion}; if (movement) { position = movement->position; reserve = movement->reserve_after; heat = movement->heat_after; } CellularStepCandidate candidate{begin_tick, begin_tick + 1, position, reserve, heat, {*maintenance}, {}, CellularMovementStatus::not_requested, CellularIntakeStatus::not_requested, {}}; if (!movement_requested) { candidate.movement_status = CellularMovementStatus::not_requested; } else if (!movement) { candidate.movement_status = CellularMovementStatus::insufficient_reserve; } else if (movement->position == original_position) { candidate.movement_status = CellularMovementStatus::blocked; } else { candidate.movement_status = CellularMovementStatus::moved; } if (movement) { candidate.movement_cost = movement->movement_cost; } if (!intent.intake_target) return candidate; const auto intake = habitat.propose_uptake(*intent.intake_target, position, extent_um, reserve, budget); if (!intake) { if (intake.error() == CellularHabitatError::missing_patch) { candidate.intake_status = CellularIntakeStatus::missing; return candidate; } if (intake.error() == CellularHabitatError::not_in_contact) { candidate.intake_status = CellularIntakeStatus::out_of_contact; return candidate; } return std::unexpected{intake.error() == CellularHabitatError::uptake ? CellularStepError::overflow : CellularStepError::motion}; } candidate.reserve_after = intake->transfer.reserve_after; candidate.intake_status = CellularIntakeStatus::acquired; candidate.intake = *intake; return candidate; } std::expected propose_liquid_cellular_step( std::uint64_t begin_tick, CellularArea area, CellularPosition position, std::uint32_t extent_um, CellularIntent movement, const CellularLiquidInput& liquid, accounting::Inventory reserve, accounting::Inventory heat, const MetabolicBudget& budget, const CellularBurn* burn) noexcept { if (begin_tick == std::numeric_limits::max()) return std::unexpected{CellularStepError::invalid_tick}; if (budget.rate_microseconds != rate_period_microseconds) return std::unexpected{CellularStepError::unsupported_rate_period}; const auto maintenance = accounting::per_tick( budget.maintenance.value, cellular_motion_tick_microseconds, accounting::Rounding::up); if (!maintenance) return std::unexpected{CellularStepError::overflow}; accounting::Inventory spent{}; // Energy goes to heat; matter standing in for it goes to the spent pool (rules 13). const auto pay = [&](std::uint64_t cost, bool upkeep) { if (burn == nullptr) { if (reserve.energy.value < cost) return false; reserve.energy.value -= cost; return credit_heat(heat, cost); } const auto paid = cellular_pay(reserve, cost, *burn, upkeep); if (!paid) return false; for (std::size_t c = 0; c < spent.matter.size(); ++c) spent.matter[c].value += paid->spent.matter[c].value; return credit_heat(heat, paid->energy); }; if (!pay(*maintenance, true)) return std::unexpected{CellularStepError::insufficient_maintenance}; // Thrust may draw on unexcluded matter too, but never on excluded matter. const accounting::Picojoules offered{ burn == nullptr ? reserve.energy.value : cellular_payable_energy(reserve, *burn, false)}; const auto motion = step_cellular_kinematics(area, position, extent_um, liquid.motion, {movement, liquid.brake}, liquid.handling, liquid.flow, offered); if (!motion) return std::unexpected{CellularStepError::motion}; if (!pay(motion->cost.value, false)) return std::unexpected{CellularStepError::overflow}; CellularStepCandidate candidate{}; candidate.begin_tick = begin_tick; candidate.end_tick = begin_tick + 1; candidate.position = motion->position; candidate.reserve_after = reserve; candidate.heat_after = heat; candidate.maintenance_cost = {*maintenance}; candidate.movement_cost = motion->cost; candidate.motion = motion->state; candidate.path_um = motion->path_um; candidate.spent = spent; switch (motion->thrust) { case CellularThrustStatus::idle: candidate.movement_status = CellularMovementStatus::not_requested; break; case CellularThrustStatus::unfunded: candidate.movement_status = CellularMovementStatus::insufficient_reserve; break; case CellularThrustStatus::applied: // Blocked only when an edge stopped it; a sub-micrometre start is still moving. candidate.movement_status = motion->position == position && motion->state.velocity_x_q10 == 0 && motion->state.velocity_y_q10 == 0 ? CellularMovementStatus::blocked : CellularMovementStatus::moved; break; } return candidate; } } // namespace evo::biology