157 lines
5.7 KiB
C++
157 lines
5.7 KiB
C++
#pragma once
|
|
#include "evo/world/generation.hpp"
|
|
#include <array>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <expected>
|
|
#include <memory>
|
|
#include <span>
|
|
|
|
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<VolumeUnits, 2> 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<const CellCut> 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<VolumeUnits, 2> removed{};
|
|
std::array<EditedChunk, 4> 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<std::uint64_t, ChunkError>
|
|
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<CompactCells, ChunkError>
|
|
create(std::span<const CellSample> cells);
|
|
[[nodiscard]] std::expected<CellSample, ChunkError> 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<const std::uint32_t> words) noexcept;
|
|
static CompactCells encode(std::span<const std::uint32_t> words);
|
|
CellLayout layout_{CellLayout::uniform};
|
|
std::uint32_t uniform_{};
|
|
std::uint32_t keys_{};
|
|
std::unique_ptr<std::uint32_t[]> 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<std::unique_ptr<ChunkStore>, 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<std::size_t, ChunkError>
|
|
minimum_bytes(std::size_t slots) noexcept;
|
|
[[nodiscard]] std::expected<ChunkView, ChunkError> load(ChunkAddress address);
|
|
[[nodiscard]] std::expected<ChunkView, ChunkError> view(ChunkAddress address) const noexcept;
|
|
[[nodiscard]] std::expected<CellSample, ChunkError> read(ChunkView view,
|
|
LocalCell local) const noexcept;
|
|
// Conservative pin only: no geometry edit or durability acknowledgement.
|
|
[[nodiscard]] std::expected<ChunkView, ChunkError> mark_dirty(ChunkView view) noexcept;
|
|
[[nodiscard]] std::expected<void, ChunkError> evict(ChunkView view) noexcept;
|
|
[[nodiscard]] std::expected<EditReceipt, ChunkError> excavate(const ExcavationRequest& request);
|
|
[[nodiscard]] std::expected<VolumeUnits, ChunkError> 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<Entry[]> entries_;
|
|
DebrisCapacity debris_capacity_;
|
|
std::array<VolumeUnits, 2> debris_{};
|
|
std::uint64_t last_command_{};
|
|
std::size_t history_next_{};
|
|
std::unique_ptr<History[]> history_;
|
|
};
|
|
} // namespace evo::world
|