Add finite producer capture and resource-limited growth process
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
#include "evo/processes/producer.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::processes {
|
||||
namespace {
|
||||
struct Quota {
|
||||
std::uint64_t whole;
|
||||
std::uint32_t remainder;
|
||||
};
|
||||
Quota quota(std::uint64_t numerator, std::uint32_t denominator, std::uint32_t carry) noexcept {
|
||||
const auto remainder = numerator % denominator + carry;
|
||||
return {numerator / denominator + remainder / denominator,
|
||||
static_cast<std::uint32_t>(remainder % denominator)};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
ProducerProcess::ProducerProcess(ProducerConfig config, accounting::Ledger ledger)
|
||||
: config_{config}, ledger_{std::move(ledger)} {}
|
||||
|
||||
std::expected<ProducerProcess, ProducerError>
|
||||
ProducerProcess::create(ProducerConfig config, accounting::Picojoules source,
|
||||
std::array<accounting::Femtograms, 4> nutrients) {
|
||||
if (config.capture.ticks == 0 || config.growth.ticks == 0 ||
|
||||
config.efficiency_per_thousand > 1000 || config.growth_cost.value == 0 ||
|
||||
std::ranges::none_of(config.recipe, [](auto mass) { return mass.value != 0; })) {
|
||||
return std::unexpected{ProducerError::invalid_config};
|
||||
}
|
||||
const std::array opening{accounting::Inventory{source, {}},
|
||||
accounting::Inventory{{}, nutrients}, accounting::Inventory{},
|
||||
accounting::Inventory{}};
|
||||
auto ledger = accounting::Ledger::create(opening);
|
||||
if (!ledger) {
|
||||
return std::unexpected{ProducerError::ledger_failure};
|
||||
}
|
||||
return ProducerProcess{config, std::move(*ledger)};
|
||||
}
|
||||
|
||||
std::expected<ProducerReport, ProducerError> ProducerProcess::advance(Tick target) {
|
||||
if (progress_.tick.value == std::numeric_limits<std::uint64_t>::max()) {
|
||||
return std::unexpected{ProducerError::tick_exhausted};
|
||||
}
|
||||
if (target.value != progress_.tick.value + 1) {
|
||||
return std::unexpected{ProducerError::wrong_tick};
|
||||
}
|
||||
const auto capture =
|
||||
quota(config_.capture.energy.value, config_.capture.ticks, progress_.capture_remainder);
|
||||
const auto growth =
|
||||
quota(config_.growth.units, config_.growth.ticks, progress_.growth_remainder);
|
||||
const auto balances = ledger_.balances();
|
||||
if (balances.size() != 4) {
|
||||
return std::unexpected{ProducerError::ledger_failure};
|
||||
}
|
||||
const auto captured = std::min(capture.whole, balances[0].energy.value);
|
||||
const auto efficiency = config_.efficiency_per_thousand;
|
||||
const auto fractional = (captured % 1000) * efficiency + progress_.efficiency_remainder;
|
||||
const auto retained = (captured / 1000) * efficiency + fractional / 1000;
|
||||
// Conserved global energy bounds this addition, including new capture.
|
||||
const auto reserve = balances[2].energy.value + retained;
|
||||
auto grown = std::min(growth.whole, reserve / config_.growth_cost.value);
|
||||
for (std::size_t index = 0; index < config_.recipe.size(); ++index) {
|
||||
if (config_.recipe[index].value != 0) {
|
||||
grown = std::min(grown, balances[1].matter[index].value / config_.recipe[index].value);
|
||||
}
|
||||
}
|
||||
accounting::Inventory material{};
|
||||
for (std::size_t index = 0; index < config_.recipe.size(); ++index) {
|
||||
material.matter[index].value = grown * config_.recipe[index].value;
|
||||
}
|
||||
const std::array legs{accounting::Transfer{{1}, {3}, {{retained}, {}}},
|
||||
accounting::Transfer{{1}, {4}, {{captured - retained}, {}}},
|
||||
accounting::Transfer{{3}, {4}, {{grown * config_.growth_cost.value}, {}}},
|
||||
accounting::Transfer{{2}, {3}, material}};
|
||||
const auto result = ledger_.transfer(target.value, legs);
|
||||
if (!result) {
|
||||
return std::unexpected{ProducerError::ledger_failure};
|
||||
}
|
||||
progress_ = {target, capture.remainder, growth.remainder,
|
||||
static_cast<std::uint32_t>(fractional % 1000)};
|
||||
return ProducerReport{
|
||||
{captured}, {retained}, {captured - retained}, grown, growth.whole - grown};
|
||||
}
|
||||
} // namespace evo::processes
|
||||
Reference in New Issue
Block a user