#pragma once #include "evo/processes/producer.hpp" namespace evo::processes { struct ConsumerConfig { accounting::Inventory opening; accounting::Inventory intake_limit; std::uint16_t assimilation_per_thousand; }; struct FeedIntent { std::uint64_t sequence; std::uint32_t consumer; std::uint32_t target; accounting::Inventory amount; }; struct FeedReceipt { std::uint64_t sequence; accounting::Inventory assimilated; bool target_depleted; auto operator<=>(const FeedReceipt&) const = default; }; struct TerminalEvent { std::uint32_t actor; }; struct DecompositionConfig { accounting::Inventory per_tick_limit{}; }; // Reserved slots are allocated at creation; a birth never grows the ledger. struct ReproductionConfig { std::uint32_t capacity{}; std::uint16_t construction_per_thousand{}; accounting::Inventory minimum_endowment{}; }; struct ReproduceIntent { std::uint64_t sequence; std::uint32_t parent; accounting::Inventory investment; }; struct ReproduceReceipt { std::uint64_t sequence; std::uint32_t child; accounting::Inventory endowment; accounting::Inventory overhead; auto operator<=>(const ReproduceReceipt&) const = default; }; enum class FeedingError : std::uint8_t { invalid_config, wrong_tick, invalid_batch, invalid_actor, inactive_actor, unborn_actor, not_corpse, intake_limit, insufficient_inventory, no_capacity, invalid_endowment, accounting_failure }; inline constexpr std::size_t max_consumers = 16; class FeedingCommunity { public: [[nodiscard]] static std::expected create(ProducerConfig producer, accounting::Picojoules source, std::array nutrients, std::span consumers, DecompositionConfig decomposition = {}, ReproductionConfig reproduction = {}); [[nodiscard]] std::expected, FeedingError> advance(Tick target, std::span inputs = {}, std::span terminal_events = {}, std::span births = {}); [[nodiscard]] std::span balances() const noexcept { return producer_.balances(); } [[nodiscard]] ProducerProgress progress() const noexcept { return producer_.progress(); } // Process IDs are one-based. Out-of-range is a query error, not an inactive actor. [[nodiscard]] std::expected active(std::uint32_t id) const noexcept; // False for a reserved slot that no reproduction has allocated yet. [[nodiscard]] std::expected born(std::uint32_t id) const noexcept; [[nodiscard]] std::expected corpse(std::uint32_t id) const noexcept; // Ancestry outlives both organisms; zero identifies a documented founder. [[nodiscard]] std::expected parent(std::uint32_t id) const noexcept; [[nodiscard]] std::expected origin(std::uint32_t id) const noexcept; [[nodiscard]] accounting::Inventory last_decomposed() const noexcept { return last_decomposed_; } // Committed births of the last successful tick; invalidated by mutation. [[nodiscard]] std::span last_births() const noexcept { return last_births_; } private: enum class State : std::uint8_t { unborn, active, corpse }; FeedingCommunity(ProducerProcess producer, std::vector consumers, DecompositionConfig decomposition, ReproductionConfig reproduction, std::uint32_t living); [[nodiscard]] std::expected feed(const FeedIntent& input); [[nodiscard]] std::expected reproduce(const ReproduceIntent& input, Tick target); [[nodiscard]] std::expected decompose(); [[nodiscard]] std::expected state(std::uint32_t id) const noexcept; ProducerProcess producer_; std::vector consumers_; std::vector states_; std::vector parents_; std::vector origins_; DecompositionConfig decomposition_; ReproductionConfig reproduction_; accounting::Inventory last_decomposed_{}; std::vector last_births_; }; } // namespace evo::processes