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:
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user