Freeze offline cells, linger, suspend and resume them
Foundation / checks (ecs) (push) Successful in 2m15s
Foundation / checks (deploy) (push) Successful in 3m53s
Foundation / checks (tsan) (push) Successful in 9m46s
Foundation / checks (client) (push) Failing after 13m47s
Foundation / checks (network) (push) Successful in 19m5s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 19m34s
Foundation / checks (postgres) (push) Canceled after 19m28s
Foundation / checks (ecs) (push) Successful in 2m15s
Foundation / checks (deploy) (push) Successful in 3m53s
Foundation / checks (tsan) (push) Successful in 9m46s
Foundation / checks (client) (push) Failing after 13m47s
Foundation / checks (network) (push) Successful in 19m5s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 19m34s
Foundation / checks (postgres) (push) Canceled after 19m28s
M11.7 (ADR-0128): a frozen step intent, set by the server for every owned life whose account holds no session, leaves the cell unchanged but engulfable. Owner format 6 / rules 5 track presence: lingering for 200 ticks, then suspended out of the world without aging; an unfrozen intent resumes it in place or at the nearest clear spot within three extents. Bundle v6, schema v38 and the cellular-v4 opening carry it. The server refuses an offline account's carried actions, ticks while anyone lingers, and skips suspended cells in replication and perception. Also fixes the TSan failure of CI run 374: the rules-4 owner test read the bud through a reference into a replaced state. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -64,6 +64,10 @@ propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
|
||||
for (const auto& life : lives) {
|
||||
if (life.life_id == 0)
|
||||
return std::unexpected{CellularRegionStepError::invalid_life_id};
|
||||
if (life.intent.frozen &&
|
||||
(life.intent.movement != CellularIntent{} || life.intent.intake_target ||
|
||||
life.intent.engulf || life.intent.bud))
|
||||
return std::unexpected{CellularRegionStepError::invalid_intent};
|
||||
if (rules.engulf_and_detritus && (life.membrane_um == 0 || life.source == 0 ||
|
||||
(life.intent.engulf && life.intent.intake_target)))
|
||||
return std::unexpected{CellularRegionStepError::invalid_intent};
|
||||
@@ -92,6 +96,18 @@ propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
|
||||
if (std::ranges::any_of(life->heat.matter,
|
||||
[](const auto& channel) { return channel.value != 0; }))
|
||||
return std::unexpected{CellularRegionStepError::invalid_heat};
|
||||
if (life->intent.frozen) {
|
||||
// ADR-0128: a frozen cell is unchanged and pays nothing; it can still be prey.
|
||||
CellularStepCandidate frozen{};
|
||||
frozen.begin_tick = begin_tick;
|
||||
frozen.end_tick = begin_tick + 1;
|
||||
frozen.position = life->position;
|
||||
frozen.reserve_after = life->reserve;
|
||||
frozen.heat_after = life->heat;
|
||||
candidate.lives.push_back({life->life_id, frozen});
|
||||
survivors.push_back(life);
|
||||
continue;
|
||||
}
|
||||
auto intent = life->intent;
|
||||
const auto target = intent.intake_target;
|
||||
intent.intake_target.reset();
|
||||
|
||||
@@ -163,6 +163,11 @@ provision_cellular_region(Postgres& owner, const FilesystemObjectStore& objects,
|
||||
// ADR-0125: a rules-4 region starts on the v5 bundle with genome revisions.
|
||||
manifest->cellular_versions = cellular_v5_versions;
|
||||
manifest->history_profile = cellular_revision_history_profile;
|
||||
} else if (manifest && opening.format_version() == server::cellular_lives_presence_format &&
|
||||
opening.rules_version() == server::cellular_lives_presence_rules) {
|
||||
// ADR-0128: a rules-5 region starts on the v6 bundle with genome revisions.
|
||||
manifest->cellular_versions = cellular_v6_versions;
|
||||
manifest->history_profile = cellular_revision_history_profile;
|
||||
} else if (manifest && (opening.format_version() != server::cellular_lives_legacy_format ||
|
||||
opening.rules_version() != 1))
|
||||
return std::unexpected{failure(PublicationError::candidate)};
|
||||
|
||||
@@ -71,7 +71,8 @@ bool structural(const CheckpointManifest& m) noexcept {
|
||||
m.cellular_versions != cellular_v2_versions &&
|
||||
m.cellular_versions != cellular_v3_versions &&
|
||||
m.cellular_versions != cellular_v4_versions &&
|
||||
m.cellular_versions != cellular_v5_versions) ||
|
||||
m.cellular_versions != cellular_v5_versions &&
|
||||
m.cellular_versions != cellular_v6_versions) ||
|
||||
(cellular && m.versions != environmental_v1_versions))
|
||||
return false;
|
||||
if (primitive && (m.manifest_version != 2 || m.tick_microseconds != 50000 ||
|
||||
@@ -165,7 +166,8 @@ bool supported(const CheckpointManifest& m) noexcept {
|
||||
m.cellular_versions == cellular_v2_versions ||
|
||||
m.cellular_versions == cellular_v3_versions ||
|
||||
m.cellular_versions == cellular_v4_versions ||
|
||||
m.cellular_versions == cellular_v5_versions) &&
|
||||
m.cellular_versions == cellular_v5_versions ||
|
||||
m.cellular_versions == cellular_v6_versions) &&
|
||||
m.versions == environmental_v1_versions && m.tick_microseconds == 50000 &&
|
||||
m.rng_streams == 0 && m.durability == DurabilityPolicy::single_volume_development;
|
||||
}
|
||||
|
||||
@@ -469,7 +469,8 @@ CellularRegionHost::select_mutation(const persistence::Uuid& account, std::uint6
|
||||
const auto life = std::ranges::find(state_->lives(), account, &CellularOwnedLife::account);
|
||||
if (life == state_->lives().end() || life->identity.life != input.life_id ||
|
||||
input.event_index == 0 || life->mutation.event_index != input.event_index ||
|
||||
life->mutation.offer_tick != input.offer_tick || input.offer_tick >= tick)
|
||||
life->mutation.offer_tick != input.offer_tick || input.offer_tick >= tick ||
|
||||
life->presence != CellularPresence::active) // ADR-0128: only an active life chooses.
|
||||
return {invalid, 0, {}};
|
||||
const auto& offer = life->mutation.offer;
|
||||
if (!offer || !std::ranges::any_of(offer->candidates, [&](const auto& candidate) {
|
||||
|
||||
@@ -37,7 +37,9 @@ bool valid_intent(const CellularLifeIntent& life) {
|
||||
return life.life_id != 0 && step.movement.x >= -movement_limit &&
|
||||
step.movement.x <= movement_limit && step.movement.y >= -movement_limit &&
|
||||
step.movement.y <= movement_limit && !(step.engulf && step.intake_target) &&
|
||||
!(step.bud && (step.intake_target || step.engulf));
|
||||
!(step.bud && (step.intake_target || step.engulf)) &&
|
||||
!(step.frozen && (step.movement != biology::CellularIntent{} || step.intake_target ||
|
||||
step.engulf || step.bud));
|
||||
}
|
||||
bool valid_ticks(std::span<const CellularTickIntents> ticks) {
|
||||
std::set<std::uint64_t> identities;
|
||||
@@ -142,7 +144,8 @@ void put_intent(std::vector<std::uint8_t>& out, const CellularLifeIntent& life)
|
||||
: life.intent.engulf ? 2
|
||||
: life.intent.bud ? 3
|
||||
: 0);
|
||||
put(out, 0, 3);
|
||||
out.push_back(life.intent.frozen ? 1 : 0); // Flags (ADR-0128): bit 0 frozen.
|
||||
put(out, 0, 2);
|
||||
put(out, life.intent.intake_target ? life.intent.intake_target->x_um : 0, 4);
|
||||
put(out, life.intent.intake_target ? life.intent.intake_target->y_um : 0, 4);
|
||||
}
|
||||
@@ -291,7 +294,7 @@ decode_cellular_batch(std::span<const std::uint8_t> bytes) {
|
||||
step.lives.reserve(static_cast<std::size_t>(count));
|
||||
for (std::uint64_t i = 0; i < count; ++i) {
|
||||
const auto record = bytes.subspan(offset, cellular_batch_intent_bytes);
|
||||
if (record[12] > 3 || !all_zero(record.subspan(13, 3)))
|
||||
if (record[12] > 3 || record[13] > 1 || !all_zero(record.subspan(14, 2)))
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
CellularLifeIntent life;
|
||||
life.life_id = get(record, 0, 8);
|
||||
@@ -308,6 +311,7 @@ decode_cellular_batch(std::span<const std::uint8_t> bytes) {
|
||||
}
|
||||
life.intent.engulf = record[12] == 2; // ADR-0124 target kind.
|
||||
life.intent.bud = record[12] == 3; // ADR-0125.
|
||||
life.intent.frozen = record[13] == 1; // ADR-0128.
|
||||
if (!valid_intent(life) ||
|
||||
(!step.lives.empty() && step.lives.back().life_id >= life.life_id))
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
|
||||
@@ -17,6 +17,7 @@ constexpr std::uint64_t cellular_lives_v2_magic = 0x3230564C43564541ULL; // AEVC
|
||||
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::uint64_t cellular_lives_v6_magic = 0x3630564C43564541ULL; // AEVCLV06
|
||||
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;
|
||||
@@ -29,6 +30,8 @@ constexpr std::size_t cellular_corpse_v4_bytes = cellular_corpse_bytes + 16;
|
||||
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;
|
||||
// Format 6 (ADR-0128): each life adds presence (1), 7 zero bytes and its freeze tick (8).
|
||||
constexpr std::size_t cellular_life_v6_bytes = cellular_life_v5_bytes + 16;
|
||||
|
||||
std::uint64_t checksum(std::span<const std::uint8_t> bytes) noexcept {
|
||||
std::uint64_t value = 14695981039346656037ULL;
|
||||
@@ -196,7 +199,8 @@ bud_position(const CellularOwnedLife& parent, std::uint32_t parent_extent, std::
|
||||
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]) ||
|
||||
clear = lives[i].presence == CellularPresence::suspended ||
|
||||
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};
|
||||
@@ -204,6 +208,53 @@ bud_position(const CellularOwnedLife& parent, std::uint32_t parent_extent, std::
|
||||
return std::optional<biology::CellularPosition>{};
|
||||
}
|
||||
|
||||
// ADR-0128 resume: its own position if clear of every cell in the world, else the first
|
||||
// clear spot at one, two or three of its extents in the eight fixed directions.
|
||||
std::expected<std::optional<biology::CellularPosition>, CellularLivesError>
|
||||
resume_position(const CellularOwnedLife& life, std::uint32_t 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 (extent == 0 || extent > config.area.width_um || extent > config.area.height_um)
|
||||
return std::optional<biology::CellularPosition>{};
|
||||
std::vector<std::pair<const CellularOwnedLife*, std::uint32_t>> present;
|
||||
for (const auto& other : lives) {
|
||||
if (other.presence == CellularPresence::suspended ||
|
||||
other.identity.life == life.identity.life)
|
||||
continue;
|
||||
const auto budget = budget_for(archive, other, config.ambient_temperature_millikelvin);
|
||||
if (!budget)
|
||||
return std::unexpected{budget.error()};
|
||||
present.emplace_back(&other, budget->phenotype.extent);
|
||||
}
|
||||
const auto clear = [&](std::int64_t x, std::int64_t y) {
|
||||
if (x < 0 || y < 0 || static_cast<std::uint64_t>(x) > config.area.width_um - extent ||
|
||||
static_cast<std::uint64_t>(y) > config.area.height_um - extent)
|
||||
return false;
|
||||
return std::ranges::all_of(present, [&](const auto& entry) {
|
||||
const auto& [other, other_extent] = entry;
|
||||
return separated(static_cast<std::uint64_t>(x), other->position.x_um, extent,
|
||||
other_extent) ||
|
||||
separated(static_cast<std::uint64_t>(y), other->position.y_um, extent,
|
||||
other_extent);
|
||||
});
|
||||
};
|
||||
const auto x0 = static_cast<std::int64_t>(life.position.x_um);
|
||||
const auto y0 = static_cast<std::int64_t>(life.position.y_um);
|
||||
if (clear(x0, y0))
|
||||
return std::optional{life.position};
|
||||
for (std::uint32_t ring = 1; ring <= cellular_resume_extents; ++ring)
|
||||
for (const auto& [dx, dy] : directions) {
|
||||
const auto step = static_cast<std::int64_t>(ring) * extent;
|
||||
if (clear(x0 + dx * step, y0 + dy * step))
|
||||
return std::optional{
|
||||
biology::CellularPosition{static_cast<std::uint64_t>(x0 + dx * step),
|
||||
static_cast<std::uint64_t>(y0 + dy * step)}};
|
||||
}
|
||||
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,
|
||||
@@ -229,7 +280,8 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
|
||||
std::span<const CellularOwnedCorpse> corpses, const biology::LineageArchive& archive,
|
||||
accounting::Inventory nursery, accounting::Inventory heat,
|
||||
accounting::Inventory opening_total, bool detritus_rules = false,
|
||||
bool bud_rules = false, std::uint64_t next_bud_sequence = 1) {
|
||||
bool bud_rules = false, std::uint64_t next_bud_sequence = 1,
|
||||
bool presence_rules = false) {
|
||||
// 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 {
|
||||
@@ -291,6 +343,17 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
|
||||
const auto& life = lives[i];
|
||||
// Founders are owned; buds are unowned wildlife (rules 4) and never earn offers.
|
||||
const bool wildlife = life.parent_life != 0;
|
||||
// ADR-0128: only owned lives leave; a lingering life is within its linger time.
|
||||
const bool presence_valid =
|
||||
life.presence == CellularPresence::active
|
||||
? life.frozen_since == 0
|
||||
: presence_rules && !wildlife && life.frozen_since != 0 &&
|
||||
life.frozen_since <= config.tick &&
|
||||
(life.presence == CellularPresence::suspended ||
|
||||
(life.presence == CellularPresence::lingering &&
|
||||
config.tick - life.frozen_since < cellular_linger_ticks));
|
||||
if (!presence_valid)
|
||||
return std::unexpected{CellularLivesError::invalid_life};
|
||||
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 ||
|
||||
@@ -472,7 +535,8 @@ 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 budding = profile == CellularLivesProfile::budding;
|
||||
const bool presence = profile == CellularLivesProfile::presence;
|
||||
const bool budding = profile == CellularLivesProfile::budding || presence;
|
||||
const bool engulf = profile == CellularLivesProfile::engulf || budding;
|
||||
if (engulf)
|
||||
archive.upgrade_to_revision_format(); // Rules 3+ regions carry genome revisions.
|
||||
@@ -480,12 +544,14 @@ CellularLives::create(const PrimitiveLives& opening,
|
||||
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);
|
||||
presence ? cellular_lives_presence_format
|
||||
: budding ? cellular_lives_bud_format
|
||||
: engulf ? cellular_lives_engulf_format
|
||||
: cellular_lives_format,
|
||||
presence ? cellular_lives_presence_rules
|
||||
: budding ? cellular_lives_bud_rules
|
||||
: engulf ? cellular_lives_engulf_rules
|
||||
: cellular_lives_rules);
|
||||
return std::unique_ptr<CellularLives>{new CellularLives{std::move(state)}};
|
||||
}
|
||||
|
||||
@@ -550,11 +616,13 @@ 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 bool parents = state_->format_version >= cellular_lives_bud_format;
|
||||
const bool presence = state_->format_version >= cellular_lives_presence_format;
|
||||
const auto life_bytes = state_->format_version == cellular_lives_legacy_format
|
||||
? cellular_life_v1_bytes
|
||||
: parents ? cellular_life_v5_bytes
|
||||
: cellular_life_v2_bytes;
|
||||
: presence ? cellular_life_v6_bytes
|
||||
: 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
|
||||
@@ -579,6 +647,7 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
: 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
|
||||
: state_->format_version == cellular_lives_presence_format ? cellular_lives_v6_magic
|
||||
: cellular_lives_v2_magic;
|
||||
out.put(magic);
|
||||
out.put(state_->format_version, 4);
|
||||
@@ -655,6 +724,11 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
}
|
||||
if (parents)
|
||||
out.put(life.parent_life);
|
||||
if (presence) {
|
||||
out.put(static_cast<std::uint8_t>(life.presence), 1);
|
||||
out.zeros(7);
|
||||
out.put(life.frozen_since);
|
||||
}
|
||||
}
|
||||
for (const auto& corpse : state_->corpses) {
|
||||
out.put(corpse.life_id);
|
||||
@@ -801,6 +875,8 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
|
||||
static_cast<std::uint64_t>(site.y_um)};
|
||||
bool occupied = false;
|
||||
for (const auto& living : state_->lives) {
|
||||
if (living.presence == CellularPresence::suspended)
|
||||
continue; // Out of the world (ADR-0128).
|
||||
const auto living_budget =
|
||||
budget_for(state_->archive, living, state_->config.ambient_temperature_millikelvin);
|
||||
if (!living_budget)
|
||||
@@ -852,11 +928,12 @@ 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,
|
||||
state_->rules_version == cellular_lives_bud_rules, state_->next_bud_sequence);
|
||||
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,
|
||||
state_->rules_version >= cellular_lives_presence_rules);
|
||||
if (!current)
|
||||
return std::unexpected{current.error()};
|
||||
|
||||
@@ -887,9 +964,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 bud_version = magic == cellular_lives_v5_magic &&
|
||||
format_version == cellular_lives_bud_format &&
|
||||
rules_version == cellular_lives_bud_rules;
|
||||
const bool presence_version = magic == cellular_lives_v6_magic &&
|
||||
format_version == cellular_lives_presence_format &&
|
||||
rules_version == cellular_lives_presence_rules;
|
||||
// Formats 5 and 6 carry the bud sequence and parent lives (rules 4 and 5).
|
||||
const bool bud_version = presence_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 &&
|
||||
@@ -931,9 +1012,10 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
const auto nursery = in.inventory();
|
||||
const auto heat = in.inventory();
|
||||
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 life_bytes = legacy ? cellular_life_v1_bytes
|
||||
: presence_version ? cellular_life_v6_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;
|
||||
@@ -1015,6 +1097,14 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
}
|
||||
if (bud_version)
|
||||
life.parent_life = in.get();
|
||||
if (presence_version) {
|
||||
const auto presence = in.get(1);
|
||||
const auto padding = in.get(7);
|
||||
life.frozen_since = in.get();
|
||||
if (presence > static_cast<std::uint8_t>(CellularPresence::suspended) || padding != 0)
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
life.presence = static_cast<CellularPresence>(presence);
|
||||
}
|
||||
lives.push_back(life);
|
||||
}
|
||||
|
||||
@@ -1096,9 +1186,10 @@ 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, bud_version, next_bud_sequence);
|
||||
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, presence_version);
|
||||
if (!current)
|
||||
return std::unexpected{current.error()};
|
||||
|
||||
@@ -1139,12 +1230,19 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
// 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;
|
||||
const bool bud_rules = state_->rules_version >= cellular_lives_bud_rules;
|
||||
const bool presence_rules = state_->rules_version >= cellular_lives_presence_rules;
|
||||
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).
|
||||
if (intent.bud && (intent.intake_target || intent.engulf))
|
||||
return std::unexpected{CellularLivesError::invalid_intents};
|
||||
// ADR-0128: only an owned life of a rules-5 region freezes, and a frozen intent
|
||||
// carries nothing else.
|
||||
if (intent.frozen && (!presence_rules || state_->lives[i].account == LifeAccount{} ||
|
||||
intent.movement != biology::CellularIntent{} ||
|
||||
intent.intake_target || intent.engulf || intent.bud))
|
||||
return std::unexpected{CellularLivesError::invalid_intents};
|
||||
}
|
||||
for (std::size_t i = 0; i < selections.size(); ++i) {
|
||||
const auto& selection = selections[i];
|
||||
@@ -1179,6 +1277,8 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
inputs.reserve(state_->lives.size());
|
||||
for (std::size_t i = 0; i < state_->lives.size(); ++i) {
|
||||
const auto& life = state_->lives[i];
|
||||
if (life.presence == CellularPresence::suspended)
|
||||
continue; // Out of the world: no step, no contact (ADR-0128).
|
||||
const auto budget =
|
||||
budget_for(state_->archive, life, state_->config.ambient_temperature_millikelvin);
|
||||
if (!budget)
|
||||
@@ -1338,6 +1438,10 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
bud_results.push_back({request.life_id, 0, CellularBudResult::life_died});
|
||||
continue;
|
||||
}
|
||||
if (parent->presence == CellularPresence::suspended) {
|
||||
bud_results.push_back({request.life_id, 0, CellularBudResult::not_able});
|
||||
continue; // Out of the world (ADR-0128).
|
||||
}
|
||||
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);
|
||||
@@ -1390,6 +1494,44 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
}
|
||||
std::ranges::sort(next_lives, {},
|
||||
[](const CellularOwnedLife& life) { return life.identity.life; });
|
||||
// Rules 5 presence (ADR-0128), after deaths, mutations and buds: a death in this tick
|
||||
// is final before any presence change.
|
||||
std::vector<std::uint64_t> resume_blocked;
|
||||
if (presence_rules) {
|
||||
for (auto& life : next_lives) {
|
||||
const auto request = std::ranges::lower_bound(ordered, life.identity.life, {},
|
||||
&CellularLifeIntent::life_id);
|
||||
if (request == ordered.end() || request->life_id != life.identity.life)
|
||||
continue; // Born this tick: active.
|
||||
const bool frozen = request->intent.frozen;
|
||||
if (life.presence == CellularPresence::active && frozen) {
|
||||
life.presence = CellularPresence::lingering;
|
||||
life.frozen_since = proposal->end_tick;
|
||||
} else if (life.presence == CellularPresence::lingering && !frozen) {
|
||||
life.presence = CellularPresence::active;
|
||||
life.frozen_since = 0;
|
||||
} else if (life.presence == CellularPresence::lingering &&
|
||||
proposal->end_tick - life.frozen_since >= cellular_linger_ticks) {
|
||||
life.presence = CellularPresence::suspended;
|
||||
} else if (life.presence == CellularPresence::suspended && !frozen) {
|
||||
const auto budget =
|
||||
budget_for(next_archive, life, state_->config.ambient_temperature_millikelvin);
|
||||
if (!budget)
|
||||
return std::unexpected{budget.error()};
|
||||
const auto spot = resume_position(life, budget->phenotype.extent, next_lives,
|
||||
next_archive, state_->config);
|
||||
if (!spot)
|
||||
return std::unexpected{spot.error()};
|
||||
if (*spot) {
|
||||
life.position = **spot;
|
||||
life.presence = CellularPresence::active;
|
||||
life.frozen_since = 0;
|
||||
} else {
|
||||
resume_blocked.push_back(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)
|
||||
@@ -1420,7 +1562,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
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);
|
||||
engulf_rules, bud_rules, next_bud_sequence, presence_rules);
|
||||
if (!total)
|
||||
return std::unexpected{total.error()};
|
||||
|
||||
@@ -1441,7 +1583,8 @@ 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, std::nullopt, 0});
|
||||
result.engulf, result.prey_id, std::nullopt, 0,
|
||||
CellularPresence::active, false});
|
||||
if (const auto bud =
|
||||
std::ranges::find(bud_results, result.life_id, &CellularBudExecution::parent_life);
|
||||
bud != bud_results.end()) {
|
||||
@@ -1451,6 +1594,32 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
if (input == ordered.end() || input->life_id != result.life_id)
|
||||
return std::unexpected{CellularLivesError::invalid_intents};
|
||||
}
|
||||
// ADR-0128: each surviving life reports its presence after the tick; a suspended life,
|
||||
// which took no step, reports its unchanged reserve and any blocked resume.
|
||||
for (const auto& life : next->lives) {
|
||||
auto entry =
|
||||
std::ranges::find(feedback, life.identity.life, &CellularLifeTickFeedback::life_id);
|
||||
if (entry == feedback.end()) {
|
||||
feedback.push_back({life.identity.life,
|
||||
proposal->end_tick,
|
||||
life.reserve,
|
||||
{},
|
||||
{},
|
||||
biology::CellularMovementStatus::not_requested,
|
||||
biology::CellularIntakeStatus::not_requested,
|
||||
biology::CellularEngulfStatus::not_requested,
|
||||
0,
|
||||
std::nullopt,
|
||||
0,
|
||||
life.presence,
|
||||
false});
|
||||
entry = std::prev(feedback.end());
|
||||
}
|
||||
entry->presence = life.presence;
|
||||
entry->resume_blocked =
|
||||
std::ranges::find(resume_blocked, life.identity.life) != resume_blocked.end();
|
||||
}
|
||||
std::ranges::sort(feedback, {}, &CellularLifeTickFeedback::life_id);
|
||||
state_.swap(next);
|
||||
last_tick_feedback_ = std::move(feedback);
|
||||
return CellularTickExecution{std::move(*proposal), std::move(mutation_results),
|
||||
|
||||
Reference in New Issue
Block a user