feat(biology): evolve photosynthetic producers that capture light
Foundation / checks (ecs) (push) Successful in 2m25s
Foundation / checks (deploy) (push) Successful in 5m15s
Foundation / checks (network) (push) Successful in 21m56s
Foundation / checks (client) (push) Successful in 22m54s
Foundation / checks (tsan) (push) Successful in 47m4s
Foundation / checks (postgres) (push) Successful in 1h15m49s
Foundation / checks (checks) (push) Failing after 1h30m4s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped

ADR-0175 implements ADR-0136 step C with trial costs to be balanced after
playtesting. Genome schema 5 adds a photosynthesis efficiency (empty in
schemas 1-4, so earlier offers and draws are unchanged; mutation operators
29-32 are appended). Development version 5 builds the organelles as
internal membrane (p/4 of structure, upkept twice) and slows movement by
p/2 per thousand. Founder 4 and cellular rules 11 let lineages earn the
capability as an offspring plan that children express.

Owner format 9 declares the region's light. Inside each tick's detached
candidate, active unfrozen producers capture cross-section x irradiance x
efficiency (a quarter while moving), credited to the reserve and boundary
inflow together and replayed from recorded intentions. Well-lit wildlife
producers hold still. The joint owner keeps infall, water and radiation and
nests a rules-11 owner unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-19 18:06:02 -04:00
co-authored by Claude Opus 5.5
parent 51991ac7ff
commit 108f29eda5
34 changed files with 856 additions and 128 deletions
+2 -2
View File
@@ -40,8 +40,8 @@ bool owner_version_matches(const CellularLives& owner, const CellularBatch& batc
if (!known_format(batch.format_version))
return false;
if (batch.format_version == cellular_batch_inheritance_format)
return owner.format_version() == cellular_lives_inheritance_format &&
owner.rules_version() == cellular_lives_inheritance_rules;
return owner.format_version() >= cellular_lives_inheritance_format &&
cellular_retained_plans(owner.rules_version());
return owner.format_version() < cellular_lives_inheritance_format;
}
bool payload_version_matches(const CellularBatch& batch) {
+2 -2
View File
@@ -57,8 +57,8 @@ CellularCensus::observe(const CellularLives& state, std::span<const CellularCoho
state.format_version() == cellular_lives_migration_format &&
(population == 0 || population == 2);
const bool inherited =
state.rules_version() == cellular_lives_inheritance_rules &&
state.format_version() == cellular_lives_inheritance_format &&
cellular_retained_plans(state.rules_version()) &&
state.format_version() >= cellular_lives_inheritance_format &&
(population == 0 || population == cellular_population_inheritance_format);
if (!inherited && ((!maturing && state.rules_version() != cellular_lives_foraging_rules) ||
(state.format_version() != cellular_lives_presence_format &&
+79 -49
View File
@@ -97,8 +97,8 @@ bool supported(const CellularLives& state) noexcept {
return (state.format_version() == cellular_lives_migration_format &&
(state.rules_version() == cellular_lives_foraging_rules ||
state.rules_version() == cellular_lives_maturation_rules)) ||
(state.format_version() == cellular_lives_inheritance_format &&
state.rules_version() == cellular_lives_inheritance_rules);
(state.format_version() >= cellular_lives_inheritance_format &&
cellular_retained_plans(state.rules_version()));
}
std::expected<world::HabitatMeasure, CellularEnvironmentError>
validate(const CellularLives& state, const CellularEnvironmentConfig& config) {
@@ -285,11 +285,20 @@ CellularEnvironment::plan_exchange(std::uint64_t tick) const {
return exchange;
}
std::expected<CellularEnvironmentTickReport, CellularEnvironmentError>
CellularEnvironment::advance_tick(std::span<const CellularLifeIntent> controlled,
std::span<const CellularMutationSelection> selections) {
// One joint tick, shared by live play, unattended recording and replay: the infall and
// heat exchange; the full intentions (live: the shared controller on the exchanged state
// plus the players'; record: the controller plus frozen players; replay: as recorded);
// then the unchanged biological step, which books a rules-11 owner's own light. Any
// refusal swaps the exchange back, so the owner is unchanged.
std::expected<CellularEnvironment::Step, CellularEnvironmentError>
CellularEnvironment::joint_step(std::vector<CellularLifeIntent>& intents,
std::span<const CellularMutationSelection> selections,
StepMode mode) {
if (tick() == std::numeric_limits<std::uint64_t>::max())
return std::unexpected(CellularEnvironmentError::overflow);
if (population_->policy_.automatic &&
population_->conversion_sequence_ == std::numeric_limits<std::uint64_t>::max())
return std::unexpected(CellularEnvironmentError::overflow);
auto exchange = plan_exchange(tick() + 1);
if (!exchange)
return std::unexpected(exchange.error());
@@ -299,53 +308,57 @@ CellularEnvironment::advance_tick(std::span<const CellularLifeIntent> controlled
return std::unexpected(savepoint.error() == CellularLivesError::habitat
? CellularEnvironmentError::habitat
: CellularEnvironmentError::conservation);
auto executed = population_->advance_tick(controlled, selections);
if (!executed) {
const auto refuse = [&](CellularEnvironmentError error) {
lives.restore_boundary(std::move(*savepoint));
return std::unexpected(CellularEnvironmentError::biology);
}
progress_ = std::move(exchange->progress);
exported_.value += exchange->heat_export.value;
exchange->report.biology = std::move(*executed);
return std::move(exchange->report);
}
std::expected<CellularEnvironmentFlow, CellularEnvironmentError>
CellularEnvironment::advance_unattended(std::vector<CellularLifeIntent>& intents, bool record) {
if (tick() == std::numeric_limits<std::uint64_t>::max())
return std::unexpected(CellularEnvironmentError::overflow);
auto exchange = plan_exchange(tick() + 1);
if (!exchange)
return std::unexpected(exchange.error());
auto& lives = *population_->state_;
auto savepoint = lives.exchange_boundary(exchange->deposits, exchange->heat_export);
if (!savepoint)
return std::unexpected(savepoint.error() == CellularLivesError::habitat
? CellularEnvironmentError::habitat
: CellularEnvironmentError::conservation);
return std::unexpected(error);
};
biology::CellularStepIntent frozen;
frozen.frozen = true;
if (record) {
if (mode != StepMode::replay) {
auto chosen = wildlife_intents(lives, lives.tick() + 1);
if (!chosen)
return std::unexpected(CellularEnvironmentError::biology);
for (const auto& life : lives.lives())
if (life.account != LifeAccount{})
return refuse(CellularEnvironmentError::biology);
std::size_t player = 0;
for (const auto& life : lives.lives()) {
if (life.account == LifeAccount{})
continue;
if (mode == StepMode::record) {
chosen->push_back({life.identity.life, frozen});
} else {
if (player == intents.size() || intents[player].life_id != life.identity.life)
return refuse(CellularEnvironmentError::biology);
chosen->push_back(intents[player++]);
}
}
if (mode == StepMode::live && player != intents.size())
return refuse(CellularEnvironmentError::biology);
std::ranges::sort(*chosen, {}, &CellularLifeIntent::life_id);
intents = std::move(*chosen);
}
for (const auto& life : lives.lives()) {
if (life.account == LifeAccount{})
continue;
const auto found =
std::ranges::find(intents, life.identity.life, &CellularLifeIntent::life_id);
if (life.presence != CellularPresence::suspended || found == intents.end() ||
found->intent != frozen)
return std::unexpected(CellularEnvironmentError::unsupported);
if (mode == StepMode::live) {
for (const auto& selection : selections) {
const auto life =
std::ranges::find(lives.lives(), selection.life_id,
[](const auto& owned) { return owned.identity.life; });
if (life == lives.lives().end() || life->account == LifeAccount{})
return refuse(CellularEnvironmentError::biology);
}
} else {
if (!selections.empty())
return refuse(CellularEnvironmentError::unsupported);
for (const auto& life : lives.lives()) {
if (life.account == LifeAccount{})
continue;
const auto found =
std::ranges::find(intents, life.identity.life, &CellularLifeIntent::life_id);
if (life.presence != CellularPresence::suspended || found == intents.end() ||
found->intent != frozen)
return refuse(CellularEnvironmentError::unsupported);
}
}
if (!lives.advance_tick(intents))
return std::unexpected(CellularEnvironmentError::biology);
auto executed = lives.advance_tick_with_mutations(intents, selections);
if (!executed)
return refuse(CellularEnvironmentError::biology);
auto& members = population_->members_;
std::erase_if(members, [&lives](const auto& member) {
return std::ranges::find(lives.lives(), member.life, [](const auto& life) {
@@ -353,14 +366,30 @@ CellularEnvironment::advance_unattended(std::vector<CellularLifeIntent>& intents
}) == lives.lives().end();
});
// The persisted policy's exact result; no random materialization or resampling.
if (population_->policy_.automatic) {
if (population_->conversion_sequence_ == std::numeric_limits<std::uint64_t>::max())
return std::unexpected(CellularEnvironmentError::overflow);
if (population_->policy_.automatic)
population_->apply_policy();
}
progress_ = std::move(exchange->progress);
exported_.value += exchange->heat_export.value;
return CellularEnvironmentFlow{exchange->report.imported, exchange->heat_export};
exchange->report.biology = std::move(*executed);
return Step{std::move(exchange->report)};
}
std::expected<CellularEnvironmentTickReport, CellularEnvironmentError>
CellularEnvironment::advance_tick(std::span<const CellularLifeIntent> controlled,
std::span<const CellularMutationSelection> selections) {
std::vector<CellularLifeIntent> intents(controlled.begin(), controlled.end());
auto step = joint_step(intents, selections, StepMode::live);
if (!step)
return std::unexpected(step.error());
return std::move(step->report);
}
std::expected<CellularEnvironmentFlow, CellularEnvironmentError>
CellularEnvironment::advance_unattended(std::vector<CellularLifeIntent>& intents, bool record) {
auto step = joint_step(intents, {}, record ? StepMode::record : StepMode::replay);
if (!step)
return std::unexpected(step.error());
return CellularEnvironmentFlow{step->report.imported, step->report.exported};
}
CellularPopulationBoundary CellularEnvironment::population_boundary() const {
return {population_->conversion_sequence_, population_->members_};
@@ -502,8 +531,9 @@ CellularEnvironment::restore(std::span<const std::uint8_t> bytes, std::uint64_t
return std::unexpected(candidate.error());
(*candidate)->progress_ = std::move(progress);
(*candidate)->exported_ = exported;
// Environmental flows are part of the nested ledger, never in addition to it.
accounting::Inventory flows;
// Environmental flows are part of the nested ledger, never in addition to it; a
// rules-11 owner's light is booked there too.
accounting::Inventory flows{(*candidate)->state().captured_light(), {}};
for (const auto& done : (*candidate)->progress_)
if (!add(flows, done.imported))
return std::unexpected(CellularEnvironmentError::conservation);
@@ -73,10 +73,9 @@ execute_environment_batch(const CellularEnvironment& base, CellularEnvironmentBa
if (record) {
population.biology.header = h;
population.biology.ticks.clear();
population.biology.format_version =
state.rules_version() == cellular_lives_inheritance_rules
? cellular_batch_inheritance_format
: cellular_batch_format;
population.biology.format_version = cellular_retained_plans(state.rules_version())
? cellular_batch_inheritance_format
: cellular_batch_format;
}
for (std::size_t index = 0; index < count; ++index) {
std::vector<CellularLifeIntent> intents;
+35
View File
@@ -0,0 +1,35 @@
#include "evo/server/lifecycle/cellular_light.hpp"
#include <limits>
namespace evo::server {
namespace {
bool multiply(std::uint64_t& value, std::uint64_t factor) noexcept {
if (factor != 0 && value > std::numeric_limits<std::uint64_t>::max() / factor)
return false;
value *= factor;
return true;
}
} // namespace
std::optional<std::uint64_t> cellular_light_capture(const biology::Genome& genome,
std::uint32_t extent_um,
const CellularLight& light,
bool moving) noexcept {
if (!genome.carries(biology::Capability::photosynthesis) || light.numerator == 0 ||
light.denominator == 0)
return 0;
const auto efficiency = genome.parameter(biology::Capability::photosynthesis);
if (!efficiency)
return std::nullopt;
// Exact checked products before the single rounding; overflow is refused.
std::uint64_t energy = extent_um;
if (!multiply(energy, extent_um) || !multiply(energy, light.numerator) ||
!multiply(energy, *efficiency) || (moving && !multiply(energy, light.moving_per_thousand)))
return std::nullopt;
std::uint64_t divisor = light.denominator;
if (!multiply(divisor, 1000) || (moving && !multiply(divisor, 1000)))
return std::nullopt;
return energy / divisor;
}
} // namespace evo::server
+130 -24
View File
@@ -22,6 +22,7 @@ constexpr std::uint64_t cellular_lives_v5_magic = 0x3530564C43564541ULL; // AEVC
constexpr std::uint64_t cellular_lives_v6_magic = 0x3630564C43564541ULL; // AEVCLV06
constexpr std::uint64_t cellular_lives_v7_magic = 0x3730564C43564541ULL; // AEVCLV07
constexpr std::uint64_t cellular_lives_v8_magic = 0x3830564C43564541ULL; // AEVCLV08
constexpr std::uint64_t cellular_lives_v9_magic = 0x3930564C43564541ULL; // AEVCLV09
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;
@@ -39,6 +40,9 @@ constexpr std::size_t cellular_life_v6_bytes = cellular_life_v5_bytes + 16;
// Format 7: two boundary-flow inventories and nonresident count/reserved.
constexpr std::size_t cellular_lives_v7_header_bytes = cellular_lives_v5_header_bytes + 88;
constexpr std::size_t cellular_lives_v8_header_bytes = cellular_lives_v7_header_bytes + 8;
// Format 9 (ADR-0175): light numerator, denominator, entitlement, moving share and
// reserved (4 each for the last two), and the cumulative captured energy.
constexpr std::size_t cellular_lives_v9_header_bytes = cellular_lives_v8_header_bytes + 40;
constexpr std::size_t cellular_offspring_revision_bytes = 32;
std::uint64_t checksum(std::span<const std::uint8_t> bytes) noexcept {
@@ -585,6 +589,8 @@ struct CellularLives::State {
std::uint64_t next_bud_sequence{1}; // Format 5: the next bud identity's sequence.
CellularBoundaryLedger boundary{};
std::vector<CellularOffspringRevision> offspring_revisions;
CellularLight light{}; // Format 9 (ADR-0175).
accounting::Picojoules captured{}; // Format 9: light credited to reserves so far.
State(std::uint64_t world_id, std::uint64_t region_id, std::uint64_t frame_id,
CellularLivesConfig cfg, NurseryConfig nursery_definition,
@@ -803,8 +809,7 @@ CellularLives::migration_replacement(std::vector<CellularOwnedLife> living,
std::expected<CellularLives::BoundarySavepoint, CellularLivesError>
CellularLives::exchange_boundary(std::span<const biology::CellularPatch> deposits,
accounting::Picojoules heat_export) {
if (state_->format_version != cellular_lives_migration_format &&
state_->format_version != cellular_lives_inheritance_format)
if (state_->format_version < cellular_lives_migration_format)
return std::unexpected(CellularLivesError::version);
if (heat_export.value > state_->heat.energy.value)
return std::unexpected(CellularLivesError::conservation);
@@ -846,13 +851,13 @@ CellularLives::exchange_boundary(std::span<const biology::CellularPatch> deposit
habitat = std::move(*created);
}
const auto rules = state_->rules_version;
const auto total = validate_state(
state_->world, state_->region, state_->frame, state_->config, state_->nursery_config,
*habitat, state_->lives, state_->corpses, state_->archive, state_->nursery, heat,
state_->opening_total, rules >= cellular_lives_engulf_rules,
rules >= cellular_lives_bud_rules, state_->next_bud_sequence,
rules >= cellular_lives_presence_rules, &boundary,
rules == cellular_lives_inheritance_rules ? &state_->offspring_revisions : nullptr);
const auto total =
validate_state(state_->world, state_->region, state_->frame, state_->config,
state_->nursery_config, *habitat, state_->lives, state_->corpses,
state_->archive, state_->nursery, heat, state_->opening_total,
rules >= cellular_lives_engulf_rules, rules >= cellular_lives_bud_rules,
state_->next_bud_sequence, rules >= cellular_lives_presence_rules, &boundary,
cellular_retained_plans(rules) ? &state_->offspring_revisions : nullptr);
if (!total)
return std::unexpected(total.error());
BoundarySavepoint savepoint{std::move(state_->habitat), state_->heat, state_->current_total,
@@ -892,6 +897,27 @@ std::span<const CellularLifeTickFeedback> CellularLives::last_tick_feedback() co
return last_tick_feedback_;
}
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
CellularLives::photosynthesis_upgrade_candidate(CellularLight light) const {
if (state_->format_version != cellular_lives_inheritance_format ||
state_->rules_version != cellular_lives_inheritance_rules)
return std::unexpected{CellularLivesError::version};
if (!cellular_light_valid(light))
return std::unexpected{CellularLivesError::invalid_config};
auto candidate = restore(snapshot(), world(), region(), frame(), tick());
if (!candidate)
return std::unexpected(candidate.error());
(*candidate)->state_->format_version = cellular_lives_light_format;
(*candidate)->state_->rules_version = cellular_lives_photosynthesis_rules;
(*candidate)->state_->light = light;
return candidate;
}
const CellularLight& CellularLives::light() const noexcept {
return state_->light;
}
accounting::Picojoules CellularLives::captured_light() const noexcept {
return state_->captured;
}
std::span<const CellularOffspringRevision> CellularLives::offspring_revisions() const noexcept {
return state_->offspring_revisions;
}
@@ -925,7 +951,8 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
const auto corpse_count = state_->corpses.size();
const bool parents = state_->format_version >= cellular_lives_bud_format;
const bool presence = state_->format_version >= cellular_lives_presence_format;
const bool inheritance = state_->format_version == cellular_lives_inheritance_format;
const bool inheritance = state_->format_version >= cellular_lives_inheritance_format;
const bool lit = state_->format_version == cellular_lives_light_format;
const auto life_bytes = inheritance ? cellular_life_v6_bytes + 8
: state_->format_version == cellular_lives_legacy_format
? cellular_life_v1_bytes
@@ -938,10 +965,11 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
: causes ? cellular_corpse_v4_bytes
: cellular_corpse_bytes;
const bool migration = state_->format_version >= cellular_lives_migration_format;
const auto header_bytes = inheritance ? cellular_lives_v8_header_bytes
: migration ? cellular_lives_v7_header_bytes
: parents ? cellular_lives_v5_header_bytes
: cellular_lives_header_bytes;
const auto header_bytes = lit ? cellular_lives_v9_header_bytes
: inheritance ? cellular_lives_v8_header_bytes
: migration ? cellular_lives_v7_header_bytes
: parents ? cellular_lives_v5_header_bytes
: cellular_lives_header_bytes;
const auto external_count = migration ? state_->boundary.nonresident_organisms.size() : 0;
const auto total_bytes =
header_bytes + cellular_site_bytes * site_count + life_bytes * life_count +
@@ -966,6 +994,7 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
: state_->format_version == cellular_lives_engulf_format ? cellular_lives_v4_magic
: state_->format_version == cellular_lives_bud_format ? cellular_lives_v5_magic
: state_->format_version == cellular_lives_presence_format ? cellular_lives_v6_magic
: lit ? cellular_lives_v9_magic
: inheritance ? cellular_lives_v8_magic
: migration ? cellular_lives_v7_magic
: cellular_lives_v2_magic;
@@ -1001,6 +1030,14 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
out.put(state_->offspring_revisions.size(), 4);
out.put(0, 4);
}
if (lit) {
out.put(state_->light.numerator);
out.put(state_->light.denominator);
out.put(state_->light.entitlement.value);
out.put(state_->light.moving_per_thousand, 4);
out.put(0, 4);
out.put(state_->captured.value);
}
for (const auto site : state_->nursery_config.sites)
out.site(site);
for (const auto& life : state_->lives) {
@@ -1267,7 +1304,7 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
auto lives = state_->lives;
lives.push_back({request.account, allocation, state_->nursery_config.source, endowed->growth,
*position, endowed->body, endowed->reserve});
if (state_->format_version == cellular_lives_inheritance_format)
if (state_->format_version >= cellular_lives_inheritance_format)
lives.back().offspring_genome = allocation.genome;
std::ranges::sort(lives, {}, [](const CellularOwnedLife& life) { return life.identity.life; });
@@ -1284,7 +1321,7 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
state_->rules_version >= cellular_lives_bud_rules, state_->next_bud_sequence,
state_->rules_version >= cellular_lives_presence_rules,
state_->format_version >= cellular_lives_migration_format ? &state_->boundary : nullptr,
state_->format_version == cellular_lives_inheritance_format ? &state_->offspring_revisions
state_->format_version >= cellular_lives_inheritance_format ? &state_->offspring_revisions
: nullptr);
if (!current)
return std::unexpected{current.error()};
@@ -1297,6 +1334,8 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
candidate->last_tick_feedback_ = last_tick_feedback_;
candidate->state_->next_bud_sequence = state_->next_bud_sequence;
candidate->state_->offspring_revisions = state_->offspring_revisions;
candidate->state_->light = state_->light;
candidate->state_->captured = state_->captured;
return candidate;
}
@@ -1319,9 +1358,14 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
format_version == cellular_lives_format &&
rules_version == cellular_lives_rules;
// Format 6 carries rules 5, 6 (founder 2) or 7 (founder 3, ADR-0137); no new state.
const bool inheritance_version = magic == cellular_lives_v8_magic &&
format_version == cellular_lives_inheritance_format &&
rules_version == cellular_lives_inheritance_rules;
// Format 9 is exactly rules 11; format 8 exactly rules 10.
const bool light_version = magic == cellular_lives_v9_magic &&
format_version == cellular_lives_light_format &&
rules_version == cellular_lives_photosynthesis_rules;
const bool inheritance_version =
light_version ||
(magic == cellular_lives_v8_magic && format_version == cellular_lives_inheritance_format &&
rules_version == cellular_lives_inheritance_rules);
const bool migration_version =
inheritance_version ||
(magic == cellular_lives_v7_magic && format_version == cellular_lives_migration_format &&
@@ -1353,6 +1397,8 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
return std::unexpected(CellularLivesError::format);
if (inheritance_version && bytes.size() < cellular_lives_v8_header_bytes + 8)
return std::unexpected(CellularLivesError::format);
if (light_version && bytes.size() < cellular_lives_v9_header_bytes + 8)
return std::unexpected(CellularLivesError::format);
const auto saved_world = in.get();
const auto saved_region = in.get();
const auto saved_frame = in.get();
@@ -1398,6 +1444,19 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
if (in.get(4) != 0 || plan_count > biology::max_archived_genomes)
return std::unexpected{CellularLivesError::format};
}
CellularLight light{};
accounting::Picojoules captured{};
if (light_version) {
light.numerator = in.get();
light.denominator = in.get();
light.entitlement.value = in.get();
light.moving_per_thousand = static_cast<std::uint32_t>(in.get(4));
const auto reserved = in.get(4);
captured.value = in.get();
if (reserved != 0 || !cellular_light_valid(light) ||
captured.value > light.entitlement.value)
return std::unexpected{CellularLivesError::format};
}
const auto life_bytes = inheritance_version ? cellular_life_v6_bytes + 8
: legacy ? cellular_life_v1_bytes
: presence_version ? cellular_life_v6_bytes
@@ -1407,10 +1466,11 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
: bud_version ? cellular_corpse_v5_bytes
: engulf_version ? cellular_corpse_v4_bytes
: cellular_corpse_bytes;
const auto expected_size = (inheritance_version ? cellular_lives_v8_header_bytes
: migration_version ? cellular_lives_v7_header_bytes
: bud_version ? cellular_lives_v5_header_bytes
: cellular_lives_header_bytes) +
const auto expected_size = (light_version ? cellular_lives_v9_header_bytes
: inheritance_version ? cellular_lives_v8_header_bytes
: migration_version ? cellular_lives_v7_header_bytes
: 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 +
nonresident_count * 8 +
@@ -1610,6 +1670,8 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
opening_total, *current, format_version, rules_version, std::move(boundary))}};
restored->state_->next_bud_sequence = next_bud_sequence;
restored->state_->offspring_revisions = std::move(plan_history);
restored->state_->light = light;
restored->state_->captured = captured;
if (!std::ranges::equal(bytes, restored->snapshot()))
return std::unexpected{CellularLivesError::noncanonical};
return restored;
@@ -1645,7 +1707,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
const bool contact_rules = state_->rules_version >= cellular_lives_foraging_rules;
const bool bud_rules = state_->rules_version >= cellular_lives_bud_rules;
const bool presence_rules = state_->rules_version >= cellular_lives_presence_rules;
const bool inheritance_rules = state_->rules_version == cellular_lives_inheritance_rules;
const bool inheritance_rules = cellular_retained_plans(state_->rules_version);
for (std::size_t i = 0; i < ordered.size(); ++i) {
const auto& intent = ordered[i].intent;
// Below rules 4 a bud is ignored, as engulfment is below rules 3 (ADR-0124/0125).
@@ -2136,6 +2198,50 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
*total, state_->format_version, state_->rules_version, state_->boundary);
next->next_bud_sequence = next_bud_sequence;
next->offspring_revisions = std::move(next_offspring_revisions);
next->light = state_->light;
next->captured = state_->captured;
// ADR-0175: light, from this tick's own intentions, credited to every active,
// unfrozen photosynthetic survivor's reserve and to the boundary inflow together.
if (next->light.numerator != 0) {
for (auto& life : next->lives) {
const auto left = next->light.entitlement.value - next->captured.value;
if (left == 0)
break;
if (life.presence != CellularPresence::active)
continue;
const auto genome = next->archive.find(life.identity.genome);
if (!genome)
return std::unexpected{CellularLivesError::archive};
if (!genome->carries(biology::Capability::photosynthesis))
continue;
const auto input = std::ranges::lower_bound(
ordered, life.identity.life, {},
[](const CellularLifeIntent& value) { return value.life_id; });
// Newborn buds had no intention this tick and capture from the next one.
if (input == ordered.end() || input->life_id != life.identity.life ||
input->intent.frozen)
continue;
const auto budget =
budget_for(next->archive, life, next->config.ambient_temperature_millikelvin);
if (!budget)
return std::unexpected{CellularLivesError::biology};
const auto energy =
cellular_light_capture(*genome, budget->phenotype.extent, next->light,
input->intent.movement != biology::CellularIntent{});
if (!energy)
return std::unexpected{CellularLivesError::overflow};
const auto granted = std::min(*energy, left);
constexpr auto most = std::numeric_limits<std::uint64_t>::max();
if (granted > most - life.reserve.energy.value ||
granted > most - next->boundary.incoming.energy.value ||
granted > most - next->current_total.energy.value)
return std::unexpected{CellularLivesError::overflow};
life.reserve.energy.value += granted;
next->boundary.incoming.energy.value += granted;
next->current_total.energy.value += granted;
next->captured.value += granted;
}
}
std::vector<CellularLifeTickFeedback> feedback;
feedback.reserve(proposal->lives.size());
for (const auto& result : proposal->lives) {
+3 -3
View File
@@ -20,8 +20,8 @@ CellularPopulation::create(std::unique_ptr<CellularLives> state, CellularTierPol
state->format_version() == cellular_lives_migration_format)) ||
(state->rules_version() == cellular_lives_maturation_rules &&
state->format_version() == cellular_lives_migration_format) ||
(state->rules_version() == cellular_lives_inheritance_rules &&
state->format_version() == cellular_lives_inheritance_format));
(cellular_retained_plans(state->rules_version()) &&
state->format_version() >= cellular_lives_inheritance_format));
if (!supported || policy.materialize_radius_um == 0 ||
policy.collapse_radius_um <= policy.materialize_radius_um ||
policy.collapse_radius_um > biology::cellular_motion_axis_limit_um)
@@ -44,7 +44,7 @@ std::uint64_t CellularPopulation::conversion_sequence() const noexcept {
return conversion_sequence_;
}
std::uint32_t CellularPopulation::format_version() const noexcept {
if (state_->format_version() == cellular_lives_inheritance_format)
if (state_->format_version() >= cellular_lives_inheritance_format)
return cellular_population_inheritance_format;
return state_->format_version() == cellular_lives_migration_format ? 2U : 1U;
}
@@ -84,8 +84,8 @@ class Reader {
bool population_interval_safe(const CellularLives& state) noexcept {
return (state.rules_version() == cellular_lives_foraging_rules ||
state.rules_version() == cellular_lives_maturation_rules ||
(state.rules_version() == cellular_lives_inheritance_rules &&
state.format_version() == cellular_lives_inheritance_format)) &&
(cellular_retained_plans(state.rules_version()) &&
state.format_version() >= cellular_lives_inheritance_format)) &&
std::ranges::all_of(state.lives(), [](const auto& life) {
return life.account == LifeAccount{} || life.presence == CellularPresence::suspended;
});
@@ -103,7 +103,7 @@ unattended_population_inputs(const CellularLives& base, const CellularBatchHeade
if (!working)
return std::unexpected(CellularPopulationError::format);
CellularBatch batch{header, {}};
if (base.rules_version() == cellular_lives_inheritance_rules)
if (cellular_retained_plans(base.rules_version()))
batch.format_version = cellular_batch_inheritance_format;
while ((*working)->tick() < header.end_tick) {
auto intents = wildlife_intents(**working, (*working)->tick() + 1);
@@ -140,6 +140,15 @@ wildlife_intents(const CellularLives& state, std::uint64_t tick) {
continue;
}
}
if (state.rules_version() >= cellular_lives_photosynthesis_rules &&
state.light().numerator != 0 && genome->carries(biology::Capability::photosynthesis)) {
const auto still = cellular_light_capture(*genome, extent, state.light(), false);
const auto period = budget->rate_microseconds / 50'000;
if (still && period != 0 && *still >= budget->maintenance.value / period) {
intents.push_back({life.identity.life, intent});
continue;
}
}
const auto perceived = state.perceive(life.identity.life);
if (!perceived)
return std::unexpected{perceived.error()};