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
+341
View File
@@ -0,0 +1,341 @@
#include "evo/client/organism_inspector.hpp"
#include "evo/render/environment.hpp"
#include "evo/render/organism.hpp"
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <exception>
#include <optional>
#include <source_location>
#include <stdexcept>
#include <string>
#include <vector>
namespace {
using namespace evo;
using biology::Capability;
using client::Freshness;
using client::OrganismCell;
using client::OrganismState;
using client::ViewError;
constexpr world::WorldIdentity identity{3, 4};
constexpr biology::EnvironmentState temperate{
293'150, {{1'000'000'000}, {accounting::Femtograms{1'000'000'000}, {}, {}, {}}}};
void check(bool ok, std::source_location at = std::source_location::current()) {
if (!ok)
throw std::runtime_error("client organisms line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
template <typename T, typename E> bool refused(const std::expected<T, E>& r, E e) {
return !r && r.error() == e;
}
bool same(render::Color a, render::Color b) {
return a.red == b.red && a.green == b.green && a.blue == b.blue && a.alpha == b.alpha;
}
bool same(const render::Rectangle& a, const render::Rectangle& b) {
return a.x == b.x && a.y == b.y && a.width == b.width && a.height == b.height &&
same(a.color, b.color);
}
bool same(const std::vector<render::Rectangle>& a, const std::vector<render::Rectangle>& b) {
return a.size() == b.size() &&
std::equal(a.begin(), a.end(), b.begin(),
[](const auto& x, const auto& y) { return same(x, y); });
}
// Development-test genome (ADR-0031): full growth is an exact 20 um cube.
biology::Genome genome(std::uint32_t conversion = 400, std::uint32_t movement = 0,
std::uint32_t ingestion = 0) {
biology::GenomeDraft draft{
7,
1,
1,
{},
{{Capability::energy_conversion, conversion, 0}, {Capability::membrane, 10, 0}}};
if (movement != 0)
draft.capabilities.push_back({Capability::movement, movement, 0});
if (ingestion != 0)
draft.capabilities.push_back({Capability::ingestion, ingestion, 0});
draft.capabilities.push_back({Capability::target_size, 8'000'000, 0});
auto result = biology::Genome::create(draft);
CHECK(result);
return *result;
}
OrganismCell organism(std::uint64_t id, std::uint64_t x_um, const biology::Genome& g,
biology::GrowthState growth = {1000, 0},
OrganismState state = OrganismState::alive) {
auto cell =
client::present_organism(id, {0, 0, 0}, {x_um}, {500000}, g, growth, temperate, state);
CHECK(cell);
return *cell;
}
client::OrganismPresentation presentation(std::vector<OrganismCell> cells,
client::OrganismRevision revision = {identity, 1, 1}) {
auto result = client::OrganismPresentation::create(revision, 0, cells);
CHECK(result);
return std::move(*result);
}
client::Camera camera(client::WorldPoint centre, double scale,
client::Viewport viewport = {800, 600, 1600, 1200}) {
auto result = client::Camera::create(viewport, centre, scale);
CHECK(result);
return *result;
}
std::vector<render::Rectangle> draw(const client::OrganismPresentation& p, const client::Camera& c,
Freshness f = Freshness::current, std::size_t budget = 1755) {
std::vector<render::Rectangle> out;
const auto report = render::append_organisms(out, budget, &p, f, c);
CHECK(report && out.size() <= budget);
return out;
}
void derived_from_biology() {
const auto g = genome();
const auto cell = organism(1, 500000, g);
const auto expected = biology::derive_phenotype(g, {1000, 0}, temperate);
CHECK(expected && cell.phenotype == *expected && cell.phenotype.extent == 20);
// Structural binding pins the field list: adding a controller/owner/label
// field fails to compile here (PROJECT_VISION shared presentation rule).
const auto& [id, voxel, x, y, phenotype, growth, state] = cell;
CHECK(id == 1 && voxel.z == 0 && x.value == 500000 && y.value == 500000 &&
phenotype.extent == 20 && growth.realized_per_thousand == 1000 &&
state == OrganismState::alive);
// 20 um at 1000 units/mm is 20 logical units, centred at voxel offset 500 mm.
const auto c = camera({500, 500}, 1000);
const auto b = render::organism_bounds(cell, c);
CHECK(b.left == 390 && b.right == 410 && b.top == 290 && b.bottom == 310);
const auto plain = draw(presentation({cell}), c);
// Body + membrane outline + status chip; no motility ring or ingestion mark.
CHECK(plain.size() == 1 + 4 + 1);
CHECK(same(plain[0].color, render::organism_fill(cell)));
// Membrane share 28.8e6 / 36.8e6 of a 20-unit side: thickness capped at side/4.
CHECK(plain[1].height == 5.0F);
const auto capable = organism(1, 500000, genome(400, 50, 2000));
CHECK(capable.phenotype.movement_speed > 0 && capable.phenotype.ingestion_rate > 0);
const auto full = draw(presentation({capable}), c);
CHECK(full.size() == render::organism_detail_rectangles + 1);
CHECK(same(full[0].color, render::organism_palette::ring));
CHECK(same(full[9].color, render::organism_palette::mouth));
// Conversion, partial growth and damage change the body colour; nothing else does.
CHECK(!same(render::organism_fill(organism(1, 500000, genome(900))),
render::organism_fill(cell)));
const auto half = organism(1, 500000, g, {500, 0});
CHECK(half.phenotype.extent == 15 &&
!same(render::organism_fill(half), render::organism_fill(cell)));
const auto hurt = organism(1, 500000, g, {1000, 600});
CHECK(render::organism_fill(hurt).red > render::organism_fill(hurt).green);
// Zoomed out, the uniform visibility floor applies and detail collapses.
const auto far = camera({500, 500}, 0.05);
const auto tiny = render::organism_bounds(cell, far);
CHECK(tiny.right - tiny.left == render::organism_visibility_floor);
const auto collapsed = draw(presentation({capable}), far);
CHECK(collapsed.size() == 2 + 1 && same(collapsed[0].color, render::organism_palette::halo));
CHECK(collapsed[0].width == 7.0F && same(collapsed[1].color, render::organism_fill(capable)));
}
// Controllers are host bookkeeping only; they never reach presentation inputs.
enum class Controller : std::uint8_t { player, npc };
struct HostOrganism {
Controller controller;
OrganismCell cell;
};
void controller_parity() {
const auto g = genome(400, 50, 2000);
const std::array<HostOrganism, 2> hosted{{{Controller::player, organism(5, 500000, g)},
{Controller::npc, organism(900, 500000, g)}}};
CHECK(hosted[0].controller != hosted[1].controller);
const auto c = camera({500, 500}, 1000);
const auto player = draw(presentation({hosted[0].cell}), c);
const auto npc = draw(presentation({hosted[1].cell}), c);
CHECK(same(player, npc)); // Identical biology renders identically.
std::vector<render::Rectangle> a, b;
CHECK(client::append_inspector(a, 25, hosted[0].cell, c) &&
client::append_inspector(b, 25, hosted[1].cell, c) && same(a, b));
// Same biology at different positions differs only by translation.
const auto moved = organism(900, 500100, g);
const auto shifted = draw(presentation({moved}), c);
CHECK(shifted.size() == player.size());
for (std::size_t i = 0; i + 1 < player.size(); ++i)
CHECK(std::abs(shifted[i].x - player[i].x - 100) < 1e-3F && shifted[i].y == player[i].y &&
shifted[i].width == player[i].width && same(shifted[i].color, player[i].color));
// Draw output contains only biology encodings and status colours.
for (const auto& r : player)
CHECK(!same(r.color, render::organism_palette::truncated));
}
void death_and_removal() {
const auto g = genome(400, 50, 2000);
const auto c = camera({500, 500}, 1000);
client::OrganismLayer layer;
CHECK(layer.accept(presentation({organism(1, 500000, g), organism(2, 500200, g)})));
CHECK(draw(*layer.current(), c).size() == 2 * render::organism_detail_rectangles + 1);
// Death: a remnant without motility ring or ingestion mark, same size.
const auto dead = organism(2, 500200, g, {1000, 0}, OrganismState::dead);
CHECK(layer.accept(presentation({organism(1, 500000, g), dead}, {identity, 2, 2})));
const auto after = draw(*layer.current(), c);
CHECK(after.size() == render::organism_detail_rectangles + 1 + 4 + 1);
const auto remnant = after[render::organism_detail_rectangles];
CHECK(same(remnant.color, render::organism_fill(dead)));
CHECK(remnant.width == 20.0F &&
same(render::organism_fill(dead),
render::organism_fill(
organism(2, 500200, genome(400), {1000, 0}, OrganismState::dead))));
// Removal: absent from the next presentation, so nothing is drawn for it.
CHECK(layer.accept(presentation({organism(1, 500000, g)}, {identity, 3, 3})));
CHECK(draw(*layer.current(), c).size() == render::organism_detail_rectangles + 1);
CHECK(layer.accept(presentation({}, {identity, 4, 4})) && layer.current()->cells().empty());
const auto empty = draw(*layer.current(), c);
CHECK(empty.size() == 1 && same(empty[0].color, render::palette::status_current));
}
void dense_scenes() {
const auto g = genome(400, 50, 2000);
std::vector<OrganismCell> cells;
cells.reserve(client::max_organism_cells);
for (std::uint64_t i = 0; i < client::max_organism_cells; ++i)
cells.push_back(organism(i + 1, 1000 * (i % 32) + 484000, g, {1000, 0}));
for (std::size_t i = 0; i < cells.size(); ++i)
cells[i].y = {1000 * (i / 32) + 484000};
const auto p = presentation(cells);
// Every organism visible at full scale: detail collapses to one body each.
const auto c = camera({500, 500}, 25, {1280, 1280, 1280, 1280});
std::vector<render::Rectangle> out;
auto report = render::append_organisms(out, 1755, &p, Freshness::current, c);
CHECK(report && report->visible == 1024 && report->drawn == 1024 &&
report->detail == render::OrganismDetail::body_only && out.size() == 1025);
// A small budget truncates explicitly: drawn < visible and the marker appears.
out.clear();
report = render::append_organisms(out, 500, &p, Freshness::stale, c);
CHECK(report && report->drawn == 498 && out.size() == 500);
CHECK(same(out.back().color, render::organism_palette::truncated));
CHECK(same(out[out.size() - 2].color, render::palette::status_stale));
out.clear();
CHECK(refused(render::append_organisms(out, 1, &p, Freshness::current, c),
render::OrganismRenderError::invalid_budget));
// A narrow view draws few organisms at full detail.
const auto zoomed = camera({500, 500}, 1000, {200, 200, 200, 200});
report = render::append_organisms(out, 1755, &p, Freshness::current, zoomed);
CHECK(report && report->detail == render::OrganismDetail::full && report->visible > 0 &&
report->visible < 20 && out.size() <= report->visible * 10 + 1);
// Worst-case frame: environment bound + organisms + inspector + host UI fit Frame.
out.assign(render::max_environment_rectangles, {0, 0, 1, 1, {}});
CHECK(render::append_organisms(out, 1755, &p, Freshness::current, c));
CHECK(client::append_inspector(out, client::inspector_rectangles, cells[0], c));
out.resize(out.size() + 3, {0, 0, 1, 1, {}});
CHECK(out.size() <= render::max_rectangles && render::Frame::create(out));
for (const auto& r : out)
CHECK(r.x >= 0 && r.y >= 0 && r.x + r.width <= 1280.01F && r.y + r.height <= 1280.01F);
}
void ordering_and_freshness() {
const auto g = genome();
client::OrganismLayer layer;
CHECK(layer.freshness() == Freshness::none);
const auto c = camera({500, 500}, 1000);
std::vector<render::Rectangle> out;
CHECK(render::append_organisms(out, 10, nullptr, Freshness::current, c));
CHECK(out.size() == 1 && same(out[0].color, render::palette::status_none));
CHECK(layer.accept(presentation({organism(1, 500000, g)}, {identity, 5, 5})));
CHECK(refused(layer.accept(presentation({}, {identity, 4, 9})), ViewError::regressed));
CHECK(refused(layer.accept(presentation({}, {identity, 9, 4})), ViewError::regressed));
CHECK(refused(layer.accept(presentation({}, {{3, 5}, 9, 9})), ViewError::foreign_world));
CHECK(refused(layer.observe({identity, 5, 4}), ViewError::regressed));
CHECK(layer.observe({identity, 5, 5}) && layer.freshness() == Freshness::current);
CHECK(layer.observe({identity, 6, 5}) && layer.freshness() == Freshness::stale);
const auto stale = draw(*layer.current(), c, layer.freshness());
CHECK(same(stale[0].color, render::dim(render::organism_fill(layer.current()->cells()[0]))));
CHECK(same(stale.back().color, render::palette::status_stale));
CHECK(layer.accept(presentation({organism(1, 500000, g)}, {identity, 6, 5})) &&
layer.freshness() == Freshness::current);
layer.mark_stale();
CHECK(layer.freshness() == Freshness::stale);
layer.reset();
CHECK(layer.freshness() == Freshness::none && layer.accept(presentation({}, {{3, 5}, 1, 1})));
}
void invalid_cells() {
const auto valid = organism(2, 500000, genome());
const auto create = [](std::vector<OrganismCell> cells, std::int64_t z = 0) {
return client::OrganismPresentation::create({identity, 1, 1}, z, cells);
};
CHECK(create({valid}) && create({}));
CHECK(refused(create({valid}, world::voxel_limit), ViewError::invalid_view));
CHECK(refused(create({valid, valid}), ViewError::invalid_view));
auto lower = valid;
lower.id = 1;
CHECK(create({lower, valid}) && refused(create({valid, lower}), ViewError::invalid_view));
const auto broken = [&](auto mutate) {
auto cell = valid;
mutate(cell);
return refused(create({cell}), ViewError::invalid_view);
};
CHECK(broken([](OrganismCell& c) { c.id = 0; }));
CHECK(broken([](OrganismCell& c) { c.voxel.z = 1; }));
CHECK(broken([](OrganismCell& c) { c.voxel.x = world::voxel_limit; }));
CHECK(broken([](OrganismCell& c) { c.x.value = 1'000'000; }));
CHECK(broken([](OrganismCell& c) { c.y.value = 1'000'000; }));
CHECK(broken([](OrganismCell& c) { c.phenotype.total_mass += 1; }));
CHECK(
broken([](OrganismCell& c) { c.phenotype.structural_mass = c.phenotype.total_mass + 1; }));
CHECK(broken([](OrganismCell& c) { c.phenotype.extent = 1'000'001; }));
CHECK(broken([](OrganismCell& c) { c.phenotype.conversion_per_thousand = 1001; }));
CHECK(broken([](OrganismCell& c) { c.growth.realized_per_thousand = 1001; }));
CHECK(broken([](OrganismCell& c) { c.growth.damage_per_thousand = 1001; }));
// Out-of-range enumerator models untrusted decoded input; rejecting it is the point.
// NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
CHECK(broken([](OrganismCell& c) { c.state = OrganismState{2}; }));
std::vector<OrganismCell> many;
for (std::uint64_t i = 1; i <= client::max_organism_cells + 1; ++i) {
auto cell = valid;
cell.id = i;
many.push_back(cell);
}
CHECK(refused(create(many), ViewError::too_many_cells));
// Invalid ids and phenotype derivation failures surface as invalid views.
CHECK(refused(client::present_organism(0, {}, {}, {}, genome(), {1000, 0}, temperate,
OrganismState::alive),
ViewError::invalid_view));
CHECK(refused(client::present_organism(1, {}, {}, {}, genome(), {1001, 0}, temperate,
OrganismState::alive),
ViewError::invalid_view));
}
void picking_and_inspector() {
const auto g = genome(400, 50, 2000);
const auto p = presentation({organism(1, 500000, g), organism(2, 500010, g)});
const auto c = camera({500, 500}, 1000);
CHECK(client::pick_organism(p, c, {395, 300}) == std::optional<std::size_t>{0});
CHECK(client::pick_organism(p, c, {405, 300}) == std::optional<std::size_t>{1}); // Topmost.
CHECK(!client::pick_organism(p, c, {100, 100}));
// Zoomed out, picking uses the same floored box that is drawn.
const auto far = camera({500, 500}, 0.05);
const auto b = render::organism_bounds(p.cells()[0], far);
CHECK(client::pick_organism(p, far, {(b.left + b.right) / 2, (b.top + b.bottom) / 2}));
const auto values = client::inspector_gauge_values(p.cells()[0]);
CHECK(std::abs(values[2] - 28.8 / 36.8) < 1e-12 && values[6] == 0.4 && values[7] == 1 &&
values[8] == 0 && values[4] > 0 && values[5] > 0);
for (const double v : values)
CHECK(v >= 0 && v <= 1);
std::vector<render::Rectangle> out;
CHECK(refused(client::append_inspector(out, client::inspector_rectangles - 1, p.cells()[0], c),
client::InspectorError::invalid_budget));
CHECK(client::append_inspector(out, client::inspector_rectangles, p.cells()[0], c));
CHECK(out.size() <= client::inspector_rectangles && out.size() >= 4 + 1 + 10);
// Gauge fill width encodes the value: conversion 0.4 of 112 units.
CHECK(std::abs(out[4 + 1 + 2 * 6 + 1].width - 44.8F) < 1e-3F);
}
} // namespace
int main() try {
derived_from_biology();
controller_parity();
death_and_removal();
dense_scenes();
ordering_and_freshness();
invalid_cells();
picking_and_inspector();
std::puts("client organism tests passed");
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "%s\n", e.what());
return 1;
}