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
+146
View File
@@ -0,0 +1,146 @@
#include "evo/render/organism.hpp"
#include "evo/render/environment.hpp"
#include <algorithm>
#include <cmath>
namespace evo::render {
namespace {
using client::OrganismState;
constexpr double pitch = static_cast<double>(world::cell_pitch_mm);
constexpr double um_per_mm = 1000;
std::uint8_t mix(std::uint8_t a, std::uint8_t b, double t) {
return static_cast<std::uint8_t>(std::lround(a + (b - a) * t));
}
Color mix(Color a, Color b, double t) {
return {mix(a.red, b.red, t), mix(a.green, b.green, t), mix(a.blue, b.blue, t), 255};
}
double fraction(std::uint32_t per_thousand) {
return static_cast<double>(std::min<std::uint32_t>(per_thousand, 1000)) / 1000;
}
class Emitter {
public:
Emitter(std::vector<Rectangle>& out, double width, double height, bool stale)
: out_{out}, width_{width}, height_{height}, stale_{stale} {}
void fill(ScreenBox b, 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))
return;
out_.push_back({static_cast<float>(left), static_cast<float>(top),
static_cast<float>(right - left), static_cast<float>(bottom - top),
stale_ ? dim(color) : color});
}
void outline(ScreenBox b, double t, Color color) {
fill({b.left, b.top, b.right, b.top + t}, color);
fill({b.left, b.bottom - t, b.right, b.bottom}, color);
fill({b.left, b.top + t, b.left + t, b.bottom - t}, color);
fill({b.right - t, b.top + t, b.right, b.bottom - t}, color);
}
private:
std::vector<Rectangle>& out_;
double width_, height_;
bool stale_;
};
bool visible(ScreenBox b, double width, double height) {
return b.right > 0 && b.left < width && b.bottom > 0 && b.top < height;
}
void draw(Emitter& emit, const client::OrganismCell& cell, ScreenBox body, OrganismDetail mode) {
const auto& p = cell.phenotype;
const bool alive = cell.state == OrganismState::alive;
const double side = body.right - body.left;
const Color fill = organism_fill(cell);
if (mode == OrganismDetail::body_only) {
emit.fill(body, fill);
return;
}
if (side < organism_detail_side) {
emit.fill({body.left - 1, body.top - 1, body.right + 1, body.bottom + 1},
organism_palette::halo);
emit.fill(body, fill);
return;
}
if (alive && p.movement_speed > 0)
emit.outline({body.left - 2, body.top - 2, body.right + 2, body.bottom + 2}, 1,
organism_palette::ring);
emit.fill(body, fill);
if (p.membrane_mass > 0 && p.total_mass > 0) {
const double share =
static_cast<double>(p.membrane_mass) / static_cast<double>(p.total_mass);
emit.outline(body, std::clamp(side * share / 2, 1.0, side / 4),
mix(fill, {255, 255, 255, 255}, 0.5));
}
if (alive && p.ingestion_rate > 0) {
const double scale =
std::min(1.0, std::log10(1 + static_cast<double>(p.ingestion_rate)) / 6);
const double half = side * (0.15 + 0.35 * scale) / 2;
const double cx = (body.left + body.right) / 2, cy = (body.top + body.bottom) / 2;
emit.fill({cx - half, cy - half, cx + half, cy + half}, organism_palette::mouth);
}
}
} // namespace
ScreenBox organism_bounds(const client::OrganismCell& cell, const client::Camera& camera) noexcept {
const auto viewport = camera.viewport();
const double scale = camera.units_per_mm();
const double x =
static_cast<double>(cell.voxel.x) * pitch + static_cast<double>(cell.x.value) / um_per_mm;
const double y =
static_cast<double>(cell.voxel.y) * pitch + static_cast<double>(cell.y.value) / um_per_mm;
// Camera projection from CAMERA_INPUT.md.
const double sx = (x - camera.centre().x_mm) * scale + viewport.width / 2.0;
const double sy = (camera.centre().y_mm - y) * scale + viewport.height / 2.0;
const double side = std::max(static_cast<double>(cell.phenotype.extent) / um_per_mm * scale,
organism_visibility_floor);
return {sx - side / 2, sy - side / 2, sx + side / 2, sy + side / 2};
}
Color organism_fill(const client::OrganismCell& cell) noexcept {
const Color base = mix(Color{40, 150, 150, 255}, Color{220, 170, 60, 255},
fraction(cell.phenotype.conversion_per_thousand));
Color color =
mix(Color{30, 35, 40, 255}, base, 0.4 + 0.6 * fraction(cell.growth.realized_per_thousand));
color = mix(color, Color{150, 30, 30, 255}, 0.8 * fraction(cell.growth.damage_per_thousand));
if (cell.state == OrganismState::dead)
color = mix(color, organism_palette::remnant, 0.75);
return color;
}
std::expected<OrganismRenderReport, OrganismRenderError>
append_organisms(std::vector<Rectangle>& out, std::size_t budget,
const client::OrganismPresentation* presentation, client::Freshness freshness,
const client::Camera& camera) {
if (budget < organism_status_rectangles)
return std::unexpected(OrganismRenderError::invalid_budget);
const auto viewport = camera.viewport();
const double width = viewport.width, height = viewport.height;
if (freshness == client::Freshness::none)
presentation = nullptr;
const bool stale = presentation != nullptr && freshness == client::Freshness::stale;
OrganismRenderReport report;
const std::size_t available = budget - organism_status_rectangles;
if (presentation != nullptr) {
for (const auto& cell : presentation->cells())
report.visible += visible(organism_bounds(cell, camera), width, height) ? 1 : 0;
if (report.visible > available / organism_detail_rectangles)
report.detail = OrganismDetail::body_only;
Emitter emit{out, width, height, stale};
for (const auto& cell : presentation->cells()) {
if (report.drawn == std::min(report.visible, available))
break;
const auto body = organism_bounds(cell, camera);
if (!visible(body, width, height))
continue;
draw(emit, cell, body, report.detail);
++report.drawn;
}
}
Emitter status{out, width, height, false};
status.fill({width - 72, 20, width - 8, 28}, presentation == nullptr ? palette::status_none
: stale ? palette::status_stale
: palette::status_current);
if (report.drawn < report.visible)
status.fill({width - 84, 20, width - 76, 28}, organism_palette::truncated);
return report;
}
} // namespace evo::render