#pragma once #include "evo/world/chunks.hpp" #include "evo/world/environment_units.hpp" #include #include #include #include #include #include namespace evo::world { enum class Field : std::uint8_t { water, nitrogen, phosphorus, organic_carbon, organic_nitrogen, organic_phosphorus, organic_other, organic_energy, incident_energy, heat }; inline constexpr std::size_t field_count = 10; struct FieldSite { VoxelAddress voxel; HabitatBox box; MaterialId substrate; }; struct FieldDefaults { std::array water_capacity{}; }; struct FieldOpening { Field field; // One value repeated at every site, or exactly site_count values; borrowed during create. std::span values; }; enum class FieldError : std::uint8_t { config, mapping, channel, missing, site, capacity, overflow, budget, stale_command, invalid_transfer, insufficient, format, version, identity, checksum }; class FieldStore { public: static constexpr std::size_t max_sites = 4096; static constexpr std::size_t scratch_bytes = 32768; static constexpr std::size_t max_snapshot_bytes = std::size_t{1024} * 1024; [[nodiscard]] static std::expected, FieldError> create(WorldIdentity identity, std::uint64_t opening_id, FieldDefaults defaults, std::span sites, std::span fields, std::size_t byte_budget); FieldStore(const FieldStore&) = delete; FieldStore& operator=(const FieldStore&) = delete; FieldStore(FieldStore&&) = delete; FieldStore& operator=(FieldStore&&) = delete; [[nodiscard]] std::expected read(std::size_t site, Field field) const noexcept; [[nodiscard]] std::expected transfer(std::uint64_t command, Field field, std::size_t from, std::size_t to, std::uint64_t amount); [[nodiscard]] WorldBytes snapshot() const; [[nodiscard]] static std::expected, FieldError> restore(std::span bytes, WorldIdentity identity, std::size_t byte_budget); [[nodiscard]] std::size_t accounted_bytes() const noexcept { return bytes_; } [[nodiscard]] std::uint64_t last_command() const noexcept { return command_; } [[nodiscard]] WorldIdentity identity() const noexcept { return identity_; } [[nodiscard]] std::expected water_capacity(std::size_t site) const noexcept { if (site >= count_) return std::unexpected{FieldError::site}; return defaults_.water_capacity[sites_[site].substrate.value]; } [[nodiscard]] std::span sites() const noexcept { return {sites_.get(), count_}; } private: friend class EnvironmentalTransfers; using Page = std::array; struct Channel { bool present{}; std::uint64_t baseline{}; std::array, max_sites / 64> pages; [[nodiscard]] std::uint64_t get(std::size_t site) const noexcept; [[nodiscard]] std::size_t bytes() const noexcept; }; FieldStore() = default; static std::size_t page_bytes(std::span values) noexcept; static Channel encode(std::span values); WorldIdentity identity_{}; std::uint64_t opening_{}, command_{}; FieldDefaults defaults_{}; std::size_t count_{}, budget_{}, bytes_{}; std::unique_ptr sites_; std::array fields_; }; } // namespace evo::world