#include "evo/server/lifecycle/cellular_environment.hpp" #include #include namespace evo::server { namespace { constexpr std::uint64_t magic = 0x315442454F564541ULL; // "AEVOEBT1" void put(std::vector& out, std::uint64_t value, unsigned width = 8) { for (unsigned i = 0; i < width; ++i) { out.push_back(static_cast(value & 255U)); value >>= 8U; } } std::uint64_t get(std::span bytes, std::size_t at, unsigned width) { std::uint64_t value = 0; for (unsigned i = 0; i < width; ++i) value |= std::uint64_t{bytes[at + i]} << (8U * i); return value; } // The nested population batch must already be canonical for its own format. bool shape(const CellularEnvironmentBatch& batch) { const auto& population = batch.population; if (population.format_version != 2 && population.format_version != cellular_population_inheritance_format) return false; const auto ticks = population.conversion ? std::size_t{0} : population.biology.ticks.size(); return batch.flows.size() == ticks && encode_population_batch(population).has_value(); } } // namespace std::expected, CellularEnvironmentError> execute_environment_batch(const CellularEnvironment& base, CellularEnvironmentBatch& batch, bool record) { auto& population = batch.population; const auto& state = base.state(); if (population.format_version != base.population().format_version()) return std::unexpected(CellularEnvironmentError::unsupported); const auto& h = population.header; if (h.world != state.world() || h.region != state.region() || h.frame != state.frame() || h.begin_tick != state.tick()) return std::unexpected(CellularEnvironmentError::identity); if (!record && !shape(batch)) return std::unexpected(CellularEnvironmentError::format); if (!population.conversion && !population_interval_safe(state)) return std::unexpected(CellularEnvironmentError::unsupported); auto candidate = CellularEnvironment::restore(base.snapshot(), state.world(), state.region(), state.frame(), state.tick()); if (!candidate) return std::unexpected(candidate.error()); auto& joint = **candidate; if (record) { population.boundaries.clear(); batch.flows.clear(); } const auto boundary = [&](std::size_t index) { auto actual = joint.population_boundary(); if (record) { population.boundaries.push_back(std::move(actual)); return true; } return population.boundaries[index] == actual; }; if (population.conversion) { const auto converted = population.conversion->to_cohort ? joint.collapse(population.conversion->genome) : joint.materialize(population.conversion->genome); if (!converted || !boundary(0)) return std::unexpected(CellularEnvironmentError::biology); } else { const auto count = record ? h.end_tick - h.begin_tick : population.biology.ticks.size(); if (record) { population.biology.header = h; population.biology.ticks.clear(); population.biology.format_version = cellular_retained_plans(state.rules_version()) ? cellular_batch_inheritance_format : cellular_batch_format; } for (std::size_t index = 0; index < count; ++index) { std::vector intents; if (!record) intents = population.biology.ticks[index].lives; const auto flow = joint.advance_unattended(intents, record); if (!flow) return std::unexpected(flow.error()); if (record) { population.biology.ticks.push_back({std::move(intents), {}, {}}); batch.flows.push_back(*flow); } else if (batch.flows[index] != *flow) { return std::unexpected(CellularEnvironmentError::conservation); } if (!boundary(index)) return std::unexpected(CellularEnvironmentError::biology); } } if (record && !shape(batch)) return std::unexpected(CellularEnvironmentError::format); return candidate; } std::expected prepare_environment_interval(const CellularEnvironment& base, const CellularBatchHeader& header) { if (header.end_tick <= header.begin_tick || header.end_tick - header.begin_tick > max_cellular_batch_ticks) return std::unexpected(CellularEnvironmentError::identity); CellularEnvironmentBatch batch; batch.population.header = header; batch.population.format_version = base.population().format_version(); auto state = execute_environment_batch(base, batch, true); if (!state) return std::unexpected(state.error()); return CellularEnvironmentExecution{std::move(batch), std::move(*state)}; } std::expected prepare_environment_conversion(const CellularEnvironment& base, CellularBatchHeader header, CellularPopulationConversion conversion) { CellularEnvironmentBatch batch; batch.population = {header, {}, conversion, {}}; batch.population.format_version = base.population().format_version(); auto state = execute_environment_batch(base, batch, true); if (!state) return std::unexpected(state.error()); return CellularEnvironmentExecution{std::move(batch), std::move(*state)}; } std::expected, CellularEnvironmentError> replay_environment_batch(const CellularEnvironment& base, const CellularEnvironmentBatch& batch) { auto copy = batch; return execute_environment_batch(base, copy, false); } std::expected, CellularEnvironmentError> encode_environment_batch(const CellularEnvironmentBatch& batch) { if (!shape(batch)) return std::unexpected(CellularEnvironmentError::format); const auto nested = encode_population_batch(batch.population); if (!nested) return std::unexpected(CellularEnvironmentError::format); std::vector out; put(out, magic); put(out, cellular_environment_batch_format, 4); put(out, batch.population.conversion ? 1 : 0, 4); put(out, nested->size()); put(out, batch.flows.size(), 4); put(out, 0, 4); out.insert(out.end(), nested->begin(), nested->end()); for (const auto& flow : batch.flows) { put(out, flow.imported.energy.value); for (const auto& channel : flow.imported.matter) put(out, channel.value); put(out, flow.exported.value); } return out; } std::expected decode_environment_batch(std::span bytes) { if (bytes.size() < environment_batch_header_bytes || bytes.size() > max_environment_batch_bytes) return std::unexpected(CellularEnvironmentError::format); if (get(bytes, 0, 8) != magic) return std::unexpected(CellularEnvironmentError::format); if (get(bytes, 8, 4) != cellular_environment_batch_format) return std::unexpected(CellularEnvironmentError::unsupported); const auto kind = get(bytes, 12, 4), nested = get(bytes, 16, 8), flows = get(bytes, 24, 4); if (kind > 1 || get(bytes, 28, 4) != 0 || nested > max_population_batch_bytes || flows > max_cellular_batch_ticks || bytes.size() != environment_batch_header_bytes + nested + flows * environment_flow_bytes) return std::unexpected(CellularEnvironmentError::format); auto population = decode_population_batch( bytes.subspan(environment_batch_header_bytes, static_cast(nested))); if (!population || population->conversion.has_value() != (kind == 1)) return std::unexpected(CellularEnvironmentError::format); CellularEnvironmentBatch batch{std::move(*population), {}}; auto at = environment_batch_header_bytes + static_cast(nested); for (std::uint64_t index = 0; index < flows; ++index) { CellularEnvironmentFlow flow; flow.imported.energy.value = get(bytes, at, 8); for (std::size_t channel = 0; channel < flow.imported.matter.size(); ++channel) flow.imported.matter[channel].value = get(bytes, at + 8 + 8 * channel, 8); flow.exported.value = get(bytes, at + 40, 8); at += environment_flow_bytes; batch.flows.push_back(flow); } const auto canonical = encode_environment_batch(batch); if (!canonical || !std::ranges::equal(*canonical, bytes)) return std::unexpected(CellularEnvironmentError::format); return batch; } } // namespace evo::server