103 lines
4.0 KiB
C++
103 lines
4.0 KiB
C++
#pragma once
|
|
#include "evo/world/fields.hpp"
|
|
#include <array>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <expected>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <variant>
|
|
#include <vector>
|
|
|
|
namespace evo::world {
|
|
inline constexpr std::size_t max_environment_footprint = 256;
|
|
inline constexpr std::uint32_t all_environment_fields = (1U << field_count) - 1;
|
|
struct EnvironmentMass {
|
|
accounting::Femtograms amount;
|
|
MassConcentration concentration;
|
|
auto operator<=>(const EnvironmentMass&) const = default;
|
|
};
|
|
using EnvironmentQuantity = std::variant<EnvironmentMass, accounting::Picojoules>;
|
|
enum class MoistureStatus : std::uint8_t { not_requested, missing_water, zero_capacity, available };
|
|
struct EnvironmentSample {
|
|
VoxelAddress voxel;
|
|
ChunkAddress chunk;
|
|
std::uint64_t chunk_revision;
|
|
FieldSite habitat;
|
|
CellSample terrain;
|
|
HabitatMeasure measure;
|
|
accounting::Femtograms water_capacity;
|
|
std::array<std::optional<EnvironmentQuantity>, field_count> fields{};
|
|
MoistureStatus moisture_status{MoistureStatus::not_requested};
|
|
std::optional<WaterSaturation> moisture;
|
|
};
|
|
enum class EnvironmentError : std::uint8_t {
|
|
invalid_request,
|
|
invalid_voxel,
|
|
missing_site,
|
|
missing_chunk,
|
|
source_error,
|
|
wrong_owner,
|
|
stale_tick,
|
|
stale_fields,
|
|
stale_terrain,
|
|
invalid_view
|
|
};
|
|
class EnvironmentView;
|
|
[[nodiscard]] std::expected<EnvironmentView, EnvironmentError>
|
|
query_environment(const FieldStore& fields, const ChunkStore& terrain, std::uint64_t tick,
|
|
std::span<const VoxelAddress> footprint, std::uint32_t requested_mask);
|
|
[[nodiscard]] std::expected<void, EnvironmentError>
|
|
validate_environment(const EnvironmentView& view, const FieldStore& fields,
|
|
const ChunkStore& terrain, std::uint64_t current_tick) noexcept;
|
|
|
|
// Owned immutable samples, non-owning store identities. Stores must outlive validation.
|
|
// Moved-from views may only be reassigned or destroyed. No concurrent store mutation.
|
|
class EnvironmentView {
|
|
public:
|
|
EnvironmentView(const EnvironmentView&) = default;
|
|
EnvironmentView& operator=(const EnvironmentView&) = default;
|
|
EnvironmentView(EnvironmentView&&) noexcept = default;
|
|
EnvironmentView& operator=(EnvironmentView&&) noexcept = default;
|
|
[[nodiscard]] std::span<const EnvironmentSample> samples() const noexcept {
|
|
return samples_;
|
|
}
|
|
[[nodiscard]] WorldIdentity identity() const noexcept {
|
|
return identity_;
|
|
}
|
|
[[nodiscard]] std::uint64_t tick() const noexcept {
|
|
return tick_;
|
|
}
|
|
[[nodiscard]] std::uint64_t field_revision() const noexcept {
|
|
return field_revision_;
|
|
}
|
|
[[nodiscard]] std::uint32_t requested_mask() const noexcept {
|
|
return mask_;
|
|
}
|
|
[[nodiscard]] SignedMillimetres terrain_pitch() const noexcept {
|
|
return {cell_pitch_mm};
|
|
}
|
|
|
|
private:
|
|
friend class EnvironmentalTransfers;
|
|
friend std::expected<EnvironmentView, EnvironmentError>
|
|
query_environment(const FieldStore&, const ChunkStore&, std::uint64_t,
|
|
std::span<const VoxelAddress>, std::uint32_t);
|
|
friend std::expected<void, EnvironmentError> validate_environment(const EnvironmentView&,
|
|
const FieldStore&,
|
|
const ChunkStore&,
|
|
std::uint64_t) noexcept;
|
|
EnvironmentView(const FieldStore& fields, const ChunkStore& terrain, std::uint64_t tick,
|
|
std::uint32_t mask)
|
|
: fields_{&fields}, terrain_{&terrain}, identity_{fields.identity()}, tick_{tick},
|
|
field_revision_{fields.last_command()}, mask_{mask} {}
|
|
const FieldStore* fields_;
|
|
const ChunkStore* terrain_;
|
|
WorldIdentity identity_;
|
|
std::uint64_t tick_, field_revision_;
|
|
std::uint32_t mask_;
|
|
std::vector<EnvironmentSample> samples_;
|
|
std::vector<ChunkView> terrain_tokens_;
|
|
};
|
|
} // namespace evo::world
|