#include "evo/processes/environmental_community.hpp" #include #include namespace evo::processes { namespace { constexpr auto maximum = std::numeric_limits::max(); constexpr std::array food{3, 4, 5, 6, 7}, growth_from{0, 3, 1, 2, 6}, growth_to{0, 3, 4, 5, 6}; bool any(const std::array& a) { return std::ranges::any_of(a, [](auto x) { return x != 0; }); } std::uint64_t portion(std::uint64_t x, std::uint64_t a) { return (x / 1000) * a + (x % 1000) * a / 1000; } } // namespace bool EnvironmentalCommunity::rebind_world(std::uint64_t world) noexcept { if (world == 0) return false; config_.world = world; resources_->rebind({world, config_.frame}); return true; } bool EnvironmentalCommunity::configuration(const EnvironmentalConfig& c, const world::EnvironmentalTransfers& r) noexcept { if ((c.rules != 1 && c.rules != 2) || (c.rules == 1 && std::ranges::any_of(c.decay, [](auto v) { return v != 0; })) || c.world == 0 || c.frame == 0 || r.fields().identity() != world::WorldIdentity{c.world, c.frame} || c.source == 0 || c.tick_microseconds == 0 || c.area_um2 == 0 || c.irradiance_denominator == 0 || c.growth_denominator == 0 || c.efficiency > 1000 || c.assimilation > 1000 || c.growth_cost == 0 || c.producer_min_mK == 0 || c.consumer_min_mK == 0 || c.producer_min_mK > c.producer_max_mK || c.consumer_min_mK > c.consumer_max_mK) return false; if (r.fields().sites().size() != 1 || r.terrain().size() != 1 || r.organisms().size() != 2 || r.organisms()[0].id.value != c.producer || r.organisms()[1].id.value != c.consumer) return false; const auto measure = world::measure_habitat(r.fields().sites()[0].box); if (!measure || c.area_um2 > measure->area.value || c.irradiance_numerator > maximum / c.area_um2) return false; for (std::size_t k = 0; k < 10; ++k) if (!r.fields().read(0, static_cast(k))) return false; for (std::size_t k = 0; k < 4; ++k) if (c.recipe[k] == 0) return false; return true; } EnvironmentalCommunity::State EnvironmentalCommunity::state() const noexcept { State s; for (std::size_t k = 0; k < 10; ++k) s.environment[k] = *resources_->fields().read(0, static_cast(k)); s.producer = resources_->organisms()[0].balances; s.consumer = resources_->organisms()[1].balances; s.progress = progress_; return s; } bool EnvironmentalCommunity::valid(const State& s) const noexcept { const auto& p = s.progress; if ((p.last_funded == 0 && (p.source_remainder != 0 || p.efficiency_remainder != 0)) || p.source_remainder >= config_.irradiance_denominator || p.growth_remainder >= config_.growth_denominator || p.efficiency_remainder >= 1000 || p.last_funded > p.tick || p.producer_terminal > 1 || p.consumer_terminal > 1 || (config_.rules == 1 && p.consumer_terminal != 0) || (p.tick == 0 && p != EnvironmentalProgress{})) return false; if (config_.rules == 1 && p.producer_terminal && std::ranges::any_of(s.producer, [](auto v) { return v != 0; })) return false; if (s.environment[0] > resources_->fields().water_capacity(0)->value) return false; auto remaining = resources_->totals(); constexpr std::array channels{3, 1, 2, 0, 1, 2, 3}; for (std::size_t k = 0; k < 10; ++k) { if (s.producer[k] > resources_->organisms()[0].capacities[k] || s.consumer[k] > resources_->organisms()[1].capacities[k]) return false; auto& total = k < 7 ? remaining.matter[channels[k]].value : remaining.energy.value; for (auto v : {s.environment[k], s.producer[k], s.consumer[k]}) { if (v > total) return false; total -= v; } } return remaining == accounting::Inventory{}; } std::expected, CommunityError> EnvironmentalCommunity::create(EnvironmentalConfig c, std::unique_ptr r, std::size_t budget) { if (!r || !configuration(c, *r) || r->fields().last_command() != 0 || r->adoption_command_ != 0) return std::unexpected{CommunityError::config}; if (budget < sizeof(EnvironmentalCommunity) + 2 * scratch_bytes || r->accounted_bytes() > budget - sizeof(EnvironmentalCommunity) - 2 * scratch_bytes) return std::unexpected{CommunityError::budget}; auto o = std::unique_ptr(new EnvironmentalCommunity); o->config_ = c; o->resources_ = std::move(r); o->budget_ = budget; o->anchor_ = o->state(); return o; } std::expected EnvironmentalCommunity::step(State& s, std::uint64_t tick, const EnvironmentalInput& input) const noexcept { if (input.temperature_mK == 0 || input.source_enabled > 1) return std::unexpected{CommunityError::input}; auto& p = s.progress; const auto& c = config_; EnvironmentalReport report{}; report.tick = tick; if (input.terminal[0] > 1 || input.terminal[1] > 1 || (c.rules == 1 && (input.terminal[0] || input.terminal[1])) || (input.terminal[0] && p.producer_terminal) || (input.terminal[1] && p.consumer_terminal)) return std::unexpected{CommunityError::input}; if (input.terminal[0]) { p.producer_terminal = 1; report.terminal_transition = 1; } if (input.terminal[1]) { p.consumer_terminal = 1; report.consumer_transition = 1; } const bool eligible = !p.producer_terminal && input.temperature_mK >= c.producer_min_mK && input.temperature_mK <= c.producer_max_mK; if (eligible && input.source_enabled) { auto quota = world::integrate_energy( {c.irradiance_numerator * c.area_um2, c.irradiance_denominator}, 1, p.source_remainder); if (!quota) return std::unexpected{CommunityError::overflow}; const auto energy = quota->amount.value; if (energy > s.environment[8]) report.source_status = 2; else { const auto frac = (energy % 1000) * c.efficiency + p.efficiency_remainder; const auto retained = (energy / 1000) * c.efficiency + frac / 1000; if (retained > resources_->organisms()[0].capacities[7] - s.producer[7]) return std::unexpected{CommunityError::capacity}; s.environment[8] -= energy; s.producer[7] += retained; s.environment[9] += energy - retained; p.source_remainder = quota->remainder; p.efficiency_remainder = frac % 1000; p.last_funded = tick; report.captured = energy; report.retained = retained; report.capture_heat = energy - retained; report.source_status = 1; } } if (eligible) { auto quota = world::integrate_energy({c.growth_numerator, c.growth_denominator}, 1, p.growth_remainder); if (!quota) return std::unexpected{CommunityError::overflow}; auto grown = std::min(quota->amount.value, s.producer[7] / c.growth_cost); for (std::size_t j = 0; j < 5; ++j) if (c.recipe[j] != 0) { grown = std::min(grown, s.environment[growth_from[j]] / c.recipe[j]); grown = std::min(grown, (resources_->organisms()[0].capacities[growth_to[j]] - s.producer[growth_to[j]]) / c.recipe[j]); } for (std::size_t j = 0; j < 5; ++j) { const auto amount = grown * c.recipe[j]; s.environment[growth_from[j]] -= amount; s.producer[growth_to[j]] += amount; } s.producer[7] -= grown * c.growth_cost; s.environment[9] += grown * c.growth_cost; p.growth_remainder = quota->remainder; report.grown = grown; report.unmet = quota->amount.value - grown; } if (any(input.bite)) { if (p.producer_terminal || p.consumer_terminal || input.temperature_mK < c.consumer_min_mK || input.temperature_mK > c.consumer_max_mK) return std::unexpected{CommunityError::input}; for (std::size_t j = 0; j < 5; ++j) { const auto amount = input.bite[j], k = food[j]; if (amount > c.intake[j]) return std::unexpected{CommunityError::input}; if (amount > s.producer[k]) return std::unexpected{CommunityError::insufficient}; const auto kept = portion(amount, c.assimilation); if (kept > resources_->organisms()[1].capacities[k] - s.consumer[k]) return std::unexpected{CommunityError::capacity}; s.producer[k] -= amount; s.consumer[k] += kept; s.environment[j == 4 ? 9 : k] += amount - kept; report.assimilated[j] = kept; report.waste[j] = amount - kept; } if (std::ranges::all_of(s.producer, [](auto v) { return v == 0; })) { p.producer_terminal = 1; report.terminal_transition = 1; } } if (c.rules == 2) { constexpr std::array destination{0, 1, 2, 3, 1, 2, 6, 9, 8, 9}; auto remaining = c.decay; for (std::size_t actor = 0; actor < 2; ++actor) { if (!(actor == 0 ? p.producer_terminal : p.consumer_terminal)) continue; auto& corpse = actor == 0 ? s.producer : s.consumer; for (std::size_t k = 0; k < 10; ++k) { auto amount = std::min(remaining[k], corpse[k]); if (k == 0) amount = std::min(amount, resources_->fields().water_capacity(0)->value - s.environment[0]); corpse[k] -= amount; s.environment[destination[k]] += amount; remaining[k] -= amount; report.decomposed[k] += amount; } } } p.tick = tick; if (!valid(s)) return std::unexpected{CommunityError::resources}; return report; } std::expected EnvironmentalCommunity::advance(std::uint64_t tick, const EnvironmentalInput& input) { if (tick == 0) return std::unexpected{CommunityError::tick}; if (tick <= progress_.tick) { for (const auto& h : history_) if (h.report.tick == tick) { if (h.input != input) return std::unexpected{CommunityError::conflict}; return h.report; } return std::unexpected{CommunityError::stale}; } if (progress_.tick == maximum || tick != progress_.tick + 1) return std::unexpected{CommunityError::tick}; auto candidate = state(); auto report = step(candidate, tick, input); if (!report) return report; auto anchor = anchor_; if (count_ == history_limit) { const auto& old = history_[next_]; auto checked = step(anchor, old.report.tick, old.input); if (!checked || *checked != old.report) return std::unexpected{CommunityError::resources}; } if (!resources_->process_checkpoint(tick, candidate.environment, candidate.producer, candidate.consumer)) return std::unexpected{CommunityError::resources}; progress_ = candidate.progress; anchor_ = anchor; history_[next_] = {input, *report}; next_ = (next_ + 1) % history_limit; count_ = std::min(count_ + 1, history_limit); return report; } } // namespace evo::processes