Resolve cellular patch contention per tick
Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (ecs) (push) Successful in 1m55s
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 (network) (push) Canceled after 1m26s
Foundation / checks (client) (push) Canceled after 3m33s
Foundation / checks (checks) (push) Canceled after 3m38s

This commit is contained in:
dingo
2026-10-10 21:51:38 -04:00
parent 747b063c65
commit b5e831f385
11 changed files with 343 additions and 11 deletions
+20 -4
View File
@@ -162,14 +162,30 @@ CellularHabitat::propose_uptake(CellularPatchAddress address, CellularPosition p
const auto index = find(address);
if (!index)
return std::unexpected{CellularHabitatError::missing_patch};
const auto& patch = config_.patches[*index];
if (!cellular_footprints_contact(position, extent_um, {patch.address.x_um, patch.address.y_um},
return propose_uptake_from_stock(address, position, extent_um, config_.patches[*index].stock,
reserve, budget);
}
std::expected<CellularPatchUptakeProposal, CellularHabitatError>
CellularHabitat::propose_uptake_from_stock(CellularPatchAddress address, CellularPosition position,
std::uint32_t extent_um,
accounting::Inventory patch_stock,
accounting::Inventory reserve,
const MetabolicBudget& budget) const noexcept {
if (extent_um == 0 || extent_um > config_.width_um || extent_um > config_.height_um ||
position.x_um > config_.width_um - extent_um ||
position.y_um > config_.height_um - extent_um)
return std::unexpected{CellularHabitatError::outside_area};
const auto index = find(address);
if (!index)
return std::unexpected{CellularHabitatError::missing_patch};
if (!cellular_footprints_contact(position, extent_um, {address.x_um, address.y_um},
cellular_patch_pitch_um))
return std::unexpected{CellularHabitatError::not_in_contact};
const auto uptake = propose_cellular_uptake(patch.stock, reserve, budget);
const auto uptake = propose_cellular_uptake(patch_stock, reserve, budget);
if (!uptake)
return std::unexpected{CellularHabitatError::uptake};
return CellularPatchUptakeProposal{address, *uptake};
return CellularPatchUptakeProposal{config_.patches[*index].address, *uptake};
}
std::vector<std::uint8_t> CellularHabitat::snapshot() const {
+76
View File
@@ -0,0 +1,76 @@
#include "evo/biology/cellular_region_step.hpp"
#include <algorithm>
#include <limits>
#include <utility>
namespace evo::biology {
std::expected<CellularRegionStepCandidate, CellularRegionStepError>
propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
std::span<const CellularLifeStepInput> lives,
const CellularHabitat& habitat) {
if (begin_tick == std::numeric_limits<std::uint64_t>::max())
return std::unexpected{CellularRegionStepError::invalid_tick};
if (lives.size() > max_cellular_tick_lives)
return std::unexpected{CellularRegionStepError::too_many_lives};
std::vector<const CellularLifeStepInput*> ordered;
ordered.reserve(lives.size());
for (const auto& life : lives) {
if (life.life_id == 0)
return std::unexpected{CellularRegionStepError::invalid_life_id};
ordered.push_back(&life);
}
std::ranges::sort(ordered, {}, [](const auto* life) { return life->life_id; });
for (std::size_t i = 1; i < ordered.size(); ++i)
if (ordered[i - 1]->life_id == ordered[i]->life_id)
return std::unexpected{CellularRegionStepError::duplicate_life_id};
CellularRegionStepCandidate candidate{begin_tick, begin_tick + 1, {}, {}};
candidate.lives.reserve(ordered.size());
std::vector<accounting::Inventory> patch_stock;
patch_stock.reserve(habitat.patches().size());
for (const auto& patch : habitat.patches())
patch_stock.push_back(patch.stock);
for (const auto* life : ordered) {
auto intent = life->intent;
const auto target = intent.intake_target;
intent.intake_target.reset();
const auto step =
propose_cellular_step(begin_tick, area, life->position, life->extent_um, intent,
life->reserve, life->heat, life->budget, habitat);
if (!step)
return std::unexpected{CellularRegionStepError::life_step};
auto result = *step;
if (target) {
const auto index = habitat.find(*target);
if (!index) {
result.intake_status = CellularIntakeStatus::missing;
} else {
const auto intake = habitat.propose_uptake_from_stock(
*target, result.position, life->extent_um, patch_stock[*index],
result.reserve_after, life->budget);
if (!intake) {
if (intake.error() == CellularHabitatError::not_in_contact) {
result.intake_status = CellularIntakeStatus::out_of_contact;
} else {
return std::unexpected{CellularRegionStepError::habitat};
}
} else {
patch_stock[*index] = intake->transfer.patch_after;
result.reserve_after = intake->transfer.reserve_after;
result.intake_status = CellularIntakeStatus::acquired;
result.intake = *intake;
}
}
}
candidate.lives.push_back({life->life_id, result});
}
for (std::size_t i = 0; i < patch_stock.size(); ++i)
if (patch_stock[i] != habitat.patches()[i].stock)
candidate.patches.push_back({habitat.patches()[i].address, patch_stock[i]});
return candidate;
}
} // namespace evo::biology