#include "evo/server/lifecycle/cellular_admin_view.hpp" #include #include #include #include #include namespace evo::server { namespace { void append(std::string& out, std::uint64_t value) { out += std::to_string(value); } void append_inventory(std::string& out, const accounting::Inventory& inventory) { append(out, inventory.energy.value); for (const auto& channel : inventory.matter) { out.push_back(','); append(out, channel.value); } } // Energy then the four matter channels, summed without the inventory's overflow checks: a // region's stock is far below 2^64 in every channel. using Totals = std::array; void add(Totals& totals, const accounting::Inventory& inventory) { totals[0] += inventory.energy.value; for (std::size_t i = 0; i < inventory.matter.size(); ++i) totals[i + 1] += inventory.matter[i].value; } void append_totals(std::string& out, std::string_view name, const Totals& totals) { out += "\""; out += name; out += "\":["; for (std::size_t i = 0; i < totals.size(); ++i) { if (i != 0) out.push_back(','); append(out, totals[i]); } out.push_back(']'); } } // namespace std::string cellular_account_text(const LifeAccount& account) { constexpr std::string_view digits = "0123456789abcdef"; std::string out; out.reserve(36); for (std::size_t i = 0; i < account.size(); ++i) { if (i == 4 || i == 6 || i == 8 || i == 10) out.push_back('-'); out.push_back(digits[account[i] >> 4U]); out.push_back(digits[account[i] & 0x0FU]); } return out; } std::string render_cellular_admin_view(const CellularLives& state, const CellularAdminOnline& online, CellularAdminViewLimits limits, std::string_view rules_sha256) { const auto lives = state.lives(); const auto patches = state.habitat().patches(); std::string out; out.reserve(256 + std::min(lives.size(), limits.cells) * 128 + std::min(patches.size(), limits.deposits) * 64); out += "{\"world\":"; append(out, state.world()); out += ",\"region\":"; append(out, state.region()); out += ",\"tick\":"; append(out, state.tick()); out += ",\"rules\":"; append(out, state.rules_version()); if (!rules_sha256.empty()) { out += ",\"rules_sha256\":\""; out += rules_sha256; out.push_back('"'); } out += ",\"area\":["; append(out, state.area().width_um); out.push_back(','); append(out, state.area().height_um); out += "],\"spent\":["; append_inventory(out, state.spent()); out += "],\"heat\":"; append(out, state.heat().energy.value); // The lineage archive never forgets: births and genomes recorded against its limits, at // which the region stops ticking. out += ",\"archive\":["; append(out, state.archive().recorded()); out.push_back(','); append(out, biology::max_archived_organisms); out.push_back(','); append(out, state.archive().archived()); out.push_back(','); append(out, biology::max_archived_genomes); out.push_back(']'); if (state.rules_version() >= cellular_lives_revamp_rules) { const auto& rules = state.revamp_rules(); out += ",\"drip\":["; append(out, rules.drip_interval_ticks); out.push_back(','); append(out, rules.drip_bubble_pj); out.push_back(','); append(out, rules.drip_bubble_fg); out.push_back(','); append(out, rules.heat_recycle_per_thousand); out += rules.drip_absorb ? ",1]" : ",0]"; // The organism cap and the wildlife cap (0: the organism cap). out += ",\"caps\":["; append(out, rules.organism_cap); out.push_back(','); append(out, rules.wildlife_cap); out.push_back(']'); } out += ",\"cells_total\":"; append(out, lives.size()); out += ",\"deposits_total\":"; append(out, patches.size()); // Counts and stock over every life and deposit, beyond the listing limits. std::array counts{}; // Players, wildlife, patches, pools, bubbles. Totals reserve{}, body{}; std::array deposits_by_kind{}; for (const auto& life : lives) { ++counts[life.account == LifeAccount{} ? 1 : 0]; add(reserve, life.reserve); add(body, life.body); } for (const auto& patch : patches) { const auto kind = static_cast(patch.kind); if (kind < deposits_by_kind.size()) { ++counts[2 + kind]; add(deposits_by_kind[kind], patch.stock); } } out += ",\"counts\":["; for (std::size_t i = 0; i < counts.size(); ++i) { if (i != 0) out.push_back(','); append(out, counts[i]); } out += "],\"totals\":{"; append_totals(out, "reserve", reserve); out.push_back(','); append_totals(out, "body", body); out.push_back(','); append_totals(out, "patches", deposits_by_kind[0]); out.push_back(','); append_totals(out, "pools", deposits_by_kind[1]); out.push_back(','); append_totals(out, "bubbles", deposits_by_kind[2]); out.push_back('}'); // Lives are kept in ascending life id; a prefix is the lowest ids. out += ",\"cells\":["; const auto cells = std::min(lives.size(), limits.cells); for (std::size_t i = 0; i < cells; ++i) { const auto& life = lives[i]; const bool wildlife = life.account == LifeAccount{}; std::uint64_t flags = wildlife ? 1U : 0U; if (!wildlife && online && online(life.account)) flags |= 2U; if (life.presence == CellularPresence::lingering) flags |= 4U; if (life.presence == CellularPresence::suspended) flags |= 8U; std::uint64_t body_mass{}; for (const auto& channel : life.body.matter) body_mass += channel.value; if (i != 0) out.push_back(','); out += "[\""; append(out, life.identity.life); out += "\",\""; append(out, life.identity.organism); out += "\",\""; if (!wildlife) out += cellular_account_text(life.account); out += "\","; append(out, life.position.x_um); out.push_back(','); append(out, life.position.y_um); out.push_back(','); append(out, state.extent_of(life).value_or(0)); out.push_back(','); append(out, flags); out.push_back(','); append(out, life.revamp.members); out.push_back(','); append_inventory(out, life.reserve); out.push_back(','); append(out, body_mass); out += ",\""; append(out, cellular_lineage_root(state, life.identity.life)); out += "\"]"; } // Patches are kept by address; the view lists them by ascending id. std::vector listed; listed.reserve(patches.size()); for (const auto& patch : patches) listed.push_back(&patch); const auto deposits = std::min(listed.size(), limits.deposits); std::ranges::partial_sort(listed, listed.begin() + static_cast(deposits), {}, [](const biology::CellularPatch* patch) { return patch->id; }); out += "],\"deposits\":["; for (std::size_t i = 0; i < deposits; ++i) { const auto& patch = *listed[i]; if (i != 0) out.push_back(','); out.push_back('['); append(out, patch.id); out.push_back(','); append(out, static_cast(patch.kind)); out.push_back(','); append(out, patch.address.x_um); out.push_back(','); append(out, patch.address.y_um); out.push_back(','); append(out, patch.radius_um); out.push_back(','); append_inventory(out, patch.stock); out.push_back(']'); } out += "]}\n"; return out; } } // namespace evo::server