Bud unowned wildlife from the cellular owner under rules 4
Owner format 5 / rules 4 (ADR-0125) adds the bud intent: after the tick's mutations a parent pays fund_bud's investment and provision, and the bud is placed touching it with the next region-namespace identity, the parent's current genome and its parent life. Buds are wildlife with no account and never earn mutation offers; restore enforces the identity namespace and sequence. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
#include "evo/server/lifecycle/cellular_lives.hpp"
|
||||
#include "evo/biology/budding.hpp"
|
||||
#include "evo/biology/founder.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <limits>
|
||||
#include <ranges>
|
||||
@@ -14,6 +16,7 @@ constexpr std::uint64_t cellular_lives_v1_magic = 0x3130564C43564541ULL; // AEVC
|
||||
constexpr std::uint64_t cellular_lives_v2_magic = 0x3230564C43564541ULL; // AEVCLV02
|
||||
constexpr std::uint64_t cellular_lives_v3_magic = 0x3330564C43564541ULL; // AEVCLV03
|
||||
constexpr std::uint64_t cellular_lives_v4_magic = 0x3430564C43564541ULL; // AEVCLV04
|
||||
constexpr std::uint64_t cellular_lives_v5_magic = 0x3530564C43564541ULL; // AEVCLV05
|
||||
constexpr std::size_t cellular_lives_header_bytes = 212;
|
||||
constexpr std::size_t cellular_site_bytes = 8;
|
||||
constexpr std::size_t cellular_life_v1_bytes = 168;
|
||||
@@ -21,6 +24,11 @@ constexpr std::size_t cellular_life_v2_bytes = 480;
|
||||
constexpr std::size_t cellular_corpse_bytes = 100;
|
||||
// Format 4: cause (1), 7 zero bytes and the predator's life id (8).
|
||||
constexpr std::size_t cellular_corpse_v4_bytes = cellular_corpse_bytes + 16;
|
||||
// Format 5 (ADR-0125): the header adds the next bud sequence, each life and each corpse
|
||||
// its parent life (8 bytes each).
|
||||
constexpr std::size_t cellular_lives_v5_header_bytes = cellular_lives_header_bytes + 8;
|
||||
constexpr std::size_t cellular_life_v5_bytes = cellular_life_v2_bytes + 8;
|
||||
constexpr std::size_t cellular_corpse_v5_bytes = cellular_corpse_v4_bytes + 8;
|
||||
|
||||
std::uint64_t checksum(std::span<const std::uint8_t> bytes) noexcept {
|
||||
std::uint64_t value = 14695981039346656037ULL;
|
||||
@@ -144,6 +152,58 @@ budget_for(const biology::LineageArchive& archive, const CellularOwnedLife& life
|
||||
return *budget;
|
||||
}
|
||||
|
||||
bool separated(std::uint64_t left, std::uint64_t right, std::uint32_t left_extent,
|
||||
std::uint32_t right_extent) noexcept {
|
||||
return left < right ? right - left >= left_extent : left - right >= right_extent;
|
||||
}
|
||||
|
||||
// ADR-0125 placement: touching the parent, centres (parent + bud extent) / 2 + 1 apart on
|
||||
// each moved axis, trying east and then counter-clockwise in 45-degree steps. The first
|
||||
// spot inside the area and clear of every living cell wins; none is a refusal.
|
||||
std::expected<std::optional<biology::CellularPosition>, CellularLivesError>
|
||||
bud_position(const CellularOwnedLife& parent, std::uint32_t parent_extent, std::uint32_t bud_extent,
|
||||
std::span<const CellularOwnedLife> lives, const biology::LineageArchive& archive,
|
||||
const CellularLivesConfig& config) {
|
||||
constexpr std::array<std::array<std::int64_t, 2>, 8> directions{
|
||||
{{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}};
|
||||
if (bud_extent == 0 || bud_extent > config.area.width_um || bud_extent > config.area.height_um)
|
||||
return std::optional<biology::CellularPosition>{};
|
||||
std::vector<std::uint32_t> extents;
|
||||
extents.reserve(lives.size());
|
||||
for (const auto& living : lives) {
|
||||
const auto budget = budget_for(archive, living, config.ambient_temperature_millikelvin);
|
||||
if (!budget)
|
||||
return std::unexpected{budget.error()};
|
||||
extents.push_back(budget->phenotype.extent);
|
||||
}
|
||||
// Doubled centre coordinates keep half extents exact.
|
||||
const auto step = std::int64_t{parent_extent} + bud_extent + 2;
|
||||
// Twice the bud's corner coordinate on one axis; negative is outside the area.
|
||||
const auto doubled_corner = [&](std::uint64_t parent_corner, std::int64_t direction) {
|
||||
return 2 * static_cast<std::int64_t>(parent_corner) + parent_extent + direction * step -
|
||||
std::int64_t{bud_extent};
|
||||
};
|
||||
for (const auto& [dx, dy] : directions) {
|
||||
const auto twice_x = doubled_corner(parent.position.x_um, dx);
|
||||
const auto twice_y = doubled_corner(parent.position.y_um, dy);
|
||||
const auto x = twice_x / 2;
|
||||
const auto y = twice_y / 2;
|
||||
if (twice_x < 0 || twice_y < 0 ||
|
||||
static_cast<std::uint64_t>(x) > config.area.width_um - bud_extent ||
|
||||
static_cast<std::uint64_t>(y) > config.area.height_um - bud_extent)
|
||||
continue;
|
||||
const biology::CellularPosition candidate{static_cast<std::uint64_t>(x),
|
||||
static_cast<std::uint64_t>(y)};
|
||||
bool clear = true;
|
||||
for (std::size_t i = 0; i < lives.size() && clear; ++i)
|
||||
clear = separated(candidate.x_um, lives[i].position.x_um, bud_extent, extents[i]) ||
|
||||
separated(candidate.y_um, lives[i].position.y_um, bud_extent, extents[i]);
|
||||
if (clear)
|
||||
return std::optional{candidate};
|
||||
}
|
||||
return std::optional<biology::CellularPosition>{};
|
||||
}
|
||||
|
||||
std::expected<accounting::Inventory, CellularLivesError>
|
||||
sum_state(accounting::Inventory nursery, accounting::Inventory heat,
|
||||
const biology::CellularHabitat& habitat, std::span<const CellularOwnedLife> lives,
|
||||
@@ -168,7 +228,20 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
|
||||
const biology::CellularHabitat& habitat, std::span<const CellularOwnedLife> lives,
|
||||
std::span<const CellularOwnedCorpse> corpses, const biology::LineageArchive& archive,
|
||||
accounting::Inventory nursery, accounting::Inventory heat,
|
||||
accounting::Inventory opening_total, bool detritus_rules = false) {
|
||||
accounting::Inventory opening_total, bool detritus_rules = false,
|
||||
bool bud_rules = false, std::uint64_t next_bud_sequence = 1) {
|
||||
// A bud's identity lies in this region's namespace below the next sequence; anything
|
||||
// else is an allocated identity, below the namespace (ADR-0125).
|
||||
const auto bud_identity = [&](std::uint64_t identity) noexcept {
|
||||
const auto first = cellular_bud_identity(region, 1);
|
||||
return bud_rules && first != 0 && next_bud_sequence > 1 && identity >= first &&
|
||||
identity - first < next_bud_sequence - 1;
|
||||
};
|
||||
const auto lineage_valid = [&](std::uint64_t life_id, std::uint64_t parent) noexcept {
|
||||
if (parent == 0)
|
||||
return life_id < cellular_bud_namespace;
|
||||
return bud_identity(life_id) && parent != life_id;
|
||||
};
|
||||
const auto& map = habitat.config();
|
||||
if (world == 0 || region == 0 || frame == 0 || nursery_config.world != world ||
|
||||
nursery_config.region != region || nursery_config.frame != frame ||
|
||||
@@ -216,13 +289,19 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
|
||||
life_ids.reserve(lives.size() + corpses.size());
|
||||
for (std::size_t i = 0; i < lives.size(); ++i) {
|
||||
const auto& life = lives[i];
|
||||
if (life.account == LifeAccount{} || life.identity.life == 0 ||
|
||||
// Founders are owned; buds are unowned wildlife (rules 4) and never earn offers.
|
||||
const bool wildlife = life.parent_life != 0;
|
||||
if ((life.account == LifeAccount{}) != wildlife || life.identity.life == 0 ||
|
||||
life.identity.organism == 0 || life.identity.genome == 0 || life.identity.event == 0 ||
|
||||
life.identity.born.value == 0 || life.identity.born.value > config.tick ||
|
||||
life.source == 0 || (i != 0 && lives[i - 1].identity.life >= life.identity.life))
|
||||
life.source == 0 || (i != 0 && lives[i - 1].identity.life >= life.identity.life) ||
|
||||
!lineage_valid(life.identity.life, life.parent_life) ||
|
||||
(wildlife && (life.identity.organism != life.identity.life ||
|
||||
life.identity.event != life.identity.life ||
|
||||
life.mutation != CellularMutationProgress{})))
|
||||
return std::unexpected{CellularLivesError::invalid_life};
|
||||
for (std::size_t j = 0; j < i; ++j)
|
||||
if (lives[j].account == life.account ||
|
||||
if ((!wildlife && lives[j].account == life.account) ||
|
||||
lives[j].identity.organism == life.identity.organism)
|
||||
return std::unexpected{CellularLivesError::duplicate_life};
|
||||
const auto record = archive.organism(life.identity.organism);
|
||||
@@ -248,7 +327,8 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
|
||||
const auto& corpse = corpses[i];
|
||||
if (corpse.life_id == 0 || corpse.organism == 0 || corpse.source == 0 ||
|
||||
corpse.died_tick == 0 || corpse.died_tick > config.tick || corpse.extent_um == 0 ||
|
||||
(i != 0 && corpses[i - 1].organism >= corpse.organism))
|
||||
(i != 0 && corpses[i - 1].organism >= corpse.organism) ||
|
||||
!lineage_valid(corpse.life_id, corpse.parent_life))
|
||||
return std::unexpected{CellularLivesError::invalid_life};
|
||||
const auto record = archive.organism(corpse.organism);
|
||||
if (!record || record->alive || record->died.value != corpse.died_tick)
|
||||
@@ -303,6 +383,7 @@ struct CellularLives::State {
|
||||
accounting::Inventory heat{};
|
||||
accounting::Inventory opening_total{};
|
||||
accounting::Inventory current_total{};
|
||||
std::uint64_t next_bud_sequence{1}; // Format 5: the next bud identity's sequence.
|
||||
|
||||
State(std::uint64_t world_id, std::uint64_t region_id, std::uint64_t frame_id,
|
||||
CellularLivesConfig cfg, NurseryConfig nursery_definition,
|
||||
@@ -391,14 +472,20 @@ CellularLives::create(const PrimitiveLives& opening,
|
||||
const auto world = nursery_config.world;
|
||||
const auto region = nursery_config.region;
|
||||
const auto frame = nursery_config.frame;
|
||||
const bool engulf = profile == CellularLivesProfile::engulf;
|
||||
const bool budding = profile == CellularLivesProfile::budding;
|
||||
const bool engulf = profile == CellularLivesProfile::engulf || budding;
|
||||
if (engulf)
|
||||
archive.upgrade_to_revision_format(); // Rules 3 regions carry genome revisions.
|
||||
auto state = std::make_unique<State>(
|
||||
world, region, frame, config, std::move(nursery_config), std::move(habitat),
|
||||
std::move(lives), std::vector<CellularOwnedCorpse>{}, archive, nursery, heat, opening_total,
|
||||
*current, engulf ? cellular_lives_engulf_format : cellular_lives_format,
|
||||
engulf ? cellular_lives_engulf_rules : cellular_lives_rules);
|
||||
archive.upgrade_to_revision_format(); // Rules 3+ regions carry genome revisions.
|
||||
auto state = std::make_unique<State>(world, region, frame, config, std::move(nursery_config),
|
||||
std::move(habitat), std::move(lives),
|
||||
std::vector<CellularOwnedCorpse>{}, archive, nursery, heat,
|
||||
opening_total, *current,
|
||||
budding ? cellular_lives_bud_format
|
||||
: engulf ? cellular_lives_engulf_format
|
||||
: cellular_lives_format,
|
||||
budding ? cellular_lives_bud_rules
|
||||
: engulf ? cellular_lives_engulf_rules
|
||||
: cellular_lives_rules);
|
||||
return std::unique_ptr<CellularLives>{new CellularLives{std::move(state)}};
|
||||
}
|
||||
|
||||
@@ -420,6 +507,9 @@ std::uint32_t CellularLives::format_version() const noexcept {
|
||||
std::uint32_t CellularLives::rules_version() const noexcept {
|
||||
return state_->rules_version;
|
||||
}
|
||||
std::uint64_t CellularLives::next_bud_sequence() const noexcept {
|
||||
return state_->next_bud_sequence;
|
||||
}
|
||||
std::uint32_t CellularLives::ambient_temperature_millikelvin() const noexcept {
|
||||
return state_->config.ambient_temperature_millikelvin;
|
||||
}
|
||||
@@ -460,12 +550,18 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
const auto site_count = state_->nursery_config.sites.size();
|
||||
const auto life_count = state_->lives.size();
|
||||
const auto corpse_count = state_->corpses.size();
|
||||
const bool parents = state_->format_version == cellular_lives_bud_format;
|
||||
const auto life_bytes = state_->format_version == cellular_lives_legacy_format
|
||||
? cellular_life_v1_bytes
|
||||
: cellular_life_v2_bytes;
|
||||
const bool causes = state_->format_version == cellular_lives_engulf_format;
|
||||
const auto corpse_bytes = causes ? cellular_corpse_v4_bytes : cellular_corpse_bytes;
|
||||
const auto total_bytes = cellular_lives_header_bytes + cellular_site_bytes * site_count +
|
||||
: parents ? cellular_life_v5_bytes
|
||||
: cellular_life_v2_bytes;
|
||||
const bool causes = state_->format_version >= cellular_lives_engulf_format;
|
||||
const auto corpse_bytes = parents ? cellular_corpse_v5_bytes
|
||||
: causes ? cellular_corpse_v4_bytes
|
||||
: cellular_corpse_bytes;
|
||||
const auto header_bytes =
|
||||
parents ? cellular_lives_v5_header_bytes : cellular_lives_header_bytes;
|
||||
const auto total_bytes = header_bytes + cellular_site_bytes * site_count +
|
||||
life_bytes * life_count + corpse_bytes * corpse_count +
|
||||
habitat.size() + archive.size() + 8;
|
||||
if (total_bytes > max_cellular_lives_bytes ||
|
||||
@@ -482,6 +578,7 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
state_->format_version == cellular_lives_legacy_format ? cellular_lives_v1_magic
|
||||
: state_->format_version == cellular_lives_revision_format ? cellular_lives_v3_magic
|
||||
: state_->format_version == cellular_lives_engulf_format ? cellular_lives_v4_magic
|
||||
: state_->format_version == cellular_lives_bud_format ? cellular_lives_v5_magic
|
||||
: cellular_lives_v2_magic;
|
||||
out.put(magic);
|
||||
out.put(state_->format_version, 4);
|
||||
@@ -503,6 +600,8 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
out.inventory(state_->opening_total);
|
||||
out.inventory(state_->nursery);
|
||||
out.inventory(state_->heat);
|
||||
if (parents)
|
||||
out.put(state_->next_bud_sequence);
|
||||
for (const auto site : state_->nursery_config.sites)
|
||||
out.site(site);
|
||||
for (const auto& life : state_->lives) {
|
||||
@@ -554,6 +653,8 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (parents)
|
||||
out.put(life.parent_life);
|
||||
}
|
||||
for (const auto& corpse : state_->corpses) {
|
||||
out.put(corpse.life_id);
|
||||
@@ -571,6 +672,8 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
out.zeros(7);
|
||||
out.put(corpse.predator_life);
|
||||
}
|
||||
if (parents)
|
||||
out.put(corpse.parent_life);
|
||||
}
|
||||
out.raw(habitat);
|
||||
out.raw(archive);
|
||||
@@ -749,10 +852,11 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
|
||||
state_->region, state_->frame);
|
||||
if (!habitat)
|
||||
return std::unexpected{CellularLivesError::habitat};
|
||||
const auto current =
|
||||
validate_state(state_->world, state_->region, state_->frame, state_->config, nursery_config,
|
||||
**habitat, lives, state_->corpses, archive, nursery, heat,
|
||||
state_->opening_total, state_->rules_version == cellular_lives_engulf_rules);
|
||||
const auto current = validate_state(
|
||||
state_->world, state_->region, state_->frame, state_->config, nursery_config, **habitat,
|
||||
lives, state_->corpses, archive, nursery, heat, state_->opening_total,
|
||||
state_->rules_version >= cellular_lives_engulf_rules,
|
||||
state_->rules_version == cellular_lives_bud_rules, state_->next_bud_sequence);
|
||||
if (!current)
|
||||
return std::unexpected{current.error()};
|
||||
|
||||
@@ -761,6 +865,7 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
|
||||
std::move(*habitat), std::move(lives), state_->corpses, archive, nursery, heat,
|
||||
state_->opening_total, *current, state_->format_version, state_->rules_version)}};
|
||||
candidate->last_tick_feedback_ = last_tick_feedback_;
|
||||
candidate->state_->next_bud_sequence = state_->next_bud_sequence;
|
||||
return candidate;
|
||||
}
|
||||
|
||||
@@ -782,9 +887,13 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
const bool offer_version = magic == cellular_lives_v2_magic &&
|
||||
format_version == cellular_lives_format &&
|
||||
rules_version == cellular_lives_rules;
|
||||
const bool engulf_version = magic == cellular_lives_v4_magic &&
|
||||
format_version == cellular_lives_engulf_format &&
|
||||
rules_version == cellular_lives_engulf_rules;
|
||||
const bool bud_version = magic == cellular_lives_v5_magic &&
|
||||
format_version == cellular_lives_bud_format &&
|
||||
rules_version == cellular_lives_bud_rules;
|
||||
// Formats 4 and 5 record death causes and leave detritus (rules 3 and 4).
|
||||
const bool engulf_version = bud_version || (magic == cellular_lives_v4_magic &&
|
||||
format_version == cellular_lives_engulf_format &&
|
||||
rules_version == cellular_lives_engulf_rules);
|
||||
const bool revision_version =
|
||||
engulf_version ||
|
||||
(magic == cellular_lives_v3_magic && format_version == cellular_lives_revision_format &&
|
||||
@@ -821,12 +930,18 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
const auto opening_total = in.inventory();
|
||||
const auto nursery = in.inventory();
|
||||
const auto heat = in.inventory();
|
||||
const auto life_bytes = legacy ? cellular_life_v1_bytes : cellular_life_v2_bytes;
|
||||
const auto corpse_bytes = engulf_version ? cellular_corpse_v4_bytes : cellular_corpse_bytes;
|
||||
const auto expected_size = cellular_lives_header_bytes + cellular_site_bytes * site_count +
|
||||
life_bytes * life_count + corpse_bytes * corpse_count +
|
||||
habitat_size + archive_size + 8;
|
||||
if (source == 0 || expected_size != bytes.size())
|
||||
const auto next_bud_sequence = bud_version ? in.get() : std::uint64_t{1};
|
||||
const auto life_bytes = legacy ? cellular_life_v1_bytes
|
||||
: bud_version ? cellular_life_v5_bytes
|
||||
: cellular_life_v2_bytes;
|
||||
const auto corpse_bytes = bud_version ? cellular_corpse_v5_bytes
|
||||
: engulf_version ? cellular_corpse_v4_bytes
|
||||
: cellular_corpse_bytes;
|
||||
const auto expected_size =
|
||||
(bud_version ? cellular_lives_v5_header_bytes : cellular_lives_header_bytes) +
|
||||
cellular_site_bytes * site_count + life_bytes * life_count + corpse_bytes * corpse_count +
|
||||
habitat_size + archive_size + 8;
|
||||
if (source == 0 || next_bud_sequence == 0 || expected_size != bytes.size())
|
||||
return std::unexpected{CellularLivesError::format};
|
||||
|
||||
NurseryConfig nursery_config{saved_world, saved_region, saved_frame, source, nursery, {}};
|
||||
@@ -898,6 +1013,8 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
mutation.offer = std::move(offer);
|
||||
}
|
||||
}
|
||||
if (bud_version)
|
||||
life.parent_life = in.get();
|
||||
lives.push_back(life);
|
||||
}
|
||||
|
||||
@@ -928,6 +1045,8 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
corpse.inventory != accounting::Inventory{})
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
}
|
||||
if (bud_version)
|
||||
corpse.parent_life = in.get();
|
||||
corpses.push_back(corpse);
|
||||
}
|
||||
const auto habitat_payload = in.raw(static_cast<std::size_t>(habitat_size));
|
||||
@@ -977,9 +1096,9 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
}
|
||||
}
|
||||
const auto current =
|
||||
validate_state(saved_world, saved_region, saved_frame, config, nursery_config, **habitat,
|
||||
lives, corpses, *archive, nursery, heat, opening_total, engulf_version);
|
||||
const auto current = validate_state(
|
||||
saved_world, saved_region, saved_frame, config, nursery_config, **habitat, lives, corpses,
|
||||
*archive, nursery, heat, opening_total, engulf_version, bud_version, next_bud_sequence);
|
||||
if (!current)
|
||||
return std::unexpected{current.error()};
|
||||
|
||||
@@ -987,6 +1106,7 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
saved_world, saved_region, saved_frame, config, std::move(nursery_config),
|
||||
std::move(*habitat), std::move(lives), std::move(corpses), *archive, nursery, heat,
|
||||
opening_total, *current, format_version, rules_version)}};
|
||||
restored->state_->next_bud_sequence = next_bud_sequence;
|
||||
if (!std::ranges::equal(bytes, restored->snapshot()))
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
return restored;
|
||||
@@ -1017,7 +1137,14 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
|
||||
if (!selections.empty() && state_->format_version < cellular_lives_revision_format)
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
const bool engulf_rules = state_->rules_version == cellular_lives_engulf_rules;
|
||||
// Rules 4 keep rules 3 (engulfment, detritus) and add budding (ADR-0125).
|
||||
const bool engulf_rules = state_->rules_version >= cellular_lives_engulf_rules;
|
||||
const bool bud_rules = state_->rules_version == cellular_lives_bud_rules;
|
||||
for (const auto& ordered_intent : ordered) {
|
||||
const auto& intent = ordered_intent.intent;
|
||||
if (intent.bud && (!bud_rules || intent.intake_target || intent.engulf))
|
||||
return std::unexpected{CellularLivesError::invalid_intents};
|
||||
}
|
||||
for (std::size_t i = 0; i < selections.size(); ++i) {
|
||||
const auto& selection = selections[i];
|
||||
if (selection.life_id == 0 || selection.event_index == 0 || selection.offer_tick == 0 ||
|
||||
@@ -1091,7 +1218,9 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
found->reserve = result.candidate.reserve_after;
|
||||
if (!add_inventory(next_heat, result.candidate.heat_after))
|
||||
return std::unexpected{CellularLivesError::overflow};
|
||||
if (state_->format_version >= cellular_lives_format && result.candidate.intake) {
|
||||
// Only owned lives earn mutation offers; wildlife never mutates in M11 (ADR-0098).
|
||||
if (state_->format_version >= cellular_lives_format && result.candidate.intake &&
|
||||
found->account != LifeAccount{}) {
|
||||
const auto acquired = result.candidate.intake->transfer.acquired.energy.value;
|
||||
if (acquired >
|
||||
std::numeric_limits<std::uint64_t>::max() - found->mutation.fed_energy_credit_pj)
|
||||
@@ -1150,7 +1279,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
proposal->end_tick, dead.position, found->growth,
|
||||
corpse_budget->phenotype.extent, dead.inventory,
|
||||
engulf_rules ? dead.cause : biology::CellularDeathCause::unrecorded,
|
||||
engulf_rules ? dead.predator_id : 0});
|
||||
engulf_rules ? dead.predator_id : 0, found->parent_life});
|
||||
next_lives.erase(found);
|
||||
}
|
||||
std::vector<CellularMutationExecution> mutation_results;
|
||||
@@ -1194,6 +1323,72 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
mutation_results.push_back(
|
||||
{selection.life_id, selection.successor_id, CellularMutationResult::applied});
|
||||
}
|
||||
// Rules 4 budding (ADR-0125), after mutations and in parent life order: a parent that
|
||||
// died this tick cannot bud, and a bud born this tick asked for nothing.
|
||||
std::vector<CellularBudExecution> bud_results;
|
||||
auto next_bud_sequence = state_->next_bud_sequence;
|
||||
for (const auto& request : ordered) {
|
||||
if (!request.intent.bud)
|
||||
continue;
|
||||
const auto parent = std::ranges::lower_bound(
|
||||
next_lives, request.life_id, {},
|
||||
[](const CellularOwnedLife& value) { return value.identity.life; });
|
||||
if (parent == next_lives.end() || parent->identity.life != request.life_id) {
|
||||
bud_results.push_back({request.life_id, 0, CellularBudResult::life_died});
|
||||
continue;
|
||||
}
|
||||
const auto temperature = state_->config.ambient_temperature_millikelvin;
|
||||
const auto genome = next_archive.find(parent->identity.genome);
|
||||
const auto parent_budget = budget_for(next_archive, *parent, temperature);
|
||||
if (!genome || !parent_budget)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto transition =
|
||||
biology::fund_bud(*genome, parent->growth, parent->reserve, temperature);
|
||||
if (!transition) {
|
||||
if (transition.error() == biology::BuddingError::development)
|
||||
return std::unexpected{CellularLivesError::biology};
|
||||
bud_results.push_back({request.life_id, 0,
|
||||
transition.error() == biology::BuddingError::insufficient_funding
|
||||
? CellularBudResult::insufficient_funding
|
||||
: CellularBudResult::not_able});
|
||||
continue;
|
||||
}
|
||||
const auto bud_budget =
|
||||
biology::metabolic_budget(*genome, transition->growth, {temperature, {}});
|
||||
if (!bud_budget || bud_budget->body.matter != transition->body.matter)
|
||||
return std::unexpected{CellularLivesError::biology};
|
||||
const auto identity = cellular_bud_identity(state_->region, next_bud_sequence);
|
||||
const auto position =
|
||||
bud_position(*parent, parent_budget->phenotype.extent, bud_budget->phenotype.extent,
|
||||
next_lives, next_archive, state_->config);
|
||||
if (!position)
|
||||
return std::unexpected{position.error()};
|
||||
if (identity == 0 || !*position || next_lives.size() >= biology::max_cellular_tick_lives ||
|
||||
next_archive.organism(identity)) {
|
||||
bud_results.push_back({request.life_id, 0, CellularBudResult::no_placement});
|
||||
continue;
|
||||
}
|
||||
if (!add_inventory(next_heat, accounting::Inventory{transition->heat, {}}) ||
|
||||
!next_archive.record_birth(identity, parent->identity.genome, {proposal->end_tick}))
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
parent->reserve = transition->parent_reserve;
|
||||
CellularOwnedLife bud{};
|
||||
bud.identity = {
|
||||
identity, identity, parent->identity.genome, identity, {proposal->end_tick}};
|
||||
bud.source = parent->source;
|
||||
bud.growth = transition->growth;
|
||||
bud.position = **position;
|
||||
bud.body = transition->body;
|
||||
bud.reserve = transition->reserve;
|
||||
bud.parent_life = parent->identity.life;
|
||||
// Bud identities increase within the region and exceed every allocated identity,
|
||||
// so the new life sorts after all existing ones and `parent` stays valid.
|
||||
next_lives.push_back(bud);
|
||||
++next_bud_sequence;
|
||||
bud_results.push_back({request.life_id, identity, CellularBudResult::budded});
|
||||
}
|
||||
std::ranges::sort(next_lives, {},
|
||||
[](const CellularOwnedLife& life) { return life.identity.life; });
|
||||
std::ranges::sort(next_corpses, {}, &CellularOwnedCorpse::organism);
|
||||
if (proposal->heat_released.value >
|
||||
std::numeric_limits<std::uint64_t>::max() - next_heat.energy.value)
|
||||
@@ -1218,12 +1413,13 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
auto next_habitat = biology::CellularHabitat::create(std::move(habitat_config));
|
||||
if (!next_habitat)
|
||||
return std::unexpected{CellularLivesError::habitat};
|
||||
const auto total = validate_state(
|
||||
state_->world, state_->region, state_->frame,
|
||||
CellularLivesConfig{state_->config.area, proposal->end_tick,
|
||||
state_->config.ambient_temperature_millikelvin},
|
||||
state_->nursery_config, **next_habitat, next_lives, next_corpses, next_archive,
|
||||
state_->nursery, next_heat, state_->opening_total, engulf_rules);
|
||||
const auto total =
|
||||
validate_state(state_->world, state_->region, state_->frame,
|
||||
CellularLivesConfig{state_->config.area, proposal->end_tick,
|
||||
state_->config.ambient_temperature_millikelvin},
|
||||
state_->nursery_config, **next_habitat, next_lives, next_corpses,
|
||||
next_archive, state_->nursery, next_heat, state_->opening_total,
|
||||
engulf_rules, bud_rules, next_bud_sequence);
|
||||
if (!total)
|
||||
return std::unexpected{total.error()};
|
||||
|
||||
@@ -1234,6 +1430,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
state_->nursery_config, std::move(*next_habitat), std::move(next_lives),
|
||||
std::move(next_corpses), next_archive, state_->nursery, next_heat, state_->opening_total,
|
||||
*total, state_->format_version, state_->rules_version);
|
||||
next->next_bud_sequence = next_bud_sequence;
|
||||
std::vector<CellularLifeTickFeedback> feedback;
|
||||
feedback.reserve(proposal->lives.size());
|
||||
for (const auto& result : proposal->lives) {
|
||||
@@ -1243,12 +1440,19 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
feedback.push_back({result.life_id, proposal->end_tick, result.candidate.reserve_after,
|
||||
result.candidate.maintenance_cost, result.candidate.movement_cost,
|
||||
result.candidate.movement_status, result.candidate.intake_status,
|
||||
result.engulf, result.prey_id});
|
||||
result.engulf, result.prey_id, std::nullopt, 0});
|
||||
if (const auto bud =
|
||||
std::ranges::find(bud_results, result.life_id, &CellularBudExecution::parent_life);
|
||||
bud != bud_results.end()) {
|
||||
feedback.back().bud = bud->result;
|
||||
feedback.back().bud_life = bud->bud_life;
|
||||
}
|
||||
if (input == ordered.end() || input->life_id != result.life_id)
|
||||
return std::unexpected{CellularLivesError::invalid_intents};
|
||||
}
|
||||
state_.swap(next);
|
||||
last_tick_feedback_ = std::move(feedback);
|
||||
return CellularTickExecution{std::move(*proposal), std::move(mutation_results)};
|
||||
return CellularTickExecution{std::move(*proposal), std::move(mutation_results),
|
||||
std::move(bud_results)};
|
||||
}
|
||||
} // namespace evo::server
|
||||
|
||||
Reference in New Issue
Block a user