Add finite cellular habitat snapshot
Foundation / checks (deploy) (push) Successful in 5s
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 (checks) (push) Canceled after 2m39s
Foundation / checks (client) (push) Canceled after 2m38s
Foundation / checks (network) (push) Canceled after 36s

This commit is contained in:
dingo
2026-10-10 21:13:51 -04:00
parent c552eb3250
commit 1787718383
8 changed files with 526 additions and 2 deletions
+92
View File
@@ -0,0 +1,92 @@
#pragma once
#include "evo/biology/cellular_motion.hpp"
#include "evo/biology/cellular_uptake.hpp"
#include <compare>
#include <cstddef>
#include <cstdint>
#include <expected>
#include <memory>
#include <optional>
#include <span>
#include <utility>
#include <vector>
namespace evo::biology {
inline constexpr std::uint32_t cellular_patch_pitch_um = 1'000;
inline constexpr std::size_t max_cellular_patches = 4'096;
inline constexpr std::uint32_t cellular_habitat_format = 1;
inline constexpr std::size_t cellular_habitat_header_bytes = 72;
inline constexpr std::size_t cellular_patch_record_bytes = 56;
inline constexpr std::size_t max_cellular_habitat_bytes =
cellular_habitat_header_bytes + cellular_patch_record_bytes * max_cellular_patches + 8;
struct CellularPatchAddress {
std::uint32_t x_um{}, y_um{};
auto operator<=>(const CellularPatchAddress&) const = default;
};
struct CellularPatch {
CellularPatchAddress address;
std::uint64_t source{}; // Explicit region-opening provenance.
accounting::Inventory stock{};
auto operator<=>(const CellularPatch&) const = default;
};
struct CellularHabitatConfig {
std::uint64_t world{}, region{}, frame{};
std::uint32_t width_um{}, height_um{};
std::vector<CellularPatch> patches; // Strictly ordered by (x_um, y_um).
auto operator<=>(const CellularHabitatConfig&) const = default;
};
struct CellularPatchUptakeProposal {
CellularPatchAddress patch;
CellularUptakeProposal transfer;
auto operator<=>(const CellularPatchUptakeProposal&) const = default;
};
enum class CellularHabitatError : std::uint8_t {
invalid_config,
invalid_patch,
duplicate_patch,
overflow,
missing_patch,
uptake,
format,
version,
checksum,
identity
};
// Region-local finite food stock for the cellular trial. Empty/unlisted patches
// contain no stock. This is a read/proposal owner; the caller commits its patch
// and organism candidate together through the durable cellular state owner.
class CellularHabitat {
public:
[[nodiscard]] static std::expected<std::unique_ptr<CellularHabitat>, CellularHabitatError>
create(CellularHabitatConfig config);
CellularHabitat(const CellularHabitat&) = delete;
CellularHabitat& operator=(const CellularHabitat&) = delete;
CellularHabitat(CellularHabitat&&) = delete;
CellularHabitat& operator=(CellularHabitat&&) = delete;
~CellularHabitat();
[[nodiscard]] const CellularHabitatConfig& config() const noexcept;
[[nodiscard]] std::span<const CellularPatch> patches() const noexcept;
[[nodiscard]] accounting::Inventory totals() const noexcept;
[[nodiscard]] std::optional<std::size_t> find(CellularPatchAddress address) const noexcept;
[[nodiscard]] std::expected<CellularPatchUptakeProposal, CellularHabitatError>
propose_uptake(CellularPatchAddress address, accounting::Inventory reserve,
const MetabolicBudget& budget) const noexcept;
// Canonical little-endian envelope, FNV-1a64 checksum for corruption checks only.
[[nodiscard]] std::vector<std::uint8_t> snapshot() const;
[[nodiscard]] static std::expected<std::unique_ptr<CellularHabitat>, CellularHabitatError>
restore(std::span<const std::uint8_t> bytes, std::uint64_t world, std::uint64_t region,
std::uint64_t frame);
private:
CellularHabitat(CellularHabitatConfig config, accounting::Inventory totals)
: config_{std::move(config)}, totals_{totals} {}
CellularHabitatConfig config_;
accounting::Inventory totals_{};
};
} // namespace evo::biology