#pragma once #include "evo/world/environment.hpp" #include #include #include #include #include #include #include namespace evo::processes { class EnvironmentalCommunity; } namespace evo::world { struct OrganismResourceId { std::uint64_t value{}; auto operator<=>(const OrganismResourceId&) const = default; }; // Tagged compartments in Field order; fg for mass, pJ for energy. No lifecycle state. struct OrganismResources { OrganismResourceId id; std::array balances{}, capacities{}; auto operator<=>(const OrganismResources&) const = default; }; enum class ResourceDirection : std::uint8_t { to_organism, to_environment }; struct EnvironmentalLeg { VoxelAddress voxel; OrganismResourceId organism; Field field; ResourceDirection direction; std::uint64_t amount{}; auto operator<=>(const EnvironmentalLeg&) const = default; }; struct EnvironmentalBatch { std::uint64_t command{}, tick{}; std::span legs; }; struct EnvironmentalReceipt { std::uint64_t command{}, tick{}; std::size_t legs{}; auto operator<=>(const EnvironmentalReceipt&) const = default; }; enum class TransferError : std::uint8_t { config, identity, terrain_binding, capacity, overflow, budget, invalid_request, wrong_owner, stale_view, uncovered, missing, unknown_organism, insufficient, conflict, stale_command, format, version, checksum }; class EnvironmentalTransfers { public: static constexpr std::size_t max_sites = 256, max_organisms = 64, max_legs = 32; static constexpr std::size_t retained_commands = 64, scratch_bytes = 65536; // Consumes both stores on success or failure. Retaining mutable aliases is forbidden. [[nodiscard]] static std::expected, TransferError> create(WorldIdentity identity, std::unique_ptr fields, std::unique_ptr terrain, std::span organisms, std::size_t byte_budget); EnvironmentalTransfers(const EnvironmentalTransfers&) = delete; EnvironmentalTransfers& operator=(const EnvironmentalTransfers&) = delete; EnvironmentalTransfers(EnvironmentalTransfers&&) = delete; EnvironmentalTransfers& operator=(EnvironmentalTransfers&&) = delete; [[nodiscard]] const FieldStore& fields() const noexcept { return *fields_; } [[nodiscard]] const ChunkStore& terrain() const noexcept { return *terrain_; } [[nodiscard]] std::span organisms() const noexcept { return std::span{organisms_}.first(count_); } static constexpr std::size_t max_snapshot_bytes = std::size_t{72} * 1024 * 1024; [[nodiscard]] std::expected snapshot() const; [[nodiscard]] static std::expected, TransferError> restore(std::span bytes, WorldIdentity identity, std::size_t byte_budget, std::size_t field_budget, std::size_t terrain_budget); [[nodiscard]] accounting::Inventory totals() const noexcept; [[nodiscard]] std::size_t accounted_bytes() const noexcept; [[nodiscard]] std::expected commit(const EnvironmentalBatch& batch, const EnvironmentView& view, std::uint64_t current_tick); private: friend class evo::processes::EnvironmentalCommunity; // Clone support (ADR-0067): the field store and, through it, terrain snapshots // carry this identity; no other resource state depends on it. void rebind(WorldIdentity identity) noexcept { fields_->identity_ = identity; } [[nodiscard]] bool process_checkpoint(std::uint64_t tick, const std::array& environment, const std::array& producer, const std::array& consumer) noexcept; struct History { EnvironmentalReceipt receipt{}; std::array legs{}; }; EnvironmentalTransfers() = default; std::unique_ptr fields_; std::unique_ptr terrain_; std::array organisms_{}; std::array history_{}; std::size_t count_{}, budget_{}, next_history_{}; std::uint64_t adoption_command_{}; }; } // namespace evo::world