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)) {
|
||||
|
||||
Reference in New Issue
Block a user