Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 5s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m28s
Foundation / postgres-tools (push) Successful in 25s
Foundation / server-image (push) Successful in 4m3s
Foundation / image (push) Successful in 4m28s
Rules 12 lives carry a liquid input (motion state, handling, brake and the local flow sample) and step through propose_liquid_cellular_step: maintenance first, then one kinematic tick funded from what is left, so unfundable thrust coasts with the current. A format-3 habitat requires liquid input on every life and older habitats refuse it. Contact feeding sweeps each liquid cell's whole path so a fast cell cannot pass over food; frozen cells keep their motion; corpse bubbles in a format-3 habitat count one merged bubble and use the larger deposit limit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
435 lines
24 KiB
C++
435 lines
24 KiB
C++
#include "evo/biology/cellular_region_step.hpp"
|
|
|
|
#include "evo/accounting/rates.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <limits>
|
|
#include <optional>
|
|
#include <utility>
|
|
|
|
namespace evo::biology {
|
|
namespace {
|
|
constexpr auto u64_max = std::numeric_limits<std::uint64_t>::max();
|
|
|
|
bool add(std::uint64_t& to, std::uint64_t amount) noexcept {
|
|
if (amount > u64_max - to)
|
|
return false;
|
|
to += amount;
|
|
return true;
|
|
}
|
|
bool add(accounting::Inventory& to, const accounting::Inventory& from) noexcept {
|
|
if (!add(to.energy.value, from.energy.value))
|
|
return false;
|
|
for (std::size_t channel = 0; channel < to.matter.size(); ++channel)
|
|
if (!add(to.matter[channel].value, from.matter[channel].value))
|
|
return false;
|
|
return true;
|
|
}
|
|
// a * ka >= b * kb without overflow: compares the exact 128-bit products.
|
|
bool product_at_least(std::uint64_t a, std::uint64_t ka, std::uint64_t b, std::uint64_t kb) {
|
|
const auto wide = [](std::uint64_t x, std::uint64_t y) {
|
|
const std::uint64_t x0 = x & 0xFFFFFFFFU, x1 = x >> 32U;
|
|
const std::uint64_t y0 = y & 0xFFFFFFFFU, y1 = y >> 32U;
|
|
const std::uint64_t p00 = x0 * y0, p01 = x0 * y1, p10 = x1 * y0, p11 = x1 * y1;
|
|
const std::uint64_t middle = (p00 >> 32U) + (p01 & 0xFFFFFFFFU) + (p10 & 0xFFFFFFFFU);
|
|
const std::uint64_t high = p11 + (p01 >> 32U) + (p10 >> 32U) + (middle >> 32U);
|
|
const std::uint64_t low = (middle << 32U) | (p00 & 0xFFFFFFFFU);
|
|
return std::pair{high, low};
|
|
};
|
|
return wide(a, ka) >= wide(b, kb);
|
|
}
|
|
CellularPatchAddress cell_of(CellularPosition position) noexcept {
|
|
return {static_cast<std::uint32_t>(position.x_um / cellular_patch_pitch_um *
|
|
cellular_patch_pitch_um),
|
|
static_cast<std::uint32_t>(position.y_um / cellular_patch_pitch_um *
|
|
cellular_patch_pitch_um)};
|
|
}
|
|
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>
|
|
propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
|
|
std::span<const CellularLifeStepInput> lives,
|
|
const CellularHabitat& habitat, CellularRegionRules rules) {
|
|
if (begin_tick == std::numeric_limits<std::uint64_t>::max())
|
|
return std::unexpected{CellularRegionStepError::invalid_tick};
|
|
if (lives.size() > max_cellular_tick_lives)
|
|
return std::unexpected{CellularRegionStepError::too_many_lives};
|
|
|
|
const bool liquid = habitat.config().format == cellular_habitat_liquid_format;
|
|
const auto deposit_limit = liquid ? max_cellular_liquid_deposits : max_cellular_patches;
|
|
std::vector<const CellularLifeStepInput*> ordered;
|
|
ordered.reserve(lives.size());
|
|
for (const auto& life : lives) {
|
|
if (life.life_id == 0)
|
|
return std::unexpected{CellularRegionStepError::invalid_life_id};
|
|
// Rules 12: liquid motion in a format-3 habitat, for every life, and nowhere else.
|
|
if (life.liquid.has_value() != liquid ||
|
|
(liquid && (life.intent.intake_target || !rules.contact_feeding)))
|
|
return std::unexpected{CellularRegionStepError::invalid_intent};
|
|
if (life.intent.frozen &&
|
|
(life.intent.movement != CellularIntent{} || life.intent.intake_target ||
|
|
life.intent.engulf || life.intent.bud))
|
|
return std::unexpected{CellularRegionStepError::invalid_intent};
|
|
if (rules.engulf_and_detritus && (life.membrane_um == 0 || life.source == 0 ||
|
|
(life.intent.engulf && life.intent.intake_target)))
|
|
return std::unexpected{CellularRegionStepError::invalid_intent};
|
|
ordered.push_back(&life);
|
|
}
|
|
std::ranges::sort(ordered, {}, [](const auto* life) { return life->life_id; });
|
|
for (std::size_t i = 1; i < ordered.size(); ++i)
|
|
if (ordered[i - 1]->life_id == ordered[i]->life_id)
|
|
return std::unexpected{CellularRegionStepError::duplicate_life_id};
|
|
|
|
CellularRegionStepCandidate candidate{begin_tick, begin_tick + 1, {}, {}, {}, {}, {}};
|
|
candidate.lives.reserve(ordered.size());
|
|
candidate.starved.reserve(ordered.size());
|
|
std::vector<accounting::Inventory> patch_stock;
|
|
patch_stock.reserve(habitat.patches().size());
|
|
for (const auto& patch : habitat.patches())
|
|
patch_stock.push_back(patch.stock);
|
|
// Inputs of the lives still in candidate.lives, in the same order.
|
|
std::vector<const CellularLifeStepInput*> survivors;
|
|
survivors.reserve(ordered.size());
|
|
std::vector<Deposit> deposits;
|
|
|
|
for (const auto* life : ordered) {
|
|
if (life->body.energy.value != 0)
|
|
return std::unexpected{CellularRegionStepError::invalid_body};
|
|
if (std::ranges::any_of(life->heat.matter,
|
|
[](const auto& channel) { return channel.value != 0; }))
|
|
return std::unexpected{CellularRegionStepError::invalid_heat};
|
|
if (life->intent.frozen) {
|
|
// ADR-0128: a frozen cell is unchanged and pays nothing; it can still be prey.
|
|
CellularStepCandidate frozen{};
|
|
frozen.begin_tick = begin_tick;
|
|
frozen.end_tick = begin_tick + 1;
|
|
frozen.position = life->position;
|
|
frozen.reserve_after = life->reserve;
|
|
frozen.heat_after = life->heat;
|
|
if (life->liquid)
|
|
frozen.motion = life->liquid->motion; // Anchored while frozen.
|
|
candidate.lives.push_back({life->life_id, frozen});
|
|
survivors.push_back(life);
|
|
continue;
|
|
}
|
|
auto intent = life->intent;
|
|
// 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 =
|
|
life->liquid
|
|
? propose_liquid_cellular_step(begin_tick, area, life->position, life->extent_um,
|
|
intent.movement, *life->liquid, life->reserve,
|
|
life->heat, life->budget)
|
|
: propose_cellular_step(begin_tick, area, life->position, life->extent_um, intent,
|
|
life->reserve, life->heat, life->budget, habitat);
|
|
if (!step && step.error() == CellularStepError::insufficient_maintenance) {
|
|
CellularCorpseCandidate corpse{life->life_id, life->position, life->reserve};
|
|
for (std::size_t channel = 0; channel < corpse.inventory.matter.size(); ++channel) {
|
|
const auto body_matter = life->body.matter[channel].value;
|
|
const auto reserve_matter = life->reserve.matter[channel].value;
|
|
if (reserve_matter > std::numeric_limits<std::uint64_t>::max() - body_matter)
|
|
return std::unexpected{CellularRegionStepError::overflow};
|
|
corpse.inventory.matter[channel].value = body_matter + reserve_matter;
|
|
}
|
|
if (life->heat.energy.value >
|
|
std::numeric_limits<std::uint64_t>::max() - candidate.heat_released.value)
|
|
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,
|
|
life->extent_um});
|
|
corpse.inventory = {};
|
|
}
|
|
candidate.starved.push_back(corpse);
|
|
continue;
|
|
}
|
|
if (!step)
|
|
return std::unexpected{CellularRegionStepError::life_step};
|
|
auto result = *step;
|
|
if (target) {
|
|
const auto index = habitat.find(*target);
|
|
if (!index) {
|
|
result.intake_status = CellularIntakeStatus::missing;
|
|
} else {
|
|
const auto intake = habitat.propose_uptake_from_stock(
|
|
*target, result.position, life->extent_um, patch_stock[*index],
|
|
result.reserve_after, life->budget);
|
|
if (!intake) {
|
|
if (intake.error() == CellularHabitatError::not_in_contact) {
|
|
result.intake_status = CellularIntakeStatus::out_of_contact;
|
|
} else {
|
|
return std::unexpected{CellularRegionStepError::habitat};
|
|
}
|
|
} else {
|
|
patch_stock[*index] = intake->transfer.patch_after;
|
|
result.reserve_after = intake->transfer.reserve_after;
|
|
result.intake_status = CellularIntakeStatus::acquired;
|
|
result.intake = *intake;
|
|
}
|
|
}
|
|
}
|
|
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];
|
|
// Liquid motion sweeps its whole path, so a fast cell cannot skip food.
|
|
if (life->liquid
|
|
? !cellular_path_contact(life->position, result.position, life->extent_um,
|
|
deposit)
|
|
: !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
|
|
: CellularEngulfStatus::unsupported;
|
|
candidate.lives.push_back(life_result);
|
|
survivors.push_back(life);
|
|
}
|
|
|
|
if (rules.engulf_and_detritus) {
|
|
// Engulf phase (ADR-0124): ascending predator id; each takes at most the lowest-id
|
|
// eligible survivor in contact. A taken life neither acts nor is taken again.
|
|
std::vector<bool> taken(candidate.lives.size(), false);
|
|
for (std::size_t p = 0; p < candidate.lives.size(); ++p) {
|
|
if (taken[p] || !survivors[p]->intent.engulf)
|
|
continue;
|
|
auto& predator = candidate.lives[p];
|
|
const auto* hunter = survivors[p];
|
|
// Why nothing was taken, for the owner: no contact, too large, or membrane.
|
|
bool membrane_blocked = false;
|
|
predator.engulf = CellularEngulfStatus::no_contact;
|
|
for (std::size_t q = 0; q < candidate.lives.size(); ++q) {
|
|
if (q == p || taken[q])
|
|
continue;
|
|
const auto* prey = survivors[q];
|
|
const auto& prey_step = candidate.lives[q].candidate;
|
|
if (!cellular_footprints_contact(predator.candidate.position, hunter->extent_um,
|
|
prey_step.position, prey->extent_um))
|
|
continue;
|
|
if (!product_at_least(hunter->budget.phenotype.total_mass, 1'000,
|
|
prey->budget.phenotype.total_mass,
|
|
cellular_engulf_ratio_milli_per_membrane_um *
|
|
prey->membrane_um)) {
|
|
membrane_blocked =
|
|
membrane_blocked ||
|
|
product_at_least(hunter->budget.phenotype.total_mass, 1'000,
|
|
prey->budget.phenotype.total_mass,
|
|
cellular_engulf_ratio_milli_per_membrane_um);
|
|
predator.engulf = membrane_blocked ? CellularEngulfStatus::membrane_too_thick
|
|
: CellularEngulfStatus::prey_too_large;
|
|
continue;
|
|
}
|
|
// The prey's whole inventory moves once: body matter and its post-step reserve.
|
|
accounting::Inventory whole = prey_step.reserve_after;
|
|
if (!add(whole, prey->body))
|
|
return std::unexpected{CellularRegionStepError::overflow};
|
|
if (!add(candidate.heat_released.value, prey_step.heat_after.energy.value))
|
|
return std::unexpected{CellularRegionStepError::overflow};
|
|
// Capacity: what is left of the predator's per-tick intake limit this tick.
|
|
const auto remaining = [&](std::uint64_t per_second,
|
|
std::uint64_t already) -> std::optional<std::uint64_t> {
|
|
const auto limit = accounting::per_tick(
|
|
per_second, cellular_motion_tick_microseconds, accounting::Rounding::down);
|
|
if (!limit)
|
|
return std::nullopt;
|
|
return *limit > already ? *limit - already : 0;
|
|
};
|
|
const auto acquired = predator.candidate.intake
|
|
? predator.candidate.intake->transfer.acquired
|
|
: accounting::Inventory{};
|
|
accounting::Inventory taken_in{};
|
|
const auto energy_room =
|
|
remaining(hunter->budget.intake_limit.energy.value, acquired.energy.value);
|
|
if (!energy_room)
|
|
return std::unexpected{CellularRegionStepError::overflow};
|
|
taken_in.energy.value = std::min(whole.energy.value, *energy_room);
|
|
for (std::size_t channel = 0; channel < whole.matter.size(); ++channel) {
|
|
const auto room = remaining(hunter->budget.intake_limit.matter[channel].value,
|
|
acquired.matter[channel].value);
|
|
if (!room)
|
|
return std::unexpected{CellularRegionStepError::overflow};
|
|
taken_in.matter[channel].value = std::min(whole.matter[channel].value, *room);
|
|
}
|
|
if (!add(predator.candidate.reserve_after, taken_in))
|
|
return std::unexpected{CellularRegionStepError::overflow};
|
|
accounting::Inventory detritus = whole;
|
|
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, prey->extent_um});
|
|
CellularCorpseCandidate corpse{prey->life_id, prey_step.position, {}};
|
|
corpse.cause = CellularDeathCause::engulfed;
|
|
corpse.predator_id = hunter->life_id;
|
|
candidate.starved.push_back(corpse);
|
|
predator.engulf = CellularEngulfStatus::engulfed;
|
|
predator.prey_id = prey->life_id;
|
|
taken[q] = true;
|
|
break;
|
|
}
|
|
}
|
|
std::vector<CellularLifeStepResult> remaining_lives;
|
|
remaining_lives.reserve(candidate.lives.size());
|
|
for (std::size_t i = 0; i < candidate.lives.size(); ++i)
|
|
if (!taken[i])
|
|
remaining_lives.push_back(candidate.lives[i]);
|
|
candidate.lives = std::move(remaining_lives);
|
|
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. 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() >= deposit_limit) {
|
|
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);
|
|
CellularPatch bubble{
|
|
address, deposit.source, single,
|
|
0, cellular_mote_radius_um, CellularDepositKind::mote};
|
|
if (liquid)
|
|
bubble.merged = 1;
|
|
candidate.new_patches.insert(at, bubble);
|
|
}
|
|
}
|
|
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)) {
|
|
if (!add(patch_stock[*index], deposit.inventory))
|
|
return std::unexpected{CellularRegionStepError::overflow};
|
|
continue;
|
|
}
|
|
const auto made = std::ranges::lower_bound(candidate.new_patches, address, {},
|
|
&CellularPatch::address);
|
|
if (made != candidate.new_patches.end() && made->address == address) {
|
|
if (!add(made->stock, deposit.inventory))
|
|
return std::unexpected{CellularRegionStepError::overflow};
|
|
continue;
|
|
}
|
|
if (habitat.patches().size() + candidate.new_patches.size() < deposit_limit) {
|
|
candidate.new_patches.insert(made, {address, deposit.source, deposit.inventory});
|
|
continue;
|
|
}
|
|
// Full habitat: nearest existing patch, lowest address on a tie.
|
|
const auto distance = [&](CellularPatchAddress other) {
|
|
const auto dx = static_cast<std::int64_t>(other.x_um) - address.x_um;
|
|
const auto dy = static_cast<std::int64_t>(other.y_um) - address.y_um;
|
|
return static_cast<std::uint64_t>(dx * dx) + static_cast<std::uint64_t>(dy * dy);
|
|
};
|
|
std::optional<std::pair<std::uint64_t, CellularPatchAddress>> best;
|
|
for (const auto& patch : habitat.patches())
|
|
if (!best || std::pair{distance(patch.address), patch.address} < *best)
|
|
best = std::pair{distance(patch.address), patch.address};
|
|
for (const auto& patch : candidate.new_patches)
|
|
if (!best || std::pair{distance(patch.address), patch.address} < *best)
|
|
best = std::pair{distance(patch.address), patch.address};
|
|
if (!best)
|
|
return std::unexpected{CellularRegionStepError::habitat};
|
|
if (const auto index = habitat.find(best->second)) {
|
|
if (!add(patch_stock[*index], deposit.inventory))
|
|
return std::unexpected{CellularRegionStepError::overflow};
|
|
} else {
|
|
const auto nearest = std::ranges::lower_bound(candidate.new_patches, best->second,
|
|
{}, &CellularPatch::address);
|
|
if (!add(nearest->stock, deposit.inventory))
|
|
return std::unexpected{CellularRegionStepError::overflow};
|
|
}
|
|
}
|
|
}
|
|
|
|
for (std::size_t i = 0; i < patch_stock.size(); ++i)
|
|
if (patch_stock[i] != habitat.patches()[i].stock)
|
|
candidate.patches.push_back({habitat.patches()[i].address, patch_stock[i]});
|
|
return candidate;
|
|
}
|
|
} // namespace evo::biology
|