106 lines
3.5 KiB
C++
106 lines
3.5 KiB
C++
#pragma once
|
|
#include "evo/world/chunks.hpp"
|
|
#include "evo/world/environment_units.hpp"
|
|
#include <array>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <expected>
|
|
#include <memory>
|
|
#include <span>
|
|
|
|
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<accounting::Femtograms, 3> water_capacity{};
|
|
};
|
|
struct FieldOpening {
|
|
Field field;
|
|
// One value repeated at every site, or exactly site_count values; borrowed during create.
|
|
std::span<const std::uint64_t> 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<std::unique_ptr<FieldStore>, FieldError>
|
|
create(WorldIdentity identity, std::uint64_t opening_id, FieldDefaults defaults,
|
|
std::span<const FieldSite> sites, std::span<const FieldOpening> 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<std::uint64_t, FieldError> read(std::size_t site,
|
|
Field field) const noexcept;
|
|
[[nodiscard]] std::expected<void, FieldError> 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<std::unique_ptr<FieldStore>, FieldError>
|
|
restore(std::span<const std::uint8_t> 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]] std::span<const FieldSite> sites() const noexcept {
|
|
return {sites_.get(), count_};
|
|
}
|
|
|
|
private:
|
|
using Page = std::array<std::uint64_t, 64>;
|
|
struct Channel {
|
|
bool present{};
|
|
std::uint64_t baseline{};
|
|
std::array<std::unique_ptr<Page>, 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<const std::uint64_t> values) noexcept;
|
|
static Channel encode(std::span<const std::uint64_t> values);
|
|
WorldIdentity identity_{};
|
|
std::uint64_t opening_{}, command_{};
|
|
FieldDefaults defaults_{};
|
|
std::size_t count_{}, budget_{}, bytes_{};
|
|
std::unique_ptr<FieldSite[]> sites_;
|
|
std::array<Channel, field_count> fields_;
|
|
};
|
|
} // namespace evo::world
|