#include "evo/server/lifecycle/wildlife_controller.hpp" #include "evo/biology/budding.hpp" #include "evo/biology/cellular_motion.hpp" #include #include #include #include #include #include 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(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::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(dx), static_cast(dy)); auto x = static_cast(static_cast(dx) * wildlife_full_speed / length); auto y = static_cast(static_cast(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(x), static_cast(y)}; } std::optional wildlife_food(std::span 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 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(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, CellularLivesError> wildlife_intents(const CellularLives& state, std::uint64_t tick) { constexpr std::array 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 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_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(life.position.x_um) + extent / 2; const auto centre_y = static_cast(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