Foundation / checks (ecs) (push) Successful in 2m25s
Foundation / checks (deploy) (push) Successful in 5m15s
Foundation / checks (network) (push) Successful in 21m56s
Foundation / checks (client) (push) Successful in 22m54s
Foundation / checks (tsan) (push) Successful in 47m4s
Foundation / checks (postgres) (push) Successful in 1h15m49s
Foundation / checks (checks) (push) Failing after 1h30m4s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
ADR-0175 implements ADR-0136 step C with trial costs to be balanced after playtesting. Genome schema 5 adds a photosynthesis efficiency (empty in schemas 1-4, so earlier offers and draws are unchanged; mutation operators 29-32 are appended). Development version 5 builds the organelles as internal membrane (p/4 of structure, upkept twice) and slows movement by p/2 per thousand. Founder 4 and cellular rules 11 let lineages earn the capability as an offspring plan that children express. Owner format 9 declares the region's light. Inside each tick's detached candidate, active unfrozen producers capture cross-section x irradiance x efficiency (a quarter while moving), credited to the reserve and boundary inflow together and replayed from recorded intentions. Well-lit wildlife producers hold still. The joint owner keeps infall, water and radiation and nests a rules-11 owner unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
190 lines
8.9 KiB
C++
190 lines
8.9 KiB
C++
#include "evo/server/lifecycle/cellular_environment.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <utility>
|
|
|
|
namespace evo::server {
|
|
namespace {
|
|
constexpr std::uint64_t magic = 0x315442454F564541ULL; // "AEVOEBT1"
|
|
|
|
void put(std::vector<std::uint8_t>& out, std::uint64_t value, unsigned width = 8) {
|
|
for (unsigned i = 0; i < width; ++i) {
|
|
out.push_back(static_cast<std::uint8_t>(value & 255U));
|
|
value >>= 8U;
|
|
}
|
|
}
|
|
std::uint64_t get(std::span<const std::uint8_t> 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<std::unique_ptr<CellularEnvironment>, 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<CellularLifeIntent> 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<CellularEnvironmentExecution, CellularEnvironmentError>
|
|
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<CellularEnvironmentExecution, CellularEnvironmentError>
|
|
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<std::unique_ptr<CellularEnvironment>, CellularEnvironmentError>
|
|
replay_environment_batch(const CellularEnvironment& base, const CellularEnvironmentBatch& batch) {
|
|
auto copy = batch;
|
|
return execute_environment_batch(base, copy, false);
|
|
}
|
|
|
|
std::expected<std::vector<std::uint8_t>, 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<std::uint8_t> 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<CellularEnvironmentBatch, CellularEnvironmentError>
|
|
decode_environment_batch(std::span<const std::uint8_t> 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<std::size_t>(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<std::size_t>(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
|