Compose single-life cellular tick proposals
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m51s
Foundation / checks (client) (push) Failing after 4m32s
Foundation / checks (network) (push) Failing after 3m3s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 52s
Foundation / checks (tsan) (push) Canceled after 24s
Foundation / checks (checks) (push) Canceled after 5m28s

This commit is contained in:
dingo
2026-10-10 21:24:39 -04:00
parent 6816eda6a5
commit 1ccc0ce476
8 changed files with 333 additions and 1 deletions
+81
View File
@@ -0,0 +1,81 @@
#include "evo/biology/cellular_step.hpp"
#include "evo/accounting/rates.hpp"
#include "evo/biology/cellular_motion.hpp"
#include <limits>
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<std::uint64_t>::max() - amount)
return false;
heat.energy.value += amount;
return true;
}
} // namespace
std::expected<CellularStepCandidate, CellularStepError>
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<std::uint64_t>::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 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}, {}, CellularIntakeStatus::not_requested,
{}};
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;
}
} // namespace evo::biology