Complete M6.3 read-only environment views
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
#include "evo/world/environment.hpp"
|
||||
#include <algorithm>
|
||||
|
||||
namespace evo::world {
|
||||
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) {
|
||||
if (footprint.empty() || footprint.size() > max_environment_footprint || requested_mask == 0 ||
|
||||
(requested_mask & ~all_environment_fields) != 0)
|
||||
return std::unexpected{EnvironmentError::invalid_request};
|
||||
for (const auto address : footprint)
|
||||
if (!split(address, base_chunk_edge))
|
||||
return std::unexpected{EnvironmentError::invalid_voxel};
|
||||
std::vector<VoxelAddress> ordered{footprint.begin(), footprint.end()};
|
||||
std::sort(ordered.begin(), ordered.end());
|
||||
if (std::adjacent_find(ordered.begin(), ordered.end()) != ordered.end())
|
||||
return std::unexpected{EnvironmentError::invalid_request};
|
||||
EnvironmentView result{fields, terrain, tick, requested_mask};
|
||||
result.samples_.reserve(ordered.size());
|
||||
result.terrain_tokens_.reserve(ordered.size());
|
||||
const auto sites = fields.sites();
|
||||
for (const auto voxel : ordered) {
|
||||
const auto site = std::lower_bound(
|
||||
sites.begin(), sites.end(), voxel,
|
||||
[](const FieldSite& s, VoxelAddress address) { return s.voxel < address; });
|
||||
if (site == sites.end() || site->voxel != voxel)
|
||||
return std::unexpected{EnvironmentError::missing_site};
|
||||
const auto location = split(voxel, base_chunk_edge);
|
||||
const auto token = terrain.view(location->chunk);
|
||||
if (!token)
|
||||
return std::unexpected{EnvironmentError::missing_chunk};
|
||||
const auto cell = terrain.read(*token, location->local);
|
||||
const auto size = measure_habitat(site->box);
|
||||
const auto site_index = static_cast<std::size_t>(site - sites.begin());
|
||||
const auto capacity = fields.water_capacity(site_index);
|
||||
if (!cell || !size || !capacity)
|
||||
return std::unexpected{EnvironmentError::source_error};
|
||||
EnvironmentSample sample{voxel,
|
||||
location->chunk,
|
||||
token->revision(),
|
||||
*site,
|
||||
*cell,
|
||||
*size,
|
||||
*capacity,
|
||||
{},
|
||||
MoistureStatus::not_requested,
|
||||
{}};
|
||||
for (std::size_t k = 0; k < field_count; ++k) {
|
||||
if ((requested_mask & (1U << k)) == 0)
|
||||
continue;
|
||||
const auto value = fields.read(site_index, static_cast<Field>(k));
|
||||
if (!value) {
|
||||
if (value.error() != FieldError::missing)
|
||||
return std::unexpected{EnvironmentError::source_error};
|
||||
if (k == 0)
|
||||
sample.moisture_status = MoistureStatus::missing_water;
|
||||
continue;
|
||||
}
|
||||
if (k >= static_cast<std::size_t>(Field::organic_energy)) {
|
||||
sample.fields[k] = accounting::Picojoules{*value};
|
||||
} else {
|
||||
const auto concentration = mass_concentration({*value}, size->volume);
|
||||
if (!concentration)
|
||||
return std::unexpected{EnvironmentError::source_error};
|
||||
sample.fields[k] = EnvironmentMass{{*value}, *concentration};
|
||||
}
|
||||
if (k == 0) {
|
||||
if (capacity->value == 0) {
|
||||
sample.moisture_status = MoistureStatus::zero_capacity;
|
||||
} else {
|
||||
const auto saturation = water_saturation({*value}, *capacity);
|
||||
if (!saturation)
|
||||
return std::unexpected{EnvironmentError::source_error};
|
||||
sample.moisture_status = MoistureStatus::available;
|
||||
sample.moisture = *saturation;
|
||||
}
|
||||
}
|
||||
}
|
||||
result.samples_.push_back(sample);
|
||||
result.terrain_tokens_.push_back(*token);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
std::expected<void, EnvironmentError> validate_environment(const EnvironmentView& view,
|
||||
const FieldStore& fields,
|
||||
const ChunkStore& terrain,
|
||||
std::uint64_t current_tick) noexcept {
|
||||
if (view.samples_.empty() || view.samples_.size() != view.terrain_tokens_.size())
|
||||
return std::unexpected{EnvironmentError::invalid_view};
|
||||
if (view.fields_ != &fields || view.terrain_ != &terrain)
|
||||
return std::unexpected{EnvironmentError::wrong_owner};
|
||||
if (view.tick_ != current_tick)
|
||||
return std::unexpected{EnvironmentError::stale_tick};
|
||||
if (view.field_revision_ != fields.last_command())
|
||||
return std::unexpected{EnvironmentError::stale_fields};
|
||||
for (const auto token : view.terrain_tokens_)
|
||||
if (!terrain.read(token, {}))
|
||||
return std::unexpected{EnvironmentError::stale_terrain};
|
||||
return {};
|
||||
}
|
||||
} // namespace evo::world
|
||||
Reference in New Issue
Block a user