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>
627 lines
32 KiB
C++
627 lines
32 KiB
C++
#include "evo/server/lifecycle/cellular_batch.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <bit>
|
|
#include <optional>
|
|
#include <ranges>
|
|
#include <set>
|
|
#include <tuple>
|
|
#include <utility>
|
|
|
|
namespace evo::server {
|
|
namespace {
|
|
constexpr std::array<std::uint8_t, 8> magic{'E', 'V', 'L', 'C', 'B', 'T', '0', '1'};
|
|
constexpr std::int16_t movement_limit = 1'000;
|
|
|
|
void put(std::vector<std::uint8_t>& out, std::uint64_t value, std::size_t width) {
|
|
for (std::size_t i = 0; i < width; ++i)
|
|
out.push_back(static_cast<std::uint8_t>(value >> (i * 8U)));
|
|
}
|
|
std::uint64_t get(std::span<const std::uint8_t> bytes, std::size_t offset, std::size_t width) {
|
|
std::uint64_t value{};
|
|
for (std::size_t i = 0; i < width; ++i)
|
|
value |= static_cast<std::uint64_t>(bytes[offset + i]) << (i * 8U);
|
|
return value;
|
|
}
|
|
bool all_zero(std::span<const std::uint8_t> bytes) {
|
|
return std::ranges::all_of(bytes, [](std::uint8_t byte) { return byte == 0; });
|
|
}
|
|
bool valid_header(const CellularBatchHeader& h, std::size_t ticks) {
|
|
return h.world != 0 && h.region != 0 && h.frame != 0 && !all_zero(h.predecessor) &&
|
|
!all_zero(h.predecessor_digest) && h.end_tick > h.begin_tick &&
|
|
h.end_tick - h.begin_tick == ticks && ticks > 0 && ticks <= max_cellular_batch_ticks;
|
|
}
|
|
bool valid_intent(const CellularLifeIntent& life) {
|
|
const auto& step = life.intent;
|
|
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.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;
|
|
std::set<LifeAccount> accounts;
|
|
for (const auto& tick : ticks) {
|
|
if (tick.lives.size() > biology::max_cellular_tick_lives ||
|
|
tick.births.size() > biology::max_cellular_tick_lives ||
|
|
tick.mutations.size() > biology::max_cellular_tick_lives)
|
|
return false;
|
|
for (std::size_t i = 0; i < tick.lives.size(); ++i) {
|
|
const auto& life = tick.lives[i];
|
|
if (!valid_intent(life) || (i > 0 && tick.lives[i - 1].life_id >= life.life_id))
|
|
return false;
|
|
}
|
|
for (std::size_t i = 0; i < tick.births.size(); ++i) {
|
|
const auto& birth = tick.births[i];
|
|
if (all_zero(birth.account) || birth.request == 0 || birth.generation == 0 ||
|
|
birth.identity == 0 ||
|
|
(i > 0 && std::tie(tick.births[i - 1].account, tick.births[i - 1].request) >=
|
|
std::tie(birth.account, birth.request)))
|
|
return false;
|
|
if (!identities.insert(birth.identity).second || !accounts.insert(birth.account).second)
|
|
return false;
|
|
}
|
|
for (std::size_t i = 0; i < tick.mutations.size(); ++i) {
|
|
const auto& mutation = tick.mutations[i];
|
|
if (mutation.life_id == 0 || mutation.event_index == 0 || mutation.offer_tick == 0 ||
|
|
mutation.successor_id == 0 || mutation.operator_id == 0 ||
|
|
(i > 0 && tick.mutations[i - 1].life_id >= mutation.life_id) ||
|
|
!identities.insert(mutation.successor_id).second)
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
std::uint64_t action_tick(const CellularActionRecord& action) {
|
|
return get(action.input, 0, 8);
|
|
}
|
|
bool valid_action_input(const CellularActionRecord& action, std::uint64_t begin_tick,
|
|
std::uint64_t end_tick) {
|
|
const auto input = std::span<const std::uint8_t>{action.input};
|
|
const auto tick = action_tick(action);
|
|
const auto move_x = std::bit_cast<std::int16_t>(static_cast<std::uint16_t>(get(input, 8, 2)));
|
|
const auto move_y = std::bit_cast<std::int16_t>(static_cast<std::uint16_t>(get(input, 10, 2)));
|
|
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 &&
|
|
all_zero(input.subspan(13, 3)) && (input[12] == 1 || all_zero(input.subspan(16, 8)));
|
|
}
|
|
bool action_less(const CellularActionRecord& a, const CellularActionRecord& b) {
|
|
const auto a_tick = action_tick(a);
|
|
const auto b_tick = action_tick(b);
|
|
return a_tick < b_tick ||
|
|
(a_tick == b_tick &&
|
|
(a.account < b.account || (a.account == b.account && a.action_id < b.action_id)));
|
|
}
|
|
bool valid_actions(std::span<const CellularActionRecord> actions, std::uint64_t begin_tick,
|
|
std::uint64_t end_tick) {
|
|
if (end_tick <= begin_tick || actions.size() > max_cellular_action_records)
|
|
return false;
|
|
for (std::size_t i = 0; i < actions.size(); ++i) {
|
|
if (!valid_action_input(actions[i], begin_tick, end_tick) ||
|
|
(i > 0 && !action_less(actions[i - 1], actions[i])))
|
|
return false;
|
|
for (std::size_t j = 0; j < i; ++j)
|
|
if (actions[j].account == actions[i].account &&
|
|
actions[j].action_id == actions[i].action_id)
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
bool mutation_action_less(const CellularMutationActionRecord& left,
|
|
const CellularMutationActionRecord& right) {
|
|
return left.tick < right.tick ||
|
|
(left.tick == right.tick &&
|
|
(left.account < right.account ||
|
|
(left.account == right.account && left.action_id < right.action_id)));
|
|
}
|
|
bool valid_mutation_actions(std::span<const CellularMutationActionRecord> actions,
|
|
std::uint64_t begin_tick, std::uint64_t end_tick) {
|
|
if (end_tick <= begin_tick || actions.size() > max_cellular_action_records)
|
|
return false;
|
|
for (std::size_t i = 0; i < actions.size(); ++i) {
|
|
const auto& action = actions[i];
|
|
const auto& input = action.input;
|
|
if (all_zero(action.account) || action.action_id == 0 || action.tick <= begin_tick ||
|
|
action.tick > end_tick || input.life_id == 0 || input.event_index == 0 ||
|
|
input.offer_tick == 0 || input.offer_tick > begin_tick || input.operator_id == 0 ||
|
|
(i != 0 && !mutation_action_less(actions[i - 1], action)))
|
|
return false;
|
|
for (std::size_t j = 0; j < i; ++j)
|
|
if (actions[j].account == action.account && actions[j].action_id == action.action_id)
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
void put_intent(std::vector<std::uint8_t>& out, const CellularLifeIntent& life) {
|
|
put(out, life.life_id, 8);
|
|
put(out, static_cast<std::uint16_t>(life.intent.movement.x), 2);
|
|
put(out, static_cast<std::uint16_t>(life.intent.movement.y), 2);
|
|
out.push_back(life.intent.intake_target ? 1 // Target kind.
|
|
: life.intent.engulf ? 2
|
|
: life.intent.bud ? 3
|
|
: 0);
|
|
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);
|
|
}
|
|
} // namespace
|
|
|
|
std::expected<std::vector<std::uint8_t>, CellularBatchError>
|
|
encode_cellular_batch(const CellularBatch& batch) {
|
|
if ((batch.format_version != cellular_batch_format &&
|
|
batch.format_version != cellular_batch_v2_format &&
|
|
batch.format_version != cellular_batch_legacy_format) ||
|
|
!valid_header(batch.header, batch.ticks.size()) || !valid_ticks(batch.ticks))
|
|
return std::unexpected{CellularBatchError::format};
|
|
if (batch.format_version == cellular_batch_legacy_format &&
|
|
std::ranges::any_of(batch.ticks, [](const auto& tick) {
|
|
return !tick.births.empty() || !tick.mutations.empty();
|
|
}))
|
|
return std::unexpected{CellularBatchError::format};
|
|
if (batch.format_version == cellular_batch_v2_format &&
|
|
std::ranges::any_of(batch.ticks, [](const auto& tick) { return !tick.mutations.empty(); }))
|
|
return std::unexpected{CellularBatchError::format};
|
|
const auto tick_header_bytes =
|
|
batch.format_version == cellular_batch_format ? cellular_batch_tick_header_bytes
|
|
: batch.format_version == cellular_batch_v2_format ? cellular_batch_v2_tick_header_bytes
|
|
: cellular_batch_v1_tick_header_bytes;
|
|
std::size_t size = cellular_batch_header_bytes;
|
|
for (const auto& tick : batch.ticks) {
|
|
const auto records = tick.lives.size() * cellular_batch_intent_bytes +
|
|
(batch.format_version >= cellular_batch_v2_format
|
|
? tick.births.size() * cellular_batch_birth_bytes
|
|
: 0) +
|
|
(batch.format_version == cellular_batch_format
|
|
? tick.mutations.size() * cellular_batch_mutation_bytes
|
|
: 0);
|
|
if (records > max_cellular_batch_bytes - size - tick_header_bytes)
|
|
return std::unexpected{CellularBatchError::format};
|
|
size += tick_header_bytes + records;
|
|
}
|
|
std::vector<std::uint8_t> out;
|
|
out.reserve(size);
|
|
out.insert(out.end(), magic.begin(), magic.end());
|
|
put(out, batch.format_version, 4);
|
|
put(out, 4, 4); // Profile 4.
|
|
put(out, batch.header.world, 8);
|
|
put(out, batch.header.region, 8);
|
|
put(out, batch.header.frame, 8);
|
|
out.insert(out.end(), batch.header.predecessor.begin(), batch.header.predecessor.end());
|
|
out.insert(out.end(), batch.header.predecessor_digest.begin(),
|
|
batch.header.predecessor_digest.end());
|
|
put(out, batch.header.begin_tick, 8);
|
|
put(out, batch.header.end_tick, 8);
|
|
put(out, batch.ticks.size(), 4);
|
|
put(out, 0, 4);
|
|
for (const auto& tick : batch.ticks) {
|
|
put(out, tick.lives.size(), 4);
|
|
if (batch.format_version >= cellular_batch_v2_format)
|
|
put(out, tick.births.size(), 4);
|
|
if (batch.format_version == cellular_batch_format) {
|
|
put(out, tick.mutations.size(), 4);
|
|
put(out, 0, 4);
|
|
}
|
|
for (const auto& life : tick.lives)
|
|
put_intent(out, life);
|
|
for (const auto& birth : tick.births) {
|
|
if (batch.format_version < cellular_batch_v2_format)
|
|
break;
|
|
out.insert(out.end(), birth.account.begin(), birth.account.end());
|
|
put(out, birth.request, 8);
|
|
put(out, birth.generation, 8);
|
|
put(out, birth.identity, 8);
|
|
}
|
|
for (const auto& mutation : tick.mutations) {
|
|
if (batch.format_version != cellular_batch_format)
|
|
break;
|
|
put(out, mutation.life_id, 8);
|
|
put(out, mutation.event_index, 8);
|
|
put(out, mutation.offer_tick, 8);
|
|
put(out, mutation.successor_id, 8);
|
|
put(out, mutation.operator_id, 2);
|
|
put(out, 0, 6);
|
|
}
|
|
}
|
|
if (out.size() != size)
|
|
return std::unexpected{CellularBatchError::format};
|
|
return out;
|
|
}
|
|
|
|
std::expected<CellularBatch, CellularBatchError>
|
|
decode_cellular_batch(std::span<const std::uint8_t> bytes) {
|
|
if (bytes.size() < cellular_batch_header_bytes || bytes.size() > max_cellular_batch_bytes ||
|
|
!std::ranges::equal(bytes.first(magic.size()), magic))
|
|
return std::unexpected{CellularBatchError::format};
|
|
const auto format = get(bytes, 8, 4);
|
|
if ((format != cellular_batch_format && format != cellular_batch_v2_format &&
|
|
format != cellular_batch_legacy_format) ||
|
|
get(bytes, 12, 4) != 4)
|
|
return std::unexpected{CellularBatchError::version};
|
|
if (get(bytes, 108, 4) != 0)
|
|
return std::unexpected{CellularBatchError::format};
|
|
|
|
CellularBatch out;
|
|
out.format_version = static_cast<std::uint32_t>(format);
|
|
auto& h = out.header;
|
|
h.world = get(bytes, 16, 8);
|
|
h.region = get(bytes, 24, 8);
|
|
h.frame = get(bytes, 32, 8);
|
|
std::ranges::copy(bytes.subspan(40, 16), h.predecessor.begin());
|
|
std::ranges::copy(bytes.subspan(56, 32), h.predecessor_digest.begin());
|
|
h.begin_tick = get(bytes, 88, 8);
|
|
h.end_tick = get(bytes, 96, 8);
|
|
const auto tick_count = get(bytes, 104, 4);
|
|
if (tick_count > max_cellular_batch_ticks ||
|
|
!valid_header(h, static_cast<std::size_t>(tick_count)))
|
|
return std::unexpected{CellularBatchError::format};
|
|
|
|
// Bound all per-tick allocation by checking each encoded count against the
|
|
// remaining bytes before resizing the tick or intent vectors.
|
|
std::size_t offset = cellular_batch_header_bytes;
|
|
out.ticks.reserve(static_cast<std::size_t>(tick_count));
|
|
for (std::uint64_t tick_index = 0; tick_index < tick_count; ++tick_index) {
|
|
const auto tick_header_bytes =
|
|
format == cellular_batch_format ? cellular_batch_tick_header_bytes
|
|
: format == cellular_batch_v2_format ? cellular_batch_v2_tick_header_bytes
|
|
: cellular_batch_v1_tick_header_bytes;
|
|
if (bytes.size() - offset < tick_header_bytes)
|
|
return std::unexpected{CellularBatchError::format};
|
|
const auto count = get(bytes, offset, 4);
|
|
offset += 4;
|
|
std::uint64_t birth_count{};
|
|
if (format >= cellular_batch_v2_format) {
|
|
birth_count = get(bytes, offset, 4);
|
|
offset += 4;
|
|
}
|
|
std::uint64_t mutation_count{};
|
|
if (format == cellular_batch_format) {
|
|
mutation_count = get(bytes, offset, 4);
|
|
if (get(bytes, offset + 4, 4) != 0)
|
|
return std::unexpected{CellularBatchError::format};
|
|
offset += 8;
|
|
}
|
|
if (count > biology::max_cellular_tick_lives ||
|
|
count > (bytes.size() - offset) / cellular_batch_intent_bytes ||
|
|
birth_count > biology::max_cellular_tick_lives ||
|
|
mutation_count > biology::max_cellular_tick_lives)
|
|
return std::unexpected{CellularBatchError::format};
|
|
CellularTickIntents step;
|
|
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)))
|
|
return std::unexpected{CellularBatchError::format};
|
|
CellularLifeIntent life;
|
|
life.life_id = get(record, 0, 8);
|
|
life.intent.movement.x =
|
|
std::bit_cast<std::int16_t>(static_cast<std::uint16_t>(get(record, 8, 2)));
|
|
life.intent.movement.y =
|
|
std::bit_cast<std::int16_t>(static_cast<std::uint16_t>(get(record, 10, 2)));
|
|
if (record[12] == 1) {
|
|
life.intent.intake_target =
|
|
biology::CellularPatchAddress{static_cast<std::uint32_t>(get(record, 16, 4)),
|
|
static_cast<std::uint32_t>(get(record, 20, 4))};
|
|
} 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.
|
|
if (!valid_intent(life) ||
|
|
(!step.lives.empty() && step.lives.back().life_id >= life.life_id))
|
|
return std::unexpected{CellularBatchError::format};
|
|
step.lives.push_back(life);
|
|
offset += cellular_batch_intent_bytes;
|
|
}
|
|
if (birth_count > (bytes.size() - offset) / cellular_batch_birth_bytes)
|
|
return std::unexpected{CellularBatchError::format};
|
|
step.births.reserve(static_cast<std::size_t>(birth_count));
|
|
for (std::uint64_t i = 0; i < birth_count; ++i) {
|
|
const auto record = bytes.subspan(offset, cellular_batch_birth_bytes);
|
|
CellularTickIntents::Birth birth;
|
|
std::ranges::copy(record.first(16), birth.account.begin());
|
|
birth.request = get(record, 16, 8);
|
|
birth.generation = get(record, 24, 8);
|
|
birth.identity = get(record, 32, 8);
|
|
if (all_zero(birth.account) || birth.request == 0 || birth.generation == 0 ||
|
|
birth.identity == 0 ||
|
|
(!step.births.empty() &&
|
|
std::tie(step.births.back().account, step.births.back().request) >=
|
|
std::tie(birth.account, birth.request)))
|
|
return std::unexpected{CellularBatchError::format};
|
|
step.births.push_back(birth);
|
|
offset += cellular_batch_birth_bytes;
|
|
}
|
|
if (mutation_count > (bytes.size() - offset) / cellular_batch_mutation_bytes)
|
|
return std::unexpected{CellularBatchError::format};
|
|
step.mutations.reserve(static_cast<std::size_t>(mutation_count));
|
|
for (std::uint64_t i = 0; i < mutation_count; ++i) {
|
|
const auto record = bytes.subspan(offset, cellular_batch_mutation_bytes);
|
|
CellularMutationSelection mutation;
|
|
mutation.life_id = get(record, 0, 8);
|
|
mutation.event_index = get(record, 8, 8);
|
|
mutation.offer_tick = get(record, 16, 8);
|
|
mutation.successor_id = get(record, 24, 8);
|
|
mutation.operator_id = static_cast<std::uint16_t>(get(record, 32, 2));
|
|
if (!all_zero(record.subspan(34, 6)) || mutation.life_id == 0 ||
|
|
mutation.event_index == 0 || mutation.offer_tick == 0 ||
|
|
mutation.successor_id == 0 || mutation.operator_id == 0 ||
|
|
(!step.mutations.empty() && step.mutations.back().life_id >= mutation.life_id))
|
|
return std::unexpected{CellularBatchError::format};
|
|
step.mutations.push_back(mutation);
|
|
offset += cellular_batch_mutation_bytes;
|
|
}
|
|
out.ticks.push_back(std::move(step));
|
|
}
|
|
if (offset != bytes.size())
|
|
return std::unexpected{CellularBatchError::format};
|
|
return out;
|
|
}
|
|
|
|
std::expected<std::unique_ptr<CellularLives>, CellularBatchError>
|
|
replay_cellular_batch(const CellularLives& base, const CellularBatch& batch) {
|
|
std::vector<CellularMutationOutcome> ignored;
|
|
return replay_cellular_batch(base, batch, ignored);
|
|
}
|
|
|
|
std::array<std::uint8_t, cellular_mutation_input_bytes>
|
|
encode_cellular_mutation_input(const CellularMutationActionRecord& action) {
|
|
std::vector<std::uint8_t> bytes;
|
|
bytes.reserve(cellular_mutation_input_bytes);
|
|
put(bytes, action.tick, 8);
|
|
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);
|
|
std::array<std::uint8_t, cellular_mutation_input_bytes> out{};
|
|
std::ranges::copy(bytes, out.begin());
|
|
return out;
|
|
}
|
|
|
|
std::optional<CellularMutationActionRecord>
|
|
decode_cellular_mutation_input(const std::array<std::uint8_t, 16>& account, std::uint64_t action_id,
|
|
std::span<const std::uint8_t> input) {
|
|
if (input.size() != cellular_mutation_input_bytes)
|
|
return std::nullopt;
|
|
CellularMutationActionRecord action;
|
|
action.account = account;
|
|
action.action_id = action_id;
|
|
action.tick = get(input, 0, 8);
|
|
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));
|
|
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.operator_id == 0)
|
|
return std::nullopt;
|
|
return action;
|
|
}
|
|
|
|
std::expected<std::unique_ptr<CellularLives>, CellularBatchError>
|
|
replay_cellular_batch(const CellularLives& base, const CellularBatch& batch,
|
|
std::vector<CellularMutationOutcome>& mutation_outcomes) {
|
|
mutation_outcomes.clear();
|
|
const auto& h = batch.header;
|
|
if (!valid_header(h, batch.ticks.size()) || !valid_ticks(batch.ticks))
|
|
return std::unexpected{CellularBatchError::format};
|
|
if (base.world() != h.world || base.region() != h.region || base.frame() != h.frame ||
|
|
base.tick() != h.begin_tick)
|
|
return std::unexpected{CellularBatchError::scope};
|
|
auto replay = CellularLives::restore(base.snapshot(), base.world(), base.region(), base.frame(),
|
|
base.tick());
|
|
if (!replay)
|
|
return std::unexpected{CellularBatchError::state};
|
|
for (const auto& tick : batch.ticks) {
|
|
const auto executed = (*replay)->advance_tick_with_mutations(tick.lives, tick.mutations);
|
|
if (!executed)
|
|
return std::unexpected{CellularBatchError::state};
|
|
for (const auto& mutation : executed->mutations)
|
|
mutation_outcomes.push_back(
|
|
{(*replay)->tick(), mutation.life_id, mutation.successor_id, mutation.result});
|
|
for (const auto& birth : tick.births) {
|
|
const LifeRequest request{birth.account, birth.request};
|
|
const LifeAllocation allocation{birth.identity,
|
|
birth.identity,
|
|
birth.identity,
|
|
birth.identity,
|
|
{(*replay)->tick()}};
|
|
auto born = (*replay)->birth_candidate(request, allocation);
|
|
if (!born)
|
|
return std::unexpected{CellularBatchError::state};
|
|
*replay = std::move(*born);
|
|
}
|
|
}
|
|
return std::move(*replay);
|
|
}
|
|
|
|
std::expected<std::vector<std::uint8_t>, CellularActionBlockError>
|
|
encode_cellular_action_block(std::span<const CellularActionRecord> actions,
|
|
std::uint64_t begin_tick, std::uint64_t end_tick) {
|
|
if (!valid_actions(actions, begin_tick, end_tick))
|
|
return std::unexpected{CellularActionBlockError::format};
|
|
std::vector<std::uint8_t> out;
|
|
out.reserve(actions.size() * cellular_action_record_bytes);
|
|
for (const auto& action : actions) {
|
|
out.insert(out.end(), action.account.begin(), action.account.end());
|
|
put(out, action.action_id, 8);
|
|
out.insert(out.end(), action.input.begin(), action.input.end());
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::expected<std::vector<CellularActionRecord>, CellularActionBlockError>
|
|
decode_cellular_action_block(std::span<const std::uint8_t> bytes, std::uint64_t begin_tick,
|
|
std::uint64_t end_tick) {
|
|
if (end_tick <= begin_tick || bytes.size() % cellular_action_record_bytes != 0 ||
|
|
bytes.size() / cellular_action_record_bytes > max_cellular_action_records)
|
|
return std::unexpected{CellularActionBlockError::format};
|
|
std::vector<CellularActionRecord> out(bytes.size() / cellular_action_record_bytes);
|
|
std::size_t offset{};
|
|
for (auto& action : out) {
|
|
const auto record = bytes.subspan(offset, cellular_action_record_bytes);
|
|
std::ranges::copy(record.first(16), action.account.begin());
|
|
action.action_id = get(record, 16, 8);
|
|
std::ranges::copy(record.subspan(24, 24), action.input.begin());
|
|
offset += cellular_action_record_bytes;
|
|
}
|
|
if (!valid_actions(out, begin_tick, end_tick))
|
|
return std::unexpected{CellularActionBlockError::format};
|
|
return out;
|
|
}
|
|
|
|
std::expected<std::vector<std::uint8_t>, CellularActionBlockError>
|
|
encode_cellular_mutation_action_block(std::span<const CellularMutationActionRecord> actions,
|
|
std::uint64_t begin_tick, std::uint64_t end_tick) {
|
|
if (!valid_mutation_actions(actions, begin_tick, end_tick))
|
|
return std::unexpected{CellularActionBlockError::format};
|
|
std::vector<std::uint8_t> out;
|
|
out.reserve(actions.size() * cellular_mutation_action_record_bytes);
|
|
for (const auto& action : actions) {
|
|
out.insert(out.end(), action.account.begin(), action.account.end());
|
|
put(out, action.action_id, 8);
|
|
put(out, action.tick, 8);
|
|
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);
|
|
put(out, 0, 6);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::expected<std::vector<CellularMutationActionRecord>, CellularActionBlockError>
|
|
decode_cellular_mutation_action_block(std::span<const std::uint8_t> bytes, std::uint64_t begin_tick,
|
|
std::uint64_t end_tick) {
|
|
if (end_tick <= begin_tick || bytes.size() % cellular_mutation_action_record_bytes != 0 ||
|
|
bytes.size() / cellular_mutation_action_record_bytes > max_cellular_action_records)
|
|
return std::unexpected{CellularActionBlockError::format};
|
|
std::vector<CellularMutationActionRecord> out;
|
|
out.reserve(bytes.size() / cellular_mutation_action_record_bytes);
|
|
std::size_t offset{};
|
|
for (std::size_t i = 0; i < bytes.size() / cellular_mutation_action_record_bytes; ++i) {
|
|
const auto record = bytes.subspan(offset, cellular_mutation_action_record_bytes);
|
|
CellularMutationActionRecord action;
|
|
std::ranges::copy(record.first(16), action.account.begin());
|
|
action.action_id = get(record, 16, 8);
|
|
action.tick = get(record, 24, 8);
|
|
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)))
|
|
return std::unexpected{CellularActionBlockError::format};
|
|
out.push_back(action);
|
|
offset += cellular_mutation_action_record_bytes;
|
|
}
|
|
if (!valid_mutation_actions(out, begin_tick, end_tick))
|
|
return std::unexpected{CellularActionBlockError::format};
|
|
return out;
|
|
}
|
|
|
|
std::expected<void, CellularActionBlockError>
|
|
match_cellular_actions_to_trace(const CellularLives& predecessor, const CellularBatch& batch,
|
|
std::span<const CellularActionRecord> actions) {
|
|
if (!valid_header(batch.header, batch.ticks.size()) || !valid_ticks(batch.ticks) ||
|
|
!valid_actions(actions, batch.header.begin_tick, batch.header.end_tick))
|
|
return std::unexpected{CellularActionBlockError::format};
|
|
if (predecessor.world() != batch.header.world || predecessor.region() != batch.header.region ||
|
|
predecessor.frame() != batch.header.frame || predecessor.tick() != batch.header.begin_tick)
|
|
return std::unexpected{CellularActionBlockError::mismatch};
|
|
for (const auto& action : actions) {
|
|
const auto life =
|
|
std::ranges::find(predecessor.lives(), action.account, &CellularOwnedLife::account);
|
|
if (life == predecessor.lives().end())
|
|
return std::unexpected{CellularActionBlockError::mismatch};
|
|
const auto index =
|
|
static_cast<std::size_t>(action_tick(action) - batch.header.begin_tick - 1);
|
|
const auto& intents = batch.ticks[index].lives;
|
|
const auto intent =
|
|
std::ranges::lower_bound(intents, life->identity.life, {},
|
|
[](const CellularLifeIntent& value) { return value.life_id; });
|
|
if (intent == intents.end() || intent->life_id != life->identity.life)
|
|
return std::unexpected{CellularActionBlockError::mismatch};
|
|
const auto input = std::span<const std::uint8_t>{action.input};
|
|
std::optional<biology::CellularPatchAddress> target;
|
|
if (input[12] == 1)
|
|
target = biology::CellularPatchAddress{static_cast<std::uint32_t>(get(input, 16, 4)),
|
|
static_cast<std::uint32_t>(get(input, 20, 4))};
|
|
if (intent->intent.movement.x !=
|
|
std::bit_cast<std::int16_t>(static_cast<std::uint16_t>(get(input, 8, 2))) ||
|
|
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))
|
|
return std::unexpected{CellularActionBlockError::mismatch};
|
|
}
|
|
return {};
|
|
}
|
|
|
|
std::expected<void, CellularActionBlockError>
|
|
match_cellular_mutation_actions_to_trace(const CellularLives& predecessor,
|
|
const CellularBatch& batch,
|
|
std::span<const CellularMutationActionRecord> actions) {
|
|
if (!valid_header(batch.header, batch.ticks.size()) || !valid_ticks(batch.ticks) ||
|
|
!valid_mutation_actions(actions, batch.header.begin_tick, batch.header.end_tick))
|
|
return std::unexpected{CellularActionBlockError::format};
|
|
if (predecessor.world() != batch.header.world || predecessor.region() != batch.header.region ||
|
|
predecessor.frame() != batch.header.frame || predecessor.tick() != batch.header.begin_tick)
|
|
return std::unexpected{CellularActionBlockError::mismatch};
|
|
std::size_t trace_selection_count{};
|
|
for (const auto& tick : batch.ticks)
|
|
trace_selection_count += tick.mutations.size();
|
|
if (trace_selection_count != actions.size())
|
|
return std::unexpected{CellularActionBlockError::mismatch};
|
|
for (std::size_t tick_index = 0; tick_index < batch.ticks.size(); ++tick_index) {
|
|
const auto tick = batch.header.begin_tick + tick_index + 1;
|
|
for (const auto& selection : batch.ticks[tick_index].mutations) {
|
|
const auto life = std::ranges::find(
|
|
predecessor.lives(), selection.life_id,
|
|
[](const CellularOwnedLife& value) { return value.identity.life; });
|
|
if (life == predecessor.lives().end())
|
|
return std::unexpected{CellularActionBlockError::mismatch};
|
|
const auto matches = std::ranges::count_if(actions, [&](const auto& action) {
|
|
return action.account == life->account && 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;
|
|
});
|
|
if (matches != 1)
|
|
return std::unexpected{CellularActionBlockError::mismatch};
|
|
}
|
|
}
|
|
for (const auto& action : actions) {
|
|
const auto life =
|
|
std::ranges::find(predecessor.lives(), action.account, &CellularOwnedLife::account);
|
|
if (life == predecessor.lives().end() ||
|
|
(predecessor.format_version() < cellular_lives_revision_format))
|
|
return std::unexpected{CellularActionBlockError::mismatch};
|
|
const auto tick_index = static_cast<std::size_t>(action.tick - batch.header.begin_tick - 1);
|
|
const auto& mutations = batch.ticks[tick_index].mutations;
|
|
const auto selection = std::ranges::lower_bound(mutations, life->identity.life, {},
|
|
&CellularMutationSelection::life_id);
|
|
if (!life->mutation.offer)
|
|
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 ||
|
|
selection->event_index != action.input.event_index ||
|
|
selection->offer_tick != action.input.offer_tick ||
|
|
selection->operator_id != action.input.operator_id ||
|
|
life->mutation.event_index != action.input.event_index ||
|
|
life->mutation.offer_tick != action.input.offer_tick ||
|
|
life->identity.life != action.input.life_id || offer.parent != life->identity.genome ||
|
|
!std::ranges::any_of(offer.candidates, [&](const auto& candidate) {
|
|
return candidate.operator_id == action.input.operator_id;
|
|
}))
|
|
return std::unexpected{CellularActionBlockError::mismatch};
|
|
}
|
|
return {};
|
|
}
|
|
} // namespace evo::server
|