Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m29s
Foundation / postgres-tools (push) Successful in 24s
Foundation / server-image (push) Successful in 4m33s
Foundation / image (push) Successful in 4m59s
Session v14 carries the rules-13 revamp: a body block in the owner's view (members, emergence threshold, exclusions, Emerge open, payable energy, mass and the 20 traits), become, emerge and channel exclusions on actions, become on mutation choices, catalog offers (id, chance, cost) and the emerged ending. Older sessions never see a v14 field. The client shows Stay / Become offspring offers named from the shared catalog, continuing as a bud, the organism and its Emerge button, kept channels, the body and the emerged end screen. Bundle 15 (rules 13 on owner format 11) is durable: schema 60 admits it raw-only, stores become as bit 15 of the choice's operator, widens the action input and mutation results, and publishes the 14 -> 15 revamp profile migration. evo-world-clone opens cellular-v10 (cellular-v9 plus the revamp upgrade from --revamp-tuning), resumable and recoverable. Wildlife counts reserve matter as energy, waits for the bud fee, grows organisms one short of emerging, and hunts by the region step's rules-13 engulf test. The play summary adds organisms, offers and the spent pool. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
280 lines
15 KiB
C++
280 lines
15 KiB
C++
#include "evo/server/lifecycle/wildlife_controller.hpp"
|
|
|
|
#include "evo/biology/budding.hpp"
|
|
#include "evo/biology/cellular_burn.hpp"
|
|
#include "evo/biology/cellular_motion.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cmath>
|
|
#include <cstdlib>
|
|
#include <limits>
|
|
#include <optional>
|
|
|
|
namespace evo::server {
|
|
namespace {
|
|
constexpr std::int64_t half_pitch = biology::cellular_patch_pitch_um / 2;
|
|
|
|
std::uint64_t mix(std::uint64_t life, std::uint64_t phase) noexcept {
|
|
auto value = 14695981039346656037ULL;
|
|
for (const auto part : {life, phase})
|
|
for (unsigned index = 0; index < 8; ++index)
|
|
value = (value ^ static_cast<std::uint8_t>(part >> (index * 8U))) * 1099511628211ULL;
|
|
return value;
|
|
}
|
|
|
|
// The region step's engulf test (ADR-0124), without overflow: hunter * 1000 must reach
|
|
// prey * 1500 per micrometre of the prey's membrane.
|
|
bool can_engulf(std::uint64_t hunter, std::uint64_t prey, std::uint32_t membrane_um) noexcept {
|
|
const auto factor = biology::cellular_engulf_ratio_milli_per_membrane_um * membrane_um;
|
|
constexpr auto most = std::numeric_limits<std::uint64_t>::max();
|
|
if (factor != 0 && prey > most / factor)
|
|
return false;
|
|
return hunter <= most / 1000 ? hunter * 1000 >= prey * factor : true;
|
|
}
|
|
|
|
bool overlaps(std::uint64_t start, std::uint32_t extent, std::uint32_t patch) noexcept {
|
|
return start < std::uint64_t{patch} + biology::cellular_patch_pitch_um &&
|
|
start + extent > patch;
|
|
}
|
|
} // namespace
|
|
|
|
// M11-PT-001: scaling the larger component to the limit made a diagonal intent longer
|
|
// than the limit; the owner refused it and every later tick of the region stalled. Each
|
|
// component is scaled by the exact length and truncated toward zero, then pulled in if
|
|
// rounding still leaves the intent a hair long.
|
|
biology::CellularIntent wildlife_toward(std::int64_t dx, std::int64_t dy) noexcept {
|
|
if (dx == 0 && dy == 0)
|
|
return {};
|
|
const double length = std::hypot(static_cast<double>(dx), static_cast<double>(dy));
|
|
auto x = static_cast<std::int64_t>(static_cast<double>(dx) * wildlife_full_speed / length);
|
|
auto y = static_cast<std::int64_t>(static_cast<double>(dy) * wildlife_full_speed / length);
|
|
constexpr std::int64_t limit = wildlife_full_speed;
|
|
while (x * x + y * y > limit * limit) {
|
|
auto& larger = std::llabs(x) >= std::llabs(y) ? x : y;
|
|
larger += larger > 0 ? -1 : 1;
|
|
}
|
|
return {static_cast<std::int16_t>(x), static_cast<std::int16_t>(y)};
|
|
}
|
|
|
|
std::optional<biology::CellularPatchPerception>
|
|
wildlife_food(std::span<const biology::CellularPatchPerception> perceived, std::int64_t centre_x,
|
|
std::int64_t centre_y, bool deposits, bool hungry) {
|
|
// 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 auto offset = deposits ? 0 : half_pitch;
|
|
// A hungry forager ignores energy-free deposits while it perceives any energy.
|
|
const bool energy_only =
|
|
deposits && hungry && std::ranges::any_of(perceived, [](const auto& patch) {
|
|
return patch.available.energy.value != 0;
|
|
});
|
|
std::optional<biology::CellularPatchPerception> chosen;
|
|
std::uint64_t nearest_distance{};
|
|
for (const auto& patch : perceived) {
|
|
if (deposits ? patch.available == accounting::Inventory{}
|
|
: patch.available.energy.value == 0)
|
|
continue;
|
|
if (energy_only && patch.available.energy.value == 0)
|
|
continue;
|
|
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 (!chosen || distance < nearest_distance) {
|
|
chosen = patch;
|
|
nearest_distance = distance;
|
|
}
|
|
}
|
|
return chosen;
|
|
}
|
|
|
|
std::expected<std::vector<CellularLifeIntent>, CellularLivesError>
|
|
wildlife_intents(const CellularLives& state, std::uint64_t tick) {
|
|
constexpr std::array<biology::CellularIntent, 8> wander{{{1'000, 0},
|
|
{707, 707},
|
|
{0, 1'000},
|
|
{-707, 707},
|
|
{-1'000, 0},
|
|
{-707, -707},
|
|
{0, -1'000},
|
|
{707, -707}}};
|
|
const auto temperature = state.ambient_temperature_millikelvin();
|
|
// Rules 13 (ADR-0179): organisms develop as their scaled genome, engulf masses carry
|
|
// reserve matter, membranes are in nanometres, and reserve matter stands in for energy.
|
|
const bool revamp = state.rules_version() >= cellular_lives_revamp_rules;
|
|
const auto& revamp_rules = state.revamp_rules();
|
|
const auto body_genome =
|
|
[&](const CellularOwnedLife& life) -> std::expected<biology::Genome, CellularLivesError> {
|
|
const auto genome = state.archive().find(life.identity.genome);
|
|
if (!genome)
|
|
return std::unexpected{CellularLivesError::archive};
|
|
return revamp ? cellular_organism_genome(*genome, life.revamp.members)
|
|
: std::expected<biology::Genome, CellularLivesError>{*genome};
|
|
};
|
|
// A life from before the upgrade keeps its membrane in micrometres.
|
|
const auto membrane_nm = [](const biology::Genome& genome, std::uint32_t membrane) {
|
|
return genome.cellular_traits() ? membrane : membrane * 1'000;
|
|
};
|
|
std::vector<CellularLifeIntent> intents;
|
|
// Rules 7 prey facts for every cell in the world, derived once per tick: a per-hunter
|
|
// derivation made the step quadratic in metabolic budgets.
|
|
struct Prey {
|
|
std::uint64_t life{};
|
|
biology::CellularPosition position;
|
|
std::uint32_t extent{}, membrane{};
|
|
std::uint64_t mass{};
|
|
};
|
|
std::vector<std::uint64_t> engulf_masses; // Rules 13, beside prey_facts.
|
|
std::vector<Prey> prey_facts;
|
|
const bool hunting = state.rules_version() >= cellular_lives_reproduction_rules &&
|
|
std::ranges::any_of(state.lives(), [](const CellularOwnedLife& life) {
|
|
return life.account == LifeAccount{};
|
|
});
|
|
if (hunting) {
|
|
prey_facts.reserve(state.lives().size());
|
|
for (const auto& other : state.lives()) {
|
|
if (other.presence == CellularPresence::suspended)
|
|
continue;
|
|
const auto other_genome = body_genome(other);
|
|
if (!other_genome)
|
|
return std::unexpected{other_genome.error()};
|
|
const auto other_budget = biology::metabolic_budget(
|
|
*other_genome, other.growth, {temperature, {}}, 1000, state.revamp_rules().body);
|
|
const auto membrane = other_genome->parameter(biology::Capability::membrane);
|
|
if (!other_budget || !membrane)
|
|
return std::unexpected{CellularLivesError::biology};
|
|
prey_facts.push_back({other.identity.life, other.position,
|
|
other_budget->phenotype.extent,
|
|
revamp ? membrane_nm(*other_genome, *membrane) : *membrane,
|
|
other_budget->phenotype.total_mass});
|
|
engulf_masses.push_back(biology::cellular_engulf_mass(
|
|
other_budget->phenotype.total_mass, other.reserve, revamp_rules.step));
|
|
}
|
|
}
|
|
for (const auto& life : state.lives()) {
|
|
if (life.account != LifeAccount{})
|
|
continue;
|
|
const auto expressed = state.archive().find(life.identity.genome);
|
|
const auto genome = body_genome(life);
|
|
if (!expressed || !genome)
|
|
return std::unexpected{CellularLivesError::archive};
|
|
const auto budget = biology::metabolic_budget(*genome, life.growth, {temperature, {}}, 1000,
|
|
state.revamp_rules().body);
|
|
if (!budget)
|
|
return std::unexpected{CellularLivesError::biology};
|
|
const auto& traits = expressed->cellular_traits();
|
|
biology::CellularStepIntent intent{};
|
|
const auto offspring = state.archive().find(cellular_offspring_genome(life));
|
|
if (!offspring)
|
|
return std::unexpected{CellularLivesError::archive};
|
|
// Rules 13: a single cell buds when it can pay the bud and its fee; an organism adds a
|
|
// member when it can, and stops one short of the emergence threshold (the owner refuses
|
|
// what it cannot fund).
|
|
const auto fee =
|
|
revamp
|
|
? cellular_mutation_cost({.cost = revamp_rules.bud_fee,
|
|
.cost_scale_percent = revamp_rules.bud_fee_scale_percent})
|
|
: accounting::Inventory{};
|
|
bool fee_paid = life.reserve.energy.value >= fee.energy.value;
|
|
for (std::size_t c = 0; c < fee.matter.size(); ++c)
|
|
fee_paid = fee_paid && life.reserve.matter[c].value >= fee.matter[c].value;
|
|
const bool organism = revamp && life.revamp.members > 1;
|
|
const bool may_bud =
|
|
organism ? fee_paid && life.revamp.members + 1U < revamp_rules.emerge_members
|
|
: fee_paid && biology::fund_offspring(*expressed, *offspring, life.growth,
|
|
life.reserve, temperature)
|
|
.has_value();
|
|
if ((tick + life.identity.life) % wildlife_bud_interval_ticks == 0 && may_bud) {
|
|
intent.bud = true;
|
|
intents.push_back({life.identity.life, intent});
|
|
continue;
|
|
}
|
|
const auto extent = budget->phenotype.extent;
|
|
// Rules 7 (ADR-0137): engulf a touching cell the region step would let it take
|
|
// (ADR-0124: mass at least 1.5 x the prey's per micrometre of prey membrane),
|
|
// players' cells included; otherwise feed.
|
|
if (hunting) {
|
|
bool prey = false;
|
|
const auto hunter_mass = biology::cellular_engulf_mass(budget->phenotype.total_mass,
|
|
life.reserve, revamp_rules.step);
|
|
const auto capacity = traits ? traits->engulf_capacity : std::uint16_t{100};
|
|
for (std::size_t k = 0; k < prey_facts.size(); ++k) {
|
|
const auto& other = prey_facts[k];
|
|
if (other.life == life.identity.life ||
|
|
!biology::cellular_footprints_contact(life.position, extent, other.position,
|
|
other.extent))
|
|
continue;
|
|
if (revamp ? biology::cellular_engulfable(hunter_mass, capacity, engulf_masses[k],
|
|
other.membrane, revamp_rules.step)
|
|
: can_engulf(budget->phenotype.total_mass, other.mass, other.membrane)) {
|
|
prey = true;
|
|
break;
|
|
}
|
|
}
|
|
if (prey) {
|
|
intent.engulf = true;
|
|
intents.push_back({life.identity.life, intent});
|
|
continue;
|
|
}
|
|
}
|
|
if (state.rules_version() >= cellular_lives_photosynthesis_rules &&
|
|
state.light().numerator != 0 && genome->carries(biology::Capability::photosynthesis)) {
|
|
const auto still = cellular_light_capture(*genome, extent, state.light(), false);
|
|
const auto period = budget->rate_microseconds / 50'000;
|
|
if (still && period != 0 && *still >= budget->maintenance.value / period) {
|
|
intents.push_back({life.identity.life, intent});
|
|
continue;
|
|
}
|
|
}
|
|
const auto perceived = state.perceive(life.identity.life);
|
|
if (!perceived)
|
|
return std::unexpected{perceived.error()};
|
|
const auto centre_x = static_cast<std::int64_t>(life.position.x_um) + extent / 2;
|
|
const auto centre_y = static_cast<std::int64_t>(life.position.y_um) + extent / 2;
|
|
const bool deposits = state.rules_version() >= cellular_lives_foraging_rules;
|
|
// Rules 11: a cell short of energy forages for energy (wildlife_food).
|
|
const auto demand_period = budget->rate_microseconds / 50'000;
|
|
const auto demand_per_tick = demand_period == 0 ? 0 : budget->demand.value / demand_period;
|
|
// Rules 13: burnable reserve matter stands in for energy, so a cell rich in matter is
|
|
// not hungry.
|
|
const auto payable =
|
|
revamp
|
|
? biology::cellular_payable_energy(
|
|
life.reserve,
|
|
biology::cellular_burn(
|
|
revamp_rules.burn_pj_per_kfg,
|
|
traits ? traits->burn : std::array<std::uint16_t, 4>{100, 100, 100, 100},
|
|
0),
|
|
false)
|
|
: life.reserve.energy.value;
|
|
const bool hungry = state.rules_version() >= cellular_lives_photosynthesis_rules &&
|
|
demand_per_tick != 0 &&
|
|
payable / demand_per_tick < wildlife_energy_hunger_ticks;
|
|
const auto chosen = wildlife_food(*perceived, centre_x, centre_y, deposits, hungry);
|
|
const auto nearest = chosen ? std::optional{chosen->address} : std::nullopt;
|
|
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))
|
|
intent.movement =
|
|
wildlife_toward(std::int64_t{nearest->x_um} + half_pitch - centre_x,
|
|
std::int64_t{nearest->y_um} + half_pitch - centre_y);
|
|
} else {
|
|
intent.movement =
|
|
wander[mix(life.identity.life, tick / wildlife_wander_interval_ticks) %
|
|
wander.size()];
|
|
}
|
|
intents.push_back({life.identity.life, intent});
|
|
}
|
|
return intents;
|
|
}
|
|
} // namespace evo::server
|