Persist fed mutation offers in cellular v2
Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (ecs) (push) Successful in 1m56s
Foundation / checks (postgres) (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (network) (push) Canceled after 5m28s
Foundation / checks (checks) (push) Canceled after 7m39s
Foundation / checks (client) (push) Canceled after 7m39s
Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (ecs) (push) Successful in 1m56s
Foundation / checks (postgres) (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (network) (push) Canceled after 5m28s
Foundation / checks (checks) (push) Canceled after 7m39s
Foundation / checks (client) (push) Canceled after 7m39s
This commit is contained in:
@@ -89,6 +89,12 @@ provision_cellular_region(Postgres& owner, const FilesystemObjectStore& objects,
|
||||
auto manifest =
|
||||
genesis_manifest(key, frame, commit, biology::cellular_motion_tick_microseconds, build,
|
||||
payload.size(), *digest, CheckpointProfile::cellular_lives_v1);
|
||||
if (manifest && opening.format_version() == server::cellular_lives_format &&
|
||||
opening.rules_version() == server::cellular_lives_rules)
|
||||
manifest->cellular_versions = cellular_v2_versions;
|
||||
else if (manifest && (opening.format_version() != server::cellular_lives_legacy_format ||
|
||||
opening.rules_version() != 1))
|
||||
return std::unexpected{failure(PublicationError::candidate)};
|
||||
auto encoded = manifest ? encode_manifest(*manifest)
|
||||
: std::expected<std::vector<std::uint8_t>, CheckpointError>{
|
||||
std::unexpected{CheckpointError::format}};
|
||||
@@ -183,6 +189,8 @@ publish_cellular_batch(Postgres& db, const FilesystemObjectStore& objects, const
|
||||
batch.header.predecessor_digest != *predecessor_digest ||
|
||||
batch.header.begin_tick != previous->end_tick || candidate.world() != previous->key.world ||
|
||||
candidate.region() != previous->key.region || candidate.frame() != previous->frame ||
|
||||
candidate.format_version() != previous->cellular_versions.envelope ||
|
||||
candidate.rules_version() != previous->cellular_versions.rules ||
|
||||
candidate.tick() != batch.header.end_tick)
|
||||
return std::unexpected{failure(PublicationError::candidate)};
|
||||
|
||||
|
||||
@@ -62,6 +62,8 @@ bool structural(const CheckpointManifest& m) noexcept {
|
||||
const bool primitive = m.profile == CheckpointProfile::primitive_lives_v1;
|
||||
const bool cellular = m.profile == CheckpointProfile::cellular_lives_v1;
|
||||
if ((!cellular && m.cellular_versions != cellular_v1_versions) ||
|
||||
(cellular && m.cellular_versions != cellular_v1_versions &&
|
||||
m.cellular_versions != cellular_v2_versions) ||
|
||||
(cellular && m.versions != environmental_v1_versions))
|
||||
return false;
|
||||
if (primitive && (m.manifest_version != 2 || m.tick_microseconds != 50000 ||
|
||||
@@ -134,7 +136,8 @@ bool supported(const CheckpointManifest& m) noexcept {
|
||||
m.rng_streams == 0 && m.durability == DurabilityPolicy::single_volume_development;
|
||||
if (m.profile == CheckpointProfile::cellular_lives_v1)
|
||||
return m.manifest_version == 2 && m.history_profile == cellular_history_profile &&
|
||||
m.cellular_versions == cellular_v1_versions &&
|
||||
(m.cellular_versions == cellular_v1_versions ||
|
||||
m.cellular_versions == cellular_v2_versions) &&
|
||||
m.versions == environmental_v1_versions && m.tick_microseconds == 50000 &&
|
||||
m.rng_streams == 0 && m.durability == DurabilityPolicy::single_volume_development;
|
||||
const bool profile =
|
||||
@@ -169,15 +172,11 @@ encode_manifest(const CheckpointManifest& m) {
|
||||
w.u(m.tick_microseconds, 8);
|
||||
const auto versions =
|
||||
m.profile == CheckpointProfile::cellular_lives_v1
|
||||
? std::array{m.cellular_versions.rules,
|
||||
m.cellular_versions.envelope,
|
||||
m.cellular_versions.habitat,
|
||||
m.cellular_versions.coordinate,
|
||||
m.cellular_versions.genome,
|
||||
m.cellular_versions.development,
|
||||
m.cellular_versions.founder,
|
||||
m.cellular_versions.archive,
|
||||
0U}
|
||||
? std::array{m.cellular_versions.rules, m.cellular_versions.envelope,
|
||||
m.cellular_versions.habitat, m.cellular_versions.coordinate,
|
||||
m.cellular_versions.genome, m.cellular_versions.development,
|
||||
m.cellular_versions.founder, m.cellular_versions.archive,
|
||||
m.cellular_versions.mutation_state}
|
||||
: std::array{m.versions.rules, m.versions.envelope, m.versions.resources,
|
||||
m.versions.fields, m.versions.terrain, m.versions.coordinate,
|
||||
m.versions.generator, m.versions.material, m.versions.interval_adapter};
|
||||
@@ -230,10 +229,8 @@ decode_manifest(std::span<const std::uint8_t> bytes) {
|
||||
for (auto& v : versions)
|
||||
v = static_cast<std::uint32_t>(r.u(4));
|
||||
if (m.profile == CheckpointProfile::cellular_lives_v1) {
|
||||
m.cellular_versions = {versions[0], versions[1], versions[2], versions[3],
|
||||
versions[4], versions[5], versions[6], versions[7]};
|
||||
if (versions[8] != 0)
|
||||
return std::unexpected{CheckpointError::format};
|
||||
m.cellular_versions = {versions[0], versions[1], versions[2], versions[3], versions[4],
|
||||
versions[5], versions[6], versions[7], versions[8]};
|
||||
} else {
|
||||
m.versions = {versions[0], versions[1], versions[2], versions[3], versions[4],
|
||||
versions[5], versions[6], versions[7], versions[8]};
|
||||
@@ -435,6 +432,9 @@ restore_cellular_payload(std::span<const std::uint8_t> payload, const Checkpoint
|
||||
return std::unexpected{restored.error() == server::CellularLivesError::version
|
||||
? CheckpointError::unsupported
|
||||
: CheckpointError::payload};
|
||||
if ((*restored)->format_version() != m.cellular_versions.envelope ||
|
||||
(*restored)->rules_version() != m.cellular_versions.rules)
|
||||
return std::unexpected{CheckpointError::unsupported};
|
||||
if (m.sequence == 0 && (!(*restored)->lives().empty() || !(*restored)->corpses().empty()))
|
||||
return std::unexpected{CheckpointError::payload};
|
||||
return std::move(*restored);
|
||||
|
||||
@@ -10,11 +10,12 @@
|
||||
|
||||
namespace evo::server {
|
||||
namespace {
|
||||
constexpr std::uint64_t cellular_lives_magic = 0x3130564C43564541ULL; // AEVCLV01
|
||||
constexpr std::uint32_t cellular_lives_rules = 1;
|
||||
constexpr std::uint64_t cellular_lives_v1_magic = 0x3130564C43564541ULL; // AEVCLV01
|
||||
constexpr std::uint64_t cellular_lives_v2_magic = 0x3230564C43564541ULL; // AEVCLV02
|
||||
constexpr std::size_t cellular_lives_header_bytes = 212;
|
||||
constexpr std::size_t cellular_site_bytes = 8;
|
||||
constexpr std::size_t cellular_life_bytes = 168;
|
||||
constexpr std::size_t cellular_life_v1_bytes = 168;
|
||||
constexpr std::size_t cellular_life_v2_bytes = 480;
|
||||
constexpr std::size_t cellular_corpse_bytes = 100;
|
||||
|
||||
std::uint64_t checksum(std::span<const std::uint8_t> bytes) noexcept {
|
||||
@@ -24,6 +25,17 @@ std::uint64_t checksum(std::span<const std::uint8_t> bytes) noexcept {
|
||||
return value;
|
||||
}
|
||||
|
||||
std::uint64_t mutation_seed(std::uint64_t world, std::uint64_t region, std::uint64_t life,
|
||||
std::uint64_t event) noexcept {
|
||||
auto value = 14695981039346656037ULL;
|
||||
for (const auto part : {world, region, life, event}) {
|
||||
for (unsigned index = 0; index < 8; ++index) {
|
||||
value = (value ^ static_cast<std::uint8_t>(part >> (index * 8U))) * 1099511628211ULL;
|
||||
}
|
||||
}
|
||||
return value == 0 ? 1 : value;
|
||||
}
|
||||
|
||||
class Writer {
|
||||
public:
|
||||
explicit Writer(std::vector<std::uint8_t>& bytes) : bytes_{bytes} {}
|
||||
@@ -46,6 +58,9 @@ class Writer {
|
||||
void raw(std::span<const std::uint8_t> value) {
|
||||
bytes_.insert(bytes_.end(), value.begin(), value.end());
|
||||
}
|
||||
void zeros(std::size_t count) {
|
||||
bytes_.insert(bytes_.end(), count, 0);
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::uint8_t>& bytes_;
|
||||
@@ -263,6 +278,7 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
|
||||
|
||||
struct CellularLives::State {
|
||||
std::uint64_t world{}, region{}, frame{};
|
||||
std::uint32_t format_version{cellular_lives_format}, rules_version{cellular_lives_rules};
|
||||
CellularLivesConfig config{};
|
||||
NurseryConfig nursery_config{};
|
||||
std::unique_ptr<biology::CellularHabitat> habitat;
|
||||
@@ -279,11 +295,13 @@ struct CellularLives::State {
|
||||
std::unique_ptr<biology::CellularHabitat> map, std::vector<CellularOwnedLife> living,
|
||||
std::vector<CellularOwnedCorpse> dead, const biology::LineageArchive& history,
|
||||
accounting::Inventory source, accounting::Inventory accumulated_heat,
|
||||
accounting::Inventory initial, accounting::Inventory current)
|
||||
: world{world_id}, region{region_id}, frame{frame_id}, config{cfg},
|
||||
nursery_config{std::move(nursery_definition)}, habitat{std::move(map)},
|
||||
lives{std::move(living)}, corpses{std::move(dead)}, archive{history}, nursery{source},
|
||||
heat{accumulated_heat}, opening_total{initial}, current_total{current} {}
|
||||
accounting::Inventory initial, accounting::Inventory current,
|
||||
std::uint32_t format = cellular_lives_format, std::uint32_t rules = cellular_lives_rules)
|
||||
: world{world_id}, region{region_id}, frame{frame_id}, format_version{format},
|
||||
rules_version{rules}, config{cfg}, nursery_config{std::move(nursery_definition)},
|
||||
habitat{std::move(map)}, lives{std::move(living)}, corpses{std::move(dead)},
|
||||
archive{history}, nursery{source}, heat{accumulated_heat}, opening_total{initial},
|
||||
current_total{current} {}
|
||||
};
|
||||
|
||||
CellularLives::CellularLives(std::unique_ptr<State> state) : state_{std::move(state)} {}
|
||||
@@ -378,6 +396,12 @@ std::uint64_t CellularLives::frame() const noexcept {
|
||||
std::uint64_t CellularLives::tick() const noexcept {
|
||||
return state_->config.tick;
|
||||
}
|
||||
std::uint32_t CellularLives::format_version() const noexcept {
|
||||
return state_->format_version;
|
||||
}
|
||||
std::uint32_t CellularLives::rules_version() const noexcept {
|
||||
return state_->rules_version;
|
||||
}
|
||||
std::uint32_t CellularLives::ambient_temperature_millikelvin() const noexcept {
|
||||
return state_->config.ambient_temperature_millikelvin;
|
||||
}
|
||||
@@ -418,10 +442,12 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
const auto site_count = state_->nursery_config.sites.size();
|
||||
const auto life_count = state_->lives.size();
|
||||
const auto corpse_count = state_->corpses.size();
|
||||
const auto life_bytes = state_->format_version == cellular_lives_legacy_format
|
||||
? cellular_life_v1_bytes
|
||||
: cellular_life_v2_bytes;
|
||||
const auto total_bytes = cellular_lives_header_bytes + cellular_site_bytes * site_count +
|
||||
cellular_life_bytes * life_count +
|
||||
cellular_corpse_bytes * corpse_count + habitat.size() +
|
||||
archive.size() + 8;
|
||||
life_bytes * life_count + cellular_corpse_bytes * corpse_count +
|
||||
habitat.size() + archive.size() + 8;
|
||||
if (total_bytes > max_cellular_lives_bytes ||
|
||||
habitat.size() > biology::max_cellular_habitat_bytes ||
|
||||
archive.size() > biology::max_archive_bytes || site_count > max_primitive_lives ||
|
||||
@@ -432,9 +458,10 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
std::vector<std::uint8_t> bytes;
|
||||
bytes.reserve(total_bytes);
|
||||
Writer out{bytes};
|
||||
out.put(cellular_lives_magic);
|
||||
out.put(cellular_lives_format, 4);
|
||||
out.put(cellular_lives_rules, 4);
|
||||
out.put(state_->format_version == cellular_lives_legacy_format ? cellular_lives_v1_magic
|
||||
: cellular_lives_v2_magic);
|
||||
out.put(state_->format_version, 4);
|
||||
out.put(state_->rules_version, 4);
|
||||
out.put(state_->world);
|
||||
out.put(state_->region);
|
||||
out.put(state_->frame);
|
||||
@@ -468,6 +495,41 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
out.put(life.position.y_um);
|
||||
out.inventory(life.body);
|
||||
out.inventory(life.reserve);
|
||||
if (state_->format_version >= cellular_lives_format) {
|
||||
const auto& mutation = life.mutation;
|
||||
out.put(mutation.fed_energy_credit_pj);
|
||||
out.put(mutation.event_index);
|
||||
out.put(mutation.offer_tick);
|
||||
out.put(mutation.offer ? mutation.offer->parent : 0);
|
||||
out.put(mutation.offer ? mutation.offer->parent_digest : 0);
|
||||
out.put(mutation.offer ? mutation.offer->context.seed : 0);
|
||||
out.put(mutation.offer ? mutation.offer->context.stream : 0, 4);
|
||||
out.put(mutation.offer ? mutation.offer->context.event_index : 0);
|
||||
out.put(mutation.offer ? mutation.offer->context.candidate_count : 0, 2);
|
||||
out.put(0, 2);
|
||||
out.put(mutation.offer ? mutation.offer->context.growth.realized_per_thousand : 0, 4);
|
||||
out.put(mutation.offer ? mutation.offer->context.growth.damage_per_thousand : 0, 4);
|
||||
out.put(mutation.offer ? mutation.offer->context.environment.temperature_millikelvin
|
||||
: 0,
|
||||
4);
|
||||
out.put(0, 4);
|
||||
out.inventory(mutation.offer ? mutation.offer->context.environment.available
|
||||
: accounting::Inventory{});
|
||||
const auto count = mutation.offer ? mutation.offer->candidates.size() : 0;
|
||||
for (std::size_t index = 0; index < biology::max_candidates; ++index) {
|
||||
if (index < count) {
|
||||
const auto& candidate = mutation.offer->candidates[index];
|
||||
out.put(candidate.operator_id, 2);
|
||||
out.put(static_cast<std::uint8_t>(candidate.target), 1);
|
||||
out.put(static_cast<std::uint8_t>(candidate.effect), 1);
|
||||
out.put(candidate.parameter, 4);
|
||||
out.put(candidate.maintenance);
|
||||
out.put(candidate.total_mass);
|
||||
} else {
|
||||
out.zeros(24);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const auto& corpse : state_->corpses) {
|
||||
out.put(corpse.life_id);
|
||||
@@ -505,6 +567,34 @@ CellularLives::perceive(std::uint64_t life_id) const {
|
||||
return std::move(*view);
|
||||
}
|
||||
|
||||
std::expected<std::optional<biology::MutationOffer>, CellularLivesError>
|
||||
CellularLives::mutation_offer(std::uint64_t life_id) const {
|
||||
const auto life = std::ranges::find(state_->lives, life_id,
|
||||
[](const auto& owned) { return owned.identity.life; });
|
||||
if (life == state_->lives.end())
|
||||
return std::unexpected{CellularLivesError::invalid_life};
|
||||
return life->mutation.offer;
|
||||
}
|
||||
|
||||
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
|
||||
CellularLives::migrate_to_v2() const {
|
||||
if (state_->format_version != cellular_lives_legacy_format || state_->rules_version != 1)
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
auto lives = state_->lives;
|
||||
for (auto& life : lives)
|
||||
life.mutation = {};
|
||||
auto habitat = biology::CellularHabitat::restore(state_->habitat->snapshot(), state_->world,
|
||||
state_->region, state_->frame);
|
||||
if (!habitat)
|
||||
return std::unexpected{CellularLivesError::habitat};
|
||||
auto candidate = std::unique_ptr<CellularLives>{new CellularLives{std::make_unique<State>(
|
||||
state_->world, state_->region, state_->frame, state_->config, state_->nursery_config,
|
||||
std::move(*habitat), std::move(lives), state_->corpses, state_->archive, state_->nursery,
|
||||
state_->heat, state_->opening_total, state_->current_total, cellular_lives_format,
|
||||
cellular_lives_rules)}};
|
||||
return candidate;
|
||||
}
|
||||
|
||||
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
|
||||
CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) const {
|
||||
if (request.action == 0 ||
|
||||
@@ -613,7 +703,7 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
|
||||
auto candidate = std::unique_ptr<CellularLives>{new CellularLives{std::make_unique<State>(
|
||||
state_->world, state_->region, state_->frame, state_->config, std::move(nursery_config),
|
||||
std::move(*habitat), std::move(lives), state_->corpses, archive, nursery, heat,
|
||||
state_->opening_total, *current)}};
|
||||
state_->opening_total, *current, state_->format_version, state_->rules_version)}};
|
||||
candidate->last_tick_feedback_ = last_tick_feedback_;
|
||||
return candidate;
|
||||
}
|
||||
@@ -628,9 +718,15 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
return std::unexpected{CellularLivesError::checksum};
|
||||
|
||||
Reader in{bytes};
|
||||
if (in.get() != cellular_lives_magic)
|
||||
return std::unexpected{CellularLivesError::format};
|
||||
if (in.get(4) != cellular_lives_format || in.get(4) != cellular_lives_rules)
|
||||
const auto magic = in.get();
|
||||
const auto format_version = static_cast<std::uint32_t>(in.get(4));
|
||||
const auto rules_version = static_cast<std::uint32_t>(in.get(4));
|
||||
const bool legacy = magic == cellular_lives_v1_magic &&
|
||||
format_version == cellular_lives_legacy_format && rules_version == 1;
|
||||
const bool current_version = magic == cellular_lives_v2_magic &&
|
||||
format_version == cellular_lives_format &&
|
||||
rules_version == cellular_lives_rules;
|
||||
if (!legacy && !current_version)
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
const auto saved_world = in.get();
|
||||
const auto saved_region = in.get();
|
||||
@@ -661,10 +757,10 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
const auto opening_total = in.inventory();
|
||||
const auto nursery = in.inventory();
|
||||
const auto heat = in.inventory();
|
||||
const auto life_bytes = legacy ? cellular_life_v1_bytes : cellular_life_v2_bytes;
|
||||
const auto expected_size = cellular_lives_header_bytes + cellular_site_bytes * site_count +
|
||||
cellular_life_bytes * life_count +
|
||||
cellular_corpse_bytes * corpse_count + habitat_size + archive_size +
|
||||
8;
|
||||
life_bytes * life_count + cellular_corpse_bytes * corpse_count +
|
||||
habitat_size + archive_size + 8;
|
||||
if (source == 0 || expected_size != bytes.size())
|
||||
return std::unexpected{CellularLivesError::format};
|
||||
|
||||
@@ -689,6 +785,54 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
life.position = {in.get(), in.get()};
|
||||
life.body = in.inventory();
|
||||
life.reserve = in.inventory();
|
||||
if (current_version) {
|
||||
auto& mutation = life.mutation;
|
||||
mutation.fed_energy_credit_pj = in.get();
|
||||
mutation.event_index = in.get();
|
||||
mutation.offer_tick = in.get();
|
||||
biology::MutationOffer offer{};
|
||||
offer.parent = in.get();
|
||||
offer.parent_digest = in.get();
|
||||
offer.context.seed = in.get();
|
||||
offer.context.stream = static_cast<std::uint32_t>(in.get(4));
|
||||
offer.context.event_index = in.get();
|
||||
const auto offered_count = static_cast<std::uint16_t>(in.get(2));
|
||||
if (offered_count != 0)
|
||||
offer.context.candidate_count = cellular_mutation_candidate_count;
|
||||
const auto reserved16 = in.get(2);
|
||||
offer.context.growth.realized_per_thousand = static_cast<std::uint32_t>(in.get(4));
|
||||
offer.context.growth.damage_per_thousand = static_cast<std::uint32_t>(in.get(4));
|
||||
offer.context.environment.temperature_millikelvin =
|
||||
static_cast<std::uint32_t>(in.get(4));
|
||||
const auto reserved32 = in.get(4);
|
||||
offer.context.environment.available = in.inventory();
|
||||
for (std::size_t slot = 0; slot < biology::max_candidates; ++slot) {
|
||||
biology::MutationCandidate candidate{};
|
||||
candidate.operator_id = static_cast<std::uint16_t>(in.get(2));
|
||||
candidate.target = static_cast<biology::Capability>(in.get(1));
|
||||
candidate.effect = static_cast<biology::MutationEffect>(in.get(1));
|
||||
candidate.parameter = static_cast<std::uint32_t>(in.get(4));
|
||||
candidate.maintenance = in.get();
|
||||
candidate.total_mass = in.get();
|
||||
if (slot < offered_count)
|
||||
offer.candidates.push_back(candidate);
|
||||
else if (candidate != biology::MutationCandidate{})
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
}
|
||||
const bool has_offer = mutation.offer_tick != 0;
|
||||
if (reserved16 != 0 || reserved32 != 0 ||
|
||||
(has_offer &&
|
||||
(offer.parent == 0 || offer.parent_digest == 0 || offer.context.seed == 0 ||
|
||||
offer.context.stream == 0 || offer.context.event_index != mutation.event_index ||
|
||||
offered_count == 0 || offered_count > cellular_mutation_candidate_count ||
|
||||
offer.candidates.size() != offered_count)) ||
|
||||
(!has_offer && (offer.parent != 0 || offer.parent_digest != 0 ||
|
||||
offer.context != biology::MutationContext{})))
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
if (has_offer) {
|
||||
mutation.offer = std::move(offer);
|
||||
}
|
||||
}
|
||||
lives.push_back(life);
|
||||
}
|
||||
|
||||
@@ -729,6 +873,19 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
}
|
||||
if (current_version) {
|
||||
for (const auto& life : lives) {
|
||||
if (!life.mutation.offer)
|
||||
continue;
|
||||
const auto genome = archive->find(life.identity.genome);
|
||||
if (!genome)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto generated =
|
||||
biology::mutation_candidates(*genome, life.mutation.offer->context);
|
||||
if (!generated || *generated != *life.mutation.offer)
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
}
|
||||
}
|
||||
const auto current =
|
||||
validate_state(saved_world, saved_region, saved_frame, config, nursery_config, **habitat,
|
||||
lives, corpses, *archive, nursery, heat, opening_total);
|
||||
@@ -738,7 +895,7 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
auto restored = std::unique_ptr<CellularLives>{new CellularLives{std::make_unique<State>(
|
||||
saved_world, saved_region, saved_frame, config, std::move(nursery_config),
|
||||
std::move(*habitat), std::move(lives), std::move(corpses), *archive, nursery, heat,
|
||||
opening_total, *current)}};
|
||||
opening_total, *current, format_version, rules_version)}};
|
||||
if (!std::ranges::equal(bytes, restored->snapshot()))
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
return restored;
|
||||
@@ -793,6 +950,50 @@ CellularLives::advance_tick(std::span<const CellularLifeIntent> intents) {
|
||||
found->reserve = result.candidate.reserve_after;
|
||||
if (!add_inventory(next_heat, result.candidate.heat_after))
|
||||
return std::unexpected{CellularLivesError::overflow};
|
||||
if (state_->format_version >= cellular_lives_format && result.candidate.intake) {
|
||||
const auto acquired = result.candidate.intake->transfer.acquired.energy.value;
|
||||
if (acquired >
|
||||
std::numeric_limits<std::uint64_t>::max() - found->mutation.fed_energy_credit_pj)
|
||||
return std::unexpected{CellularLivesError::overflow};
|
||||
found->mutation.fed_energy_credit_pj += acquired;
|
||||
if (!found->mutation.offer) {
|
||||
const auto budget = budget_for(next_archive, *found,
|
||||
state_->config.ambient_temperature_millikelvin);
|
||||
if (!budget ||
|
||||
budget->maintenance.value > std::numeric_limits<std::uint64_t>::max() /
|
||||
cellular_mutation_surplus_seconds)
|
||||
return std::unexpected{CellularLivesError::mutation};
|
||||
const auto threshold =
|
||||
budget->maintenance.value * cellular_mutation_surplus_seconds;
|
||||
if (threshold == 0)
|
||||
return std::unexpected{CellularLivesError::mutation};
|
||||
if (found->mutation.fed_energy_credit_pj >= threshold) {
|
||||
if (found->mutation.event_index == std::numeric_limits<std::uint64_t>::max())
|
||||
return std::unexpected{CellularLivesError::overflow};
|
||||
const auto genome = next_archive.find(found->identity.genome);
|
||||
if (!genome)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto next_event = found->mutation.event_index + 1;
|
||||
biology::MutationContext context{
|
||||
mutation_seed(state_->world, state_->region, result.life_id, next_event),
|
||||
cellular_mutation_stream,
|
||||
next_event,
|
||||
cellular_mutation_candidate_count,
|
||||
found->growth,
|
||||
{state_->config.ambient_temperature_millikelvin,
|
||||
result.candidate.intake->transfer.patch_after}};
|
||||
const auto offer = biology::mutation_candidates(*genome, context);
|
||||
if (offer) {
|
||||
found->mutation.fed_energy_credit_pj -= threshold;
|
||||
found->mutation.event_index = next_event;
|
||||
found->mutation.offer_tick = proposal->end_tick;
|
||||
found->mutation.offer = *offer;
|
||||
} else if (offer.error() != biology::MutationError::no_candidates) {
|
||||
return std::unexpected{CellularLivesError::mutation};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const auto& dead : proposal->starved) {
|
||||
const auto found = std::ranges::lower_bound(
|
||||
@@ -834,13 +1035,13 @@ CellularLives::advance_tick(std::span<const CellularLifeIntent> intents) {
|
||||
if (!total)
|
||||
return std::unexpected{total.error()};
|
||||
|
||||
auto next =
|
||||
std::make_unique<State>(state_->world, state_->region, state_->frame,
|
||||
CellularLivesConfig{state_->config.area, proposal->end_tick,
|
||||
state_->config.ambient_temperature_millikelvin},
|
||||
state_->nursery_config, std::move(*next_habitat),
|
||||
std::move(next_lives), std::move(next_corpses), next_archive,
|
||||
state_->nursery, next_heat, state_->opening_total, *total);
|
||||
auto next = std::make_unique<State>(
|
||||
state_->world, state_->region, state_->frame,
|
||||
CellularLivesConfig{state_->config.area, proposal->end_tick,
|
||||
state_->config.ambient_temperature_millikelvin},
|
||||
state_->nursery_config, std::move(*next_habitat), std::move(next_lives),
|
||||
std::move(next_corpses), next_archive, state_->nursery, next_heat, state_->opening_total,
|
||||
*total, state_->format_version, state_->rules_version);
|
||||
std::vector<CellularLifeTickFeedback> feedback;
|
||||
feedback.reserve(proposal->lives.size());
|
||||
for (const auto& result : proposal->lives) {
|
||||
|
||||
Reference in New Issue
Block a user