Propose a funded bud from a grown parent's reserve
ADR-0125 and the M11.5 plan define budding and wildlife descendants. The first piece is biology only: fund_bud pays the ADR-0032 investment and a 30 s provision from the parent's reserve, builds the bud at the largest growth the investment matter allows, and sends construction energy to heat, refusing with nothing spent when any channel is short. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
#include "evo/biology/budding.hpp"
|
||||
|
||||
#include <limits>
|
||||
|
||||
namespace evo::biology {
|
||||
namespace {
|
||||
constexpr std::uint64_t maximum = std::numeric_limits<std::uint64_t>::max();
|
||||
|
||||
bool fits(const accounting::Inventory& part, const accounting::Inventory& whole) noexcept {
|
||||
for (std::size_t c = 0; c < part.matter.size(); ++c)
|
||||
if (part.matter[c].value > whole.matter[c].value)
|
||||
return false;
|
||||
return part.energy.value <= whole.energy.value;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<BudTransition, BuddingError> fund_bud(const Genome& genome, GrowthState parent_growth,
|
||||
const accounting::Inventory& parent_reserve,
|
||||
std::uint32_t temperature_millikelvin) {
|
||||
if (!genome.carries(Capability::reproduction))
|
||||
return std::unexpected{BuddingError::no_capability};
|
||||
if (parent_growth.realized_per_thousand != 1000 || parent_growth.damage_per_thousand != 0)
|
||||
return std::unexpected{BuddingError::not_grown};
|
||||
const auto parent = metabolic_budget(genome, parent_growth, {temperature_millikelvin, {}});
|
||||
if (!parent)
|
||||
return std::unexpected{BuddingError::development};
|
||||
const auto& investment = parent->reproduction_investment;
|
||||
std::uint64_t invested_matter = 0;
|
||||
for (const auto& mass : investment.matter)
|
||||
invested_matter += mass.value;
|
||||
if (invested_matter == 0)
|
||||
return std::unexpected{BuddingError::no_capability};
|
||||
|
||||
// The largest realized growth whose developed body fits the invested matter on every
|
||||
// channel. Bodies grow with realized growth, so the scan stops at the first fit.
|
||||
for (std::uint32_t realized = 1000; realized > 0; --realized) {
|
||||
const GrowthState growth{realized, 0};
|
||||
const auto bud = metabolic_budget(genome, growth, {temperature_millikelvin, {}});
|
||||
if (!bud)
|
||||
return std::unexpected{BuddingError::development};
|
||||
accounting::Inventory body_only = bud->body;
|
||||
body_only.energy = {};
|
||||
accounting::Inventory matter_only = investment;
|
||||
matter_only.energy = {};
|
||||
if (!fits(body_only, matter_only))
|
||||
continue;
|
||||
if (bud->rate_microseconds == 0)
|
||||
return std::unexpected{BuddingError::development};
|
||||
const auto periods = bud_provision_seconds * 1'000'000 / bud->rate_microseconds;
|
||||
if (periods != 0 && bud->demand.value > maximum / periods)
|
||||
return std::unexpected{BuddingError::development};
|
||||
const auto provision = bud->demand.value * periods;
|
||||
if (provision > maximum - investment.energy.value)
|
||||
return std::unexpected{BuddingError::insufficient_funding};
|
||||
accounting::Inventory cost = investment;
|
||||
cost.energy.value = investment.energy.value + provision;
|
||||
if (!fits(cost, parent_reserve))
|
||||
return std::unexpected{BuddingError::insufficient_funding};
|
||||
|
||||
BudTransition transition;
|
||||
transition.growth = growth;
|
||||
transition.body = body_only;
|
||||
transition.heat = investment.energy;
|
||||
transition.reserve.energy.value = provision;
|
||||
transition.parent_reserve = parent_reserve;
|
||||
transition.parent_reserve.energy.value -= cost.energy.value;
|
||||
for (std::size_t c = 0; c < investment.matter.size(); ++c) {
|
||||
transition.reserve.matter[c].value =
|
||||
investment.matter[c].value - body_only.matter[c].value;
|
||||
transition.parent_reserve.matter[c].value -= investment.matter[c].value;
|
||||
}
|
||||
return transition;
|
||||
}
|
||||
return std::unexpected{BuddingError::insufficient_funding}; // Too little to build a cell.
|
||||
}
|
||||
} // namespace evo::biology
|
||||
Reference in New Issue
Block a user