Phase 2 biology. CellularFlowField generates 2-n seeded closed looping currents inside the region, each with its own width, speed and direction, strongest at the centreline and still at the stream edges and walls, rasterized to a Q10 grid with integer bilinear sampling. The field digest matches across g++-14, clang-19 and MSVC, so clients regenerate it from the configuration. Habitat format 3 adds per-deposit induced drift, sub-micrometre carry and a merged count, with up to 8,192 deposits. advect_cellular_deposits moves bubbles with the flow plus drift from passing cells (falloff and decay), merges touching bubbles into the lower id up to the merged cap, keeps pools fixed and addresses unique. A 2,000-tick run of 1,800 bubbles conserves stock exactly and leaves nothing piled at the walls; it costs under 0.7 ms per tick optimized. The tuned grid pitch is capped at 10 mm. Refreshes the DEV session window. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
162 lines
8.0 KiB
C++
162 lines
8.0 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;
|
|
// ADR-0136 step A: format 2 holds deposits (pools and bubbles) at free micrometre centres,
|
|
// each with a stable id, a radius and a kind, and the next id to give a new deposit.
|
|
inline constexpr std::uint32_t cellular_habitat_deposit_format = 2;
|
|
// Format 3 (rules 12, Cellular_Region_Movement_Plan.md): format 2 deposits that move. Each
|
|
// record adds the deposit's induced drift, its sub-micrometre carry and how many scattered
|
|
// bubbles merged into it. Addresses stay unique: they change between ticks, never within one.
|
|
inline constexpr std::uint32_t cellular_habitat_liquid_format = 3;
|
|
inline constexpr std::size_t max_cellular_liquid_deposits = 8'192;
|
|
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 cellular_deposit_record_bytes = 64;
|
|
inline constexpr std::size_t cellular_liquid_record_bytes = cellular_deposit_record_bytes + 20;
|
|
inline constexpr std::size_t max_cellular_habitat_bytes =
|
|
cellular_habitat_header_bytes + cellular_liquid_record_bytes * max_cellular_liquid_deposits + 8;
|
|
// Carry below one micrometre, in Q10 velocity-microseconds (as CellularMotionState).
|
|
inline constexpr std::uint32_t cellular_deposit_carry_limit = 1'024U * 1'000'000U;
|
|
// A bubble's contact radius (tunable); pools carry their own.
|
|
inline constexpr std::uint32_t cellular_mote_radius_um = 30;
|
|
inline constexpr std::uint32_t max_cellular_deposit_radius_um = 5'000;
|
|
// A pool yields at most this share (per thousand) of a cell's intake rate per tick.
|
|
inline constexpr std::uint64_t cellular_pool_yield_per_thousand = 250;
|
|
enum class CellularDepositKind : std::uint8_t { patch, pool, mote };
|
|
|
|
struct CellularPatchAddress {
|
|
std::uint32_t x_um{}, y_um{};
|
|
auto operator<=>(const CellularPatchAddress&) const = default;
|
|
};
|
|
// Format 1: a 1 mm square patch at a pitch-aligned corner. Format 2: a deposit centred at
|
|
// `address`, a disc of `radius_um`; `id` is stable and only labels it for clients.
|
|
struct CellularPatch {
|
|
CellularPatchAddress address;
|
|
std::uint64_t source{}; // Explicit region-opening provenance.
|
|
accounting::Inventory stock{};
|
|
std::uint32_t id{}, radius_um{};
|
|
CellularDepositKind kind{};
|
|
// Format 3 only (zero otherwise): drift induced by passing cells (Q10 velocity), the
|
|
// sub-micrometre carry, and the scattered bubbles merged into this one (1 when unmerged).
|
|
std::int32_t drift_x_q10{}, drift_y_q10{};
|
|
std::uint32_t carry_x{}, carry_y{};
|
|
std::uint16_t merged{};
|
|
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).
|
|
std::uint32_t format{cellular_habitat_format};
|
|
std::uint32_t next_deposit{}; // Format 2: above every deposit id.
|
|
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;
|
|
std::uint32_t id{}, radius_um{}; // Format 2 deposits; zero for format 1 patches.
|
|
CellularDepositKind kind{};
|
|
auto operator<=>(const CellularPatchPerception&) const = default;
|
|
};
|
|
// Whether a cell's square footprint touches a patch (format 1 square) or a deposit's disc.
|
|
[[nodiscard]] bool cellular_patch_contact(CellularPosition position, std::uint32_t extent_um,
|
|
const CellularPatch& patch) noexcept;
|
|
enum class CellularHabitatError : std::uint8_t {
|
|
invalid_config,
|
|
invalid_patch,
|
|
duplicate_patch,
|
|
overflow,
|
|
missing_patch,
|
|
not_in_contact,
|
|
uptake,
|
|
outside_area,
|
|
format,
|
|
version,
|
|
checksum,
|
|
identity
|
|
};
|
|
|
|
// ADR-0136 step A opening: bubbles scattered over the whole area. Each holds `amount` on
|
|
// one channel (cycling through the channels `amount` stocks), three in ten of them in
|
|
// loose clumps; positions follow `seed` only, so a genesis is reproducible.
|
|
struct CellularMoteField {
|
|
std::uint32_t count{};
|
|
std::uint64_t source{};
|
|
accounting::Inventory amount{}; // Per bubble, per channel.
|
|
std::uint64_t seed{};
|
|
};
|
|
// A format-2 habitat of the given pools (centre, radius, source and stock; their ids and kind
|
|
// are assigned) and the scattered bubbles, sorted by address with ids 1..n.
|
|
[[nodiscard]] std::expected<CellularHabitatConfig, CellularHabitatError>
|
|
scatter_deposits(CellularHabitatConfig base, std::span<const CellularPatch> pools,
|
|
const CellularMoteField& field);
|
|
|
|
// 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, CellularPosition position, std::uint32_t extent_um,
|
|
accounting::Inventory reserve, const MetabolicBudget& budget) const noexcept;
|
|
// Same authoritative map/contact checks with stock supplied by a detached shared-tick
|
|
// candidate after earlier life claims have been resolved.
|
|
[[nodiscard]] std::expected<CellularPatchUptakeProposal, CellularHabitatError>
|
|
propose_uptake_from_stock(CellularPatchAddress address, CellularPosition position,
|
|
std::uint32_t extent_um, accounting::Inventory patch_stock,
|
|
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
|