Implement M9.4 revision-tagged microscopic environment rendering

Add SDL-free owned EnvironmentPresentation copied from M6.3 views (no
store pointers or validation tokens), a single-slot EnvironmentLayer that
rejects foreign/regressed updates and reports none/current/stale against
observed authority watermarks, and a bounded camera projection with
explicit missing, not-requested, present-zero, undefined-moisture and
stale encodings (at most 2313 clipped rectangles, reused buffer).

The engineering app renders a local in-memory fixture authority with
configurable refresh cadence and layer selection. ADR-0080 records the
decision; validation evidence and limits are in VALIDATION.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-01 17:45:53 -04:00
co-authored by Claude Opus 5.5
parent bb0412cd0f
commit feca07f791
20 changed files with 1393 additions and 62 deletions
+163
View File
@@ -0,0 +1,163 @@
#include "evo/client/environment_view.hpp"
#include <algorithm>
#include <utility>
#include <variant>
namespace evo::client {
namespace {
using world::MoistureStatus;
constexpr auto water = static_cast<std::size_t>(world::Field::water);
bool in_range(std::int64_t value) {
return value >= -world::voxel_limit && value < world::voxel_limit;
}
bool ordered(const EnvironmentCell& a, const EnvironmentCell& b) {
return std::pair{a.voxel.x, a.voxel.y} < std::pair{b.voxel.x, b.voxel.y};
}
bool valid_moisture(const EnvironmentCell& cell, std::uint32_t mask) {
if ((mask & (1U << water)) == 0)
return cell.moisture_status == MoistureStatus::not_requested;
switch (cell.moisture_status) {
case MoistureStatus::missing_water:
return cell.presence[water] == FieldPresence::missing;
case MoistureStatus::zero_capacity:
return cell.presence[water] == FieldPresence::present;
case MoistureStatus::available:
return cell.presence[water] == FieldPresence::present && cell.moisture.denominator != 0 &&
cell.moisture.numerator <= cell.moisture.denominator;
case MoistureStatus::not_requested:
return false;
}
return false;
}
bool valid(const EnvironmentCell& cell, std::uint32_t mask, std::int64_t z) {
if (cell.voxel.z != z || !in_range(cell.voxel.x) || !in_range(cell.voxel.y))
return false; // Any u16 fill is within fill_scale.
const auto measure = world::measure_habitat(cell.habitat);
if (!measure || measure->volume != cell.volume)
return false;
for (std::size_t k = 0; k < world::field_count; ++k) {
const bool requested = (mask & (1U << k)) != 0;
if (requested == (cell.presence[k] == FieldPresence::not_requested) ||
(cell.presence[k] != FieldPresence::present && cell.quantity[k] != 0))
return false;
}
return valid_moisture(cell, mask);
}
bool regressed(EnvironmentRevision next, EnvironmentRevision previous) {
return next.tick < previous.tick || next.field_revision < previous.field_revision;
}
// Both sides are strictly ordered by (x, y): a linear merge finds shared voxels.
bool chunk_regressed(std::span<const EnvironmentCell> next,
std::span<const EnvironmentCell> previous) {
std::size_t i = 0, j = 0;
while (i < next.size() && j < previous.size()) {
if (ordered(next[i], previous[j]))
++i;
else if (ordered(previous[j], next[i]))
++j;
else if (next[i++].chunk_revision < previous[j++].chunk_revision)
return true;
}
return false;
}
} // namespace
std::expected<EnvironmentPresentation, ViewError>
EnvironmentPresentation::create(EnvironmentRevision revision, std::uint32_t requested_mask,
std::int64_t slice_z, std::span<const EnvironmentCell> cells) {
if (requested_mask == 0 || (requested_mask & ~world::all_environment_fields) != 0 ||
!in_range(slice_z))
return std::unexpected(ViewError::invalid_view);
if (cells.empty())
return std::unexpected(ViewError::empty_slice);
if (cells.size() > max_environment_cells)
return std::unexpected(ViewError::too_many_cells);
for (std::size_t i = 0; i < cells.size(); ++i)
if (!valid(cells[i], requested_mask, slice_z) ||
(i > 0 && !ordered(cells[i - 1], cells[i])))
return std::unexpected(ViewError::invalid_view);
return EnvironmentPresentation{revision, requested_mask, slice_z, cells};
}
std::expected<EnvironmentPresentation, ViewError>
present_environment(const world::EnvironmentView& view, std::int64_t slice_z) {
std::vector<EnvironmentCell> cells;
for (const auto& sample : view.samples()) {
if (sample.voxel.z != slice_z)
continue;
EnvironmentCell cell;
cell.voxel = sample.voxel;
cell.chunk_revision = sample.chunk_revision;
cell.habitat = sample.habitat.box;
cell.volume = sample.measure.volume;
cell.substrate = sample.habitat.substrate;
cell.terrain_material = sample.terrain.material();
cell.terrain_fill = sample.terrain.fill();
cell.moisture_status = sample.moisture_status;
cell.moisture = sample.moisture.value_or(world::WaterSaturation{});
for (std::size_t k = 0; k < world::field_count; ++k) {
if ((view.requested_mask() & (1U << k)) == 0)
continue;
const auto& field = sample.fields[k];
cell.presence[k] = field ? FieldPresence::present : FieldPresence::missing;
if (field)
cell.quantity[k] = std::visit(
[](const auto& value) -> std::uint64_t {
if constexpr (std::is_same_v<std::decay_t<decltype(value)>,
world::EnvironmentMass>)
return value.amount.value;
else
return value.value;
},
*field);
}
cells.push_back(cell);
}
// World views order by (x, y, z); one Z slice therefore keeps (x, y) order.
return EnvironmentPresentation::create({view.identity(), view.tick(), view.field_revision()},
view.requested_mask(), slice_z, cells);
}
std::expected<void, ViewError> EnvironmentLayer::accept(EnvironmentPresentation next) {
const auto revision = next.revision();
if (latest_ && revision.identity != latest_->identity)
return std::unexpected(ViewError::foreign_world);
if (current_ && (regressed(revision, current_->revision()) ||
chunk_regressed(next.cells(), current_->cells())))
return std::unexpected(ViewError::regressed);
if (latest_)
latest_ = EnvironmentRevision{revision.identity, std::max(latest_->tick, revision.tick),
std::max(latest_->field_revision, revision.field_revision)};
else
latest_ = revision;
current_ = std::move(next);
stale_ = *latest_ != revision;
return {};
}
std::expected<void, ViewError> EnvironmentLayer::observe(EnvironmentRevision latest) {
if (latest_ && latest.identity != latest_->identity)
return std::unexpected(ViewError::foreign_world);
if (latest_ && regressed(latest, *latest_))
return std::unexpected(ViewError::regressed);
latest_ = latest;
stale_ = stale_ || (current_ && latest != current_->revision());
return {};
}
void EnvironmentLayer::mark_stale() noexcept {
stale_ = current_.has_value();
}
void EnvironmentLayer::reset() noexcept {
current_.reset();
latest_.reset();
stale_ = false;
}
Freshness EnvironmentLayer::freshness() const noexcept {
if (!current_)
return Freshness::none;
return stale_ ? Freshness::stale : Freshness::current;
}
} // namespace evo::client