#pragma once #include "evo/world/generation.hpp" #include #include #include #include #include #include namespace evo::world { enum class ChunkError : std::uint8_t { invalid_config, invalid_descriptor, invalid_address, invalid_cells, capacity, memory_budget, missing, stale_view, invalid_local, dirty, revision_exhausted, invalid_edit, digest_mismatch, command_conflict, stale_command, destination_full, insufficient_material }; enum class CellLayout : std::uint8_t { uniform, palette, dense }; // Exact occupied-volume quanta, NOT mass: one unit = 1/65535 m^3 for v1. struct VolumeUnits { std::uint64_t value{}; auto operator<=>(const VolumeUnits&) const = default; }; struct DebrisCapacity { std::array materials{}; }; // rock, regolith struct CellCut { ChunkAddress chunk{}; LocalCell local{}; std::uint64_t expected_revision{}; std::uint32_t removed_fill{}; auto operator<=>(const CellCut&) const = default; }; struct ExcavationRequest { std::uint64_t command{}; std::uint32_t destination{1}; // the store's finite debris inventory std::span cuts; std::uint64_t digest{}; }; struct EditedChunk { ChunkAddress address{}; std::uint64_t revision{}; auto operator<=>(const EditedChunk&) const = default; }; struct EditReceipt { std::uint64_t command{}, digest{}; std::array removed{}; std::array chunks{}; std::size_t chunk_count{}; auto operator<=>(const EditReceipt&) const = default; }; // Validates request shape; digest excludes the digest field itself. Not authentication. [[nodiscard]] std::expected excavation_digest(const ExcavationRequest& request) noexcept; class ChunkStore; // Immutable owned geometry. Construction does not authorize a terrain edit. class CompactCells { public: // Moves reset the source to a readable uniform void chunk. CompactCells(CompactCells&& other) noexcept; CompactCells& operator=(CompactCells&& other) noexcept; CompactCells(const CompactCells&) = delete; CompactCells& operator=(const CompactCells&) = delete; [[nodiscard]] static std::expected create(std::span cells); [[nodiscard]] std::expected read(std::uint32_t index) const noexcept; [[nodiscard]] std::size_t payload_bytes() const noexcept; [[nodiscard]] CellLayout layout() const noexcept { return layout_; } private: friend class ChunkStore; CompactCells() = default; static std::size_t required_bytes(std::span words) noexcept; static CompactCells encode(std::span words); CellLayout layout_{CellLayout::uniform}; std::uint32_t uniform_{}; std::uint32_t keys_{}; std::unique_ptr data_; }; // Non-owning token; must not outlive the issuing store. Never exposes storage pointers. class ChunkView { public: [[nodiscard]] std::uint64_t revision() const noexcept { return revision_; } private: friend class ChunkStore; ChunkView(const ChunkStore* owner, std::size_t slot, std::uint64_t revision) : owner_{owner}, slot_{slot}, revision_{revision} {} const ChunkStore* owner_; std::size_t slot_; std::uint64_t revision_; }; class ChunkStore { public: static constexpr std::size_t max_slots = 4096; static constexpr std::size_t scratch_bytes = 32768; static constexpr std::size_t retained_commands = 64; [[nodiscard]] static std::expected, ChunkError> create(BaseDescriptor descriptor, std::size_t slots, std::size_t byte_budget, DebrisCapacity debris = {}); ChunkStore(const ChunkStore&) = delete; ChunkStore& operator=(const ChunkStore&) = delete; ChunkStore(ChunkStore&&) = delete; ChunkStore& operator=(ChunkStore&&) = delete; ~ChunkStore(); [[nodiscard]] static std::expected minimum_bytes(std::size_t slots) noexcept; [[nodiscard]] std::expected load(ChunkAddress address); [[nodiscard]] std::expected view(ChunkAddress address) const noexcept; [[nodiscard]] std::expected read(ChunkView view, LocalCell local) const noexcept; // Conservative pin only: no geometry edit or durability acknowledgement. [[nodiscard]] std::expected mark_dirty(ChunkView view) noexcept; [[nodiscard]] std::expected evict(ChunkView view) noexcept; [[nodiscard]] std::expected excavate(const ExcavationRequest& request); [[nodiscard]] std::expected debris(MaterialId material) const noexcept; [[nodiscard]] std::size_t accounted_bytes() const noexcept { return bytes_; } [[nodiscard]] std::size_t size() const noexcept { return size_; } private: struct Entry; struct History; ChunkStore(BaseDescriptor descriptor, std::size_t slots, std::size_t budget, std::size_t bytes, DebrisCapacity debris); [[nodiscard]] bool current(ChunkView view) const noexcept; BaseDescriptor descriptor_; std::size_t slots_, budget_, bytes_, size_{}; std::uint64_t revision_{}; std::unique_ptr entries_; DebrisCapacity debris_capacity_; std::array debris_{}; std::uint64_t last_command_{}; std::size_t history_next_{}; std::unique_ptr history_; }; } // namespace evo::world