Cellular rules 8: pools and bubbles fed by contact (ADR-0136 step A)
Foundation / checks (checks) (push) Successful in 47m40s
Foundation / checks (client) (push) Successful in 17m27s
Foundation / checks (deploy) (push) Successful in 4m17s
Foundation / checks (ecs) (push) Successful in 4m31s
Foundation / checks (network) (push) Successful in 22m1s
Foundation / checks (tsan) (push) Successful in 13m10s
Foundation / checks (postgres) (push) Successful in 20m54s
Foundation / image (push) Successful in 4m15s
Foundation / postgres-tools (push) Successful in 5m32s
Foundation / server-image (push) Successful in 7m15s
Foundation / checks (checks) (push) Successful in 47m40s
Foundation / checks (client) (push) Successful in 17m27s
Foundation / checks (deploy) (push) Successful in 4m17s
Foundation / checks (ecs) (push) Successful in 4m31s
Foundation / checks (network) (push) Successful in 22m1s
Foundation / checks (tsan) (push) Successful in 13m10s
Foundation / checks (postgres) (push) Successful in 20m54s
Foundation / image (push) Successful in 4m15s
Foundation / postgres-tools (push) Successful in 5m32s
Foundation / server-image (push) Successful in 7m15s
Square patches invited camping and left the space between them empty. Rules 8 run on a format-2 habitat of deposits: - pools and single-nutrient bubbles at free micrometre centres, each with a stable id and a contact radius; the cellular-v7 opening scatters bubbles reproducibly from the genesis commit (three in ten in loose clumps) and lists pools with --pool; - every cell drains what it touches, up to its intake rate per tick, with no target; a pool gives at most a quarter of that rate; an emptied deposit disappears; - a dead cell's detritus breaks into one bubble per non-empty channel; - wildlife swims to the nearest non-empty deposit and feeds by touch. Bundle v9 (habitat 2) and schema v43 admit it. Session v11 carries the owner's 512 nearest deposits (id, centre, radius, shape, a drained flag and 16-bit amount codes) instead of patches; every other cellular message is v10's. The client asks for v11, draws bubbles (nutrient-coloured, pulsing while drained, popping when gone) and pools as puddles, slows near food and sends no feed target; the HUD summarises food in range. Earlier rules, regions and v10 clients are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -25,6 +25,21 @@ bool add(accounting::Inventory value, accounting::Inventory& total) noexcept {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Squared distance from a centre to the nearest point of a square footprint.
|
||||
std::uint64_t square_gap_squared(CellularPosition position, std::uint32_t extent_um,
|
||||
CellularPatchAddress centre) noexcept {
|
||||
const auto axis = [](std::uint64_t start, std::uint32_t extent, std::uint64_t point) {
|
||||
if (point < start)
|
||||
return start - point;
|
||||
if (point > start + extent)
|
||||
return point - (start + extent);
|
||||
return std::uint64_t{};
|
||||
};
|
||||
const auto dx = axis(position.x_um, extent_um, centre.x_um);
|
||||
const auto dy = axis(position.y_um, extent_um, centre.y_um);
|
||||
return dx * dx + dy * dy;
|
||||
}
|
||||
|
||||
std::uint64_t checksum(std::span<const std::uint8_t> bytes) noexcept {
|
||||
std::uint64_t value = 14695981039346656037ULL;
|
||||
for (const auto byte : bytes)
|
||||
@@ -81,17 +96,33 @@ CellularHabitat::create(CellularHabitatConfig config) {
|
||||
config.height_um > cellular_motion_axis_limit_um ||
|
||||
config.width_um % cellular_patch_pitch_um != 0 ||
|
||||
config.height_um % cellular_patch_pitch_um != 0 ||
|
||||
config.patches.size() > max_cellular_patches)
|
||||
config.patches.size() > max_cellular_patches ||
|
||||
(config.format != cellular_habitat_format &&
|
||||
config.format != cellular_habitat_deposit_format) ||
|
||||
(config.format == cellular_habitat_format && config.next_deposit != 0))
|
||||
return std::unexpected{CellularHabitatError::invalid_config};
|
||||
const bool deposits = config.format == cellular_habitat_deposit_format;
|
||||
|
||||
accounting::Inventory totals{};
|
||||
std::vector<std::uint32_t> ids;
|
||||
ids.reserve(deposits ? config.patches.size() : 0);
|
||||
for (std::size_t index = 0; index < config.patches.size(); ++index) {
|
||||
const auto& patch = config.patches[index];
|
||||
if (patch.source == 0 || patch.address.x_um >= config.width_um ||
|
||||
patch.address.y_um >= config.height_um ||
|
||||
patch.address.x_um % cellular_patch_pitch_um != 0 ||
|
||||
patch.address.y_um % cellular_patch_pitch_um != 0)
|
||||
patch.address.y_um >= config.height_um)
|
||||
return std::unexpected{CellularHabitatError::invalid_patch};
|
||||
if (!deposits && (patch.address.x_um % cellular_patch_pitch_um != 0 ||
|
||||
patch.address.y_um % cellular_patch_pitch_um != 0 || patch.id != 0 ||
|
||||
patch.radius_um != 0 || patch.kind != CellularDepositKind::patch))
|
||||
return std::unexpected{CellularHabitatError::invalid_patch};
|
||||
// Format 2: an id below next_deposit, a radius and a kind (ADR-0136).
|
||||
if (deposits &&
|
||||
(patch.id == 0 || patch.id >= config.next_deposit || patch.radius_um == 0 ||
|
||||
patch.radius_um > max_cellular_deposit_radius_um ||
|
||||
(patch.kind != CellularDepositKind::pool && patch.kind != CellularDepositKind::mote)))
|
||||
return std::unexpected{CellularHabitatError::invalid_patch};
|
||||
if (deposits)
|
||||
ids.push_back(patch.id);
|
||||
if (index != 0 && !(config.patches[index - 1].address < patch.address))
|
||||
return std::unexpected{config.patches[index - 1].address == patch.address
|
||||
? CellularHabitatError::duplicate_patch
|
||||
@@ -99,6 +130,9 @@ CellularHabitat::create(CellularHabitatConfig config) {
|
||||
if (!add(patch.stock, totals))
|
||||
return std::unexpected{CellularHabitatError::overflow};
|
||||
}
|
||||
std::ranges::sort(ids);
|
||||
if (std::ranges::adjacent_find(ids) != ids.end())
|
||||
return std::unexpected{CellularHabitatError::duplicate_patch};
|
||||
return std::unique_ptr<CellularHabitat>{new CellularHabitat{std::move(config), totals}};
|
||||
}
|
||||
|
||||
@@ -131,6 +165,16 @@ CellularHabitat::perceive(CellularPosition position, std::uint32_t extent_um) co
|
||||
return std::unexpected{CellularHabitatError::outside_area};
|
||||
|
||||
std::vector<CellularPatchPerception> visible;
|
||||
if (config_.format == cellular_habitat_deposit_format) {
|
||||
// A deposit is visible when the gap from the footprint to its disc is within range.
|
||||
for (const auto& patch : config_.patches) {
|
||||
const auto reach = std::uint64_t{cellular_trial_perception_radius_um} + patch.radius_um;
|
||||
if (square_gap_squared(position, extent_um, patch.address) <= reach * reach)
|
||||
visible.push_back(
|
||||
{patch.address, patch.stock, patch.id, patch.radius_um, patch.kind});
|
||||
}
|
||||
return visible;
|
||||
}
|
||||
for (const auto& patch : config_.patches) {
|
||||
const auto axis_gap = [](std::uint64_t body_start, std::uint32_t body_extent,
|
||||
std::uint32_t patch_start) {
|
||||
@@ -179,8 +223,7 @@ CellularHabitat::propose_uptake_from_stock(CellularPatchAddress address, Cellula
|
||||
const auto index = find(address);
|
||||
if (!index)
|
||||
return std::unexpected{CellularHabitatError::missing_patch};
|
||||
if (!cellular_footprints_contact(position, extent_um, {address.x_um, address.y_um},
|
||||
cellular_patch_pitch_um))
|
||||
if (!cellular_patch_contact(position, extent_um, config_.patches[*index]))
|
||||
return std::unexpected{CellularHabitatError::not_in_contact};
|
||||
const auto uptake = propose_cellular_uptake(patch_stock, reserve, budget);
|
||||
if (!uptake)
|
||||
@@ -194,20 +237,27 @@ std::vector<std::uint8_t> CellularHabitat::snapshot() const {
|
||||
cellular_patch_record_bytes * config_.patches.size() + 8);
|
||||
Writer out{bytes};
|
||||
out.put(habitat_magic);
|
||||
out.put(cellular_habitat_format, 4);
|
||||
const bool deposits = config_.format == cellular_habitat_deposit_format;
|
||||
out.put(config_.format, 4);
|
||||
out.put(habitat_rules, 4);
|
||||
out.put(config_.world);
|
||||
out.put(config_.region);
|
||||
out.put(config_.frame);
|
||||
out.put(config_.width_um, 4);
|
||||
out.put(config_.height_um, 4);
|
||||
out.put(cellular_patch_pitch_um, 4);
|
||||
out.put(deposits ? 0 : cellular_patch_pitch_um, 4);
|
||||
out.put(config_.patches.size(), 4);
|
||||
out.put(0); // Reserved.
|
||||
out.put(0); // Reserved.
|
||||
out.put(config_.next_deposit); // Reserved (zero) in format 1.
|
||||
out.put(0); // Reserved.
|
||||
for (const auto& patch : config_.patches) {
|
||||
out.put(patch.address.x_um, 4);
|
||||
out.put(patch.address.y_um, 4);
|
||||
if (deposits) {
|
||||
out.put(patch.id, 4);
|
||||
out.put(patch.radius_um, 2);
|
||||
out.put(static_cast<std::uint8_t>(patch.kind), 1);
|
||||
out.put(0, 1);
|
||||
}
|
||||
out.put(patch.source);
|
||||
out.inventory(patch.stock);
|
||||
}
|
||||
@@ -227,9 +277,13 @@ CellularHabitat::restore(std::span<const std::uint8_t> bytes, std::uint64_t worl
|
||||
Reader in{bytes};
|
||||
if (in.get() != habitat_magic)
|
||||
return std::unexpected{CellularHabitatError::format};
|
||||
if (in.get(4) != cellular_habitat_format || in.get(4) != habitat_rules)
|
||||
const auto format = in.get(4);
|
||||
if ((format != cellular_habitat_format && format != cellular_habitat_deposit_format) ||
|
||||
in.get(4) != habitat_rules)
|
||||
return std::unexpected{CellularHabitatError::version};
|
||||
const bool deposits = format == cellular_habitat_deposit_format;
|
||||
CellularHabitatConfig config;
|
||||
config.format = static_cast<std::uint32_t>(format);
|
||||
config.world = in.get();
|
||||
config.region = in.get();
|
||||
config.frame = in.get();
|
||||
@@ -237,17 +291,29 @@ CellularHabitat::restore(std::span<const std::uint8_t> bytes, std::uint64_t worl
|
||||
return std::unexpected{CellularHabitatError::identity};
|
||||
config.width_um = static_cast<std::uint32_t>(in.get(4));
|
||||
config.height_um = static_cast<std::uint32_t>(in.get(4));
|
||||
if (in.get(4) != cellular_patch_pitch_um)
|
||||
if (in.get(4) != (deposits ? 0U : cellular_patch_pitch_um))
|
||||
return std::unexpected{CellularHabitatError::version};
|
||||
const auto count = in.get(4);
|
||||
if (in.get() != 0 || in.get() != 0 || count > max_cellular_patches ||
|
||||
bytes.size() != cellular_habitat_header_bytes + cellular_patch_record_bytes * count + 8)
|
||||
const auto next_deposit = in.get();
|
||||
const auto record = deposits ? cellular_deposit_record_bytes : cellular_patch_record_bytes;
|
||||
if (in.get() != 0 || count > max_cellular_patches ||
|
||||
next_deposit > std::numeric_limits<std::uint32_t>::max() ||
|
||||
bytes.size() != cellular_habitat_header_bytes + record * count + 8)
|
||||
return std::unexpected{CellularHabitatError::format};
|
||||
config.next_deposit = static_cast<std::uint32_t>(next_deposit);
|
||||
config.patches.reserve(static_cast<std::size_t>(count));
|
||||
for (std::uint64_t index = 0; index < count; ++index) {
|
||||
CellularPatch patch{};
|
||||
patch.address.x_um = static_cast<std::uint32_t>(in.get(4));
|
||||
patch.address.y_um = static_cast<std::uint32_t>(in.get(4));
|
||||
if (deposits) {
|
||||
patch.id = static_cast<std::uint32_t>(in.get(4));
|
||||
patch.radius_um = static_cast<std::uint32_t>(in.get(2));
|
||||
const auto kind = in.get(1);
|
||||
if (kind > static_cast<std::uint8_t>(CellularDepositKind::mote) || in.get(1) != 0)
|
||||
return std::unexpected{CellularHabitatError::format};
|
||||
patch.kind = static_cast<CellularDepositKind>(kind);
|
||||
}
|
||||
patch.source = in.get();
|
||||
patch.stock = in.inventory();
|
||||
config.patches.push_back(patch);
|
||||
@@ -261,4 +327,95 @@ CellularHabitat::restore(std::span<const std::uint8_t> bytes, std::uint64_t worl
|
||||
return std::unexpected{CellularHabitatError::format};
|
||||
return std::move(*restored);
|
||||
}
|
||||
std::expected<CellularHabitatConfig, CellularHabitatError>
|
||||
scatter_deposits(CellularHabitatConfig base, std::span<const CellularPatch> pools,
|
||||
const CellularMoteField& field) {
|
||||
if (base.width_um == 0 || base.height_um == 0 || field.source == 0 ||
|
||||
pools.size() + field.count > max_cellular_patches)
|
||||
return std::unexpected{CellularHabitatError::invalid_config};
|
||||
std::vector<std::size_t> channels; // 0 energy, 1-4 matter
|
||||
if (field.amount.energy.value != 0)
|
||||
channels.push_back(0);
|
||||
for (std::size_t c = 0; c < field.amount.matter.size(); ++c)
|
||||
if (field.amount.matter[c].value != 0)
|
||||
channels.push_back(c + 1);
|
||||
if (field.count != 0 && channels.empty())
|
||||
return std::unexpected{CellularHabitatError::invalid_config};
|
||||
// splitmix64: a fixed, portable stream.
|
||||
std::uint64_t state = field.seed;
|
||||
const auto next = [&state] {
|
||||
state += 0x9E3779B97F4A7C15ULL;
|
||||
auto z = state;
|
||||
z = (z ^ (z >> 30U)) * 0xBF58476D1CE4E5B9ULL;
|
||||
z = (z ^ (z >> 27U)) * 0x94D049BB133111EBULL;
|
||||
return z ^ (z >> 31U);
|
||||
};
|
||||
const auto below = [&](std::uint64_t bound) { return bound == 0 ? 0 : next() % bound; };
|
||||
std::vector<CellularPatch> deposits;
|
||||
deposits.reserve(pools.size() + field.count);
|
||||
for (auto pool : pools) {
|
||||
pool.kind = CellularDepositKind::pool;
|
||||
deposits.push_back(pool);
|
||||
}
|
||||
const auto clumps = std::max<std::uint32_t>(1, field.count / 40);
|
||||
std::vector<CellularPatchAddress> centres;
|
||||
centres.reserve(clumps);
|
||||
for (std::uint32_t i = 0; i < clumps; ++i)
|
||||
centres.push_back({static_cast<std::uint32_t>(below(base.width_um)),
|
||||
static_cast<std::uint32_t>(below(base.height_um))});
|
||||
constexpr std::int64_t spread = 600; // Clump half-width (tunable).
|
||||
for (std::uint32_t i = 0; i < field.count; ++i) {
|
||||
std::int64_t x{}, y{};
|
||||
if (below(10) < 3) {
|
||||
const auto& centre = centres[below(centres.size())];
|
||||
// Two summed uniforms: denser towards the clump centre.
|
||||
x = std::int64_t{centre.x_um} + static_cast<std::int64_t>(below(spread + 1)) +
|
||||
static_cast<std::int64_t>(below(spread + 1)) - spread;
|
||||
y = std::int64_t{centre.y_um} + static_cast<std::int64_t>(below(spread + 1)) +
|
||||
static_cast<std::int64_t>(below(spread + 1)) - spread;
|
||||
x = std::clamp<std::int64_t>(x, 0, std::int64_t{base.width_um} - 1);
|
||||
y = std::clamp<std::int64_t>(y, 0, std::int64_t{base.height_um} - 1);
|
||||
} else {
|
||||
x = static_cast<std::int64_t>(below(base.width_um));
|
||||
y = static_cast<std::int64_t>(below(base.height_um));
|
||||
}
|
||||
CellularPatch mote{};
|
||||
mote.address = {static_cast<std::uint32_t>(x), static_cast<std::uint32_t>(y)};
|
||||
mote.source = field.source;
|
||||
mote.radius_um = cellular_mote_radius_um;
|
||||
mote.kind = CellularDepositKind::mote;
|
||||
const auto channel = channels[i % channels.size()];
|
||||
if (channel == 0)
|
||||
mote.stock.energy = field.amount.energy;
|
||||
else
|
||||
mote.stock.matter[channel - 1] = field.amount.matter[channel - 1];
|
||||
deposits.push_back(mote);
|
||||
}
|
||||
// Unique addresses: a repeat moves east (wrapping) until free.
|
||||
std::ranges::sort(deposits, {}, &CellularPatch::address);
|
||||
for (std::size_t i = 1; i < deposits.size(); ++i)
|
||||
while (std::ranges::any_of(
|
||||
deposits.begin(), deposits.begin() + static_cast<std::ptrdiff_t>(i),
|
||||
[&](const CellularPatch& other) { return other.address == deposits[i].address; }))
|
||||
deposits[i].address.x_um =
|
||||
deposits[i].address.x_um + 1 < base.width_um ? deposits[i].address.x_um + 1 : 0;
|
||||
std::ranges::sort(deposits, {}, &CellularPatch::address);
|
||||
std::uint32_t id = 1;
|
||||
for (auto& deposit : deposits)
|
||||
deposit.id = id++;
|
||||
base.format = cellular_habitat_deposit_format;
|
||||
base.next_deposit = id;
|
||||
base.patches = std::move(deposits);
|
||||
return base;
|
||||
}
|
||||
|
||||
bool cellular_patch_contact(CellularPosition position, std::uint32_t extent_um,
|
||||
const CellularPatch& patch) noexcept {
|
||||
if (patch.kind == CellularDepositKind::patch)
|
||||
return cellular_footprints_contact(
|
||||
position, extent_um, {patch.address.x_um, patch.address.y_um}, cellular_patch_pitch_um);
|
||||
const auto radius = std::uint64_t{patch.radius_um};
|
||||
return extent_um != 0 &&
|
||||
square_gap_squared(position, extent_um, patch.address) <= radius * radius;
|
||||
}
|
||||
} // namespace evo::biology
|
||||
|
||||
@@ -3,7 +3,9 @@
|
||||
#include "evo/accounting/rates.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::biology {
|
||||
@@ -47,7 +49,12 @@ struct Deposit {
|
||||
std::uint64_t life_id{}, source{};
|
||||
CellularPosition position;
|
||||
accounting::Inventory inventory;
|
||||
std::uint32_t extent_um{};
|
||||
};
|
||||
// One channel of an inventory: 0 energy, 1-4 matter.
|
||||
std::uint64_t& channel_of(accounting::Inventory& value, std::size_t channel) noexcept {
|
||||
return channel == 0 ? value.energy.value : value.matter[channel - 1].value;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<CellularRegionStepCandidate, CellularRegionStepError>
|
||||
@@ -109,7 +116,8 @@ propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
|
||||
continue;
|
||||
}
|
||||
auto intent = life->intent;
|
||||
const auto target = intent.intake_target;
|
||||
// Contact feeding needs no target; one sent anyway is ignored (ADR-0136).
|
||||
const auto target = rules.contact_feeding ? std::nullopt : intent.intake_target;
|
||||
intent.intake_target.reset();
|
||||
intent.engulf = false;
|
||||
const auto step =
|
||||
@@ -129,7 +137,8 @@ propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
|
||||
return std::unexpected{CellularRegionStepError::overflow};
|
||||
candidate.heat_released.value += life->heat.energy.value;
|
||||
if (rules.engulf_and_detritus) {
|
||||
deposits.push_back({life->life_id, life->source, life->position, corpse.inventory});
|
||||
deposits.push_back({life->life_id, life->source, life->position, corpse.inventory,
|
||||
life->extent_um});
|
||||
corpse.inventory = {};
|
||||
}
|
||||
candidate.starved.push_back(corpse);
|
||||
@@ -160,6 +169,53 @@ propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
|
||||
}
|
||||
}
|
||||
}
|
||||
if (rules.contact_feeding) {
|
||||
// ADR-0136: drain every touched deposit (ascending address) up to the per-tick
|
||||
// intake limit; a pool gives at most a quarter of that limit.
|
||||
std::array<std::uint64_t, 5> limit{};
|
||||
for (std::size_t channel = 0; channel < limit.size(); ++channel) {
|
||||
const auto per_second = channel == 0
|
||||
? life->budget.intake_limit.energy.value
|
||||
: life->budget.intake_limit.matter[channel - 1].value;
|
||||
const auto per_tick = accounting::per_tick(
|
||||
per_second, cellular_motion_tick_microseconds, accounting::Rounding::down);
|
||||
if (!per_tick)
|
||||
return std::unexpected{CellularRegionStepError::overflow};
|
||||
limit.at(channel) = *per_tick;
|
||||
}
|
||||
accounting::Inventory acquired{};
|
||||
std::optional<std::size_t> first;
|
||||
for (std::size_t index = 0; index < patch_stock.size(); ++index) {
|
||||
const auto& deposit = habitat.patches()[index];
|
||||
if (!cellular_patch_contact(result.position, life->extent_um, deposit))
|
||||
continue;
|
||||
bool took = false;
|
||||
for (std::size_t channel = 0; channel < limit.size(); ++channel) {
|
||||
auto& stock = channel_of(patch_stock[index], channel);
|
||||
auto& gained = channel_of(acquired, channel);
|
||||
auto& reserve = channel_of(result.reserve_after, channel);
|
||||
auto allowance = limit.at(channel) - gained;
|
||||
if (deposit.kind == CellularDepositKind::pool)
|
||||
allowance = std::min(
|
||||
allowance, limit.at(channel) * cellular_pool_yield_per_thousand / 1000);
|
||||
const auto take = std::min({stock, allowance, u64_max - reserve});
|
||||
if (take == 0)
|
||||
continue;
|
||||
stock -= take;
|
||||
gained += take;
|
||||
reserve += take;
|
||||
took = true;
|
||||
}
|
||||
if (took && !first)
|
||||
first = index;
|
||||
}
|
||||
if (first) {
|
||||
result.intake_status = CellularIntakeStatus::acquired;
|
||||
result.intake = CellularPatchUptakeProposal{
|
||||
habitat.patches()[*first].address,
|
||||
{patch_stock[*first], result.reserve_after, acquired}};
|
||||
}
|
||||
}
|
||||
CellularLifeStepResult life_result{life->life_id, result};
|
||||
if (life->intent.engulf)
|
||||
life_result.engulf = rules.engulf_and_detritus ? CellularEngulfStatus::no_eligible_prey
|
||||
@@ -238,7 +294,8 @@ propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
|
||||
detritus.energy.value -= taken_in.energy.value;
|
||||
for (std::size_t channel = 0; channel < detritus.matter.size(); ++channel)
|
||||
detritus.matter[channel].value -= taken_in.matter[channel].value;
|
||||
deposits.push_back({prey->life_id, prey->source, prey_step.position, detritus});
|
||||
deposits.push_back(
|
||||
{prey->life_id, prey->source, prey_step.position, detritus, prey->extent_um});
|
||||
CellularCorpseCandidate corpse{prey->life_id, prey_step.position, {}};
|
||||
corpse.cause = CellularDeathCause::engulfed;
|
||||
corpse.predator_id = hunter->life_id;
|
||||
@@ -258,8 +315,54 @@ propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
|
||||
std::ranges::sort(candidate.starved, {}, &CellularCorpseCandidate::life_id);
|
||||
|
||||
// Detritus: each dead life's inventory goes to the patch at its cell, a new patch at
|
||||
// an empty cell, or the nearest patch once the habitat is full.
|
||||
// an empty cell, or the nearest patch once the habitat is full. Under contact
|
||||
// feeding (ADR-0136) it breaks into one bubble per non-empty channel instead.
|
||||
std::ranges::sort(deposits, {}, &Deposit::life_id);
|
||||
if (rules.contact_feeding) {
|
||||
const auto taken_address = [&](CellularPatchAddress address) {
|
||||
if (habitat.find(address))
|
||||
return true;
|
||||
const auto made = std::ranges::lower_bound(candidate.new_patches, address, {},
|
||||
&CellularPatch::address);
|
||||
return made != candidate.new_patches.end() && made->address == address;
|
||||
};
|
||||
std::vector<Deposit> overflow;
|
||||
for (const auto& deposit : deposits) {
|
||||
const auto centre_x = deposit.position.x_um + deposit.extent_um / 2;
|
||||
const auto centre_y = deposit.position.y_um + deposit.extent_um / 2;
|
||||
for (std::size_t channel = 0; channel < 5; ++channel) {
|
||||
auto inventory = deposit.inventory;
|
||||
const auto amount = channel_of(inventory, channel);
|
||||
if (amount == 0)
|
||||
continue;
|
||||
accounting::Inventory single{};
|
||||
channel_of(single, channel) = amount;
|
||||
if (habitat.patches().size() + candidate.new_patches.size() >=
|
||||
max_cellular_patches) {
|
||||
overflow.push_back({deposit.life_id, deposit.source, deposit.position,
|
||||
single, deposit.extent_um});
|
||||
continue;
|
||||
}
|
||||
// A small ring around the corpse's centre; nudged east past any taken spot.
|
||||
const std::int64_t offset = (static_cast<std::int64_t>(channel) - 2) *
|
||||
std::int64_t{cellular_mote_radius_um};
|
||||
auto x = static_cast<std::int64_t>(centre_x) + offset;
|
||||
x = std::clamp<std::int64_t>(x, 0,
|
||||
static_cast<std::int64_t>(area.width_um) - 1);
|
||||
CellularPatchAddress address{static_cast<std::uint32_t>(x),
|
||||
static_cast<std::uint32_t>(std::min<std::uint64_t>(
|
||||
centre_y, area.height_um - 1))};
|
||||
while (taken_address(address))
|
||||
address.x_um = address.x_um + 1 < area.width_um ? address.x_um + 1 : 0;
|
||||
const auto at = std::ranges::lower_bound(candidate.new_patches, address, {},
|
||||
&CellularPatch::address);
|
||||
candidate.new_patches.insert(at, CellularPatch{address, deposit.source, single,
|
||||
0, cellular_mote_radius_um,
|
||||
CellularDepositKind::mote});
|
||||
}
|
||||
}
|
||||
deposits = std::move(overflow); // A full habitat: the nearest deposit takes it.
|
||||
}
|
||||
for (const auto& deposit : deposits) {
|
||||
const auto address = cell_of(deposit.position);
|
||||
if (const auto index = habitat.find(address)) {
|
||||
|
||||
@@ -189,7 +189,7 @@ RegionSessionClient::connect(RegionSessionClientConfig config, Clock::time_point
|
||||
config.protocol_version != network::session_protocol_v7 &&
|
||||
config.protocol_version != network::session_protocol_v8 &&
|
||||
config.protocol_version != network::session_protocol_v9 &&
|
||||
config.protocol_version != network::session_protocol_v10)
|
||||
!network::cellular_v10_family(config.protocol_version))
|
||||
return std::unexpected(network::quic::EndpointError::configuration);
|
||||
auto endpoint = network::quic::Endpoint::connect(config.transport);
|
||||
if (!endpoint)
|
||||
@@ -297,7 +297,7 @@ bool RegionSessionClient::send_cellular_action(
|
||||
return (m.protocol_version == network::session_protocol_v7 ||
|
||||
m.protocol_version == network::session_protocol_v8 ||
|
||||
m.protocol_version == network::session_protocol_v9 ||
|
||||
m.protocol_version == network::session_protocol_v10) &&
|
||||
network::cellular_v10_family(m.protocol_version)) &&
|
||||
m.state == RegionSessionState::established && m.welcome && action_id != 0 && tick != 0 &&
|
||||
m.send(network::CellularAction{m.welcome->session, m.welcome->epoch, action_id, tick,
|
||||
move_x, move_y, intake_target.has_value(),
|
||||
@@ -311,7 +311,7 @@ bool RegionSessionClient::send_cellular_mutation(std::uint64_t action_id, std::u
|
||||
std::uint64_t offer_tick,
|
||||
std::uint16_t operator_id) {
|
||||
auto& m = *impl_;
|
||||
return m.protocol_version == network::session_protocol_v10 &&
|
||||
return network::cellular_v10_family(m.protocol_version) &&
|
||||
m.state == RegionSessionState::established && m.welcome && action_id != 0 &&
|
||||
offer_tick != 0 && tick > offer_tick &&
|
||||
m.send(network::CellularMutationAction{m.welcome->session, m.welcome->epoch, action_id,
|
||||
@@ -325,7 +325,7 @@ bool RegionSessionClient::query_action(std::uint64_t action_id) {
|
||||
m.protocol_version == network::session_protocol_v7 ||
|
||||
m.protocol_version == network::session_protocol_v8 ||
|
||||
m.protocol_version == network::session_protocol_v9 ||
|
||||
m.protocol_version == network::session_protocol_v10) &&
|
||||
network::cellular_v10_family(m.protocol_version)) &&
|
||||
m.state == RegionSessionState::established && m.send(network::ActionQuery{action_id});
|
||||
}
|
||||
|
||||
@@ -334,7 +334,7 @@ bool RegionSessionClient::request_primitive_life(std::uint64_t request_id) {
|
||||
return (m.protocol_version == network::session_protocol_v5 ||
|
||||
m.protocol_version == network::session_protocol_v8 ||
|
||||
m.protocol_version == network::session_protocol_v9 ||
|
||||
m.protocol_version == network::session_protocol_v10) &&
|
||||
network::cellular_v10_family(m.protocol_version)) &&
|
||||
m.state == RegionSessionState::established && m.welcome && request_id != 0 &&
|
||||
m.send(network::PrimitiveLifeRequest{m.welcome->session, m.welcome->epoch, request_id});
|
||||
}
|
||||
@@ -344,7 +344,7 @@ bool RegionSessionClient::query_primitive_life(std::uint64_t request_id) {
|
||||
return (m.protocol_version == network::session_protocol_v5 ||
|
||||
m.protocol_version == network::session_protocol_v8 ||
|
||||
m.protocol_version == network::session_protocol_v9 ||
|
||||
m.protocol_version == network::session_protocol_v10) &&
|
||||
network::cellular_v10_family(m.protocol_version)) &&
|
||||
m.state == RegionSessionState::established && request_id != 0 &&
|
||||
m.send(network::PrimitiveLifeQuery{request_id});
|
||||
}
|
||||
@@ -383,7 +383,7 @@ bool RegionSessionClient::set_interest(const network::SessionInterest& interest)
|
||||
m.protocol_version == network::session_protocol_v7 ||
|
||||
m.protocol_version == network::session_protocol_v8 ||
|
||||
m.protocol_version == network::session_protocol_v9 ||
|
||||
m.protocol_version == network::session_protocol_v10) &&
|
||||
network::cellular_v10_family(m.protocol_version)) &&
|
||||
m.state == RegionSessionState::established && m.send(interest);
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,8 @@ constexpr std::size_t cellular_bud_cost_bytes = std::size_t{5} * 8;
|
||||
constexpr std::size_t cellular_perception_v10_prefix_bytes =
|
||||
cellular_perception_v9_prefix_bytes + cellular_bud_cost_bytes;
|
||||
constexpr std::size_t cellular_perceived_patch_bytes = 8 + 8 + (4 * 8);
|
||||
// v11: id, centre, radius, shape, drained flag and five 16-bit amount codes.
|
||||
constexpr std::size_t cellular_perceived_deposit_bytes = 4 + 4 + 4 + 2 + 1 + 1 + (5 * 2);
|
||||
constexpr std::size_t intention_receipt_bytes = 8 + 1 + 8;
|
||||
constexpr std::size_t simulated_bytes = std::size_t{3} * 8;
|
||||
constexpr std::size_t action_bytes = (std::size_t{3} * 8) + 2 + 8;
|
||||
@@ -171,7 +173,7 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message, std::uin
|
||||
if (version != session_protocol_version && version != session_protocol_v5 &&
|
||||
version != session_protocol_v6 && version != session_protocol_v7 &&
|
||||
version != session_protocol_v8 && version != session_protocol_v9 &&
|
||||
version != session_protocol_v10)
|
||||
!cellular_v10_family(version))
|
||||
return {};
|
||||
if (version == session_protocol_version &&
|
||||
(std::holds_alternative<PrimitiveLifeRequest>(message) ||
|
||||
@@ -182,27 +184,27 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message, std::uin
|
||||
if (version != session_protocol_v6 && std::holds_alternative<CellularIntention>(message))
|
||||
return {};
|
||||
if (version != session_protocol_v7 && version != session_protocol_v8 &&
|
||||
version != session_protocol_v9 && version != session_protocol_v10 &&
|
||||
version != session_protocol_v9 && !cellular_v10_family(version) &&
|
||||
std::holds_alternative<CellularAction>(message))
|
||||
return {};
|
||||
if (version != session_protocol_v7 && version != session_protocol_v8 &&
|
||||
version != session_protocol_v9 && version != session_protocol_v10 &&
|
||||
version != session_protocol_v9 && !cellular_v10_family(version) &&
|
||||
std::holds_alternative<ActionOutcome>(message) &&
|
||||
std::get<ActionOutcome>(message).result == ActionResult::committed)
|
||||
return {};
|
||||
if (version != session_protocol_v10 && std::holds_alternative<CellularMutationAction>(message))
|
||||
if (!cellular_v10_family(version) && std::holds_alternative<CellularMutationAction>(message))
|
||||
return {};
|
||||
if (const auto* offer = std::get_if<CellularMutationOffer>(&message);
|
||||
offer && (version != session_protocol_v10 || !valid_offer(*offer)))
|
||||
offer && (!cellular_v10_family(version) || !valid_offer(*offer)))
|
||||
return {};
|
||||
if (const auto* ended = std::get_if<CellularLifeEnded>(&message);
|
||||
ended && (version != session_protocol_v10 || ended->tick == 0 ||
|
||||
ended->life_identity == 0 || ended->cause > CellularDeathReason::engulfed))
|
||||
ended && (!cellular_v10_family(version) || ended->tick == 0 || ended->life_identity == 0 ||
|
||||
ended->cause > CellularDeathReason::engulfed))
|
||||
return {};
|
||||
// v9 has no room for engulf and bud effects or the bud cost; only v10 reports them
|
||||
// (ADR-0129).
|
||||
if (const auto* perception = std::get_if<CellularPerception>(&message);
|
||||
perception && version != session_protocol_v10 && perception->diagnostics &&
|
||||
perception && !cellular_v10_family(version) && perception->diagnostics &&
|
||||
(perception->diagnostics->engulf != CellularEngulfResult::not_reported ||
|
||||
perception->diagnostics->bud != CellularBudOutcome::not_reported ||
|
||||
perception->diagnostics->bud_cost_energy_pj != 0 ||
|
||||
@@ -211,17 +213,35 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message, std::uin
|
||||
return {};
|
||||
if (const auto* action = std::get_if<CellularAction>(&message);
|
||||
action && (action->engulf || action->bud) &&
|
||||
(version != session_protocol_v10 || action->has_intake_target || action->intake_x_um ||
|
||||
(!cellular_v10_family(version) || action->has_intake_target || action->intake_x_um ||
|
||||
action->intake_y_um || (action->engulf && action->bud)))
|
||||
return {};
|
||||
if (version != session_protocol_v8 && version != session_protocol_v9 &&
|
||||
version != session_protocol_v10 && std::holds_alternative<CellularPerception>(message))
|
||||
!cellular_v10_family(version) && std::holds_alternative<CellularPerception>(message))
|
||||
return {};
|
||||
if (const auto* perception = std::get_if<CellularPerception>(&message)) {
|
||||
// v11 (ADR-0136) lists deposits by ascending id, never patches; earlier versions the
|
||||
// reverse.
|
||||
if (version == session_protocol_v11
|
||||
? !perception->patches.empty() ||
|
||||
perception->deposits.size() > max_cellular_perceived_deposits ||
|
||||
(!perception->life_identity && !perception->deposits.empty()) ||
|
||||
std::ranges::adjacent_find(perception->deposits,
|
||||
[](const auto& a, const auto& b) {
|
||||
return a.id >= b.id;
|
||||
}) != perception->deposits.end() ||
|
||||
std::ranges::any_of(perception->deposits,
|
||||
[](const auto& d) {
|
||||
return d.id == 0 ||
|
||||
(d.shape != CellularDepositShape::pool &&
|
||||
d.shape != CellularDepositShape::bubble);
|
||||
})
|
||||
: !perception->deposits.empty())
|
||||
return {};
|
||||
if (!perception->tick || perception->patches.size() > max_cellular_perceived_patches ||
|
||||
(!perception->life_identity && !perception->patches.empty()) ||
|
||||
(version == session_protocol_v8 && perception->diagnostics) ||
|
||||
((version == session_protocol_v9 || version == session_protocol_v10) &&
|
||||
((version == session_protocol_v9 || cellular_v10_family(version)) &&
|
||||
perception->life_identity && !perception->diagnostics) ||
|
||||
(perception->life_identity == 0 && perception->diagnostics) ||
|
||||
(perception->diagnostics && !valid_diagnostics(*perception->diagnostics)) ||
|
||||
@@ -344,15 +364,16 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message, std::uin
|
||||
std::vector<std::uint8_t> operator()(const CellularPerception& m) const {
|
||||
auto out =
|
||||
header(SessionMessageType::cellular_perception,
|
||||
(version == session_protocol_v10 ? cellular_perception_v10_prefix_bytes
|
||||
(cellular_v10_family(version) ? cellular_perception_v10_prefix_bytes
|
||||
: version == session_protocol_v9 ? cellular_perception_v9_prefix_bytes
|
||||
: cellular_perception_prefix_bytes) +
|
||||
m.patches.size() * cellular_perceived_patch_bytes,
|
||||
m.patches.size() * cellular_perceived_patch_bytes +
|
||||
m.deposits.size() * cellular_perceived_deposit_bytes,
|
||||
version);
|
||||
put(out, m.tick, 8);
|
||||
put(out, m.life_identity, 8);
|
||||
put(out, m.patches.size(), 2);
|
||||
if (version == session_protocol_v9 || version == session_protocol_v10) {
|
||||
put(out, version == session_protocol_v11 ? m.deposits.size() : m.patches.size(), 2);
|
||||
if (version == session_protocol_v9 || cellular_v10_family(version)) {
|
||||
const auto& d = m.diagnostics;
|
||||
put(out, d ? d->reserve_energy_pj : 0, 8);
|
||||
for (const auto value : d ? d->reserve_matter_fg : std::array<std::uint64_t, 4>{})
|
||||
@@ -364,7 +385,7 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message, std::uin
|
||||
out.push_back(d ? static_cast<std::uint8_t>(d->intake)
|
||||
: static_cast<std::uint8_t>(CellularIntakeResult::not_reported));
|
||||
out.push_back(d && d->has_tick_effects ? 1 : 0);
|
||||
if (version == session_protocol_v10) {
|
||||
if (cellular_v10_family(version)) {
|
||||
out.push_back(d ? static_cast<std::uint8_t>(d->engulf) : 0);
|
||||
out.push_back(d ? static_cast<std::uint8_t>(d->bud) : 0);
|
||||
put(out, 0, 3);
|
||||
@@ -383,6 +404,16 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message, std::uin
|
||||
for (const auto matter : patch.matter_fg)
|
||||
put(out, matter, 8);
|
||||
}
|
||||
for (const auto& deposit : m.deposits) {
|
||||
put(out, deposit.id, 4);
|
||||
put(out, deposit.x_um, 4);
|
||||
put(out, deposit.y_um, 4);
|
||||
put(out, deposit.radius_um, 2);
|
||||
out.push_back(static_cast<std::uint8_t>(deposit.shape));
|
||||
out.push_back(deposit.drained ? 1 : 0);
|
||||
for (const auto code : deposit.amount_codes)
|
||||
put(out, code, 2);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
std::vector<std::uint8_t> operator()(const IntentionReceipt& m) const {
|
||||
@@ -518,7 +549,7 @@ std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const
|
||||
if (wire_version != session_protocol_version && wire_version != session_protocol_v5 &&
|
||||
wire_version != session_protocol_v6 && wire_version != session_protocol_v7 &&
|
||||
wire_version != session_protocol_v8 && wire_version != session_protocol_v9 &&
|
||||
wire_version != session_protocol_v10)
|
||||
!cellular_v10_family(wire_version))
|
||||
return std::unexpected(SessionCodecError::unsupported_version);
|
||||
if (version)
|
||||
*version = wire_version;
|
||||
@@ -602,7 +633,7 @@ std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const
|
||||
}
|
||||
case static_cast<std::uint8_t>(SessionMessageType::cellular_action): {
|
||||
if (wire_version != session_protocol_v7 && wire_version != session_protocol_v8 &&
|
||||
wire_version != session_protocol_v9 && wire_version != session_protocol_v10)
|
||||
wire_version != session_protocol_v9 && !cellular_v10_family(wire_version))
|
||||
return std::unexpected(SessionCodecError::unsupported_version);
|
||||
if (!expect(cellular_action_bytes))
|
||||
return std::unexpected(SessionCodecError::wrong_length);
|
||||
@@ -614,7 +645,7 @@ std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const
|
||||
m.move_x = static_cast<std::int16_t>(static_cast<std::uint16_t>(get(payload, 32, 2)));
|
||||
m.move_y = static_cast<std::int16_t>(static_cast<std::uint16_t>(get(payload, 34, 2)));
|
||||
// Target kind: 0 none, 1 patch intake, 2 engulf and 3 bud (v10 only, ADR-0124/0125).
|
||||
if (payload[36] > (wire_version == session_protocol_v10 ? 3 : 1) || payload[37] != 0 ||
|
||||
if (payload[36] > (cellular_v10_family(wire_version) ? 3 : 1) || payload[37] != 0 ||
|
||||
payload[38] != 0 || payload[39] != 0)
|
||||
return std::unexpected(SessionCodecError::invalid_value);
|
||||
m.has_intake_target = payload[36] == 1;
|
||||
@@ -628,7 +659,7 @@ std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const
|
||||
return m;
|
||||
}
|
||||
case static_cast<std::uint8_t>(SessionMessageType::cellular_mutation_action): {
|
||||
if (wire_version != session_protocol_v10)
|
||||
if (!cellular_v10_family(wire_version))
|
||||
return std::unexpected(SessionCodecError::unsupported_version);
|
||||
if (!expect(cellular_mutation_action_bytes))
|
||||
return std::unexpected(SessionCodecError::wrong_length);
|
||||
@@ -649,7 +680,7 @@ std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const
|
||||
return m;
|
||||
}
|
||||
case static_cast<std::uint8_t>(SessionMessageType::cellular_life_ended): {
|
||||
if (wire_version != session_protocol_v10)
|
||||
if (!cellular_v10_family(wire_version))
|
||||
return std::unexpected(SessionCodecError::unsupported_version);
|
||||
if (!expect(cellular_life_ended_bytes))
|
||||
return std::unexpected(SessionCodecError::wrong_length);
|
||||
@@ -665,7 +696,7 @@ std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const
|
||||
return m;
|
||||
}
|
||||
case static_cast<std::uint8_t>(SessionMessageType::cellular_mutation_offer): {
|
||||
if (wire_version != session_protocol_v10)
|
||||
if (!cellular_v10_family(wire_version))
|
||||
return std::unexpected(SessionCodecError::unsupported_version);
|
||||
if (payload.size() < cellular_offer_prefix_bytes)
|
||||
return std::unexpected(SessionCodecError::wrong_length);
|
||||
@@ -702,7 +733,7 @@ std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const
|
||||
}
|
||||
case static_cast<std::uint8_t>(SessionMessageType::cellular_perception): {
|
||||
if (wire_version != session_protocol_v8 && wire_version != session_protocol_v9 &&
|
||||
wire_version != session_protocol_v10)
|
||||
!cellular_v10_family(wire_version))
|
||||
return std::unexpected(SessionCodecError::unsupported_version);
|
||||
if (payload.size() < cellular_perception_prefix_bytes)
|
||||
return std::unexpected(SessionCodecError::wrong_length);
|
||||
@@ -713,20 +744,23 @@ std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const
|
||||
const bool without_bud_cost = wire_version == session_protocol_v10 &&
|
||||
payload.size() == cellular_perception_v9_prefix_bytes +
|
||||
count * cellular_perceived_patch_bytes;
|
||||
const auto prefix = wire_version == session_protocol_v10 && !without_bud_cost
|
||||
const auto prefix = cellular_v10_family(wire_version) && !without_bud_cost
|
||||
? cellular_perception_v10_prefix_bytes
|
||||
: wire_version == session_protocol_v9 || without_bud_cost
|
||||
? cellular_perception_v9_prefix_bytes
|
||||
: cellular_perception_prefix_bytes;
|
||||
if (count > max_cellular_perceived_patches ||
|
||||
payload.size() != prefix + count * cellular_perceived_patch_bytes)
|
||||
const bool deposits = wire_version == session_protocol_v11;
|
||||
const auto entry =
|
||||
deposits ? cellular_perceived_deposit_bytes : cellular_perceived_patch_bytes;
|
||||
if (count > (deposits ? max_cellular_perceived_deposits : max_cellular_perceived_patches) ||
|
||||
payload.size() != prefix + count * entry)
|
||||
return std::unexpected(SessionCodecError::wrong_length);
|
||||
CellularPerception m;
|
||||
m.tick = get(payload, 0, 8);
|
||||
m.life_identity = get(payload, 8, 8);
|
||||
if (!m.tick || (!m.life_identity && count))
|
||||
return std::unexpected(SessionCodecError::invalid_value);
|
||||
if (wire_version == session_protocol_v9 || wire_version == session_protocol_v10) {
|
||||
if (wire_version == session_protocol_v9 || cellular_v10_family(wire_version)) {
|
||||
CellularLifeDiagnostics d;
|
||||
d.reserve_energy_pj = get(payload, 18, 8);
|
||||
for (std::size_t channel = 0; channel < d.reserve_matter_fg.size(); ++channel)
|
||||
@@ -737,13 +771,13 @@ std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const
|
||||
static_cast<std::uint8_t>(CellularMovementResult::insufficient_energy) ||
|
||||
payload[75] > static_cast<std::uint8_t>(CellularIntakeResult::out_of_contact) ||
|
||||
payload[76] > 1 ||
|
||||
std::ranges::any_of(payload.subspan(wire_version == session_protocol_v10 ? 79 : 77,
|
||||
wire_version == session_protocol_v10 ? 3 : 5),
|
||||
std::ranges::any_of(payload.subspan(cellular_v10_family(wire_version) ? 79 : 77,
|
||||
cellular_v10_family(wire_version) ? 3 : 5),
|
||||
[](std::uint8_t value) { return value != 0; }) ||
|
||||
payload[77] > static_cast<std::uint8_t>(CellularEngulfResult::membrane_too_thick) ||
|
||||
payload[78] > static_cast<std::uint8_t>(CellularBudOutcome::no_placement))
|
||||
return std::unexpected(SessionCodecError::invalid_value);
|
||||
if (wire_version == session_protocol_v10) {
|
||||
if (cellular_v10_family(wire_version)) {
|
||||
d.engulf = static_cast<CellularEngulfResult>(payload[77]);
|
||||
d.bud = static_cast<CellularBudOutcome>(payload[78]);
|
||||
if (!without_bud_cost) {
|
||||
@@ -769,13 +803,37 @@ std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const
|
||||
[](std::uint64_t value) { return value != 0; }))
|
||||
return std::unexpected(SessionCodecError::invalid_value);
|
||||
}
|
||||
if ((wire_version == session_protocol_v9 || wire_version == session_protocol_v10) &&
|
||||
if ((wire_version == session_protocol_v9 || cellular_v10_family(wire_version)) &&
|
||||
m.life_identity && !m.diagnostics)
|
||||
return std::unexpected(SessionCodecError::invalid_value);
|
||||
if (m.diagnostics && !valid_diagnostics(*m.diagnostics))
|
||||
return std::unexpected(SessionCodecError::invalid_value);
|
||||
m.patches.reserve(count);
|
||||
std::size_t offset = prefix;
|
||||
if (deposits) {
|
||||
m.deposits.reserve(count);
|
||||
for (std::size_t i = 0; i < count; ++i, offset += entry) {
|
||||
CellularPerceivedDeposit d;
|
||||
d.id = static_cast<std::uint32_t>(get(payload, offset, 4));
|
||||
d.x_um = static_cast<std::uint32_t>(get(payload, offset + 4, 4));
|
||||
d.y_um = static_cast<std::uint32_t>(get(payload, offset + 8, 4));
|
||||
d.radius_um = static_cast<std::uint16_t>(get(payload, offset + 12, 2));
|
||||
const auto shape = payload[offset + 14];
|
||||
const auto drained = payload[offset + 15];
|
||||
if (d.id == 0 || drained > 1 ||
|
||||
(shape != static_cast<std::uint8_t>(CellularDepositShape::pool) &&
|
||||
shape != static_cast<std::uint8_t>(CellularDepositShape::bubble)) ||
|
||||
(!m.deposits.empty() && m.deposits.back().id >= d.id))
|
||||
return std::unexpected(SessionCodecError::invalid_value);
|
||||
d.shape = static_cast<CellularDepositShape>(shape);
|
||||
d.drained = drained == 1;
|
||||
for (std::size_t c = 0; c < d.amount_codes.size(); ++c)
|
||||
d.amount_codes.at(c) =
|
||||
static_cast<std::uint16_t>(get(payload, offset + 16 + c * 2, 2));
|
||||
m.deposits.push_back(d);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
m.patches.reserve(count);
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
CellularPerceivedPatch patch;
|
||||
patch.x_um = static_cast<std::uint32_t>(get(payload, offset, 4));
|
||||
@@ -830,7 +888,7 @@ std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const
|
||||
payload[8] > static_cast<std::uint8_t>(ActionResult::committed) ||
|
||||
(payload[8] == static_cast<std::uint8_t>(ActionResult::committed) &&
|
||||
wire_version != session_protocol_v7 && wire_version != session_protocol_v8 &&
|
||||
wire_version != session_protocol_v9 && wire_version != session_protocol_v10))
|
||||
wire_version != session_protocol_v9 && !cellular_v10_family(wire_version)))
|
||||
return std::unexpected(SessionCodecError::invalid_value);
|
||||
const ActionOutcome m{get(payload, 0, 8), static_cast<ActionResult>(payload[8]),
|
||||
get(payload, 9, 8), get(payload, 17, 8)};
|
||||
|
||||
@@ -168,6 +168,11 @@ provision_cellular_region(Postgres& owner, const FilesystemObjectStore& objects,
|
||||
// ADR-0128: a rules-5 region starts on the v6 bundle with genome revisions.
|
||||
manifest->cellular_versions = cellular_v6_versions;
|
||||
manifest->history_profile = cellular_revision_history_profile;
|
||||
} else if (manifest && opening.format_version() == server::cellular_lives_presence_format &&
|
||||
opening.rules_version() == server::cellular_lives_foraging_rules) {
|
||||
// ADR-0136 step A: a rules-8 region starts on the v9 bundle with genome revisions.
|
||||
manifest->cellular_versions = cellular_v9_versions;
|
||||
manifest->history_profile = cellular_revision_history_profile;
|
||||
} else if (manifest && opening.format_version() == server::cellular_lives_presence_format &&
|
||||
opening.rules_version() == server::cellular_lives_reproduction_rules) {
|
||||
// ADR-0137: a rules-7 region starts on the v8 bundle with genome revisions.
|
||||
|
||||
@@ -74,7 +74,8 @@ bool structural(const CheckpointManifest& m) noexcept {
|
||||
m.cellular_versions != cellular_v5_versions &&
|
||||
m.cellular_versions != cellular_v6_versions &&
|
||||
m.cellular_versions != cellular_v7_versions &&
|
||||
m.cellular_versions != cellular_v8_versions) ||
|
||||
m.cellular_versions != cellular_v8_versions &&
|
||||
m.cellular_versions != cellular_v9_versions) ||
|
||||
(cellular && m.versions != environmental_v1_versions))
|
||||
return false;
|
||||
if (primitive && (m.manifest_version != 2 || m.tick_microseconds != 50000 ||
|
||||
@@ -171,7 +172,8 @@ bool supported(const CheckpointManifest& m) noexcept {
|
||||
m.cellular_versions == cellular_v5_versions ||
|
||||
m.cellular_versions == cellular_v6_versions ||
|
||||
m.cellular_versions == cellular_v7_versions ||
|
||||
m.cellular_versions == cellular_v8_versions) &&
|
||||
m.cellular_versions == cellular_v8_versions ||
|
||||
m.cellular_versions == cellular_v9_versions) &&
|
||||
m.versions == environmental_v1_versions && m.tick_microseconds == 50000 &&
|
||||
m.rng_streams == 0 && m.durability == DurabilityPolicy::single_volume_development;
|
||||
}
|
||||
|
||||
@@ -535,7 +535,11 @@ CellularLives::create(const PrimitiveLives& opening,
|
||||
const auto world = nursery_config.world;
|
||||
const auto region = nursery_config.region;
|
||||
const auto frame = nursery_config.frame;
|
||||
const bool reproduction = profile == CellularLivesProfile::reproduction; // Rules 7.
|
||||
const bool foraging = profile == CellularLivesProfile::foraging; // Rules 8.
|
||||
// A rules-8 opening needs a deposit habitat, and only it may have one.
|
||||
if (foraging != (habitat->config().format == biology::cellular_habitat_deposit_format))
|
||||
return std::unexpected{CellularLivesError::habitat};
|
||||
const bool reproduction = profile == CellularLivesProfile::reproduction || foraging;
|
||||
const bool balance = profile == CellularLivesProfile::balance || reproduction; // Rules 6+.
|
||||
const bool presence = profile == CellularLivesProfile::presence || balance;
|
||||
const bool budding = profile == CellularLivesProfile::budding || presence;
|
||||
@@ -550,12 +554,13 @@ CellularLives::create(const PrimitiveLives& opening,
|
||||
: budding ? cellular_lives_bud_format
|
||||
: engulf ? cellular_lives_engulf_format
|
||||
: cellular_lives_format,
|
||||
reproduction ? cellular_lives_reproduction_rules
|
||||
: balance ? cellular_lives_balance_rules
|
||||
: presence ? cellular_lives_presence_rules
|
||||
: budding ? cellular_lives_bud_rules
|
||||
: engulf ? cellular_lives_engulf_rules
|
||||
: cellular_lives_rules);
|
||||
foraging ? cellular_lives_foraging_rules
|
||||
: reproduction ? cellular_lives_reproduction_rules
|
||||
: balance ? cellular_lives_balance_rules
|
||||
: presence ? cellular_lives_presence_rules
|
||||
: budding ? cellular_lives_bud_rules
|
||||
: engulf ? cellular_lives_engulf_rules
|
||||
: cellular_lives_rules);
|
||||
return std::unique_ptr<CellularLives>{new CellularLives{std::move(state)}};
|
||||
}
|
||||
|
||||
@@ -984,7 +989,8 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
format_version == cellular_lives_presence_format &&
|
||||
(rules_version == cellular_lives_presence_rules ||
|
||||
rules_version == cellular_lives_balance_rules ||
|
||||
rules_version == cellular_lives_reproduction_rules);
|
||||
rules_version == cellular_lives_reproduction_rules ||
|
||||
rules_version == cellular_lives_foraging_rules);
|
||||
// Formats 5 and 6 carry the bud sequence and parent lives (rules 4 and 5).
|
||||
const bool bud_version = presence_version || (magic == cellular_lives_v5_magic &&
|
||||
format_version == cellular_lives_bud_format &&
|
||||
@@ -1169,6 +1175,10 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
return std::unexpected{CellularLivesError::checksum};
|
||||
return std::unexpected{CellularLivesError::habitat};
|
||||
}
|
||||
// Rules 8 run on a deposit habitat, and nothing else does (ADR-0136).
|
||||
if ((rules_version >= cellular_lives_foraging_rules) !=
|
||||
((*habitat)->config().format == biology::cellular_habitat_deposit_format))
|
||||
return std::unexpected{CellularLivesError::habitat};
|
||||
auto archive = biology::LineageArchive::decode(archive_payload);
|
||||
if (!archive) {
|
||||
if (archive.error() == biology::ArchiveCodecError::version)
|
||||
@@ -1248,6 +1258,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
// Rules 4 keep rules 3 (engulfment, detritus) and add budding (ADR-0125).
|
||||
const bool engulf_rules = state_->rules_version >= cellular_lives_engulf_rules;
|
||||
const bool contact_rules = state_->rules_version >= cellular_lives_foraging_rules;
|
||||
const bool bud_rules = state_->rules_version >= cellular_lives_bud_rules;
|
||||
const bool presence_rules = state_->rules_version >= cellular_lives_presence_rules;
|
||||
for (std::size_t i = 0; i < ordered.size(); ++i) {
|
||||
@@ -1319,8 +1330,9 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
*membrane,
|
||||
life.source});
|
||||
}
|
||||
auto proposal = biology::propose_cellular_region_step(state_->config.tick, state_->config.area,
|
||||
inputs, *state_->habitat, {engulf_rules});
|
||||
auto proposal =
|
||||
biology::propose_cellular_region_step(state_->config.tick, state_->config.area, inputs,
|
||||
*state_->habitat, {engulf_rules, contact_rules});
|
||||
if (!proposal)
|
||||
return std::unexpected{CellularLivesError::region_step};
|
||||
|
||||
@@ -1606,13 +1618,25 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
found->stock = update.stock;
|
||||
}
|
||||
// Rules 3 detritus at empty cells becomes new patches; create() revalidates the map.
|
||||
for (const auto& made : proposal->new_patches) {
|
||||
// Format 2 (ADR-0136): new bubbles take the next deposit ids, and a deposit emptied this
|
||||
// tick is gone.
|
||||
const bool deposits = habitat_config.format == biology::cellular_habitat_deposit_format;
|
||||
for (auto made : proposal->new_patches) {
|
||||
const auto at = std::ranges::lower_bound(habitat_config.patches, made.address, {},
|
||||
[](const auto& patch) { return patch.address; });
|
||||
if (at != habitat_config.patches.end() && at->address == made.address)
|
||||
return std::unexpected{CellularLivesError::habitat};
|
||||
if (deposits) {
|
||||
if (habitat_config.next_deposit == std::numeric_limits<std::uint32_t>::max())
|
||||
return std::unexpected{CellularLivesError::overflow};
|
||||
made.id = habitat_config.next_deposit++;
|
||||
}
|
||||
habitat_config.patches.insert(at, made);
|
||||
}
|
||||
if (deposits)
|
||||
std::erase_if(habitat_config.patches, [](const biology::CellularPatch& patch) {
|
||||
return patch.stock == accounting::Inventory{};
|
||||
});
|
||||
auto next_habitat = biology::CellularHabitat::create(std::move(habitat_config));
|
||||
if (!next_habitat)
|
||||
return std::unexpected{CellularLivesError::habitat};
|
||||
|
||||
@@ -124,19 +124,34 @@ wildlife_intents(const CellularLives& state, std::uint64_t tick) {
|
||||
const auto centre_y = static_cast<std::int64_t>(life.position.y_um) + extent / 2;
|
||||
std::optional<biology::CellularPatchAddress> nearest;
|
||||
std::uint64_t nearest_distance{};
|
||||
// Rules 8 (ADR-0136): deposits are discs at their centres, any non-empty one is food,
|
||||
// and touching feeds; earlier rules seek energy patches and name them.
|
||||
const bool deposits = state.rules_version() >= cellular_lives_foraging_rules;
|
||||
const auto offset = deposits ? 0 : half_pitch;
|
||||
std::optional<biology::CellularPatchPerception> chosen;
|
||||
for (const auto& patch : *perceived) {
|
||||
if (patch.available.energy.value == 0)
|
||||
if (deposits ? patch.available == accounting::Inventory{}
|
||||
: patch.available.energy.value == 0)
|
||||
continue;
|
||||
const auto dx = std::int64_t{patch.address.x_um} + half_pitch - centre_x;
|
||||
const auto dy = std::int64_t{patch.address.y_um} + half_pitch - centre_y;
|
||||
const auto dx = std::int64_t{patch.address.x_um} + offset - centre_x;
|
||||
const auto dy = std::int64_t{patch.address.y_um} + offset - centre_y;
|
||||
const auto distance = static_cast<std::uint64_t>(dx * dx + dy * dy);
|
||||
// Perception is ordered by address, so the first of equal distances wins.
|
||||
if (!nearest || distance < nearest_distance) {
|
||||
nearest = patch.address;
|
||||
nearest_distance = distance;
|
||||
chosen = patch;
|
||||
}
|
||||
}
|
||||
if (nearest) {
|
||||
if (nearest && deposits && chosen) {
|
||||
biology::CellularPatch disc{};
|
||||
disc.address = chosen->address;
|
||||
disc.radius_um = chosen->radius_um;
|
||||
disc.kind = chosen->kind;
|
||||
if (!biology::cellular_patch_contact(life.position, extent, disc))
|
||||
intent.movement = wildlife_toward(std::int64_t{nearest->x_um} - centre_x,
|
||||
std::int64_t{nearest->y_um} - centre_y);
|
||||
} else if (nearest) {
|
||||
intent.intake_target = nearest;
|
||||
if (!overlaps(life.position.x_um, extent, nearest->x_um) ||
|
||||
!overlaps(life.position.y_um, extent, nearest->y_um))
|
||||
|
||||
@@ -153,7 +153,7 @@ struct RegionSessions::Impl {
|
||||
const network::SessionMessage& message) {
|
||||
for (const auto& c : connections) {
|
||||
if (c.state != State::established ||
|
||||
c.protocol_version != network::session_protocol_v10 || c.account_id != account)
|
||||
!network::cellular_v10_family(c.protocol_version) || c.account_id != account)
|
||||
continue;
|
||||
const auto bytes = network::encode_session(message, c.protocol_version);
|
||||
return !bytes.empty() && endpoint->send_message(c.id, bytes).has_value();
|
||||
@@ -447,7 +447,7 @@ struct RegionSessions::Impl {
|
||||
(c.protocol_version == network::session_protocol_v7 ||
|
||||
c.protocol_version == network::session_protocol_v8 ||
|
||||
c.protocol_version == network::session_protocol_v9 ||
|
||||
c.protocol_version == network::session_protocol_v10)))))
|
||||
network::cellular_v10_family(c.protocol_version))))))
|
||||
return close(c, SessionCloseReason::protocol, now);
|
||||
if (const auto* ack = std::get_if<network::SnapshotAck>(&*decoded))
|
||||
return replication.acknowledge(c.session, *ack);
|
||||
@@ -616,9 +616,9 @@ struct RegionSessions::Impl {
|
||||
((wire_version == network::session_protocol_v7 ||
|
||||
wire_version == network::session_protocol_v8 ||
|
||||
wire_version == network::session_protocol_v9 ||
|
||||
wire_version == network::session_protocol_v10) &&
|
||||
network::cellular_v10_family(wire_version)) &&
|
||||
!cellular_actions) ||
|
||||
wire_version > network::session_protocol_v10)
|
||||
wire_version > network::session_protocol_v11)
|
||||
return close(c, SessionCloseReason::unsupported_version, now);
|
||||
} else if (wire_version != c.protocol_version) {
|
||||
return close(c, SessionCloseReason::unsupported_version, now);
|
||||
@@ -644,14 +644,14 @@ struct RegionSessions::Impl {
|
||||
if (c.protocol_version != network::session_protocol_v7 &&
|
||||
c.protocol_version != network::session_protocol_v8 &&
|
||||
c.protocol_version != network::session_protocol_v9 &&
|
||||
c.protocol_version != network::session_protocol_v10)
|
||||
!network::cellular_v10_family(c.protocol_version))
|
||||
return close(c, SessionCloseReason::protocol, now);
|
||||
if (const auto receipt = intents.admit(c.session, *m, admission()))
|
||||
answer(c, *receipt);
|
||||
return;
|
||||
}
|
||||
if (const auto* m = std::get_if<network::CellularMutationAction>(&*decoded)) {
|
||||
if (c.protocol_version != network::session_protocol_v10 ||
|
||||
if (!network::cellular_v10_family(c.protocol_version) ||
|
||||
!config.select_cellular_mutation)
|
||||
return close(c, SessionCloseReason::protocol, now);
|
||||
CellularMutationAnswer answer{
|
||||
@@ -934,10 +934,15 @@ bool RegionSessions::send_cellular_perception(const network::AccountId& account,
|
||||
if (c.state != Impl::State::established ||
|
||||
(c.protocol_version != network::session_protocol_v8 &&
|
||||
c.protocol_version != network::session_protocol_v9 &&
|
||||
c.protocol_version != network::session_protocol_v10) ||
|
||||
!network::cellular_v10_family(c.protocol_version)) ||
|
||||
c.account_id != account)
|
||||
continue;
|
||||
auto delivered = perception;
|
||||
// v11 sees deposits (ADR-0136), earlier versions square patches; never both.
|
||||
if (c.protocol_version == network::session_protocol_v11)
|
||||
delivered.patches.clear();
|
||||
else
|
||||
delivered.deposits.clear();
|
||||
if (c.protocol_version == network::session_protocol_v8)
|
||||
delivered.diagnostics.reset();
|
||||
if (c.protocol_version == network::session_protocol_v9 && delivered.diagnostics) {
|
||||
@@ -975,7 +980,7 @@ void RegionSessions::clear_cellular_perceptions(std::uint64_t tick) {
|
||||
if (c.state == Impl::State::established &&
|
||||
(c.protocol_version == network::session_protocol_v8 ||
|
||||
c.protocol_version == network::session_protocol_v9 ||
|
||||
c.protocol_version == network::session_protocol_v10))
|
||||
network::cellular_v10_family(c.protocol_version)))
|
||||
m.send(c, cleared);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user