Files
aevolution/src/biology/cellular_uptake.cpp
T
dingo c552eb3250
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m53s
Foundation / checks (postgres) (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (client) (push) Canceled after 3m14s
Foundation / checks (checks) (push) Canceled after 3m19s
Foundation / checks (network) (push) Canceled after 1m19s
Add channel-limited cellular uptake proposal
2026-10-10 21:11:00 -04:00

51 lines
2.1 KiB
C++

#include "evo/biology/cellular_uptake.hpp"
#include "evo/accounting/rates.hpp"
#include "evo/biology/cellular_motion.hpp"
#include <algorithm>
#include <limits>
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<std::uint64_t>::max() - reserve;
return std::min({stock, rate_limit, room});
}
} // namespace
std::expected<CellularUptakeProposal, CellularUptakeError>
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