#pragma once #include "evo/biology/cellular_motion.hpp" #include "evo/biology/cellular_uptake.hpp" #include #include #include #include #include #include #include #include #include namespace evo::biology { inline constexpr std::uint32_t cellular_patch_pitch_um = 1'000; inline constexpr std::uint32_t cellular_trial_perception_radius_um = 5'000; inline constexpr std::size_t max_cellular_patches = 4'096; inline constexpr std::uint32_t cellular_habitat_format = 1; inline constexpr std::size_t cellular_habitat_header_bytes = 72; inline constexpr std::size_t cellular_patch_record_bytes = 56; inline constexpr std::size_t max_cellular_habitat_bytes = cellular_habitat_header_bytes + cellular_patch_record_bytes * max_cellular_patches + 8; struct CellularPatchAddress { std::uint32_t x_um{}, y_um{}; auto operator<=>(const CellularPatchAddress&) const = default; }; struct CellularPatch { CellularPatchAddress address; std::uint64_t source{}; // Explicit region-opening provenance. accounting::Inventory stock{}; auto operator<=>(const CellularPatch&) const = default; }; struct CellularHabitatConfig { std::uint64_t world{}, region{}, frame{}; std::uint32_t width_um{}, height_um{}; std::vector patches; // Strictly ordered by (x_um, y_um). auto operator<=>(const CellularHabitatConfig&) const = default; }; struct CellularPatchUptakeProposal { CellularPatchAddress patch; CellularUptakeProposal transfer; auto operator<=>(const CellularPatchUptakeProposal&) const = default; }; // Client-safe view of an in-range patch. Provenance remains server-owned. struct CellularPatchPerception { CellularPatchAddress address; accounting::Inventory available; auto operator<=>(const CellularPatchPerception&) const = default; }; enum class CellularHabitatError : std::uint8_t { invalid_config, invalid_patch, duplicate_patch, overflow, missing_patch, not_in_contact, uptake, outside_area, format, version, checksum, identity }; // Region-local finite food stock for the cellular trial. Empty/unlisted patches // contain no stock. This is a read/proposal owner; the caller commits its patch // and organism candidate together through the durable cellular state owner. class CellularHabitat { public: [[nodiscard]] static std::expected, CellularHabitatError> create(CellularHabitatConfig config); CellularHabitat(const CellularHabitat&) = delete; CellularHabitat& operator=(const CellularHabitat&) = delete; CellularHabitat(CellularHabitat&&) = delete; CellularHabitat& operator=(CellularHabitat&&) = delete; ~CellularHabitat(); [[nodiscard]] const CellularHabitatConfig& config() const noexcept; [[nodiscard]] std::span patches() const noexcept; [[nodiscard]] accounting::Inventory totals() const noexcept; [[nodiscard]] std::optional find(CellularPatchAddress address) const noexcept; // Returns patch stock only within the fixed M11.3 sensor radius. Patches // are visible when the organism-to-patch-square gap is within that radius. [[nodiscard]] std::expected, CellularHabitatError> perceive(CellularPosition position, std::uint32_t extent_um) const; [[nodiscard]] std::expected propose_uptake(CellularPatchAddress address, CellularPosition position, std::uint32_t extent_um, accounting::Inventory reserve, const MetabolicBudget& budget) const noexcept; // Canonical little-endian envelope, FNV-1a64 checksum for corruption checks only. [[nodiscard]] std::vector snapshot() const; [[nodiscard]] static std::expected, CellularHabitatError> restore(std::span bytes, std::uint64_t world, std::uint64_t region, std::uint64_t frame); private: CellularHabitat(CellularHabitatConfig config, accounting::Inventory totals) : config_{std::move(config)}, totals_{totals} {} CellularHabitatConfig config_; accounting::Inventory totals_{}; }; } // namespace evo::biology