Files
aevolution/src/server/lifecycle/wildlife_controller.cpp
T
dingoandClaude Opus 5.5 c7b4846901
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (checks) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 3s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m30s
Foundation / postgres-tools (push) Successful in 22s
Foundation / server-image (push) Successful in 4m12s
Foundation / image (push) Successful in 4m40s
feat(lifecycle): rules 13 owner on format 11 with the revamp tuning file
Phase 1 of the cellular revamp build plan (ADR-0179). The cellular owner gains
rules 13 on owner format 11: an explicit no-tick format-10/rules-12 upgrade
declares the region's revamp rules (so far the body's essential upkeep share),
existing lives keep their genomes, and founders born afterwards are founder 6
(genome schema 7, development 7). Format 11 is format 10 plus the revamp rules
block; it stays undeployed while later phases extend it. The tick, perception
and wildlife controller derive budgets with the declared body tuning, which
earlier development versions ignore.

Revamp tunables live in their own Git file,
deploy/kubernetes/region/dev/tuning/cellular-revamp.tuning, parsed by
parse_cellular_revamp_tuning with the same format and refusals as the rules-12
file, so images from before rules 13 never meet keys they do not know. Both
parsers now share read_tuning_lines; the rules-12 file's behaviour is unchanged.
The revamp file is not yet mounted into the region (build plan, phase 6).

Tests cover the upgrade and its refusals, mixed founder 5 and founder 6 lives
ticking with closure, exact restore and continuation, trace replay, and the new
tuning file including the checked-in DEV copy. Refreshes the DEV session window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 18:19:18 -04:00

223 lines
11 KiB
C++

#include "evo/server/lifecycle/wildlife_controller.hpp"
#include "evo/biology/budding.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();
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<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 = state.archive().find(other.identity.genome);
if (!other_genome)
return std::unexpected{CellularLivesError::archive};
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, *membrane,
other_budget->phenotype.total_mass});
}
}
for (const auto& life : state.lives()) {
if (life.account != LifeAccount{})
continue;
const auto genome = state.archive().find(life.identity.genome);
if (!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};
biology::CellularStepIntent intent{};
const auto offspring = state.archive().find(cellular_offspring_genome(life));
if (!offspring)
return std::unexpected{CellularLivesError::archive};
if ((tick + life.identity.life) % wildlife_bud_interval_ticks == 0 &&
biology::fund_offspring(*genome, *offspring, life.growth, life.reserve, temperature)) {
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;
for (const auto& other : prey_facts) {
if (other.life == life.identity.life ||
!biology::cellular_footprints_contact(life.position, extent, other.position,
other.extent))
continue;
if (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;
const bool hungry =
state.rules_version() >= cellular_lives_photosynthesis_rules && demand_per_tick != 0 &&
life.reserve.energy.value / 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