feat(lifecycle): rules 12 liquid owner on format 10 with brake traces
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m23s
Foundation / postgres-tools (push) Successful in 29s
Foundation / server-image (push) Successful in 4m10s
Foundation / image (push) Successful in 4m31s

The cellular owner gains rules 12 on owner format 10: an explicit no-tick
format-9/rules-11 upgrade declares the region's liquid rules (current
configuration, founder handling and deposit motion, from the tuning file and a
seed when the region opens), converts the habitat to format 3 and starts lives at
rest. Founders born afterwards are founder 5 (genome schema 6).

Each tick derives every life's handling from its genome and the declared founder
handling, samples the current at its footprint and steps the shared liquid
motion; afterwards bubbles move with the current and the drift moving cells
induce, and touching bubbles merge. Format 10 persists the rules and each life's
motion state; restore regenerates the identical current field.

Brake is a new step intent, valid only under rules 12, carried in trace 4 as flag
bit 1 (SQL never parses that byte; player action records keep it zero). Tests
cover the upgrade, exact restore, swimming and braking, moving food with fixed
pools, restore continuing a live run, and brake traces replaying exactly. Genomes
from before schema 6 handle as founders. Refreshes the DEV session window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-21 17:28:58 -04:00
co-authored by Claude Opus 5.5
parent 190a66ad5e
commit 3f82795c18
13 changed files with 564 additions and 51 deletions
+10 -6
View File
@@ -57,7 +57,7 @@ bool valid_intent(const CellularLifeIntent& life) {
step.movement.y <= movement_limit && !(step.engulf && step.intake_target) &&
!(step.bud && (step.intake_target || step.engulf)) &&
!(step.frozen && (step.movement != biology::CellularIntent{} || step.intake_target ||
step.engulf || step.bud));
step.engulf || step.bud || step.brake));
}
bool valid_ticks(std::span<const CellularTickIntents> ticks) {
std::set<std::uint64_t> identities;
@@ -162,7 +162,10 @@ void put_intent(std::vector<std::uint8_t>& out, const CellularLifeIntent& life)
: life.intent.engulf ? 2
: life.intent.bud ? 3
: 0);
out.push_back(life.intent.frozen ? 1 : 0); // Flags (ADR-0128): bit 0 frozen.
// Flags: bit 0 frozen (ADR-0128), bit 1 brake (rules 12). SQL never parses this byte, and
// player action records keep it zero: the trace alone carries a brake.
out.push_back(
static_cast<std::uint8_t>((life.intent.frozen ? 1U : 0U) | (life.intent.brake ? 2U : 0U)));
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);
@@ -301,7 +304,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 || record[13] > 1 || !all_zero(record.subspan(14, 2)))
if (record[12] > 3 || record[13] > 3 || !all_zero(record.subspan(14, 2)))
return std::unexpected{CellularBatchError::format};
CellularLifeIntent life;
life.life_id = get(record, 0, 8);
@@ -316,9 +319,10 @@ decode_cellular_batch(std::span<const std::uint8_t> bytes) {
} else if (!all_zero(record.subspan(16, 8))) {
return std::unexpected{CellularBatchError::format};
}
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.
life.intent.engulf = record[12] == 2; // ADR-0124 target kind.
life.intent.bud = record[12] == 3; // ADR-0125.
life.intent.frozen = (record[13] & 1U) != 0; // ADR-0128.
life.intent.brake = (record[13] & 2U) != 0; // Rules 12; owners below refuse it.
if (!valid_intent(life) ||
(!step.lives.empty() && step.lives.back().life_id >= life.life_id))
return std::unexpected{CellularBatchError::format};
+47
View File
@@ -0,0 +1,47 @@
#include "evo/server/lifecycle/cellular_liquid.hpp"
namespace evo::server {
bool cellular_liquid_valid(const CellularLiquidRules& rules, biology::CellularArea area) noexcept {
const auto& c = rules.currents;
const auto& f = rules.founder;
const auto& d = rules.deposits;
// Generation itself is checked once, by the owner that keeps the generated field.
return c.width_um == area.width_um && c.height_um == area.height_um && c.pitch_um != 0 &&
c.pitch_um <= biology::max_cellular_current_pitch_um && c.min_count <= c.max_count &&
f.thrust_q10 <= static_cast<std::uint32_t>(biology::cellular_velocity_limit_q10) &&
f.reverse_thrust_q10 <=
static_cast<std::uint32_t>(biology::cellular_velocity_limit_q10) &&
f.forward_drag_ppm < biology::cellular_drag_ppm_limit &&
f.lateral_drag_ppm < biology::cellular_drag_ppm_limit &&
f.turn <= biology::cellular_heading_turn / 2 && f.thrust_cost.value == 0 &&
f.reverse_cost.value == 0 && d.merged_cap != 0 && d.bubble_radius_um != 0 &&
d.bubble_radius_um <= biology::max_cellular_deposit_radius_um && d.max_drift_q10 >= 0 &&
d.max_drift_q10 <= biology::cellular_velocity_limit_q10 &&
d.drift_decay_ppm < biology::cellular_drag_ppm_limit;
}
CellularLiquidRules cellular_liquid_rules(const biology::CellularTuning& t,
biology::CellularArea area, std::uint64_t seed) {
const auto u32 = [](std::int64_t value) { return static_cast<std::uint32_t>(value); };
return {
.currents = {seed, u32(static_cast<std::int64_t>(area.width_um)),
u32(static_cast<std::int64_t>(area.height_um)), u32(t.currents_grid_pitch_um),
u32(t.currents_min_count), u32(t.currents_max_count),
u32(t.currents_min_width_um), u32(t.currents_max_width_um),
u32(t.currents_min_speed_um_per_s), u32(t.currents_max_speed_um_per_s)},
.founder = biology::founder_cellular_handling(t, {}, {}),
.deposits = {.drift_radius_um = u32(t.food_induced_drift_radius_um),
.max_drift_q10 =
static_cast<std::int32_t>(t.food_induced_drift_max_um_per_s *
biology::cellular_velocity_q10_per_um_s),
.drift_decay_ppm = u32(t.food_induced_drift_decay_ppm_per_tick),
.merged_cap = static_cast<std::uint16_t>(t.food_merged_bubble_cap_multiple),
.bubble_radius_um = biology::cellular_mote_radius_um}};
}
std::expected<biology::CellularHandling, biology::CellularHandlingError>
cellular_liquid_handling(const biology::Genome& genome, const biology::MetabolicBudget& budget,
const CellularLiquidRules& rules) {
return biology::derive_cellular_handling(genome, budget, rules.founder);
}
} // namespace evo::server
+220 -26
View File
@@ -14,15 +14,16 @@
namespace evo::server {
namespace {
constexpr std::uint64_t cellular_lives_v1_magic = 0x3130564C43564541ULL; // AEVCLV01
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::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::uint64_t cellular_lives_v1_magic = 0x3130564C43564541ULL; // AEVCLV01
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::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::uint64_t cellular_lives_v10_magic = 0x3031564C43564541ULL; // AEVCLV10
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;
@@ -43,6 +44,20 @@ constexpr std::size_t cellular_lives_v8_header_bytes = cellular_lives_v7_header_
// 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;
// Format 10 (rules 12): the current seed and nine 4-byte current fields (44), five founder
// handling fields (20), five deposit motion fields and reserved (24): 88 bytes. Each life adds
// its motion state (two velocities, heading, reserved, two carries), 20 bytes.
constexpr std::size_t cellular_lives_v10_header_bytes = cellular_lives_v9_header_bytes + 88;
constexpr std::size_t cellular_life_motion_bytes = 20;
bool valid_motion(const biology::CellularMotionState& m) noexcept {
constexpr std::int64_t carry = biology::cellular_velocity_q10_per_um_s * 1'000'000;
return m.velocity_x_q10 >= -biology::cellular_velocity_limit_q10 &&
m.velocity_x_q10 <= biology::cellular_velocity_limit_q10 &&
m.velocity_y_q10 >= -biology::cellular_velocity_limit_q10 &&
m.velocity_y_q10 <= biology::cellular_velocity_limit_q10 && m.remainder_x >= 0 &&
m.remainder_x < carry && m.remainder_y >= 0 && m.remainder_y < carry;
}
constexpr std::size_t cellular_offspring_revision_bytes = 32;
std::uint64_t checksum(std::span<const std::uint8_t> bytes) noexcept {
@@ -591,6 +606,8 @@ struct CellularLives::State {
std::vector<CellularOffspringRevision> offspring_revisions;
CellularLight light{}; // Format 9 (ADR-0175).
accounting::Picojoules captured{}; // Format 9: light credited to reserves so far.
CellularLiquidRules liquid{}; // Format 10 (rules 12).
std::shared_ptr<const biology::CellularFlowField> field; // Generated from `liquid`.
State(std::uint64_t world_id, std::uint64_t region_id, std::uint64_t frame_id,
CellularLivesConfig cfg, NurseryConfig nursery_definition,
@@ -912,6 +929,45 @@ CellularLives::photosynthesis_upgrade_candidate(CellularLight light) const {
(*candidate)->state_->light = light;
return candidate;
}
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
CellularLives::liquid_upgrade_candidate(const CellularLiquidRules& rules) const {
if (state_->format_version != cellular_lives_light_format ||
state_->rules_version != cellular_lives_photosynthesis_rules)
return std::unexpected{CellularLivesError::version};
if (!cellular_liquid_valid(rules, state_->config.area))
return std::unexpected{CellularLivesError::invalid_config};
auto field = biology::CellularFlowField::generate(rules.currents);
if (!field)
return std::unexpected{CellularLivesError::invalid_config};
auto config = state_->habitat->config();
if (config.format != biology::cellular_habitat_deposit_format ||
config.patches.size() > biology::max_cellular_liquid_deposits)
return std::unexpected{CellularLivesError::habitat};
config.format = biology::cellular_habitat_liquid_format;
for (auto& patch : config.patches)
patch.merged = 1;
auto habitat = biology::CellularHabitat::create(std::move(config));
if (!habitat)
return std::unexpected{CellularLivesError::habitat};
auto candidate = restore(snapshot(), world(), region(), frame(), tick());
if (!candidate)
return std::unexpected(candidate.error());
auto& next = *(*candidate)->state_;
next.habitat = std::move(*habitat);
next.format_version = cellular_lives_liquid_format;
next.rules_version = cellular_lives_liquid_rules;
next.liquid = rules;
next.field = std::make_shared<const biology::CellularFlowField>(std::move(*field));
for (auto& life : next.lives)
life.motion = {};
return candidate;
}
const CellularLiquidRules& CellularLives::liquid_rules() const noexcept {
return state_->liquid;
}
const biology::CellularFlowField* CellularLives::flow_field() const noexcept {
return state_->field.get();
}
const CellularLight& CellularLives::light() const noexcept {
return state_->light;
}
@@ -952,8 +1008,10 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
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 lit = state_->format_version == cellular_lives_light_format;
const auto life_bytes = inheritance ? cellular_life_v6_bytes + 8
const bool lit = state_->format_version >= cellular_lives_light_format;
const bool liquid = state_->format_version == cellular_lives_liquid_format;
const auto life_bytes = liquid ? cellular_life_v6_bytes + 8 + cellular_life_motion_bytes
: inheritance ? cellular_life_v6_bytes + 8
: state_->format_version == cellular_lives_legacy_format
? cellular_life_v1_bytes
: presence ? cellular_life_v6_bytes
@@ -965,7 +1023,8 @@ 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 = lit ? cellular_lives_v9_header_bytes
const auto header_bytes = liquid ? cellular_lives_v10_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
@@ -994,6 +1053,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
: liquid ? cellular_lives_v10_magic
: lit ? cellular_lives_v9_magic
: inheritance ? cellular_lives_v8_magic
: migration ? cellular_lives_v7_magic
@@ -1038,6 +1098,25 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
out.put(0, 4);
out.put(state_->captured.value);
}
if (liquid) {
const auto& c = state_->liquid.currents;
const auto& f = state_->liquid.founder;
const auto& d = state_->liquid.deposits;
out.put(c.seed);
for (const auto value :
{c.width_um, c.height_um, c.pitch_um, c.min_count, c.max_count, c.min_route_width_um,
c.max_route_width_um, c.min_speed_um_per_s, c.max_speed_um_per_s})
out.put(value, 4);
for (const auto value : {f.thrust_q10, f.reverse_thrust_q10, f.forward_drag_ppm,
f.lateral_drag_ppm, static_cast<std::uint32_t>(f.turn)})
out.put(value, 4);
out.put(d.drift_radius_um, 4);
out.put(static_cast<std::uint32_t>(d.max_drift_q10), 4);
out.put(d.drift_decay_ppm, 4);
out.put(d.merged_cap, 4);
out.put(d.bubble_radius_um, 4);
out.put(0, 4);
}
for (const auto site : state_->nursery_config.sites)
out.site(site);
for (const auto& life : state_->lives) {
@@ -1098,6 +1177,14 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
}
if (inheritance)
out.put(life.offspring_genome);
if (liquid) {
out.put(static_cast<std::uint32_t>(life.motion.velocity_x_q10), 4);
out.put(static_cast<std::uint32_t>(life.motion.velocity_y_q10), 4);
out.put(life.motion.heading, 2);
out.put(0, 2);
out.put(static_cast<std::uint64_t>(life.motion.remainder_x), 4);
out.put(static_cast<std::uint64_t>(life.motion.remainder_y), 4);
}
}
for (const auto& corpse : state_->corpses) {
out.put(corpse.life_id);
@@ -1336,6 +1423,8 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
candidate->state_->offspring_revisions = state_->offspring_revisions;
candidate->state_->light = state_->light;
candidate->state_->captured = state_->captured;
candidate->state_->liquid = state_->liquid;
candidate->state_->field = state_->field;
return candidate;
}
@@ -1359,9 +1448,14 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
rules_version == cellular_lives_rules;
// Format 6 carries rules 5, 6 (founder 2) or 7 (founder 3, ADR-0137); no new state.
// 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;
// Format 10 is exactly rules 12.
const bool liquid_version = magic == cellular_lives_v10_magic &&
format_version == cellular_lives_liquid_format &&
rules_version == cellular_lives_liquid_rules;
const bool light_version =
liquid_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 &&
@@ -1399,6 +1493,8 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
return std::unexpected(CellularLivesError::format);
if (light_version && bytes.size() < cellular_lives_v9_header_bytes + 8)
return std::unexpected(CellularLivesError::format);
if (liquid_version && bytes.size() < cellular_lives_v10_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();
@@ -1457,16 +1553,55 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
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
: bud_version ? cellular_life_v5_bytes
: cellular_life_v2_bytes;
CellularLiquidRules liquid{};
std::shared_ptr<const biology::CellularFlowField> field;
if (liquid_version) {
auto& c = liquid.currents;
auto& f = liquid.founder;
auto& d = liquid.deposits;
const auto u32 = [&in] { return static_cast<std::uint32_t>(in.get(4)); };
c.seed = in.get();
c.width_um = u32();
c.height_um = u32();
c.pitch_um = u32();
c.min_count = u32();
c.max_count = u32();
c.min_route_width_um = u32();
c.max_route_width_um = u32();
c.min_speed_um_per_s = u32();
c.max_speed_um_per_s = u32();
f.thrust_q10 = u32();
f.reverse_thrust_q10 = u32();
f.forward_drag_ppm = u32();
f.lateral_drag_ppm = u32();
const auto turn = u32();
f.turn = static_cast<std::uint16_t>(turn);
d.drift_radius_um = u32();
d.max_drift_q10 = static_cast<std::int32_t>(u32());
d.drift_decay_ppm = u32();
const auto cap = u32();
d.merged_cap = static_cast<std::uint16_t>(cap);
d.bubble_radius_um = u32();
if (in.get(4) != 0 || turn > 0xFFFFU || cap > 0xFFFFU ||
!cellular_liquid_valid(liquid, config.area))
return std::unexpected{CellularLivesError::format};
auto generated = biology::CellularFlowField::generate(liquid.currents);
if (!generated)
return std::unexpected{CellularLivesError::format};
field = std::make_shared<const biology::CellularFlowField>(std::move(*generated));
}
const auto life_bytes = liquid_version ? cellular_life_v6_bytes + 8 + cellular_life_motion_bytes
: inheritance_version ? cellular_life_v6_bytes + 8
: 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 = inheritance_version ? cellular_corpse_v5_bytes + 8
: bud_version ? cellular_corpse_v5_bytes
: engulf_version ? cellular_corpse_v4_bytes
: cellular_corpse_bytes;
const auto expected_size = (light_version ? cellular_lives_v9_header_bytes
const auto expected_size = (liquid_version ? cellular_lives_v10_header_bytes
: 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
@@ -1559,6 +1694,18 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
}
if (inheritance_version)
life.offspring_genome = in.get();
if (liquid_version) {
life.motion.velocity_x_q10 =
static_cast<std::int32_t>(static_cast<std::uint32_t>(in.get(4)));
life.motion.velocity_y_q10 =
static_cast<std::int32_t>(static_cast<std::uint32_t>(in.get(4)));
life.motion.heading = static_cast<std::uint16_t>(in.get(2));
const auto padding = in.get(2);
life.motion.remainder_x = static_cast<std::int64_t>(in.get(4));
life.motion.remainder_y = static_cast<std::int64_t>(in.get(4));
if (padding != 0 || !valid_motion(life.motion))
return std::unexpected{CellularLivesError::noncanonical};
}
lives.push_back(life);
}
@@ -1607,9 +1754,12 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
return std::unexpected{CellularLivesError::checksum};
return std::unexpected{CellularLivesError::habitat};
}
// Rules 8 run on a deposit habitat, and nothing else does (ADR-0136).
if ((rules_version >= cellular_lives_foraging_rules) !=
((*habitat)->config().format == biology::cellular_habitat_deposit_format))
// Rules 8 to 11 run on a deposit habitat, rules 12 on a liquid one (ADR-0136, rules 12).
const auto habitat_format = (*habitat)->config().format;
if ((rules_version >= cellular_lives_liquid_rules)
? habitat_format != biology::cellular_habitat_liquid_format
: (rules_version >= cellular_lives_foraging_rules) !=
(habitat_format == biology::cellular_habitat_deposit_format))
return std::unexpected{CellularLivesError::habitat};
auto archive = biology::LineageArchive::decode(archive_payload);
if (!archive) {
@@ -1672,6 +1822,8 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
restored->state_->offspring_revisions = std::move(plan_history);
restored->state_->light = light;
restored->state_->captured = captured;
restored->state_->liquid = liquid;
restored->state_->field = std::move(field);
if (!std::ranges::equal(bytes, restored->snapshot()))
return std::unexpected{CellularLivesError::noncanonical};
return restored;
@@ -1708,6 +1860,9 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
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 = cellular_retained_plans(state_->rules_version);
const bool liquid_rules = state_->rules_version >= cellular_lives_liquid_rules;
if (liquid_rules && !state_->field)
return std::unexpected{CellularLivesError::invalid_config};
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).
@@ -1717,7 +1872,9 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
// carries nothing else.
if (intent.frozen && (!presence_rules || state_->lives[i].account == LifeAccount{} ||
intent.movement != biology::CellularIntent{} ||
intent.intake_target || intent.engulf || intent.bud))
intent.intake_target || intent.engulf || intent.bud || intent.brake))
return std::unexpected{CellularLivesError::invalid_intents};
if (intent.brake && !liquid_rules)
return std::unexpected{CellularLivesError::invalid_intents};
}
for (std::size_t i = 0; i < selections.size(); ++i) {
@@ -1776,6 +1933,15 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
*budget,
*membrane,
life.source});
if (liquid_rules) {
const auto handling = cellular_liquid_handling(*genome, *budget, state_->liquid);
if (!handling)
return std::unexpected{CellularLivesError::biology};
inputs.back().liquid = biology::CellularLiquidInput{
life.motion, *handling,
state_->field->at_footprint(life.position, budget->phenotype.extent),
ordered[i].intent.brake};
}
}
auto proposal =
biology::propose_cellular_region_step(state_->config.tick, state_->config.area, inputs,
@@ -1796,6 +1962,8 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
return std::unexpected{CellularLivesError::invalid_life};
found->position = result.candidate.position;
found->reserve = result.candidate.reserve_after;
if (liquid_rules)
found->motion = result.candidate.motion;
if (!add_inventory(next_heat, result.candidate.heat_after))
return std::unexpected{CellularLivesError::overflow};
// Only owned lives earn mutation offers; wildlife never mutates in M11 (ADR-0098).
@@ -2157,7 +2325,8 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
// Rules 3 detritus at empty cells becomes new patches; create() revalidates the map.
// Format 2 (ADR-0136): new bubbles take the next deposit ids, and a deposit emptied this
// tick is gone.
const bool deposits = habitat_config.format == biology::cellular_habitat_deposit_format;
const bool deposits = habitat_config.format == biology::cellular_habitat_deposit_format ||
habitat_config.format == biology::cellular_habitat_liquid_format;
for (auto made : proposal->new_patches) {
const auto at = std::ranges::lower_bound(habitat_config.patches, made.address, {},
[](const auto& patch) { return patch.address; });
@@ -2174,6 +2343,29 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
std::erase_if(habitat_config.patches, [](const biology::CellularPatch& patch) {
return patch.stock == accounting::Inventory{};
});
// Rules 12: after every life fed where the food was, bubbles move with the current and
// the drift that this tick's moving cells induce, then touching bubbles merge.
if (liquid_rules) {
std::vector<biology::CellularWake> wakes;
wakes.reserve(proposal->lives.size());
for (const auto& result : proposal->lives) {
const auto input = std::ranges::lower_bound(
inputs, result.life_id, {},
[](const biology::CellularLifeStepInput& value) { return value.life_id; });
if (input == inputs.end() || input->life_id != result.life_id || input->intent.frozen)
continue;
const auto flow =
state_->field->at_footprint(result.candidate.position, input->extent_um);
wakes.push_back({result.candidate.position, input->extent_um,
result.candidate.motion.velocity_x_q10 + flow.x_q10,
result.candidate.motion.velocity_y_q10 + flow.y_q10});
}
auto moved = biology::advect_cellular_deposits(habitat_config, *state_->field, wakes,
state_->liquid.deposits);
if (!moved)
return std::unexpected{CellularLivesError::habitat};
habitat_config = std::move(moved->config);
}
auto next_habitat = biology::CellularHabitat::create(std::move(habitat_config));
if (!next_habitat)
return std::unexpected{CellularLivesError::habitat};
@@ -2200,6 +2392,8 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
next->offspring_revisions = std::move(next_offspring_revisions);
next->light = state_->light;
next->captured = state_->captured;
next->liquid = state_->liquid;
next->field = state_->field;
// ADR-0175: light, from this tick's own intentions, credited to every active,
// unfrozen photosynthetic survivor's reserve and to the boundary inflow together.
std::vector<std::pair<std::uint64_t, std::uint64_t>> lit_lives;