Drive wildlife with a deterministic controller recorded in the trace

M11.5d (ADR-0125): wildlife_intents gives each unowned cell a bud intent when it
can fund one (at most once a second), else steers it to the nearest perceived
food patch and feeds, else wanders. The cellular server puts these intentions
into each tick's baseline, so replay and publication treat them like player
input. fund_bud refuses a reserve short of the bare investment before scanning
growth levels.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-14 06:45:48 -04:00
co-authored by Claude Opus 5.5
parent f22a540135
commit 212ca14e83
8 changed files with 174 additions and 1 deletions
@@ -0,0 +1,100 @@
#include "evo/server/lifecycle/wildlife_controller.hpp"
#include "evo/biology/budding.hpp"
#include <algorithm>
#include <array>
#include <cstdlib>
#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;
}
// Full speed toward (dx, dy), keeping the larger component at the limit.
biology::CellularIntent toward(std::int64_t dx, std::int64_t dy) noexcept {
const auto larger = std::max(std::llabs(dx), std::llabs(dy));
if (larger == 0)
return {};
return {static_cast<std::int16_t>(dx * wildlife_full_speed / larger),
static_cast<std::int16_t>(dy * wildlife_full_speed / larger)};
}
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
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;
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, {}});
if (!budget)
return std::unexpected{CellularLivesError::biology};
biology::CellularStepIntent intent{};
if ((tick + life.identity.life) % wildlife_bud_interval_ticks == 0 &&
biology::fund_bud(*genome, life.growth, life.reserve, temperature)) {
intent.bud = true;
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 extent = budget->phenotype.extent;
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;
std::optional<biology::CellularPatchAddress> nearest;
std::uint64_t nearest_distance{};
for (const auto& patch : *perceived) {
if (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 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;
}
}
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 = 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