#include "evo/biology/cellular_uptake.hpp" #include "evo/accounting/rates.hpp" #include "evo/biology/cellular_motion.hpp" #include #include namespace evo::biology { namespace { constexpr std::uint64_t rate_period_microseconds = 1'000'000; std::uint64_t take(std::uint64_t stock, std::uint64_t reserve, std::uint64_t rate_limit) noexcept { const auto room = std::numeric_limits::max() - reserve; return std::min({stock, rate_limit, room}); } } // namespace std::expected propose_cellular_uptake(accounting::Inventory patch, accounting::Inventory reserve, const MetabolicBudget& budget) noexcept { if (budget.rate_microseconds != rate_period_microseconds) return std::unexpected{CellularUptakeError::unsupported_rate_period}; const auto energy_limit = accounting::per_tick(budget.intake_limit.energy.value, cellular_motion_tick_microseconds, accounting::Rounding::down); if (!energy_limit) return std::unexpected{CellularUptakeError::overflow}; CellularUptakeProposal proposal{patch, reserve, {}}; const auto energy = take(patch.energy.value, reserve.energy.value, *energy_limit); proposal.acquired.energy.value = energy; proposal.patch_after.energy.value -= energy; proposal.reserve_after.energy.value += energy; for (std::size_t channel = 0; channel < proposal.acquired.matter.size(); ++channel) { const auto limit = accounting::per_tick(budget.intake_limit.matter[channel].value, cellular_motion_tick_microseconds, accounting::Rounding::down); if (!limit) return std::unexpected{CellularUptakeError::overflow}; const auto amount = take(patch.matter[channel].value, reserve.matter[channel].value, *limit); proposal.acquired.matter[channel].value = amount; proposal.patch_after.matter[channel].value -= amount; proposal.reserve_after.matter[channel].value += amount; } return proposal; } } // namespace evo::biology