diff --git a/src/server/lifecycle/cellular_lives.cpp b/src/server/lifecycle/cellular_lives.cpp index 2f5bb03..1e2b309 100644 --- a/src/server/lifecycle/cellular_lives.cpp +++ b/src/server/lifecycle/cellular_lives.cpp @@ -523,10 +523,10 @@ std::vector CellularLives::snapshot() const { out.put(0, 4); out.inventory(mutation.offer ? mutation.offer->context.environment.available : accounting::Inventory{}); - const auto count = mutation.offer ? mutation.offer->candidates.size() : 0; for (std::size_t index = 0; index < biology::max_candidates; ++index) { - if (index < count) { - const auto& candidate = mutation.offer->candidates[index]; + if (mutation.offer && index < mutation.offer->candidates.size()) { + const auto& candidates = mutation.offer.value().candidates; + const auto& candidate = candidates[index]; out.put(candidate.operator_id, 2); out.put(static_cast(candidate.target), 1); out.put(static_cast(candidate.effect), 1); @@ -986,11 +986,15 @@ CellularLives::advance_tick_with_mutations(std::span i state_->lives, selection.life_id, {}, [](const CellularOwnedLife& value) { return value.identity.life; }); if (life == state_->lives.end() || life->identity.life != selection.life_id || - !life->mutation.offer || life->mutation.event_index != selection.event_index || - life->mutation.offer_tick != selection.offer_tick || - life->mutation.offer->parent != life->identity.genome || - life->mutation.offer->context.event_index != selection.event_index || - !std::ranges::any_of(life->mutation.offer->candidates, + life->mutation.event_index != selection.event_index || + life->mutation.offer_tick != selection.offer_tick) + return std::unexpected{CellularLivesError::mutation}; + if (!life->mutation.offer.has_value()) + return std::unexpected{CellularLivesError::mutation}; + const auto& offer = *life->mutation.offer; + if (offer.parent != life->identity.genome || + offer.context.event_index != selection.event_index || + !std::ranges::any_of(offer.candidates, [&](const auto& candidate) { return candidate.operator_id == selection.operator_id; }) || @@ -1109,11 +1113,12 @@ CellularLives::advance_tick_with_mutations(std::span i continue; } const auto parent = next_archive.find(found->identity.genome); - if (!parent || !found->mutation.offer) + if (!parent || !found->mutation.offer.has_value()) return std::unexpected{CellularLivesError::mutation}; - auto transition = biology::fund_mutation( - *parent, found->mutation.offer->context, *found->mutation.offer, selection.operator_id, - selection.successor_id, selection.successor_id, found->reserve); + const auto& offer = *found->mutation.offer; + auto transition = + biology::fund_mutation(*parent, offer.context, offer, selection.operator_id, + selection.successor_id, selection.successor_id, found->reserve); if (!transition) { if (transition.error() == biology::MutationError::insufficient_funding) { mutation_results.push_back({selection.life_id, selection.successor_id,