feat(cellular): session v14, bundle 15 and rules-13 wildlife
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m29s
Foundation / postgres-tools (push) Successful in 24s
Foundation / server-image (push) Successful in 4m33s
Foundation / image (push) Successful in 4m59s

Session v14 carries the rules-13 revamp: a body block in the owner's view
(members, emergence threshold, exclusions, Emerge open, payable energy,
mass and the 20 traits), become, emerge and channel exclusions on actions,
become on mutation choices, catalog offers (id, chance, cost) and the
emerged ending. Older sessions never see a v14 field. The client shows
Stay / Become offspring offers named from the shared catalog, continuing
as a bud, the organism and its Emerge button, kept channels, the body and
the emerged end screen.

Bundle 15 (rules 13 on owner format 11) is durable: schema 60 admits it
raw-only, stores become as bit 15 of the choice's operator, widens the
action input and mutation results, and publishes the 14 -> 15 revamp
profile migration. evo-world-clone opens cellular-v10 (cellular-v9 plus the
revamp upgrade from --revamp-tuning), resumable and recoverable.

Wildlife counts reserve matter as energy, waits for the bud fee, grows
organisms one short of emerging, and hunts by the region step's rules-13
engulf test. The play summary adds organisms, offers and the spent pool.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-26 07:31:32 -04:00
co-authored by Claude Opus 5.5
parent f454e038a1
commit 4bc5d5eee7
48 changed files with 1602 additions and 174 deletions
+14 -5
View File
@@ -101,16 +101,25 @@ std::optional<Limits> revamp_limits(const CellularLifeStepInput& life,
limits.shared = shared * static_cast<std::uint64_t>(mult) / 1'000;
return limits;
}
// Rules 13: engulf mass is the body plus the declared share of reserve matter.
std::uint64_t engulf_mass(const CellularLifeStepInput& life, const accounting::Inventory& reserve,
const CellularRevampStepRules& rules) noexcept {
const auto carried = matter_of(reserve) / 100 * rules.engulf_reserve_percent +
matter_of(reserve) % 100 * rules.engulf_reserve_percent / 100;
const auto body = life.budget.phenotype.total_mass;
return carried > u64_max - body ? u64_max : body + carried;
return cellular_engulf_mass(life.budget.phenotype.total_mass, reserve, rules);
}
} // namespace
std::uint64_t cellular_engulf_mass(std::uint64_t body_mass, const accounting::Inventory& reserve,
const CellularRevampStepRules& rules) noexcept {
const auto carried = matter_of(reserve) / 100 * rules.engulf_reserve_percent +
matter_of(reserve) % 100 * rules.engulf_reserve_percent / 100;
return carried > u64_max - body_mass ? u64_max : body_mass + carried;
}
bool cellular_engulfable(std::uint64_t hunter_mass, std::uint16_t engulf_capacity,
std::uint64_t prey_mass, std::uint32_t prey_membrane_nm,
const CellularRevampStepRules& rules) noexcept {
return product_at_least(hunter_mass, std::uint64_t{engulf_capacity} * 10'000, prey_mass,
std::uint64_t{rules.engulf_ratio_milli} * prey_membrane_nm);
}
std::expected<CellularRegionStepCandidate, CellularRegionStepError>
propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
std::span<const CellularLifeStepInput> lives,
+15 -9
View File
@@ -309,33 +309,39 @@ bool RegionSessionClient::send_action(std::uint64_t action_id, network::ActionKi
bool RegionSessionClient::send_cellular_action(
std::uint64_t action_id, std::uint64_t tick, std::int16_t move_x, std::int16_t move_y,
std::optional<std::pair<std::uint32_t, std::uint32_t>> intake_target, bool engulf, bool bud,
bool brake) {
bool brake, bool become, bool emerge, std::uint8_t exclude) {
auto& m = *impl_;
const bool revamp = m.protocol_version == network::session_protocol_v14;
network::CellularAction action{
m.welcome ? m.welcome->session : 0, m.welcome ? m.welcome->epoch : 0, action_id, tick,
move_x, move_y, intake_target.has_value(), intake_target ? intake_target->first : 0,
intake_target ? intake_target->second : 0, engulf, bud,
// A brake needs v13 or v14; older sessions never send one.
brake && network::cellular_liquid_version(m.protocol_version)};
action.become = revamp && become;
action.emerge = revamp && emerge;
action.exclude = revamp ? exclude : std::uint8_t{0};
return (m.protocol_version == network::session_protocol_v7 ||
m.protocol_version == network::session_protocol_v8 ||
m.protocol_version == network::session_protocol_v9 ||
network::cellular_v10_family(m.protocol_version)) &&
m.state == RegionSessionState::established && m.welcome && action_id != 0 && tick != 0 &&
m.send(network::CellularAction{
m.welcome->session, m.welcome->epoch, action_id, tick, move_x, move_y,
intake_target.has_value(), intake_target ? intake_target->first : 0,
intake_target ? intake_target->second : 0, engulf, bud,
// A brake needs v13; older sessions never send one.
brake && m.protocol_version == network::session_protocol_v13});
m.send(action);
}
bool RegionSessionClient::send_cellular_mutation(std::uint64_t action_id, std::uint64_t tick,
std::uint64_t life_identity,
std::uint64_t event_index,
std::uint64_t offer_tick,
std::uint16_t operator_id) {
std::uint16_t operator_id, bool become) {
auto& m = *impl_;
return network::cellular_v10_family(m.protocol_version) &&
m.state == RegionSessionState::established && m.welcome && action_id != 0 &&
offer_tick != 0 && tick > offer_tick &&
(!become || m.protocol_version == network::session_protocol_v14) &&
m.send(network::CellularMutationAction{m.welcome->session, m.welcome->epoch, action_id,
tick, life_identity, event_index, offer_tick,
operator_id});
operator_id, become});
}
bool RegionSessionClient::query_action(std::uint64_t action_id) {
+6 -6
View File
@@ -238,9 +238,9 @@ ActionReceipt IntentAdmission::action(std::uint64_t session, const CellularActio
const AdmissionFacts& facts) {
const SessionAction base{m.session, m.epoch, m.action_id,
static_cast<std::uint16_t>(ActionKind::cellular), m.tick};
const auto bytes =
encode_cellular_action_input({m.tick, m.move_x, m.move_y, m.has_intake_target,
m.intake_x_um, m.intake_y_um, m.engulf, m.bud, m.brake});
const auto bytes = encode_cellular_action_input(
{m.tick, m.move_x, m.move_y, m.has_intake_target, m.intake_x_um, m.intake_y_um, m.engulf,
m.bud, m.brake, m.become, m.emerge, m.exclude});
auto* s = find(session);
if (!s)
return {m.action_id, AdmitStatus::unknown_session, facts.server_tick, m.tick};
@@ -280,9 +280,9 @@ std::optional<ActionReceipt> IntentAdmission::admit(std::uint64_t session, const
const AdmissionFacts& facts) {
const SessionAction base{m.session, m.epoch, m.action_id,
static_cast<std::uint16_t>(ActionKind::cellular), m.tick};
const auto bytes =
encode_cellular_action_input({m.tick, m.move_x, m.move_y, m.has_intake_target,
m.intake_x_um, m.intake_y_um, m.engulf, m.bud, m.brake});
const auto bytes = encode_cellular_action_input(
{m.tick, m.move_x, m.move_y, m.has_intake_target, m.intake_x_um, m.intake_y_um, m.engulf,
m.bud, m.brake, m.become, m.emerge, m.exclude});
auto* s = find(session);
if (!s)
return ActionReceipt{m.action_id, AdmitStatus::unknown_session, facts.server_tick, m.tick};
+121 -23
View File
@@ -37,6 +37,12 @@ constexpr std::size_t cellular_perceived_producer_bytes = 8 + 2 + 6;
constexpr std::size_t cellular_liquid_block_bytes = 4 + 4 + 2 + 1 + 1 + 8 + (9 * 4);
constexpr std::size_t cellular_perception_v13_prefix_bytes =
cellular_perception_v12_prefix_bytes + cellular_liquid_block_bytes;
// v14 (rules 13) appends the body block: members and threshold (2 x u16), exclusions, flags
// (bit 0 Emerge open), two reserved bytes, payable energy and total mass (2 x u64) and twenty
// u16 traits.
constexpr std::size_t cellular_revamp_block_bytes = 2 + 2 + 1 + 1 + 2 + 8 + 8 + (20 * 2);
constexpr std::size_t cellular_perception_v14_prefix_bytes =
cellular_perception_v13_prefix_bytes + cellular_revamp_block_bytes;
constexpr std::size_t cellular_perceived_patch_bytes = 8 + 8 + (4 * 8);
// v11: id, centre, radius, shape, drained flag and five 16-bit amount codes.
constexpr std::size_t cellular_perceived_deposit_bytes = 4 + 4 + 4 + 2 + 1 + 1 + (5 * 2);
@@ -70,8 +76,14 @@ bool valid_offer(const CellularMutationOffer& offer, std::uint16_t version) {
return offer.event_index == 0 && offer.offer_tick == 0;
std::uint16_t previous = 0;
for (const auto& candidate : offer.candidates) {
// v14: catalog ids with a chance in parts per million and nothing operator-shaped.
const bool named = version == session_protocol_v14;
if (candidate.operator_id <= previous ||
candidate.capability > cellular_offer_capability_limit(version) || candidate.effect > 3)
(named ? candidate.capability != 0 || candidate.effect != 0 ||
candidate.parameter > 1'000'000 || candidate.maintenance != 0 ||
candidate.total_mass != 0
: candidate.capability > cellular_offer_capability_limit(version) ||
candidate.effect > 3))
return false;
previous = candidate.operator_id;
}
@@ -214,7 +226,18 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message, std::uin
return {};
if (const auto* ended = std::get_if<CellularLifeEnded>(&message);
ended && (!cellular_v10_family(version) || ended->tick == 0 || ended->life_identity == 0 ||
ended->cause > CellularDeathReason::engulfed))
ended->cause > (version == session_protocol_v14 ? CellularDeathReason::emerged
: CellularDeathReason::engulfed)))
return {};
if (const auto* choice = std::get_if<CellularMutationAction>(&message);
choice && choice->become && version != session_protocol_v14)
return {};
// v14 alone carries become, emerge and exclusions; become needs a bud, emerge no target.
if (const auto* action = std::get_if<CellularAction>(&message);
action && (action->become || action->emerge || action->exclude != 0) &&
(version != session_protocol_v14 || action->exclude > 0x0F ||
(action->become && !action->bud) ||
(action->emerge && (action->bud || action->engulf || action->has_intake_target))))
return {};
// v9 has no room for engulf and bud effects or the bud cost; only v10 reports them
// (ADR-0129).
@@ -238,7 +261,15 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message, std::uin
// v12 and v13 carry captured light and producers (ADR-0176); v13 alone carries
// liquid motion.
const bool producers = cellular_light_version(version);
const bool liquid = version == session_protocol_v13;
const bool liquid = cellular_liquid_version(version);
const auto& body = perception->diagnostics;
if (version != session_protocol_v14 && body &&
(body->members || body->emerge_members || body->exclude || body->can_emerge ||
body->payable_energy_pj || body->total_mass_fg ||
std::ranges::any_of(body->traits, [](std::uint16_t value) { return value != 0; })))
return {};
if (version == session_protocol_v14 && body && (body->members == 0 || body->exclude > 0x0F))
return {};
if (!liquid && (perception->currents ||
(perception->diagnostics && (perception->diagnostics->velocity_x_q10 ||
perception->diagnostics->velocity_y_q10 ||
@@ -357,8 +388,11 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message, std::uin
put(out, static_cast<std::uint16_t>(m.move_x), 2);
put(out, static_cast<std::uint16_t>(m.move_y), 2);
out.push_back(m.has_intake_target ? 1 : m.engulf ? 2 : m.bud ? 3 : 0); // Target kind.
out.push_back(m.brake ? 1 : 0); // v13 flags: bit 0 brake.
put(out, 0, 2);
// v13 flags: bit 0 brake; v14 adds bit 1 become and bit 2 emerge.
out.push_back(static_cast<std::uint8_t>((m.brake ? 1U : 0U) | (m.become ? 2U : 0U) |
(m.emerge ? 4U : 0U)));
out.push_back(m.exclude); // v14: channels excluded from burning.
out.push_back(0);
put(out, m.intake_x_um, 4);
put(out, m.intake_y_um, 4);
return out;
@@ -370,7 +404,8 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message, std::uin
m.event_index, m.offer_tick})
put(out, value, 8);
put(out, m.operator_id, 2);
put(out, 0, 6);
out.push_back(m.become ? 1 : 0); // v14.
put(out, 0, 5);
return out;
}
std::vector<std::uint8_t> operator()(const CellularLifeEnded& m) const {
@@ -407,13 +442,14 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message, std::uin
std::vector<std::uint8_t> operator()(const CellularPerception& m) const {
auto out =
header(SessionMessageType::cellular_perception,
(version == session_protocol_v13 ? cellular_perception_v13_prefix_bytes
(version == session_protocol_v14 ? cellular_perception_v14_prefix_bytes
: version == session_protocol_v13 ? cellular_perception_v13_prefix_bytes
: version == session_protocol_v12 ? cellular_perception_v12_prefix_bytes
: cellular_v10_family(version) ? cellular_perception_v10_prefix_bytes
: version == session_protocol_v9 ? cellular_perception_v9_prefix_bytes
: cellular_perception_prefix_bytes) +
m.patches.size() * cellular_perceived_patch_bytes +
m.deposits.size() * (version == session_protocol_v13
m.deposits.size() * (cellular_liquid_version(version)
? cellular_perceived_liquid_deposit_bytes
: cellular_perceived_deposit_bytes) +
m.producers.size() * cellular_perceived_producer_bytes,
@@ -446,7 +482,7 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message, std::uin
put(out, m.producers.size(), 2);
put(out, 0, 6);
}
if (version == session_protocol_v13) {
if (cellular_liquid_version(version)) {
put(out, static_cast<std::uint32_t>(d ? d->velocity_x_q10 : 0), 4);
put(out, static_cast<std::uint32_t>(d ? d->velocity_y_q10 : 0), 4);
put(out, d ? d->heading : 0, 2);
@@ -460,6 +496,18 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message, std::uin
c.max_speed_um_per_s})
put(out, value, 4);
}
if (version == session_protocol_v14) {
const auto b = d.value_or(CellularLifeDiagnostics{});
put(out, b.members, 2);
put(out, b.emerge_members, 2);
out.push_back(b.exclude);
out.push_back(b.can_emerge ? 1 : 0);
put(out, 0, 2);
put(out, b.payable_energy_pj, 8);
put(out, b.total_mass_fg, 8);
for (const auto value : b.traits)
put(out, value, 2);
}
} else {
put(out, 0, 5);
}
@@ -480,7 +528,7 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message, std::uin
out.push_back(deposit.drained ? 1 : 0);
for (const auto code : deposit.amount_codes)
put(out, code, 2);
if (version == session_protocol_v13) {
if (cellular_liquid_version(version)) {
put(out, static_cast<std::uint16_t>(deposit.velocity_x_um_s), 2);
put(out, static_cast<std::uint16_t>(deposit.velocity_y_um_s), 2);
}
@@ -721,14 +769,22 @@ std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const
m.move_x = static_cast<std::int16_t>(static_cast<std::uint16_t>(get(payload, 32, 2)));
m.move_y = static_cast<std::int16_t>(static_cast<std::uint16_t>(get(payload, 34, 2)));
// Target kind: 0 none, 1 patch intake, 2 engulf and 3 bud (v10 only, ADR-0124/0125).
const bool revamp = wire_version == session_protocol_v14;
if (payload[36] > (cellular_v10_family(wire_version) ? 3 : 1) ||
payload[37] > (wire_version == session_protocol_v13 ? 1 : 0) || payload[38] != 0 ||
payload[39] != 0)
payload[37] > (revamp ? 7
: wire_version == session_protocol_v13 ? 1
: 0) ||
payload[38] > (revamp ? 0x0F : 0) || payload[39] != 0)
return std::unexpected(SessionCodecError::invalid_value);
m.has_intake_target = payload[36] == 1;
m.engulf = payload[36] == 2;
m.bud = payload[36] == 3;
m.brake = payload[37] == 1;
m.brake = (payload[37] & 1U) != 0;
m.become = (payload[37] & 2U) != 0;
m.emerge = (payload[37] & 4U) != 0;
m.exclude = payload[38];
if ((m.become && !m.bud) || (m.emerge && payload[36] != 0))
return std::unexpected(SessionCodecError::invalid_value);
m.intake_x_um = static_cast<std::uint32_t>(get(payload, 40, 4));
m.intake_y_um = static_cast<std::uint32_t>(get(payload, 44, 4));
if (!m.session || !m.epoch || !m.action_id || !m.tick ||
@@ -750,9 +806,11 @@ std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const
m.event_index = get(payload, 40, 8);
m.offer_tick = get(payload, 48, 8);
m.operator_id = static_cast<std::uint16_t>(get(payload, 56, 2));
m.become = payload[58] == 1;
if (!m.session || !m.epoch || !m.action_id || !m.tick || !m.life_identity ||
!m.event_index || !m.offer_tick || m.offer_tick >= m.tick || // operator 0 declines
std::ranges::any_of(payload.subspan(58, 6),
payload[58] > (wire_version == session_protocol_v14 ? 1 : 0) ||
std::ranges::any_of(payload.subspan(59, 5),
[](std::uint8_t value) { return value != 0; }))
return std::unexpected(SessionCodecError::invalid_value);
return m;
@@ -766,7 +824,9 @@ std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const
m.tick = get(payload, 0, 8);
m.life_identity = get(payload, 8, 8);
if (m.tick == 0 || m.life_identity == 0 ||
payload[16] > static_cast<std::uint8_t>(CellularDeathReason::engulfed) ||
payload[16] > static_cast<std::uint8_t>(wire_version == session_protocol_v14
? CellularDeathReason::emerged
: CellularDeathReason::engulfed) ||
std::ranges::any_of(payload.subspan(17, 7),
[](std::uint8_t value) { return value != 0; }))
return std::unexpected(SessionCodecError::invalid_value);
@@ -823,9 +883,11 @@ std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const
payload.size() == cellular_perception_v9_prefix_bytes +
count * cellular_perceived_patch_bytes;
const bool lit = cellular_light_version(wire_version);
const bool liquid = wire_version == session_protocol_v13;
const auto prefix = liquid ? cellular_perception_v13_prefix_bytes
: lit ? cellular_perception_v12_prefix_bytes
const bool liquid = cellular_liquid_version(wire_version);
const bool revamp = wire_version == session_protocol_v14;
const auto prefix = revamp ? cellular_perception_v14_prefix_bytes
: liquid ? cellular_perception_v13_prefix_bytes
: lit ? cellular_perception_v12_prefix_bytes
: cellular_v10_family(wire_version) && !without_bud_cost
? cellular_perception_v10_prefix_bytes
: wire_version == session_protocol_v9 || without_bud_cost
@@ -935,6 +997,35 @@ std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const
if (flags == 1)
m.currents = c;
}
if (revamp) {
constexpr auto at = cellular_perception_v13_prefix_bytes;
CellularLifeDiagnostics b;
b.members = static_cast<std::uint16_t>(get(payload, at, 2));
b.emerge_members = static_cast<std::uint16_t>(get(payload, at + 2, 2));
b.exclude = payload[at + 4];
b.can_emerge = payload[at + 5] == 1;
b.payable_energy_pj = get(payload, at + 8, 8);
b.total_mass_fg = get(payload, at + 16, 8);
for (std::size_t i = 0; i < b.traits.size(); ++i)
b.traits.at(i) = static_cast<std::uint16_t>(get(payload, at + 24 + i * 2, 2));
const bool empty = b.members == 0 && b.emerge_members == 0 && b.exclude == 0 &&
payload[at + 5] == 0 && b.payable_energy_pj == 0 &&
b.total_mass_fg == 0 &&
std::ranges::all_of(b.traits, [](auto v) { return v == 0; });
if (b.exclude > 0x0F || payload[at + 5] > 1 || payload[at + 6] != 0 ||
payload[at + 7] != 0 || (m.diagnostics ? b.members == 0 : !empty))
return std::unexpected(SessionCodecError::invalid_value);
if (m.diagnostics) {
auto& d = *m.diagnostics;
d.members = b.members;
d.emerge_members = b.emerge_members;
d.exclude = b.exclude;
d.can_emerge = b.can_emerge;
d.payable_energy_pj = b.payable_energy_pj;
d.total_mass_fg = b.total_mass_fg;
d.traits = b.traits;
}
}
std::size_t offset = prefix;
if (deposits) {
m.deposits.reserve(count);
@@ -1167,8 +1258,11 @@ encode_cellular_action_input(const CellularActionInput& input) {
: input.engulf ? 2
: input.bud ? 3
: 0);
bytes.push_back(input.brake ? 1 : 0); // Flags (schema 58): bit 0 brake.
put(bytes, 0, 2);
// Flags: bit 0 brake (schema 58), bit 1 become and bit 2 emerge (schema 60).
bytes.push_back(static_cast<std::uint8_t>((input.brake ? 1U : 0U) | (input.become ? 2U : 0U) |
(input.emerge ? 4U : 0U)));
bytes.push_back(input.exclude); // Schema 60: channels excluded from burning.
bytes.push_back(0);
put(bytes, input.intake_x_um, 4);
put(bytes, input.intake_y_um, 4);
std::array<std::uint8_t, cellular_action_input_bytes> out{};
@@ -1180,7 +1274,7 @@ std::expected<CellularActionInput, SessionCodecError>
decode_cellular_action_input(std::span<const std::uint8_t> bytes) {
if (bytes.size() != cellular_action_input_bytes)
return std::unexpected(SessionCodecError::wrong_length);
if (bytes[12] > 3 || bytes[13] > 1 || bytes[14] != 0 || bytes[15] != 0)
if (bytes[12] > 3 || bytes[13] > 7 || bytes[14] > 0x0F || bytes[15] != 0)
return std::unexpected(SessionCodecError::invalid_value);
CellularActionInput input;
input.tick = get(bytes, 0, 8);
@@ -1189,11 +1283,15 @@ decode_cellular_action_input(std::span<const std::uint8_t> bytes) {
input.has_intake_target = bytes[12] == 1;
input.engulf = bytes[12] == 2;
input.bud = bytes[12] == 3;
input.brake = bytes[13] == 1;
input.brake = (bytes[13] & 1U) != 0;
input.become = (bytes[13] & 2U) != 0;
input.emerge = (bytes[13] & 4U) != 0;
input.exclude = bytes[14];
input.intake_x_um = static_cast<std::uint32_t>(get(bytes, 16, 4));
input.intake_y_um = static_cast<std::uint32_t>(get(bytes, 20, 4));
if (input.tick == 0 ||
(!input.has_intake_target && (input.intake_x_um != 0 || input.intake_y_um != 0)))
(!input.has_intake_target && (input.intake_x_um != 0 || input.intake_y_um != 0)) ||
(input.become && !input.bud) || (input.emerge && bytes[12] != 0))
return std::unexpected(SessionCodecError::invalid_value);
return input;
}
+30 -4
View File
@@ -93,7 +93,8 @@ std::optional<std::uint64_t> applied_tick(const server::CellularBatch& trace,
if (selection.life_id == action.input.life_id &&
selection.event_index == action.input.event_index &&
selection.offer_tick == action.input.offer_tick &&
selection.operator_id == action.input.operator_id) {
selection.operator_id == action.input.operator_id &&
selection.become == action.input.become) {
if (found)
return std::nullopt;
found = trace.header.begin_tick + i + 1;
@@ -120,7 +121,10 @@ bool prepared_mutations_are_canonical(const CellularPreparedCommit& prepared,
prepared.mutations, manifest.begin_tick, manifest.end_tick);
if (!decoded || decoded->size() != selections || prepared.results.size() != selections ||
std::ranges::any_of(prepared.results, [&](std::uint8_t result) {
return cellular_plan_bundle(manifest.cellular_versions)
// Rules 13 (bundle 15): applied (an organism), life died, insufficient funding,
// declined, split applied and split refused.
return manifest.cellular_versions == cellular_v15_versions ? result == 4 || result > 6
: cellular_plan_bundle(manifest.cellular_versions)
? result != 1 && result != 3 && result != 4
: result > 3;
}))
@@ -396,6 +400,17 @@ std::expected<CommittedCheckpoint, CellularProfileMigrationFailure> migrate_cell
},
cellular_liquid_upgrade_manifest);
}
std::expected<CommittedCheckpoint, CellularProfileMigrationFailure> migrate_cellular_revamp_profile(
Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease,
const CommittedCheckpoint& predecessor, Uuid commit, const Sha256Digest& build,
const server::CellularRevampRules& rules, milliseconds timeout, const PublishHooks& hooks) {
return migrate_rules_profile(
db, objects, lease, predecessor, commit, build, timeout, hooks,
[rules](std::span<const std::uint8_t> payload, const CheckpointManifest& previous) {
return cellular_revamp_upgrade_payload(payload, previous, rules);
},
cellular_revamp_upgrade_manifest);
}
std::expected<CommittedCheckpoint, CellularProfileMigrationFailure>
migrate_cellular_profile_to_v2(Postgres& db, const FilesystemObjectStore& objects,
@@ -685,7 +700,9 @@ verify_cellular_no_tick_checkpoint(Postgres& db, const FilesystemObjectStore& ob
const bool inheritance = m.cellular_versions == cellular_v12_versions;
const bool photosynthesis = m.cellular_versions == cellular_v13_versions;
const bool liquid = m.cellular_versions == cellular_v14_versions;
const auto derive = liquid ? cellular_liquid_upgrade_manifest
const bool revamp = m.cellular_versions == cellular_v15_versions;
const auto derive = revamp ? cellular_revamp_upgrade_manifest
: liquid ? cellular_liquid_upgrade_manifest
: photosynthesis ? cellular_photosynthesis_upgrade_manifest
: inheritance ? cellular_inheritance_upgrade_manifest
: maturation ? cellular_maturation_upgrade_manifest
@@ -698,7 +715,16 @@ verify_cellular_no_tick_checkpoint(Postgres& db, const FilesystemObjectStore& ob
auto bytes = objects.get({source->payload_sha256, source->payload_bytes});
if (!bytes)
return fail(RecoveryError::tail);
if (liquid) {
if (revamp) {
// The declared revamp rules are the upgrade's only input: read them from the target,
// then require the source to re-derive the exact target bytes with them.
auto target = restore_cellular_payload(payload, m);
if (!target)
return fail(RecoveryError::payload);
auto upgraded = cellular_revamp_upgrade_payload(*bytes, *source, (*target)->revamp_rules());
if (!upgraded || !std::ranges::equal(*upgraded, payload))
return fail(RecoveryError::history);
} else if (liquid) {
// The declared liquid rules are the upgrade's only input: read them from the target,
// then require the source to re-derive the exact target bytes with them.
auto target = restore_cellular_payload(payload, m);
+27
View File
@@ -251,4 +251,31 @@ cellular_liquid_upgrade_payload(std::span<const std::uint8_t> payload,
return std::unexpected(CheckpointError::payload);
return (*upgraded)->snapshot();
}
std::expected<CheckpointManifest, CheckpointError>
cellular_revamp_upgrade_manifest(const CheckpointManifest& previous,
const Sha256Digest& previous_digest, Uuid commit,
const LeaseGrant& lease, const Sha256Digest& build,
std::uint64_t bytes, const Sha256Digest& digest) {
if (previous.profile != CheckpointProfile::cellular_lives_v1)
return std::unexpected(CheckpointError::unsupported);
return upgrade_manifest(previous, previous_digest, commit, lease, build, bytes, digest,
cellular_v14_versions, cellular_v15_versions);
}
std::expected<std::vector<std::uint8_t>, CheckpointError>
cellular_revamp_upgrade_payload(std::span<const std::uint8_t> payload,
const CheckpointManifest& previous,
const server::CellularRevampRules& rules) {
if (!supported(previous) || previous.profile != CheckpointProfile::cellular_lives_v1 ||
previous.cellular_versions != cellular_v14_versions)
return std::unexpected(CheckpointError::unsupported);
if (!encode_manifest(previous) || previous.sequence == 0 || previous.end_tick == 0)
return std::unexpected(CheckpointError::chain);
auto state = restore_cellular_payload(payload, previous);
if (!state)
return std::unexpected(state.error());
auto upgraded = (*state)->revamp_upgrade_candidate(rules);
if (!upgraded)
return std::unexpected(CheckpointError::payload);
return (*upgraded)->snapshot();
}
} // namespace evo::persistence
+1 -1
View File
@@ -606,7 +606,7 @@ load_cellular_mutation_publications(Postgres& db, RegionKey key, const LeaseGran
const auto action = parse_integer<std::uint64_t>(row[1]);
const auto input = unhex<34>(row[2].value_or(std::string{}));
const auto result = parse_integer<std::uint32_t>(row[3]);
if (!account || !action || *action == 0 || !input || !result || *result > 4)
if (!account || !action || *action == 0 || !input || !result || *result > 6)
return std::unexpected{malformed(false)};
publications.push_back({*account, *action, *input, static_cast<std::uint8_t>(*result)});
}
+43 -9
View File
@@ -128,6 +128,28 @@ network::ActionOutcome action_outcome(const FinalCellularMutation& mutation) noe
network::CellularMutationOffer offer_view(const CellularLives& state, const CellularOwnedLife& life,
std::uint64_t tick) {
network::CellularMutationOffer view{tick, life.identity.life, 0, 0, {}};
if (state.rules_version() >= cellular_lives_revamp_rules) {
// Rules 13 (v14): catalog mutations with their chance and tuned cost; the client names
// them from the same catalog.
const auto& pending = life.revamp.offer;
if (pending.count == 0)
return view;
view.event_index = pending.event_index;
view.offer_tick = pending.offer_tick;
for (std::size_t k = 0; k < pending.count; ++k) {
const auto id = pending.ids.at(k);
if (id == 0 || id > biology::cellular_catalog_size)
continue;
const auto cost = cellular_mutation_cost(state.revamp_rules().mutations.at(id - 1u));
network::CellularOfferedCandidate shown{
id, 0, 0, pending.chances.at(k), 0, 0, cost.energy.value, {}};
for (std::size_t channel = 0; channel < shown.cost_matter_fg.size(); ++channel)
shown.cost_matter_fg.at(channel) = cost.matter.at(channel).value;
view.candidates.push_back(shown);
}
std::ranges::sort(view.candidates, {}, &network::CellularOfferedCandidate::operator_id);
return view;
}
const auto& offer = life.mutation.offer;
if (!offer)
return view;
@@ -842,17 +864,29 @@ CellularRegionHost::select_mutation(const persistence::Uuid& account, std::uint6
// The choice must name this account's living cell and its exact outstanding offer.
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 ||
input.event_index == 0 || input.offer_tick >= tick ||
life->presence != CellularPresence::active) // ADR-0128: only an active life chooses.
return {invalid, 0, {}};
const auto& offer = life->mutation.offer;
// Operator 0 declines the exact offer (ADR-0120 amendment); otherwise it must be offered.
if (!offer || (input.operator_id != 0 &&
!std::ranges::any_of(offer->candidates, [&](const auto& candidate) {
return candidate.operator_id == input.operator_id;
})))
return {invalid, 0, {}};
if (state_->rules_version() >= cellular_lives_revamp_rules) {
// Rules 13: the catalog offer; operator 0 declines, and become needs a choice.
const auto& offer = life->revamp.offer;
const auto end = offer.ids.begin() + offer.count;
if (offer.count == 0 || offer.event_index != input.event_index ||
offer.offer_tick != input.offer_tick || (input.become && input.operator_id == 0) ||
(input.operator_id != 0 &&
std::ranges::find(offer.ids.begin(), end, input.operator_id) == end))
return {invalid, 0, {}};
} else {
const auto& offer = life->mutation.offer;
// Operator 0 declines the exact offer (ADR-0120 amendment); otherwise it must be offered.
if (input.become || life->mutation.event_index != input.event_index ||
life->mutation.offer_tick != input.offer_tick || !offer ||
(input.operator_id != 0 &&
!std::ranges::any_of(offer->candidates, [&](const auto& candidate) {
return candidate.operator_id == input.operator_id;
})))
return {invalid, 0, {}};
}
const auto grant = authority_.grant();
if (!grant)
return {not_ready, 0, {}};
+42 -12
View File
@@ -107,7 +107,9 @@ bool valid_action_input(const CellularActionRecord& action, std::uint64_t begin_
return !all_zero(action.account) && action.action_id != 0 && tick > begin_tick &&
tick <= end_tick && move_x >= -movement_limit && move_x <= movement_limit &&
move_y >= -movement_limit && move_y <= movement_limit && input[12] <= 3 &&
input[13] <= 1 && all_zero(input.subspan(14, 2)) &&
// Flags: brake, become (with a bud) and emerge (no target); then exclusions.
input[13] <= 7 && ((input[13] & 2U) == 0 || input[12] == 3) &&
((input[13] & 4U) == 0 || input[12] == 0) && input[14] <= 0x0F && input[15] == 0 &&
(input[12] == 1 || all_zero(input.subspan(16, 8)));
}
bool action_less(const CellularActionRecord& a, const CellularActionRecord& b) {
@@ -400,7 +402,7 @@ encode_cellular_mutation_input(const CellularMutationActionRecord& action) {
put(bytes, action.input.life_id, 8);
put(bytes, action.input.event_index, 8);
put(bytes, action.input.offer_tick, 8);
put(bytes, action.input.operator_id, 2);
put(bytes, action.input.operator_id | (action.input.become ? 0x8000U : 0U), 2);
std::array<std::uint8_t, cellular_mutation_input_bytes> out{};
std::ranges::copy(bytes, out.begin());
return out;
@@ -418,10 +420,12 @@ decode_cellular_mutation_input(const std::array<std::uint8_t, 16>& account, std:
action.input.life_id = get(input, 8, 8);
action.input.event_index = get(input, 16, 8);
action.input.offer_tick = get(input, 24, 8);
action.input.operator_id = static_cast<std::uint16_t>(get(input, 32, 2));
const auto stored = static_cast<std::uint16_t>(get(input, 32, 2));
action.input.operator_id = static_cast<std::uint16_t>(stored & 0x7FFFU);
action.input.become = (stored & 0x8000U) != 0;
if (action_id == 0 || action.tick == 0 || action.input.life_id == 0 ||
action.input.event_index == 0 || action.input.offer_tick == 0 ||
action.input.offer_tick >= action.tick)
(action.input.become && action.input.operator_id == 0) || action.input.event_index == 0 ||
action.input.offer_tick == 0 || action.input.offer_tick >= action.tick)
return std::nullopt;
return action;
}
@@ -517,7 +521,8 @@ encode_cellular_mutation_action_block(std::span<const CellularMutationActionReco
put(out, action.input.life_id, 8);
put(out, action.input.event_index, 8);
put(out, action.input.offer_tick, 8);
put(out, action.input.operator_id, 2);
// Rules 13: become is bit 15 of the stored operator, as in the reservation input.
put(out, action.input.operator_id | (action.input.become ? 0x8000U : 0U), 2);
put(out, 0, 6);
}
return out;
@@ -541,8 +546,11 @@ decode_cellular_mutation_action_block(std::span<const std::uint8_t> bytes, std::
action.input.life_id = get(record, 32, 8);
action.input.event_index = get(record, 40, 8);
action.input.offer_tick = get(record, 48, 8);
action.input.operator_id = static_cast<std::uint16_t>(get(record, 56, 2));
if (!all_zero(record.subspan(58, 6)))
const auto stored = static_cast<std::uint16_t>(get(record, 56, 2));
action.input.operator_id = static_cast<std::uint16_t>(stored & 0x7FFFU);
action.input.become = (stored & 0x8000U) != 0;
if (!all_zero(record.subspan(58, 6)) ||
(action.input.become && action.input.operator_id == 0))
return std::unexpected{CellularActionBlockError::format};
out.push_back(action);
offset += cellular_mutation_action_record_bytes;
@@ -587,7 +595,10 @@ match_cellular_actions_to_trace(const CellularLives& predecessor, const Cellular
intent->intent.movement.y !=
std::bit_cast<std::int16_t>(static_cast<std::uint16_t>(get(input, 10, 2))) ||
intent->intent.intake_target != target || intent->intent.engulf != (input[12] == 2) ||
intent->intent.bud != (input[12] == 3) || intent->intent.brake != (input[13] == 1))
intent->intent.bud != (input[12] == 3) ||
intent->intent.brake != ((input[13] & 1U) != 0) ||
intent->intent.become != ((input[13] & 2U) != 0) ||
intent->intent.emerge != ((input[13] & 4U) != 0) || intent->intent.exclude != input[14])
return std::unexpected{CellularActionBlockError::mismatch};
}
return {};
@@ -623,7 +634,8 @@ match_cellular_mutation_actions_to_trace(const CellularLives& predecessor,
action.tick == tick && action.input.life_id == selection.life_id &&
action.input.event_index == selection.event_index &&
action.input.offer_tick == selection.offer_tick &&
action.input.operator_id == selection.operator_id;
action.input.operator_id == selection.operator_id &&
action.input.become == selection.become;
});
if (matches != 1)
return std::unexpected{CellularActionBlockError::mismatch};
@@ -645,7 +657,8 @@ match_cellular_mutation_actions_to_trace(const CellularLives& predecessor,
if (departed == mutations.end() || departed->life_id != action.input.life_id ||
departed->event_index != action.input.event_index ||
departed->offer_tick != action.input.offer_tick ||
departed->operator_id != action.input.operator_id)
departed->operator_id != action.input.operator_id ||
departed->become != action.input.become)
return std::unexpected{CellularActionBlockError::mismatch};
continue;
}
@@ -655,7 +668,24 @@ match_cellular_mutation_actions_to_trace(const CellularLives& predecessor,
return std::unexpected{CellularActionBlockError::mismatch};
const auto selection = std::ranges::lower_bound(mutations, life->identity.life, {},
&CellularMutationSelection::life_id);
if (!life->mutation.offer)
if (predecessor.rules_version() >= cellular_lives_revamp_rules) {
// Rules 13: the catalog offer the life held; operator 0 declines it.
const auto& offer = life->revamp.offer;
const auto end = offer.ids.begin() + offer.count;
if (selection == mutations.end() || selection->life_id != life->identity.life ||
selection->event_index != action.input.event_index ||
selection->offer_tick != action.input.offer_tick ||
selection->operator_id != action.input.operator_id ||
selection->become != action.input.become || offer.count == 0 ||
offer.event_index != action.input.event_index ||
offer.offer_tick != action.input.offer_tick ||
life->identity.life != action.input.life_id ||
(action.input.operator_id != 0 &&
std::ranges::find(offer.ids.begin(), end, action.input.operator_id) == end))
return std::unexpected{CellularActionBlockError::mismatch};
continue;
}
if (!life->mutation.offer || action.input.become)
return std::unexpected{CellularActionBlockError::mismatch};
const auto offer = life->mutation.offer.value_or(biology::MutationOffer{});
if (selection == mutations.end() || selection->life_id != life->identity.life ||
+9
View File
@@ -1023,6 +1023,11 @@ CellularLives::create(const PrimitiveLives& opening,
return std::unique_ptr<CellularLives>{new CellularLives{std::move(state)}};
}
std::expected<biology::Genome, CellularLivesError>
cellular_organism_genome(const biology::Genome& genome, std::uint32_t members) {
return organism_genome(genome, members);
}
bool cellular_can_emerge(const CellularOwnedLife& life, const CellularRevampRules& rules) noexcept {
std::uint64_t mass{};
for (std::size_t c = 0; c < life.body.matter.size(); ++c)
@@ -1084,6 +1089,9 @@ accounting::Inventory CellularLives::nursery() const noexcept {
accounting::Inventory CellularLives::heat() const noexcept {
return state_->heat;
}
accounting::Inventory CellularLives::spent() const noexcept {
return state_->spent;
}
accounting::Inventory CellularLives::opening_total() const noexcept {
return state_->opening_total;
}
@@ -3555,6 +3563,7 @@ 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};
feedback.back().exclude = input->intent.exclude;
}
// 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.
@@ -15,7 +15,16 @@ summarize_cellular_play(const CellularLives& state) {
summary.lingering += life.presence == CellularPresence::lingering ? 1 : 0;
summary.suspended += life.presence == CellularPresence::suspended ? 1 : 0;
genomes.push_back(life.identity.genome);
if (life.revamp.members > 1) {
++summary.organisms;
summary.largest_organism =
std::max<std::uint64_t>(summary.largest_organism, life.revamp.members);
}
summary.offers_pending += life.revamp.offer.count != 0 ? 1 : 0;
summary.offers_earned += life.revamp.events;
}
for (const auto& channel : state.spent().matter)
summary.spent_matter_fg += channel.value;
std::ranges::sort(genomes);
const auto distinct = std::ranges::unique(genomes);
genomes.erase(distinct.begin(), distinct.end());
+69 -12
View File
@@ -1,6 +1,7 @@
#include "evo/server/lifecycle/wildlife_controller.hpp"
#include "evo/biology/budding.hpp"
#include "evo/biology/cellular_burn.hpp"
#include "evo/biology/cellular_motion.hpp"
#include <algorithm>
@@ -98,6 +99,22 @@ wildlife_intents(const CellularLives& state, std::uint64_t tick) {
{0, -1'000},
{707, -707}}};
const auto temperature = state.ambient_temperature_millikelvin();
// Rules 13 (ADR-0179): organisms develop as their scaled genome, engulf masses carry
// reserve matter, membranes are in nanometres, and reserve matter stands in for energy.
const bool revamp = state.rules_version() >= cellular_lives_revamp_rules;
const auto& revamp_rules = state.revamp_rules();
const auto body_genome =
[&](const CellularOwnedLife& life) -> std::expected<biology::Genome, CellularLivesError> {
const auto genome = state.archive().find(life.identity.genome);
if (!genome)
return std::unexpected{CellularLivesError::archive};
return revamp ? cellular_organism_genome(*genome, life.revamp.members)
: std::expected<biology::Genome, CellularLivesError>{*genome};
};
// A life from before the upgrade keeps its membrane in micrometres.
const auto membrane_nm = [](const biology::Genome& genome, std::uint32_t membrane) {
return genome.cellular_traits() ? membrane : membrane * 1'000;
};
std::vector<CellularLifeIntent> intents;
// Rules 7 prey facts for every cell in the world, derived once per tick: a per-hunter
// derivation made the step quadratic in metabolic budgets.
@@ -107,6 +124,7 @@ wildlife_intents(const CellularLives& state, std::uint64_t tick) {
std::uint32_t extent{}, membrane{};
std::uint64_t mass{};
};
std::vector<std::uint64_t> engulf_masses; // Rules 13, beside prey_facts.
std::vector<Prey> prey_facts;
const bool hunting = state.rules_version() >= cellular_lives_reproduction_rules &&
std::ranges::any_of(state.lives(), [](const CellularOwnedLife& life) {
@@ -117,35 +135,56 @@ wildlife_intents(const CellularLives& state, std::uint64_t tick) {
for (const auto& other : state.lives()) {
if (other.presence == CellularPresence::suspended)
continue;
const auto other_genome = state.archive().find(other.identity.genome);
const auto other_genome = body_genome(other);
if (!other_genome)
return std::unexpected{CellularLivesError::archive};
return std::unexpected{other_genome.error()};
const auto other_budget = biology::metabolic_budget(
*other_genome, other.growth, {temperature, {}}, 1000, state.revamp_rules().body);
const auto membrane = other_genome->parameter(biology::Capability::membrane);
if (!other_budget || !membrane)
return std::unexpected{CellularLivesError::biology};
prey_facts.push_back({other.identity.life, other.position,
other_budget->phenotype.extent, *membrane,
other_budget->phenotype.extent,
revamp ? membrane_nm(*other_genome, *membrane) : *membrane,
other_budget->phenotype.total_mass});
engulf_masses.push_back(biology::cellular_engulf_mass(
other_budget->phenotype.total_mass, other.reserve, revamp_rules.step));
}
}
for (const auto& life : state.lives()) {
if (life.account != LifeAccount{})
continue;
const auto genome = state.archive().find(life.identity.genome);
if (!genome)
const auto expressed = state.archive().find(life.identity.genome);
const auto genome = body_genome(life);
if (!expressed || !genome)
return std::unexpected{CellularLivesError::archive};
const auto budget = biology::metabolic_budget(*genome, life.growth, {temperature, {}}, 1000,
state.revamp_rules().body);
if (!budget)
return std::unexpected{CellularLivesError::biology};
const auto& traits = expressed->cellular_traits();
biology::CellularStepIntent intent{};
const auto offspring = state.archive().find(cellular_offspring_genome(life));
if (!offspring)
return std::unexpected{CellularLivesError::archive};
if ((tick + life.identity.life) % wildlife_bud_interval_ticks == 0 &&
biology::fund_offspring(*genome, *offspring, life.growth, life.reserve, temperature)) {
// Rules 13: a single cell buds when it can pay the bud and its fee; an organism adds a
// member when it can, and stops one short of the emergence threshold (the owner refuses
// what it cannot fund).
const auto fee =
revamp
? cellular_mutation_cost({.cost = revamp_rules.bud_fee,
.cost_scale_percent = revamp_rules.bud_fee_scale_percent})
: accounting::Inventory{};
bool fee_paid = life.reserve.energy.value >= fee.energy.value;
for (std::size_t c = 0; c < fee.matter.size(); ++c)
fee_paid = fee_paid && life.reserve.matter[c].value >= fee.matter[c].value;
const bool organism = revamp && life.revamp.members > 1;
const bool may_bud =
organism ? fee_paid && life.revamp.members + 1U < revamp_rules.emerge_members
: fee_paid && biology::fund_offspring(*expressed, *offspring, life.growth,
life.reserve, temperature)
.has_value();
if ((tick + life.identity.life) % wildlife_bud_interval_ticks == 0 && may_bud) {
intent.bud = true;
intents.push_back({life.identity.life, intent});
continue;
@@ -156,12 +195,18 @@ wildlife_intents(const CellularLives& state, std::uint64_t tick) {
// players' cells included; otherwise feed.
if (hunting) {
bool prey = false;
for (const auto& other : prey_facts) {
const auto hunter_mass = biology::cellular_engulf_mass(budget->phenotype.total_mass,
life.reserve, revamp_rules.step);
const auto capacity = traits ? traits->engulf_capacity : std::uint16_t{100};
for (std::size_t k = 0; k < prey_facts.size(); ++k) {
const auto& other = prey_facts[k];
if (other.life == life.identity.life ||
!biology::cellular_footprints_contact(life.position, extent, other.position,
other.extent))
continue;
if (can_engulf(budget->phenotype.total_mass, other.mass, other.membrane)) {
if (revamp ? biology::cellular_engulfable(hunter_mass, capacity, engulf_masses[k],
other.membrane, revamp_rules.step)
: can_engulf(budget->phenotype.total_mass, other.mass, other.membrane)) {
prey = true;
break;
}
@@ -190,9 +235,21 @@ wildlife_intents(const CellularLives& state, std::uint64_t tick) {
// Rules 11: a cell short of energy forages for energy (wildlife_food).
const auto demand_period = budget->rate_microseconds / 50'000;
const auto demand_per_tick = demand_period == 0 ? 0 : budget->demand.value / demand_period;
const bool hungry =
state.rules_version() >= cellular_lives_photosynthesis_rules && demand_per_tick != 0 &&
life.reserve.energy.value / demand_per_tick < wildlife_energy_hunger_ticks;
// Rules 13: burnable reserve matter stands in for energy, so a cell rich in matter is
// not hungry.
const auto payable =
revamp
? biology::cellular_payable_energy(
life.reserve,
biology::cellular_burn(
revamp_rules.burn_pj_per_kfg,
traits ? traits->burn : std::array<std::uint16_t, 4>{100, 100, 100, 100},
0),
false)
: life.reserve.energy.value;
const bool hungry = state.rules_version() >= cellular_lives_photosynthesis_rules &&
demand_per_tick != 0 &&
payable / demand_per_tick < wildlife_energy_hunger_ticks;
const auto chosen = wildlife_food(*perceived, centre_x, centre_y, deposits, hungry);
const auto nearest = chosen ? std::optional{chosen->address} : std::nullopt;
if (nearest && deposits && chosen) {
+26 -8
View File
@@ -78,6 +78,10 @@ prepare_cellular_session_tick(const CellularLives& state, std::uint64_t tick,
owned_intent->intent.bud = input->bud;
// Rules 12: below them the owner refuses a brake, so only v13 sessions send one.
owned_intent->intent.brake = input->brake;
// Rules 13 (v14): below them the owner refuses each of these.
owned_intent->intent.become = input->become;
owned_intent->intent.emerge = input->emerge;
owned_intent->intent.exclude = input->exclude;
CellularActionRecord record;
record.account = action.account.bytes;
record.action_id = action.action_id;
@@ -101,7 +105,10 @@ prepare_cellular_session_tick(const CellularLives& state, std::uint64_t tick,
const auto life_id = little_endian(bytes, 8, 8);
const auto event_index = little_endian(bytes, 16, 8);
const auto offer_tick = little_endian(bytes, 24, 8);
const auto operator_id = static_cast<std::uint16_t>(little_endian(bytes, 32, 2));
const auto stored_operator = static_cast<std::uint16_t>(little_endian(bytes, 32, 2));
const auto operator_id = static_cast<std::uint16_t>(stored_operator & 0x7FFFU);
const bool become = (stored_operator & 0x8000U) != 0;
const bool revamp = state.rules_version() >= cellular_lives_revamp_rules;
// ADR-0121 amendment (schema 59): the reservation's tick is the earliest the choice
// may bind at; the queue binds it at the first tick simulated after admission.
if (reservation.account == persistence::Uuid{} || reservation.action_id == 0 ||
@@ -114,7 +121,17 @@ prepare_cellular_session_tick(const CellularLives& state, std::uint64_t tick,
const auto life =
std::ranges::find(state.lives(), life_id,
[](const CellularOwnedLife& value) { return value.identity.life; });
if (life != state.lives().end()) {
if ((become && (!revamp || operator_id == 0)))
return std::unexpected{CellularSessionBatchError::mutation};
if (life != state.lives().end() && revamp) {
// Rules 13: the catalog offer the life holds; operator 0 declines it.
const auto& offer = life->revamp.offer;
const auto end = offer.ids.begin() + offer.count;
if (life->account != reservation.account || offer.count == 0 ||
offer.event_index != event_index || offer.offer_tick != offer_tick ||
(operator_id != 0 && std::ranges::find(offer.ids.begin(), end, operator_id) == end))
return std::unexpected{CellularSessionBatchError::mutation};
} else if (life != state.lives().end()) {
if (life->account != reservation.account || life->mutation.event_index != event_index ||
life->mutation.offer_tick != offer_tick)
return std::unexpected{CellularSessionBatchError::mutation};
@@ -136,12 +153,13 @@ prepare_cellular_session_tick(const CellularLives& state, std::uint64_t tick,
previous.action_id == reservation.action_id;
}))
return std::unexpected{CellularSessionBatchError::duplicate_mutation};
prepared.intents.mutations.push_back(
{life_id, event_index, offer_tick, reservation.successor_identity, operator_id});
prepared.mutation_actions.push_back({reservation.account,
reservation.action_id,
tick,
{life_id, event_index, offer_tick, operator_id}});
prepared.intents.mutations.push_back({life_id, event_index, offer_tick,
reservation.successor_identity, operator_id, become});
prepared.mutation_actions.push_back(
{reservation.account,
reservation.action_id,
tick,
{life_id, event_index, offer_tick, operator_id, become}});
}
std::ranges::sort(prepared.intents.mutations, {}, &CellularMutationSelection::life_id);
std::ranges::sort(prepared.mutation_actions, [](const auto& left, const auto& right) {
+24 -6
View File
@@ -152,12 +152,21 @@ struct RegionSessions::Impl {
}
// Reliable owner-only v10 messages, to the account's established v10 session.
bool send_to_v10_owner(const network::AccountId& account,
const network::SessionMessage& message) {
const network::SessionMessage& message, bool named_offer = false) {
for (const auto& c : connections) {
if (c.state != State::established ||
!network::cellular_v10_family(c.protocol_version) || c.account_id != account)
continue;
// Rules 13 offers name catalog mutations (v14); operator offers are v10-v13's.
if (std::holds_alternative<network::CellularMutationOffer>(message) &&
named_offer != (c.protocol_version == network::session_protocol_v14))
return false;
auto delivered = message;
// An emerged ending reads as unrecorded before v14.
if (auto* ended = std::get_if<network::CellularLifeEnded>(&delivered);
ended && ended->cause == network::CellularDeathReason::emerged &&
c.protocol_version != network::session_protocol_v14)
ended->cause = network::CellularDeathReason::unrecorded;
// ADR-0176: before v12 an offer cannot name photosynthesis; its other choices
// stay valid, so those candidates alone are withheld.
if (auto* offer = std::get_if<network::CellularMutationOffer>(&delivered))
@@ -628,7 +637,7 @@ struct RegionSessions::Impl {
wire_version == network::session_protocol_v9 ||
network::cellular_v10_family(wire_version)) &&
!cellular_actions) ||
wire_version > network::session_protocol_v13)
wire_version > network::session_protocol_v14)
return close(c, SessionCloseReason::unsupported_version, now);
} else if (wire_version != c.protocol_version) {
return close(c, SessionCloseReason::unsupported_version, now);
@@ -960,8 +969,17 @@ bool RegionSessions::send_cellular_perception(const network::AccountId& account,
if (delivered.diagnostics)
delivered.diagnostics->light_pj = 0;
}
// Only v13 carries liquid motion (rules 12).
if (c.protocol_version != network::session_protocol_v13) {
// Only v14 carries the rules-13 body block.
if (c.protocol_version != network::session_protocol_v14 && delivered.diagnostics) {
auto& d = *delivered.diagnostics;
d.members = d.emerge_members = 0;
d.exclude = 0;
d.can_emerge = false;
d.payable_energy_pj = d.total_mass_fg = 0;
d.traits = {};
}
// Only v13 and v14 carry liquid motion (rules 12).
if (!network::cellular_liquid_version(c.protocol_version)) {
delivered.currents.reset();
if (delivered.diagnostics) {
delivered.diagnostics->velocity_x_q10 = 0;
@@ -995,8 +1013,8 @@ bool RegionSessions::send_life_ended(const network::AccountId& account,
}
bool RegionSessions::send_mutation_offer(const network::AccountId& account,
const network::CellularMutationOffer& offer) {
return impl_->send_to_v10_owner(account, offer);
const network::CellularMutationOffer& offer, bool named) {
return impl_->send_to_v10_owner(account, offer, named);
}
void RegionSessions::clear_cellular_perceptions(std::uint64_t tick) {