From bed069e91ca78804fccaa3d6673304d41b5d3027 Mon Sep 17 00:00:00 2001 From: dingo Date: Wed, 21 Oct 2026 11:03:23 -0400 Subject: [PATCH] fix(wildlife): rules-11 foragers short of energy seek energy, not the nearest mote The controller steered to the nearest non-empty deposit of any kind and held still once touching it. A bud parked on a carbon mote fed on carbon until it starved, and later buds stopped on the first bubble of a corpse's row rather than its energy one. On rules 11 a wildlife cell whose energy reserve covers fewer than 1,200 ticks (60 s) of its full demand now passes over energy-free deposits while it perceives any energy; a fed cell, or one with no energy in view, still takes the nearest food. Earlier rules are unchanged, so their recorded and population histories replay exactly. The choice is the new wildlife_food(), tested directly in photosynthesis-contracts. Co-Authored-By: Claude Opus 5.5 --- .../server/lifecycle/wildlife_controller.hpp | 13 ++++ src/server/lifecycle/wildlife_controller.cpp | 59 ++++++++++++------- tests/photosynthesis_tests.cpp | 26 ++++++++ 3 files changed, 78 insertions(+), 20 deletions(-) diff --git a/include/evo/server/lifecycle/wildlife_controller.hpp b/include/evo/server/lifecycle/wildlife_controller.hpp index ad24abe..1758b8a 100644 --- a/include/evo/server/lifecycle/wildlife_controller.hpp +++ b/include/evo/server/lifecycle/wildlife_controller.hpp @@ -4,6 +4,8 @@ #include #include +#include +#include #include // M11.5d (ADR-0125): the bounded deterministic wildlife controller. It reads only the @@ -16,11 +18,22 @@ namespace evo::server { inline constexpr std::uint64_t wildlife_bud_interval_ticks = 20; // 1 s inline constexpr std::uint64_t wildlife_wander_interval_ticks = 40; // 2 s inline constexpr std::int16_t wildlife_full_speed = 1'000; +// Rules 11 (trial value): a wildlife cell whose energy reserve covers fewer than this many +// ticks of its full demand seeks energy-bearing deposits only, so it never parks on a +// carbon or nutrient mote while it starves. Earlier rules keep nearest-any foraging. +inline constexpr std::uint64_t wildlife_energy_hunger_ticks = 1'200; // 60 s // Full speed toward (dx, dy): the intent's length is at most wildlife_full_speed, as a // cellular intent requires (a longer one is refused and fails the whole tick). [[nodiscard]] biology::CellularIntent wildlife_toward(std::int64_t dx, std::int64_t dy) noexcept; +// The perceived food a forager centred at (x, y) heads for: the nearest non-empty deposit +// (or, before rules 8, the nearest energy patch). A hungry forager on deposits passes over +// energy-free ones while it perceives any energy; otherwise it takes the nearest anything. +[[nodiscard]] std::optional +wildlife_food(std::span perceived, std::int64_t centre_x, + std::int64_t centre_y, bool deposits, bool hungry); + // One intention per wildlife life (zero account), sorted by life id: bud when the reserve // funds one, else steer to and feed from the nearest perceived patch holding energy, // else wander. Owned lives are left to their players. ADR-0175: under rules 11 with diff --git a/src/server/lifecycle/wildlife_controller.cpp b/src/server/lifecycle/wildlife_controller.cpp index e5c5750..c25f7cd 100644 --- a/src/server/lifecycle/wildlife_controller.cpp +++ b/src/server/lifecycle/wildlife_controller.cpp @@ -56,6 +56,37 @@ biology::CellularIntent wildlife_toward(std::int64_t dx, std::int64_t dy) noexce 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}, @@ -154,27 +185,15 @@ wildlife_intents(const CellularLives& state, std::uint64_t tick) { 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; - std::optional nearest; - std::uint64_t nearest_distance{}; - // 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 bool deposits = state.rules_version() >= cellular_lives_foraging_rules; - const auto offset = deposits ? 0 : half_pitch; - std::optional chosen; - for (const auto& patch : *perceived) { - if (deposits ? patch.available == accounting::Inventory{} - : 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 (!nearest || distance < nearest_distance) { - nearest = patch.address; - nearest_distance = distance; - chosen = patch; - } - } + // 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; diff --git a/tests/photosynthesis_tests.cpp b/tests/photosynthesis_tests.cpp index 6c7ebad..3c0a572 100644 --- a/tests/photosynthesis_tests.cpp +++ b/tests/photosynthesis_tests.cpp @@ -1,8 +1,10 @@ #include "evo/biology/founder.hpp" #include "evo/biology/mutation.hpp" #include "evo/server/lifecycle/cellular_light.hpp" +#include "evo/server/lifecycle/wildlife_controller.hpp" #include +#include #include #include #include @@ -130,6 +132,29 @@ void offers() { } require(offered, "schema 5 never offered photosynthesis"); } +// Rules 11 foraging: a hungry cell passes a nearer carbon mote for energy, a fed cell takes +// the nearest food, and with no energy in view the hungry cell still takes the nearest. +void foraging() { + const auto mote = [](std::uint32_t x, bool energy) { + CellularPatchPerception p{}; + p.address = {x, 0}; + p.radius_um = cellular_mote_radius_um; + p.kind = CellularDepositKind::mote; + if (energy) + p.available.energy.value = 1'000; + else + p.available.matter[0].value = 1'000; // Carbon only. + return p; + }; + const std::array view{mote(40, false), mote(400, true)}; + const auto hungry = wildlife_food(view, 0, 0, true, true); + const auto fed = wildlife_food(view, 0, 0, true, false); + require(hungry && hungry->address.x_um == 400, "a hungry cell parked on carbon"); + require(fed && fed->address.x_um == 40, "a fed cell skipped the nearest food"); + const std::array carbon{mote(40, false)}; + const auto only = wildlife_food(carbon, 0, 0, true, true); + require(only && only->address.x_um == 40, "a hungry cell ignored the only food in view"); +} } // namespace int main() { @@ -137,6 +162,7 @@ int main() { founders(); costs(); offers(); + foraging(); std::cout << "photosynthesis contracts passed\n"; return 0; } catch (const std::exception& error) {