Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (network) (push) Canceled after 0s
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 (client) (push) Canceled after 1m8s
Foundation / checks (checks) (push) Canceled after 1m13s
Foundation / checks (ecs) (push) Canceled after 1m4s
105 lines
4.2 KiB
C++
105 lines
4.2 KiB
C++
#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::uint32_t cellular_trial_perception_radius_um = 5'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;
|
|
};
|
|
// Client-safe view of an in-range patch. Provenance remains server-owned.
|
|
struct CellularPatchPerception {
|
|
CellularPatchAddress address;
|
|
accounting::Inventory available;
|
|
auto operator<=>(const CellularPatchPerception&) const = default;
|
|
};
|
|
enum class CellularHabitatError : std::uint8_t {
|
|
invalid_config,
|
|
invalid_patch,
|
|
duplicate_patch,
|
|
overflow,
|
|
missing_patch,
|
|
uptake,
|
|
outside_area,
|
|
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;
|
|
// Returns patch stock only within the fixed M11.3 sensor radius. Patches
|
|
// are visible when the organism-to-patch-square gap is within that radius.
|
|
[[nodiscard]] std::expected<std::vector<CellularPatchPerception>, CellularHabitatError>
|
|
perceive(CellularPosition position, std::uint32_t extent_um) const;
|
|
[[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
|