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 <noreply@anthropic.com>
This commit is contained in:
@@ -56,6 +56,37 @@ biology::CellularIntent wildlife_toward(std::int64_t dx, std::int64_t dy) noexce
|
||||
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},
|
||||
@@ -154,27 +185,15 @@ wildlife_intents(const CellularLives& state, std::uint64_t tick) {
|
||||
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;
|
||||
std::optional<biology::CellularPatchAddress> 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<biology::CellularPatchPerception> 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<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;
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user