#pragma once #include "evo/network/session_protocol.hpp" #include #include #include #include #include #include #include #include namespace evo::client_app { // Presentation and outstanding requests only. No biological prediction or saved authority. struct MutationChoice { std::uint64_t life{}, event{}, offer_tick{}; std::uint16_t operator_id{}; bool become{}; // Rules 13 (v14): continue as the offspring the choice splits off. friend bool operator==(const MutationChoice&, const MutationChoice&) = default; }; enum class CellAction : std::uint8_t { engulf, bud, mutation, emerge }; enum class CellActionState : std::uint8_t { queued, sent, admitted, unresolved, committed, no_effect, refused }; struct CellActionRecord { CellAction kind{}; CellActionState state{CellActionState::queued}; std::uint64_t id{}, life{}, tick{}; MutationChoice choice{}; network::AdmitStatus refusal{network::AdmitStatus::admitted}; // The region's reported effect of this engulf or bud, once a view carries it. std::optional engulf; std::optional bud; bool become{}; // A bud the player continues as (v14). }; struct CellEnergyChange { std::uint64_t from_tick{}, to_tick{}, amount{}; bool falling{}; }; // Rules 13 (v14): each channel's flows over the last second of views (energy, C, N, P, other). struct CellFlows { std::array collected{}, into_growth{}, burned{}; }; class CellularPlay { public: static constexpr std::size_t history_limit = 8; void initialize(std::uint64_t first_id) { next_id_ = first_id == 0 ? 1 : first_id; birth_request_ = allocate_id(); birth_pending_ = true; } [[nodiscard]] std::uint64_t allocate_id() { const auto id = next_id_; if (next_id_ != 0) next_id_ = next_id_ == std::numeric_limits::max() ? 0 : next_id_ + 1; return id; } void connected() { connected_ = true; } void disconnected() { connected_ = false; fresh_ = false; energy_sample_.reset(); energy_change_.reset(); offer_.reset(); if (pending_) { if (pending_->state == CellActionState::queued) { pending_->state = CellActionState::refused; finish(); } else { pending_->state = CellActionState::unresolved; } } } void observe(const network::CellularPerception& view) { if (view.tick < view_tick_) return; view_tick_ = view.tick; fresh_ = true; if (ended_ && view.life_identity == ended_->life_identity) return; // A retained pre-death view must never revive this identity. if (view.life_identity != 0 && view.diagnostics) { const auto energy = view.diagnostics->reserve_energy_pj; if (energy_sample_ && energy_sample_->life == view.life_identity) { if (view.tick > energy_sample_->tick) energy_change_ = CellEnergyChange{energy_sample_->tick, view.tick, energy < energy_sample_->energy ? energy_sample_->energy - energy : energy - energy_sample_->energy, energy < energy_sample_->energy}; } else { energy_change_.reset(); } energy_sample_ = EnergySample{view.life_identity, view.tick, energy}; // One sample per tick of this life, the last second (20 ticks) kept. if (!flow_samples_.empty() && flow_samples_.back().life != view.life_identity) flow_samples_.clear(); const auto& d = *view.diagnostics; if (d.has_tick_effects && (flow_samples_.empty() || flow_samples_.back().tick < view.tick)) flow_samples_.push_back( {view.life_identity, view.tick, {d.collected, d.into_growth, d.burned}}); while (!flow_samples_.empty() && flow_samples_.front().tick + 20 <= view.tick) flow_samples_.pop_front(); } // Never replay an intention onto a later life. Keep its uncertain result visible. if (life_ != view.life_identity) { offer_.reset(); engulf_feedback_.reset(); bud_feedback_.reset(); if (pending_) { pending_->state = pending_->state == CellActionState::queued ? CellActionState::refused : CellActionState::unresolved; finish(); } } life_ = view.life_identity; if (view.diagnostics && view.diagnostics->has_tick_effects) { const auto& d = *view.diagnostics; if (d.engulf != network::CellularEngulfResult::not_reported && d.engulf != network::CellularEngulfResult::not_requested) { engulf_feedback_ = std::pair{view.tick, d.engulf}; if (auto* record = awaiting_effect(CellAction::engulf, view.tick)) record->engulf = d.engulf; } if (d.bud != network::CellularBudOutcome::not_reported && d.bud != network::CellularBudOutcome::not_requested) { bud_feedback_ = std::pair{view.tick, d.bud}; if (auto* record = awaiting_effect(CellAction::bud, view.tick)) record->bud = d.bud; } } if (life_ != 0) { birth_pending_ = false; birth_result_ = network::PrimitiveLifeResult::born; if (ended_ && ended_->life_identity != life_) ended_.reset(); } } void observe_offer(const std::optional& offer) { offer_ = offer && offer->life_identity == life_ && fresh_ && !ended_ ? offer : std::nullopt; } void observe_end(const network::CellularLifeEnded& ended) { if (ended.life_identity == seen_end_ || (life_ != 0 && life_ != ended.life_identity) || (ended_ && ended_->tick >= ended.tick) || ended.tick < birth_tick_) return; seen_end_ = ended.life_identity; ended_ = ended; life_ = 0; offer_.reset(); if (pending_ && pending_->state == CellActionState::queued) { pending_->state = CellActionState::refused; finish(); } // The discrete action may still have a final result arriving in this poll. } [[nodiscard]] bool can_act() const { return connected_ && fresh_ && life_ != 0 && !ended_ && !pending_ && !birth_pending_; } [[nodiscard]] bool queue(CellAction kind, std::uint64_t tick, MutationChoice choice = {}, bool become = false) { if (!can_act() || tick == 0) return false; if (kind == CellAction::mutation && (!offer_ || choice.life != life_ || choice.event != offer_->event_index || choice.offer_tick != offer_->offer_tick || tick <= choice.offer_tick || (choice.operator_id != 0 && // Operator 0 declines the offer (ADR-0120 amendment). !std::ranges::any_of(offer_->candidates, [&](const auto& c) { return c.operator_id == choice.operator_id; })))) return false; const auto id = allocate_id(); if (id == 0) return false; pending_ = CellActionRecord{kind, CellActionState::queued, id, life_, tick, choice, network::AdmitStatus::admitted, std::nullopt, std::nullopt, kind == CellAction::bud && become}; return true; } void sent() { if (pending_) pending_->state = CellActionState::sent; } void rechecking() { if (pending_ && pending_->state != CellActionState::queued) pending_->state = CellActionState::unresolved; } void receipt(const network::ActionReceipt& receipt) { if (!pending_ || pending_->id != receipt.action_id) return; if (receipt.status == network::AdmitStatus::admitted || receipt.status == network::AdmitStatus::duplicate) { pending_->state = CellActionState::admitted; } else if (pending_->kind == CellAction::mutation && receipt.status == network::AdmitStatus::not_ready) { // The host can report not_ready after an uncertain durable reservation. pending_->state = CellActionState::unresolved; } else if (pending_->state != CellActionState::unresolved && pending_->state != CellActionState::admitted) { pending_->state = CellActionState::refused; pending_->refusal = receipt.status; finish(); } // A refused retry cannot disprove a previously admitted or uncertain action. } void outcome(const network::ActionOutcome& outcome) { if (!pending_ || pending_->id != outcome.action_id) { // Final replies may arrive after the life ended or the user kept an unknown. for (auto& record : history_) if (record.id == outcome.action_id && record.state == CellActionState::unresolved) { if (outcome.result == network::ActionResult::committed) record.state = CellActionState::committed; else if (outcome.result == network::ActionResult::committed_no_effect) record.state = CellActionState::no_effect; else if (outcome.result == network::ActionResult::not_committed) record.state = CellActionState::refused; } return; } using enum network::ActionResult; switch (outcome.result) { case committed: pending_->state = CellActionState::committed; break; case committed_no_effect: pending_->state = CellActionState::no_effect; break; case not_committed: pending_->state = CellActionState::refused; break; case pending: pending_->state = CellActionState::admitted; return; case unknown: pending_->state = CellActionState::unresolved; return; } finish(); } // User may leave an uncertain result in the bounded record, never as a success/refusal. void keep_unknown() { if (pending_ && pending_->state == CellActionState::unresolved) finish(); } [[nodiscard]] bool can_restart() const { const bool refused = birth_result_ == network::PrimitiveLifeResult::insufficient_funding || birth_result_ == network::PrimitiveLifeResult::no_placement || birth_result_ == network::PrimitiveLifeResult::cancelled; return connected_ && (fresh_ || refused) && life_ == 0 && !birth_pending_ && (birth_result_ != network::PrimitiveLifeResult::existing_life || ended_); } [[nodiscard]] bool restart() { if (!can_restart()) return false; const auto id = allocate_id(); if (id == 0) return false; birth_request_ = id; birth_pending_ = true; birth_result_ = network::PrimitiveLifeResult::pending; return true; } void birth(const network::PrimitiveLifeOutcome& result) { if (result.request_id != birth_request_) return; birth_result_ = result.result; birth_pending_ = result.result == network::PrimitiveLifeResult::pending || result.result == network::PrimitiveLifeResult::unknown || result.result == network::PrimitiveLifeResult::born; if (result.result == network::PrimitiveLifeResult::born) { ended_.reset(); birth_tick_ = view_tick_; } } [[nodiscard]] std::uint64_t birth_request() const { return birth_request_; } [[nodiscard]] bool birth_pending() const { return birth_pending_; } [[nodiscard]] network::PrimitiveLifeResult birth_result() const { return birth_result_; } [[nodiscard]] std::uint64_t life() const { return life_; } [[nodiscard]] bool fresh() const { return connected_ && fresh_; } [[nodiscard]] const auto& offer() const { return offer_; } [[nodiscard]] const auto& ended() const { return ended_; } [[nodiscard]] const auto& pending() const { return pending_; } [[nodiscard]] const auto& history() const { return history_; } [[nodiscard]] const auto& engulf_feedback() const { return engulf_feedback_; } [[nodiscard]] const auto& bud_feedback() const { return bud_feedback_; } [[nodiscard]] const auto& energy_change() const { return energy_change_; } // Flows per second, scaled up when views were missed. [[nodiscard]] CellFlows flows_per_second() const { CellFlows total; for (const auto& sample : flow_samples_) for (std::size_t c = 0; c < 5; ++c) { total.collected.at(c) += sample.flows.collected.at(c); total.into_growth.at(c) += sample.flows.into_growth.at(c); total.burned.at(c) += sample.flows.burned.at(c); } if (!flow_samples_.empty() && flow_samples_.size() < 20) for (auto* channel : {&total.collected, &total.into_growth, &total.burned}) for (auto& value : *channel) value = value * 20 / flow_samples_.size(); return total; } private: void finish() { if (!pending_) return; if (history_.size() == history_limit) history_.pop_front(); history_.push_back(*pending_); pending_.reset(); } // The newest engulf/bud of this life, at or before `tick`, still without a reported // effect: the view for its tick carries what the region did with it. CellActionRecord* awaiting_effect(CellAction kind, std::uint64_t tick) { const auto waiting = [&](const CellActionRecord& r) { return r.kind == kind && r.life == life_ && r.tick <= tick && !(kind == CellAction::engulf ? r.engulf.has_value() : r.bud.has_value()); }; if (pending_ && waiting(*pending_)) return &*pending_; for (auto it = history_.rbegin(); it != history_.rend(); ++it) if (waiting(*it)) return &*it; return nullptr; } std::uint64_t next_id_{1}, life_{}, view_tick_{}, birth_tick_{}, birth_request_{}, seen_end_{}; bool connected_{}, fresh_{}, birth_pending_{}; network::PrimitiveLifeResult birth_result_{network::PrimitiveLifeResult::pending}; std::optional offer_; std::optional ended_; std::optional pending_; std::deque history_; std::optional> engulf_feedback_; std::optional> bud_feedback_; struct EnergySample { std::uint64_t life{}, tick{}, energy{}; }; std::optional energy_sample_; struct FlowSample { std::uint64_t life{}, tick{}; CellFlows flows; }; std::deque flow_samples_; std::optional energy_change_; }; } // namespace evo::client_app