Add cellular owner checkpoint codec
Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (checks) (push) Failing after 41s
Foundation / checks (ecs) (push) Successful in 1m55s
Foundation / checks (postgres) (push) Failing after 52s
Foundation / checks (tsan) (push) Failing after 31s
Foundation / checks (network) (push) Failing after 3m3s
Foundation / checks (client) (push) Failing after 4m26s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (checks) (push) Failing after 41s
Foundation / checks (ecs) (push) Successful in 1m55s
Foundation / checks (postgres) (push) Failing after 52s
Foundation / checks (tsan) (push) Failing after 31s
Foundation / checks (network) (push) Failing after 3m3s
Foundation / checks (client) (push) Failing after 4m26s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
This commit is contained in:
@@ -1,11 +1,92 @@
|
||||
#include "evo/server/lifecycle/cellular_lives.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <bit>
|
||||
#include <limits>
|
||||
#include <ranges>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::server {
|
||||
namespace {
|
||||
constexpr std::uint64_t cellular_lives_magic = 0x3130564C43564541ULL; // AEVCLV01
|
||||
constexpr std::uint32_t cellular_lives_rules = 1;
|
||||
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_corpse_bytes = 100;
|
||||
|
||||
std::uint64_t checksum(std::span<const std::uint8_t> bytes) noexcept {
|
||||
std::uint64_t value = 14695981039346656037ULL;
|
||||
for (const auto byte : bytes)
|
||||
value = (value ^ byte) * 1099511628211ULL;
|
||||
return value;
|
||||
}
|
||||
|
||||
class Writer {
|
||||
public:
|
||||
explicit Writer(std::vector<std::uint8_t>& bytes) : bytes_{bytes} {}
|
||||
void put(std::uint64_t value, unsigned width = 8) {
|
||||
for (unsigned i = 0; i < width; ++i, value >>= 8U)
|
||||
bytes_.push_back(static_cast<std::uint8_t>(value & 255U));
|
||||
}
|
||||
void inventory(const accounting::Inventory& value) {
|
||||
put(value.energy.value);
|
||||
for (const auto mass : value.matter)
|
||||
put(mass.value);
|
||||
}
|
||||
void site(SpawnSite value) {
|
||||
put(std::bit_cast<std::uint32_t>(value.x_um), 4);
|
||||
put(std::bit_cast<std::uint32_t>(value.y_um), 4);
|
||||
}
|
||||
template <std::size_t N> void bytes(const std::array<std::uint8_t, N>& value) {
|
||||
bytes_.insert(bytes_.end(), value.begin(), value.end());
|
||||
}
|
||||
void raw(std::span<const std::uint8_t> value) {
|
||||
bytes_.insert(bytes_.end(), value.begin(), value.end());
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::uint8_t>& bytes_;
|
||||
};
|
||||
|
||||
class Reader {
|
||||
public:
|
||||
explicit Reader(std::span<const std::uint8_t> bytes) : bytes_{bytes} {}
|
||||
std::uint64_t get(unsigned width = 8) {
|
||||
std::uint64_t value{};
|
||||
for (unsigned i = 0; i < width; ++i)
|
||||
value |= std::uint64_t{bytes_[at_++]} << (8U * i);
|
||||
return value;
|
||||
}
|
||||
accounting::Inventory inventory() {
|
||||
accounting::Inventory value{};
|
||||
value.energy.value = get();
|
||||
for (auto& mass : value.matter)
|
||||
mass.value = get();
|
||||
return value;
|
||||
}
|
||||
SpawnSite site() {
|
||||
return {std::bit_cast<std::int32_t>(static_cast<std::uint32_t>(get(4))),
|
||||
std::bit_cast<std::int32_t>(static_cast<std::uint32_t>(get(4)))};
|
||||
}
|
||||
template <std::size_t N> std::array<std::uint8_t, N> bytes() {
|
||||
std::array<std::uint8_t, N> result{};
|
||||
std::ranges::copy(bytes_.subspan(at_, N), result.begin());
|
||||
at_ += N;
|
||||
return result;
|
||||
}
|
||||
std::span<const std::uint8_t> raw(std::size_t size) {
|
||||
const auto result = bytes_.subspan(at_, size);
|
||||
at_ += size;
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
std::span<const std::uint8_t> bytes_;
|
||||
std::size_t at_{};
|
||||
};
|
||||
|
||||
bool add_inventory(accounting::Inventory& total, const accounting::Inventory& value) noexcept {
|
||||
constexpr auto maximum = std::numeric_limits<std::uint64_t>::max();
|
||||
if (value.energy.value > maximum - total.energy.value)
|
||||
@@ -48,6 +129,128 @@ sum_state(accounting::Inventory nursery, accounting::Inventory heat,
|
||||
return std::unexpected{CellularLivesError::overflow};
|
||||
return total;
|
||||
}
|
||||
|
||||
std::expected<accounting::Inventory, CellularLivesError>
|
||||
validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
|
||||
const CellularLivesConfig& config, const NurseryConfig& nursery_config,
|
||||
const biology::CellularHabitat& habitat,
|
||||
std::span<const CellularOwnedLife> lives,
|
||||
std::span<const CellularOwnedCorpse> corpses,
|
||||
const biology::LineageArchive& archive, accounting::Inventory nursery,
|
||||
accounting::Inventory heat, accounting::Inventory opening_total) {
|
||||
const auto& map = habitat.config();
|
||||
if (world == 0 || region == 0 || frame == 0 || nursery_config.world != world ||
|
||||
nursery_config.region != region || nursery_config.frame != frame ||
|
||||
nursery_config.source == 0 || nursery_config.funding != nursery ||
|
||||
map.world != world || map.region != region || map.frame != frame ||
|
||||
config.area.width_um == 0 || config.area.height_um == 0 ||
|
||||
map.width_um != config.area.width_um || map.height_um != config.area.height_um ||
|
||||
config.ambient_temperature_millikelvin == 0 ||
|
||||
nursery_config.sites.empty() || nursery_config.sites.size() > max_primitive_lives ||
|
||||
!std::ranges::is_sorted(nursery_config.sites) ||
|
||||
lives.size() > biology::max_cellular_tick_lives ||
|
||||
corpses.size() > biology::max_archived_organisms ||
|
||||
archive.recorded() != lives.size() + corpses.size())
|
||||
return std::unexpected{CellularLivesError::invalid_config};
|
||||
if (std::ranges::any_of(heat.matter,
|
||||
[](const auto& value) { return value.value != 0; }))
|
||||
return std::unexpected{CellularLivesError::conservation};
|
||||
|
||||
const auto founder = biology::primitive_founder(1, 1);
|
||||
if (!founder)
|
||||
return std::unexpected{CellularLivesError::biology};
|
||||
const auto founder_budget =
|
||||
biology::metabolic_budget(*founder, {1000, 0}, {config.ambient_temperature_millikelvin, {}});
|
||||
if (!founder_budget)
|
||||
return std::unexpected{CellularLivesError::biology};
|
||||
const auto spawn_extent = founder_budget->phenotype.extent;
|
||||
for (std::size_t i = 0; i < nursery_config.sites.size(); ++i) {
|
||||
const auto site = nursery_config.sites[i];
|
||||
if (site.x_um < 0 || site.y_um < 0 || spawn_extent > config.area.width_um ||
|
||||
spawn_extent > config.area.height_um ||
|
||||
static_cast<std::uint64_t>(site.x_um) > config.area.width_um - spawn_extent ||
|
||||
static_cast<std::uint64_t>(site.y_um) > config.area.height_um - spawn_extent)
|
||||
return std::unexpected{CellularLivesError::invalid_config};
|
||||
for (std::size_t j = 0; j < i; ++j) {
|
||||
const auto delta_x = std::int64_t{site.x_um} - nursery_config.sites[j].x_um;
|
||||
const auto delta_y = std::int64_t{site.y_um} - nursery_config.sites[j].y_um;
|
||||
const auto dx = static_cast<std::uint64_t>(delta_x < 0 ? -delta_x : delta_x);
|
||||
const auto dy = static_cast<std::uint64_t>(delta_y < 0 ? -delta_y : delta_y);
|
||||
if (dx < spawn_extent && dy < spawn_extent)
|
||||
return std::unexpected{CellularLivesError::invalid_config};
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::uint64_t> live_organisms;
|
||||
std::vector<std::uint64_t> life_ids;
|
||||
live_organisms.reserve(lives.size());
|
||||
life_ids.reserve(lives.size() + corpses.size());
|
||||
for (std::size_t i = 0; i < lives.size(); ++i) {
|
||||
const auto& life = lives[i];
|
||||
if (life.account == LifeAccount{} || life.identity.life == 0 ||
|
||||
life.identity.organism == 0 || life.identity.genome == 0 ||
|
||||
life.identity.event == 0 || life.identity.born.value == 0 ||
|
||||
life.identity.born.value > config.tick || life.source == 0 ||
|
||||
(i != 0 && lives[i - 1].identity.life >= life.identity.life))
|
||||
return std::unexpected{CellularLivesError::invalid_life};
|
||||
for (std::size_t j = 0; j < i; ++j)
|
||||
if (lives[j].account == life.account ||
|
||||
lives[j].identity.organism == life.identity.organism)
|
||||
return std::unexpected{CellularLivesError::duplicate_life};
|
||||
const auto record = archive.organism(life.identity.organism);
|
||||
if (!record || !record->alive || record->genome != life.identity.genome ||
|
||||
record->born != life.identity.born)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto budget = budget_for(archive, life, config.ambient_temperature_millikelvin);
|
||||
if (!budget || budget->body != life.body || life.body.energy.value != 0 ||
|
||||
budget->phenotype.extent == 0 ||
|
||||
budget->phenotype.extent > config.area.width_um ||
|
||||
budget->phenotype.extent > config.area.height_um ||
|
||||
life.position.x_um > config.area.width_um - budget->phenotype.extent ||
|
||||
life.position.y_um > config.area.height_um - budget->phenotype.extent)
|
||||
return std::unexpected{CellularLivesError::invalid_life};
|
||||
live_organisms.push_back(life.identity.organism);
|
||||
life_ids.push_back(life.identity.life);
|
||||
}
|
||||
std::ranges::sort(live_organisms);
|
||||
if (archive.living_organisms() != live_organisms)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
|
||||
for (std::size_t i = 0; i < corpses.size(); ++i) {
|
||||
const auto& corpse = corpses[i];
|
||||
if (corpse.life_id == 0 || corpse.organism == 0 || corpse.source == 0 ||
|
||||
corpse.died_tick == 0 || corpse.died_tick > config.tick || corpse.extent_um == 0 ||
|
||||
(i != 0 && corpses[i - 1].organism >= corpse.organism))
|
||||
return std::unexpected{CellularLivesError::invalid_life};
|
||||
const auto record = archive.organism(corpse.organism);
|
||||
if (!record || record->alive || record->died.value != corpse.died_tick)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto genome = archive.find(record->genome);
|
||||
if (!genome)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto budget = biology::metabolic_budget(
|
||||
*genome, corpse.growth, {config.ambient_temperature_millikelvin, {}});
|
||||
if (!budget || budget->phenotype.extent != corpse.extent_um ||
|
||||
corpse.extent_um > config.area.width_um || corpse.extent_um > config.area.height_um ||
|
||||
corpse.position.x_um > config.area.width_um - corpse.extent_um ||
|
||||
corpse.position.y_um > config.area.height_um - corpse.extent_um)
|
||||
return std::unexpected{CellularLivesError::invalid_life};
|
||||
for (std::size_t channel = 0; channel < corpse.inventory.matter.size(); ++channel)
|
||||
if (corpse.inventory.matter[channel].value < budget->body.matter[channel].value)
|
||||
return std::unexpected{CellularLivesError::conservation};
|
||||
life_ids.push_back(corpse.life_id);
|
||||
}
|
||||
std::ranges::sort(life_ids);
|
||||
if (std::ranges::adjacent_find(life_ids) != life_ids.end())
|
||||
return std::unexpected{CellularLivesError::duplicate_life};
|
||||
|
||||
const auto current = sum_state(nursery, heat, habitat, lives, corpses);
|
||||
if (!current)
|
||||
return std::unexpected{current.error()};
|
||||
if (*current != opening_total)
|
||||
return std::unexpected{CellularLivesError::conservation};
|
||||
return *current;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
struct CellularLives::State {
|
||||
@@ -64,7 +267,8 @@ struct CellularLives::State {
|
||||
accounting::Inventory current_total{};
|
||||
|
||||
State(std::uint64_t world_id, std::uint64_t region_id, std::uint64_t frame_id,
|
||||
CellularLivesConfig cfg, NurseryConfig nursery, std::unique_ptr<biology::CellularHabitat> map,
|
||||
CellularLivesConfig cfg, NurseryConfig nursery,
|
||||
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,
|
||||
@@ -134,23 +338,26 @@ CellularLives::create(const PrimitiveLives& opening,
|
||||
life.position.y_um > config.area.height_um - budget->phenotype.extent)
|
||||
return std::unexpected{CellularLivesError::invalid_life};
|
||||
}
|
||||
auto initial_total = opening.totals();
|
||||
if (!add_inventory(initial_total, habitat->totals()))
|
||||
auto opening_total = opening.totals();
|
||||
if (!add_inventory(opening_total, habitat->totals()))
|
||||
return std::unexpected{CellularLivesError::overflow};
|
||||
const auto nursery = opening.balances()[0];
|
||||
nursery_config.funding = nursery;
|
||||
const auto heat = opening.balances()[1];
|
||||
const auto current = sum_state(nursery, heat, *habitat, lives, {});
|
||||
const auto current = validate_state(nursery_config.world, nursery_config.region,
|
||||
nursery_config.frame, config, nursery_config, *habitat,
|
||||
lives, {}, opening.archive(), nursery, heat, opening_total);
|
||||
if (!current)
|
||||
return std::unexpected{current.error()};
|
||||
if (*current != initial_total)
|
||||
return std::unexpected{CellularLivesError::conservation};
|
||||
|
||||
const auto world = nursery_config.world;
|
||||
const auto region = nursery_config.region;
|
||||
const auto frame = nursery_config.frame;
|
||||
auto state = std::make_unique<State>(
|
||||
nursery_config.world, nursery_config.region, nursery_config.frame, config,
|
||||
world, region, frame, config,
|
||||
std::move(nursery_config),
|
||||
std::move(habitat), std::move(lives), std::vector<CellularOwnedCorpse>{},
|
||||
opening.archive(), nursery, heat, initial_total, *current);
|
||||
opening.archive(), nursery, heat, opening_total, *current);
|
||||
return std::unique_ptr<CellularLives>{new CellularLives{std::move(state)}};
|
||||
}
|
||||
|
||||
@@ -174,6 +381,208 @@ accounting::Inventory CellularLives::current_total() const noexcept {
|
||||
return state_->current_total;
|
||||
}
|
||||
|
||||
std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
const auto habitat = state_->habitat->snapshot();
|
||||
const auto archive = state_->archive.encode();
|
||||
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 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;
|
||||
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 ||
|
||||
life_count > biology::max_cellular_tick_lives ||
|
||||
corpse_count > biology::max_archived_organisms)
|
||||
throw std::length_error{"cellular lives checkpoint exceeds its format bounds"};
|
||||
|
||||
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_->world);
|
||||
out.put(state_->region);
|
||||
out.put(state_->frame);
|
||||
out.put(state_->config.tick);
|
||||
out.put(state_->config.area.width_um, 4);
|
||||
out.put(state_->config.area.height_um, 4);
|
||||
out.put(state_->config.ambient_temperature_millikelvin, 4);
|
||||
out.put(site_count, 4);
|
||||
out.put(life_count, 4);
|
||||
out.put(corpse_count, 4);
|
||||
out.put(habitat.size(), 4);
|
||||
out.put(archive.size(), 4);
|
||||
out.put(0, 4);
|
||||
out.put(state_->nursery_config.source);
|
||||
out.inventory(state_->opening_total);
|
||||
out.inventory(state_->nursery);
|
||||
out.inventory(state_->heat);
|
||||
for (const auto site : state_->nursery_config.sites)
|
||||
out.site(site);
|
||||
for (const auto& life : state_->lives) {
|
||||
out.bytes(life.account);
|
||||
out.put(life.identity.life);
|
||||
out.put(life.identity.organism);
|
||||
out.put(life.identity.genome);
|
||||
out.put(life.identity.event);
|
||||
out.put(life.identity.born.value);
|
||||
out.put(life.source);
|
||||
out.put(life.growth.realized_per_thousand, 4);
|
||||
out.put(life.growth.damage_per_thousand, 4);
|
||||
out.put(life.position.x_um);
|
||||
out.put(life.position.y_um);
|
||||
out.inventory(life.body);
|
||||
out.inventory(life.reserve);
|
||||
}
|
||||
for (const auto& corpse : state_->corpses) {
|
||||
out.put(corpse.life_id);
|
||||
out.put(corpse.organism);
|
||||
out.put(corpse.source);
|
||||
out.put(corpse.died_tick);
|
||||
out.put(corpse.position.x_um);
|
||||
out.put(corpse.position.y_um);
|
||||
out.put(corpse.growth.realized_per_thousand, 4);
|
||||
out.put(corpse.growth.damage_per_thousand, 4);
|
||||
out.put(corpse.extent_um, 4);
|
||||
out.inventory(corpse.inventory);
|
||||
}
|
||||
out.raw(habitat);
|
||||
out.raw(archive);
|
||||
if (bytes.size() + 8 != total_bytes)
|
||||
throw std::logic_error{"cellular lives checkpoint size calculation mismatch"};
|
||||
out.put(checksum(bytes));
|
||||
return bytes;
|
||||
}
|
||||
|
||||
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
|
||||
CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
std::uint64_t region, std::uint64_t frame, std::uint64_t tick) {
|
||||
if (bytes.size() < cellular_lives_header_bytes + 8 || bytes.size() > max_cellular_lives_bytes)
|
||||
return std::unexpected{CellularLivesError::format};
|
||||
Reader tail{bytes.last(8)};
|
||||
if (checksum(bytes.first(bytes.size() - 8)) != tail.get())
|
||||
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)
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
const auto saved_world = in.get();
|
||||
const auto saved_region = in.get();
|
||||
const auto saved_frame = in.get();
|
||||
const auto saved_tick = in.get();
|
||||
if (saved_world != world || saved_region != region || saved_frame != frame)
|
||||
return std::unexpected{CellularLivesError::identity};
|
||||
if (saved_tick != tick)
|
||||
return std::unexpected{CellularLivesError::invalid_tick};
|
||||
|
||||
CellularLivesConfig config;
|
||||
config.area.width_um = static_cast<std::uint32_t>(in.get(4));
|
||||
config.area.height_um = static_cast<std::uint32_t>(in.get(4));
|
||||
config.tick = saved_tick;
|
||||
config.ambient_temperature_millikelvin = static_cast<std::uint32_t>(in.get(4));
|
||||
const auto site_count = in.get(4);
|
||||
const auto life_count = in.get(4);
|
||||
const auto corpse_count = in.get(4);
|
||||
const auto habitat_size = in.get(4);
|
||||
const auto archive_size = in.get(4);
|
||||
if (in.get(4) != 0 || site_count == 0 || site_count > max_primitive_lives ||
|
||||
life_count > biology::max_cellular_tick_lives ||
|
||||
corpse_count > biology::max_archived_organisms ||
|
||||
habitat_size > biology::max_cellular_habitat_bytes ||
|
||||
archive_size > biology::max_archive_bytes)
|
||||
return std::unexpected{CellularLivesError::format};
|
||||
const auto source = in.get();
|
||||
const auto opening_total = in.inventory();
|
||||
const auto nursery = in.inventory();
|
||||
const auto heat = in.inventory();
|
||||
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;
|
||||
if (source == 0 || expected_size != bytes.size())
|
||||
return std::unexpected{CellularLivesError::format};
|
||||
|
||||
NurseryConfig nursery_config{saved_world, saved_region, saved_frame, source, nursery, {}};
|
||||
nursery_config.sites.reserve(static_cast<std::size_t>(site_count));
|
||||
for (std::uint64_t i = 0; i < site_count; ++i)
|
||||
nursery_config.sites.push_back(in.site());
|
||||
|
||||
std::vector<CellularOwnedLife> lives;
|
||||
lives.reserve(static_cast<std::size_t>(life_count));
|
||||
for (std::uint64_t i = 0; i < life_count; ++i) {
|
||||
CellularOwnedLife life{};
|
||||
life.account = in.bytes<16>();
|
||||
life.identity.life = in.get();
|
||||
life.identity.organism = in.get();
|
||||
life.identity.genome = in.get();
|
||||
life.identity.event = in.get();
|
||||
life.identity.born.value = in.get();
|
||||
life.source = in.get();
|
||||
life.growth.realized_per_thousand = static_cast<std::uint32_t>(in.get(4));
|
||||
life.growth.damage_per_thousand = static_cast<std::uint32_t>(in.get(4));
|
||||
life.position = {in.get(), in.get()};
|
||||
life.body = in.inventory();
|
||||
life.reserve = in.inventory();
|
||||
lives.push_back(life);
|
||||
}
|
||||
|
||||
std::vector<CellularOwnedCorpse> corpses;
|
||||
corpses.reserve(static_cast<std::size_t>(corpse_count));
|
||||
for (std::uint64_t i = 0; i < corpse_count; ++i) {
|
||||
CellularOwnedCorpse corpse{};
|
||||
corpse.life_id = in.get();
|
||||
corpse.organism = in.get();
|
||||
corpse.source = in.get();
|
||||
corpse.died_tick = in.get();
|
||||
corpse.position = {in.get(), in.get()};
|
||||
corpse.growth.realized_per_thousand = static_cast<std::uint32_t>(in.get(4));
|
||||
corpse.growth.damage_per_thousand = static_cast<std::uint32_t>(in.get(4));
|
||||
corpse.extent_um = static_cast<std::uint32_t>(in.get(4));
|
||||
corpse.inventory = in.inventory();
|
||||
corpses.push_back(corpse);
|
||||
}
|
||||
const auto habitat_payload = in.raw(static_cast<std::size_t>(habitat_size));
|
||||
const auto archive_payload = in.raw(static_cast<std::size_t>(archive_size));
|
||||
|
||||
auto habitat = biology::CellularHabitat::restore(habitat_payload, saved_world, saved_region,
|
||||
saved_frame);
|
||||
if (!habitat) {
|
||||
if (habitat.error() == biology::CellularHabitatError::version)
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
if (habitat.error() == biology::CellularHabitatError::checksum)
|
||||
return std::unexpected{CellularLivesError::checksum};
|
||||
return std::unexpected{CellularLivesError::habitat};
|
||||
}
|
||||
auto archive = biology::LineageArchive::decode(archive_payload);
|
||||
if (!archive) {
|
||||
if (archive.error() == biology::ArchiveCodecError::version)
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
if (archive.error() == biology::ArchiveCodecError::checksum)
|
||||
return std::unexpected{CellularLivesError::checksum};
|
||||
if (archive.error() == biology::ArchiveCodecError::noncanonical)
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
}
|
||||
const auto current = validate_state(saved_world, saved_region, saved_frame, config,
|
||||
nursery_config, **habitat, lives, corpses, *archive,
|
||||
nursery, heat, opening_total);
|
||||
if (!current)
|
||||
return std::unexpected{current.error()};
|
||||
|
||||
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)}};
|
||||
if (!std::ranges::equal(bytes, restored->snapshot()))
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
return restored;
|
||||
}
|
||||
|
||||
std::expected<biology::CellularRegionStepCandidate, CellularLivesError>
|
||||
CellularLives::advance_tick(std::span<const CellularLifeIntent> intents) {
|
||||
if (state_->config.tick == std::numeric_limits<std::uint64_t>::max())
|
||||
@@ -261,12 +670,14 @@ CellularLives::advance_tick(std::span<const CellularLifeIntent> intents) {
|
||||
auto next_habitat = biology::CellularHabitat::create(std::move(habitat_config));
|
||||
if (!next_habitat)
|
||||
return std::unexpected{CellularLivesError::habitat};
|
||||
const auto total = sum_state(state_->nursery, next_heat, **next_habitat, next_lives,
|
||||
next_corpses);
|
||||
const auto total = validate_state(
|
||||
state_->world, state_->region, state_->frame,
|
||||
CellularLivesConfig{state_->config.area, proposal->end_tick,
|
||||
state_->config.ambient_temperature_millikelvin},
|
||||
state_->nursery_config, **next_habitat, next_lives, next_corpses, next_archive,
|
||||
state_->nursery, next_heat, state_->opening_total);
|
||||
if (!total)
|
||||
return std::unexpected{total.error()};
|
||||
if (*total != state_->opening_total)
|
||||
return std::unexpected{CellularLivesError::conservation};
|
||||
|
||||
auto next = std::make_unique<State>(
|
||||
state_->world, state_->region, state_->frame,
|
||||
|
||||
Reference in New Issue
Block a user