Implement M9.5 biology-derived organism rendering and inspector

Add OrganismPresentation cells derived via biology::derive_phenotype with
no controller, owner or level fields (pinned by test), so identical
biology renders identically for players and NPCs. OrganismLayer reuses
the ADR-0080 ordering/freshness semantics; death is a remnant state and
removal is absence. Rendering takes an explicit rectangle budget, falls
back to body-only detail and marks truncation instead of dropping data.
An opt-in rectangle-only hover inspector shows phenotype gauges; ImGui is
not adopted. ADR-0081 records the decision; evidence in VALIDATION.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-01 18:21:53 -04:00
co-authored by Claude Opus 5.5
parent feca07f791
commit 9bb147de82
21 changed files with 1169 additions and 33 deletions
+77
View File
@@ -0,0 +1,77 @@
#include "evo/client/organism_inspector.hpp"
#include "evo/render/organism.hpp"
#include <algorithm>
#include <cmath>
namespace evo::client {
namespace {
double logarithmic(std::uint64_t value, double decades) {
return std::clamp(std::log10(1 + static_cast<double>(value)) / decades, 0.0, 1.0);
}
double linear(std::uint64_t value, std::uint64_t full) {
return std::clamp(static_cast<double>(value) / static_cast<double>(full), 0.0, 1.0);
}
void fill(std::vector<render::Rectangle>& out, render::ScreenBox b, double width, double height,
render::Color color) {
const double left = std::max(b.left, 0.0), top = std::max(b.top, 0.0);
const double right = std::min(b.right, width), bottom = std::min(b.bottom, height);
if (right > left && bottom > top)
out.push_back({static_cast<float>(left), static_cast<float>(top),
static_cast<float>(right - left), static_cast<float>(bottom - top), color});
}
} // namespace
std::array<double, inspector_gauges> inspector_gauge_values(const OrganismCell& cell) noexcept {
const auto& p = cell.phenotype;
return {logarithmic(p.extent, 3),
logarithmic(p.total_mass, 12),
p.total_mass == 0 ? 0.0 : linear(p.membrane_mass, p.total_mass),
logarithmic(p.maintenance, 12),
logarithmic(p.movement_speed, 5),
logarithmic(p.ingestion_rate, 6),
linear(p.conversion_per_thousand, 1000),
linear(cell.growth.realized_per_thousand, 1000),
linear(cell.growth.damage_per_thousand, 1000),
logarithmic(p.thermal_shortfall, 5)};
}
std::optional<std::size_t> pick_organism(const OrganismPresentation& presentation,
const Camera& camera, ScreenPoint pointer) noexcept {
const auto cells = presentation.cells();
for (std::size_t i = cells.size(); i-- > 0;) {
const auto b = render::organism_bounds(cells[i], camera);
if (pointer.x >= b.left && pointer.x < b.right && pointer.y >= b.top &&
pointer.y < b.bottom)
return i;
}
return std::nullopt;
}
std::expected<void, InspectorError> append_inspector(std::vector<render::Rectangle>& out,
std::size_t budget, const OrganismCell& cell,
const Camera& camera) {
if (budget < inspector_rectangles)
return std::unexpected(InspectorError::invalid_budget);
const auto viewport = camera.viewport();
const double width = viewport.width, height = viewport.height;
constexpr render::Color highlight{240, 240, 240, 255};
const auto b = render::organism_bounds(cell, camera);
const render::ScreenBox h{b.left - 3, b.top - 3, b.right + 3, b.bottom + 3};
fill(out, {h.left, h.top, h.right, h.top + 1}, width, height, highlight);
fill(out, {h.left, h.bottom - 1, h.right, h.bottom}, width, height, highlight);
fill(out, {h.left, h.top + 1, h.left + 1, h.bottom - 1}, width, height, highlight);
fill(out, {h.right - 1, h.top + 1, h.right, h.bottom - 1}, width, height, highlight);
const double left = width - 136, top = height - 104;
fill(out, {left, top, width - 8, height - 8}, width, height,
cell.state == OrganismState::dead ? render::Color{70, 60, 52, 255}
: render::Color{24, 28, 36, 255});
const auto values = inspector_gauge_values(cell);
for (std::size_t i = 0; i < values.size(); ++i) {
const double y = top + 8 + 9 * static_cast<double>(i);
fill(out, {left + 8, y, left + 120, y + 6}, width, height, {60, 64, 72, 255});
fill(out, {left + 8, y, left + 8 + 112 * values.at(i), y + 6}, width, height,
{220, 220, 220, 255});
}
return {};
}
} // namespace evo::client
+100
View File
@@ -0,0 +1,100 @@
#include "evo/client/organism_view.hpp"
#include <algorithm>
#include <utility>
namespace evo::client {
namespace {
constexpr std::uint64_t voxel_um = world::habitat_axis_limit_um;
bool in_range(std::int64_t value) {
return value >= -world::voxel_limit && value < world::voxel_limit;
}
bool valid(const OrganismCell& cell, std::int64_t z) {
const auto& p = cell.phenotype;
return cell.id != 0 && cell.voxel.z == z && in_range(cell.voxel.x) && in_range(cell.voxel.y) &&
cell.x.value < voxel_um && cell.y.value < voxel_um &&
p.structural_mass <= p.total_mass &&
p.total_mass - p.structural_mass == p.membrane_mass && p.extent <= voxel_um &&
p.conversion_per_thousand <= 1000 && cell.growth.realized_per_thousand <= 1000 &&
cell.growth.damage_per_thousand <= 1000 &&
(cell.state == OrganismState::alive || cell.state == OrganismState::dead);
}
bool regressed(OrganismRevision next, OrganismRevision previous) {
return next.tick < previous.tick || next.revision < previous.revision;
}
} // namespace
std::expected<OrganismPresentation, ViewError>
OrganismPresentation::create(OrganismRevision revision, std::int64_t slice_z,
std::span<const OrganismCell> cells) {
if (!in_range(slice_z))
return std::unexpected(ViewError::invalid_view);
if (cells.size() > max_organism_cells)
return std::unexpected(ViewError::too_many_cells);
for (std::size_t i = 0; i < cells.size(); ++i)
if (!valid(cells[i], slice_z) || (i > 0 && cells[i - 1].id >= cells[i].id))
return std::unexpected(ViewError::invalid_view);
return OrganismPresentation{revision, slice_z, cells};
}
std::expected<OrganismCell, ViewError>
present_organism(std::uint64_t id, world::VoxelAddress voxel, world::Micrometres x,
world::Micrometres y, const biology::Genome& genome, biology::GrowthState growth,
const biology::EnvironmentState& environment, OrganismState state) {
const auto phenotype = biology::derive_phenotype(genome, growth, environment);
if (!phenotype)
return std::unexpected(ViewError::invalid_view);
OrganismCell cell;
cell.id = id;
cell.voxel = voxel;
cell.x = x;
cell.y = y;
cell.phenotype = *phenotype;
cell.growth = growth;
cell.state = state;
if (!valid(cell, voxel.z))
return std::unexpected(ViewError::invalid_view);
return cell;
}
std::expected<void, ViewError> OrganismLayer::accept(OrganismPresentation next) {
const auto revision = next.revision();
if (latest_ && revision.identity != latest_->identity)
return std::unexpected(ViewError::foreign_world);
if (current_ && regressed(revision, current_->revision()))
return std::unexpected(ViewError::regressed);
if (latest_)
latest_ = OrganismRevision{revision.identity, std::max(latest_->tick, revision.tick),
std::max(latest_->revision, revision.revision)};
else
latest_ = revision;
current_ = std::move(next);
stale_ = *latest_ != revision;
return {};
}
std::expected<void, ViewError> OrganismLayer::observe(OrganismRevision 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 OrganismLayer::mark_stale() noexcept {
stale_ = current_.has_value();
}
void OrganismLayer::reset() noexcept {
current_.reset();
latest_.reset();
stale_ = false;
}
Freshness OrganismLayer::freshness() const noexcept {
if (!current_)
return Freshness::none;
return stale_ ? Freshness::stale : Freshness::current;
}
} // namespace evo::client