feat(biology): rules 13 mutation catalog, behaviour triggers and offer draw
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m30s
Foundation / postgres-tools (push) Successful in 22s
Foundation / server-image (push) Successful in 4m10s
Foundation / image (push) Successful in 4m46s
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m30s
Foundation / postgres-tools (push) Successful in 22s
Foundation / server-image (push) Successful in 4m10s
Foundation / image (push) Successful in 4m46s
Phase 3 of the cellular revamp build plan (ADR-0179), first part: the headless pieces the owner will use. - The named catalog: all 49 mutations of Cellular_mutation_plan.md as data (stable ids and tuning keys, name, pro, con, effect, availability, increase and decrease triggers, base weight). Applying one edits a schema-7 plan by fixed reference points (a point is a hundredth of the founder's value), with paired edits, spreads across a channel group, recipe shifts, photosynthesis acquire and shed, and Adhesion; bounds, gates (carbon burned, buds made) and the enabled switch close it. 43 mutations are open at birth, as planned. - Triggers: per-tick activity signals and eight trigger kinds (accumulate, absence, count in a window, event, share over a window, regular recurrence, straight heading, sharp turns) move each mutation's affinity score within bounds; integer state only, so replay reproduces scores. - Offers: weight = base x a score multiplier; chances clamp(k x weight, floor, cap) summing to the options, in parts per million (floor 0.001%, cap 70%), found exactly by water-filling over sorted weights; a systematic draw picks distinct mutations whose inclusion frequencies equal their chances exactly. Tests cover the catalog's identities, the pool at birth, every kind of edit, bounds and gates; every trigger kind; score bounds; weights; a 300-seed chance ensemble; and an exhaustive draw over all million draws. Refreshes the DEV session window. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+10
-1
@@ -77,7 +77,8 @@ add_library(evo_biology STATIC
|
||||
src/biology/mutation.cpp src/biology/lineage.cpp src/biology/founder.cpp
|
||||
src/biology/cellular_motion.cpp src/biology/cellular_kinematics.cpp
|
||||
src/biology/cellular_tuning.cpp src/biology/cellular_revamp_tuning.cpp
|
||||
src/biology/cellular_burn.cpp
|
||||
src/biology/cellular_burn.cpp src/biology/cellular_catalog.cpp
|
||||
src/biology/cellular_triggers.cpp src/biology/cellular_offers.cpp
|
||||
src/biology/cellular_currents.cpp
|
||||
src/biology/cellular_deposit_motion.cpp
|
||||
src/biology/cellular_uptake.cpp
|
||||
@@ -505,6 +506,14 @@ if(BUILD_TESTING)
|
||||
target_link_libraries(evo-cellular-revamp-step-tests PRIVATE evo::biology)
|
||||
evo_owned_target(evo-cellular-revamp-step-tests)
|
||||
add_test(NAME cellular-revamp-step-contracts COMMAND evo-cellular-revamp-step-tests)
|
||||
add_executable(evo-cellular-offers-tests tests/cellular_offers_tests.cpp)
|
||||
target_link_libraries(evo-cellular-offers-tests PRIVATE evo::biology)
|
||||
evo_owned_target(evo-cellular-offers-tests)
|
||||
add_test(NAME cellular-offers-contracts COMMAND evo-cellular-offers-tests)
|
||||
add_executable(evo-cellular-catalog-tests tests/cellular_catalog_tests.cpp)
|
||||
target_link_libraries(evo-cellular-catalog-tests PRIVATE evo::biology)
|
||||
evo_owned_target(evo-cellular-catalog-tests)
|
||||
add_test(NAME cellular-catalog-contracts COMMAND evo-cellular-catalog-tests)
|
||||
add_executable(evo-cellular-burn-tests tests/cellular_burn_tests.cpp)
|
||||
target_link_libraries(evo-cellular-burn-tests PRIVATE evo::biology)
|
||||
evo_owned_target(evo-cellular-burn-tests)
|
||||
|
||||
@@ -12,7 +12,7 @@ spec:
|
||||
env:
|
||||
- {name: REGION_SESSION, value: "dev"}
|
||||
- {name: REGION_SESSION_LISTEN, value: "0.0.0.0:7400"}
|
||||
- {name: REGION_SESSION_WINDOW, value: "dev-20261022T223519-812afd47:1792722919"}
|
||||
- {name: REGION_SESSION_WINDOW, value: "dev-20261022T224609-01218f90:1792723569"}
|
||||
- {name: REGION_M10_MAX_GRANT_LIFETIME, value: "14400"}
|
||||
ports:
|
||||
# QUIC (ADR-0090): UDP only; nothing listens on TCP 7400.
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
#pragma once
|
||||
|
||||
#include "evo/accounting/ledger.hpp"
|
||||
#include "evo/biology/genome.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <compare>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
|
||||
// The named mutation catalog of rules 13 (ADR-0179; Cellular_mutation_plan.md sections 5-9).
|
||||
// Each entry edits a genome's plan within bounds; what triggers raise or lower its chance and
|
||||
// how offers are drawn live in cellular_triggers.hpp and cellular_offers.hpp. IDs are stable.
|
||||
namespace evo::biology {
|
||||
// What a mutation edits. Point-based traits move in fixed reference points: one point is a
|
||||
// hundredth of the founder's value, whatever the trait's unit.
|
||||
enum class CellularTrait : std::uint8_t {
|
||||
none,
|
||||
move_output,
|
||||
move_efficiency,
|
||||
move_steering,
|
||||
move_streamlining,
|
||||
move_braking,
|
||||
move_coasting,
|
||||
collect_energy,
|
||||
collect_carbon,
|
||||
collect_nitrogen,
|
||||
collect_phosphorus,
|
||||
collect_other,
|
||||
intake, // Ingestion capability, femtograms per second.
|
||||
energy_kept, // Energy conversion capability, per thousand.
|
||||
burn_carbon,
|
||||
burn_nitrogen,
|
||||
burn_phosphorus,
|
||||
burn_other,
|
||||
light, // Photosynthesis capability, per thousand.
|
||||
membrane, // Nanometres.
|
||||
engulf_capacity,
|
||||
kill_absorption,
|
||||
size, // Target size, femtograms.
|
||||
recipe_carbon,
|
||||
recipe_nitrogen,
|
||||
recipe_phosphorus,
|
||||
recipe_other,
|
||||
upkeep_efficiency,
|
||||
growth_speed,
|
||||
density,
|
||||
bud_share, // Reproduction capability, per thousand.
|
||||
litter, // Hundredths of an extra offspring; moves in its own units, not points.
|
||||
};
|
||||
|
||||
enum class CellularEffect : std::uint8_t {
|
||||
adjust, // Primary by the primary step, paired by the paired step, spread.
|
||||
recipe, // Recipe: primary loses the recipe step, the other three share it.
|
||||
acquire_light, // Photosynthesis at the founder reference, with the paired edit.
|
||||
shed_light, // Photosynthesis removed.
|
||||
acquire_adhesion, // Adhesion set.
|
||||
};
|
||||
// A spread lowers each other channel of the primary's group by the spread step.
|
||||
enum class CellularSpread : std::uint8_t { none, collect_matter, burn };
|
||||
|
||||
// Extra eligibility beyond staying within bounds.
|
||||
enum class CellularAvailability : std::uint8_t {
|
||||
always,
|
||||
light_absent_after_carbon, // No photosynthesis, and enough carbon burned.
|
||||
light_present,
|
||||
adhesion_absent_after_buds, // No adhesion, and enough on-demand buds.
|
||||
};
|
||||
|
||||
// What a cell does that a trigger counts. Flags are 0 or 1 per tick; amounts are pJ or fg.
|
||||
enum class CellularSignal : std::uint8_t {
|
||||
thrust, // Thrust applied this tick.
|
||||
drifting, // No thrust this tick.
|
||||
brake_press, // Brake pressed this tick and not the last.
|
||||
fed, // Anything collected.
|
||||
intake_limited, // Matter collection held by a limit.
|
||||
energy_limited, // Energy collection held by its limit.
|
||||
energy_collected, // Amount of energy collected.
|
||||
collected_carbon, // Amounts of matter collected, by channel, and in total.
|
||||
collected_nitrogen,
|
||||
collected_phosphorus,
|
||||
collected_other,
|
||||
collected_matter,
|
||||
burned_carbon, // Amounts of matter burned for energy, by channel, and in total.
|
||||
burned_nitrogen,
|
||||
burned_phosphorus,
|
||||
burned_other,
|
||||
burned_matter,
|
||||
burned_not_carbon,
|
||||
energy_upkeep, // Energy spent on upkeep, on movement, and in total.
|
||||
energy_movement,
|
||||
energy_spent,
|
||||
larger_contact, // Touching a cell over 1.5 times this cell's mass.
|
||||
engulf_attempt, // An engulf was asked for.
|
||||
engulf_success,
|
||||
engulf_too_large, // Failed: prey too large.
|
||||
engulf_too_large_or_membrane, // Failed: prey too large or membrane too thick.
|
||||
reserve_over_body, // More matter in the reserve than in the body.
|
||||
regrowing, // Growing toward full size.
|
||||
fully_grown,
|
||||
blocked_carbon, // Growth or a bud held back by this channel.
|
||||
blocked_nitrogen,
|
||||
blocked_phosphorus,
|
||||
blocked_other,
|
||||
bud_done, // An on-demand bud was made.
|
||||
bud_refused, // An on-demand bud was refused for lack of resources.
|
||||
bud_lost_early, // One of this cell's buds was engulfed within 2 minutes of birth.
|
||||
bud_with_surplus, // A bud made with the reserve covering twice its cost.
|
||||
};
|
||||
inline constexpr std::size_t cellular_signal_count = 37;
|
||||
|
||||
enum class CellularTriggerKind : std::uint8_t {
|
||||
accumulate, // Fires each time the signal has been present for `ticks` ticks in total.
|
||||
absence, // Fires when the signal has been absent for `ticks` ticks running.
|
||||
count, // Fires when the signal happened `count` times within `ticks` (0: any time).
|
||||
event, // Fires each time the signal happens.
|
||||
share, // Every `ticks` ticks: fires if signal / denominator >= per_thousand.
|
||||
regular, // Fires when the signal recurred with gaps of at most `gap` for `ticks`.
|
||||
straight, // Fires after `ticks` thrust ticks within `angle` of one heading.
|
||||
turns, // Fires after `count` turns of more than `angle` within `ticks`.
|
||||
};
|
||||
struct CellularTriggerSpec {
|
||||
CellularTriggerKind kind{};
|
||||
CellularSignal signal{};
|
||||
std::uint32_t ticks{};
|
||||
std::uint32_t count{};
|
||||
CellularSignal denominator{}; // share only.
|
||||
std::uint32_t per_thousand{}; // share only.
|
||||
std::uint32_t gap{}; // regular only.
|
||||
std::uint16_t angle{}; // straight and turns: heading units (65,536 per turn).
|
||||
auto operator<=>(const CellularTriggerSpec&) const = default;
|
||||
};
|
||||
|
||||
struct CellularMutationSpec {
|
||||
std::uint16_t id{};
|
||||
std::string_view key, name, pro, con;
|
||||
CellularEffect effect{};
|
||||
CellularTrait primary{}, paired{};
|
||||
std::int8_t primary_sign{}, paired_sign{};
|
||||
CellularSpread spread{};
|
||||
CellularAvailability available{};
|
||||
CellularTriggerSpec increase{}, decrease{};
|
||||
std::uint32_t base_weight{100};
|
||||
std::uint32_t primary_step{5}; // Default primary step in its units (points; recipe 20).
|
||||
};
|
||||
inline constexpr std::size_t cellular_catalog_size = 49;
|
||||
[[nodiscard]] std::span<const CellularMutationSpec> cellular_catalog() noexcept;
|
||||
[[nodiscard]] const CellularMutationSpec* cellular_mutation(std::uint16_t id) noexcept;
|
||||
|
||||
// One mutation's tunables (Cellular_mutation_plan.md sections 3-4); defaults from its spec.
|
||||
struct CellularMutationTuning {
|
||||
bool enabled{true};
|
||||
std::uint32_t base_weight{100};
|
||||
std::int32_t increase_points{100}, decrease_points{100};
|
||||
std::uint32_t primary_step{5}, paired_step{2}, spread_step{1};
|
||||
accounting::Inventory cost{};
|
||||
std::uint32_t cost_scale_percent{};
|
||||
std::uint32_t increase_ticks{}, decrease_ticks{}; // 0: the spec's threshold.
|
||||
auto operator<=>(const CellularMutationTuning&) const = default;
|
||||
};
|
||||
// Bounds and gates shared by every mutation.
|
||||
struct CellularCatalogBounds {
|
||||
std::uint32_t min_points{25}, max_points{1'000};
|
||||
std::uint32_t recipe_min_share{20}; // Per thousand; the founder's phosphorus is 50.
|
||||
std::uint32_t max_litter{900}; // 10 offspring per bud.
|
||||
std::uint64_t carbon_for_light{}; // Femtograms of carbon burned before photosynthesis.
|
||||
std::uint32_t buds_for_adhesion{5};
|
||||
auto operator<=>(const CellularCatalogBounds&) const = default;
|
||||
};
|
||||
// Lineage progress the gates read.
|
||||
struct CellularLineageProgress {
|
||||
std::uint64_t carbon_burned{};
|
||||
std::uint32_t buds{};
|
||||
auto operator<=>(const CellularLineageProgress&) const = default;
|
||||
};
|
||||
[[nodiscard]] CellularMutationTuning
|
||||
cellular_default_tuning(const CellularMutationSpec& spec) noexcept;
|
||||
|
||||
// The successor plan the mutation makes from `plan` (a schema-7 genome), or nothing when it is
|
||||
// not available: a gate is closed, the trait is absent, or an edit would leave its bounds. The
|
||||
// successor has one parent, `plan`, and identity `successor_id` from `origin_event`.
|
||||
[[nodiscard]] std::optional<GenomeDraft>
|
||||
apply_cellular_mutation(const Genome& plan, const CellularMutationSpec& spec,
|
||||
const CellularMutationTuning& tuning, const CellularCatalogBounds& bounds,
|
||||
const CellularLineageProgress& progress, std::uint64_t successor_id,
|
||||
std::uint64_t origin_event);
|
||||
} // namespace evo::biology
|
||||
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <compare>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <vector>
|
||||
|
||||
// Mutation offers (rules 13, ADR-0179; Cellular_mutation_plan.md section 3): a mutation's
|
||||
// weight from its affinity score, every eligible mutation's chance of appearing in an offer
|
||||
// (floored and capped, summing to the number of options), and a systematic draw whose
|
||||
// inclusion frequencies equal those chances. Chances are parts per million (1,000,000 = 100%).
|
||||
namespace evo::biology {
|
||||
inline constexpr std::uint32_t cellular_chance_whole = 1'000'000;
|
||||
|
||||
struct CellularOfferRules {
|
||||
std::uint32_t options{3};
|
||||
std::uint32_t floor_ppm{10}; // 0.001%.
|
||||
std::uint32_t cap_ppm{700'000}; // 70%.
|
||||
std::int32_t score_min{-1'000}, score_max{1'000};
|
||||
std::uint32_t max_multiplier_per_mille{10'000}; // At score_max: 10x.
|
||||
std::uint32_t min_multiplier_per_mille{0}; // At score_min: 0x.
|
||||
auto operator<=>(const CellularOfferRules&) const = default;
|
||||
};
|
||||
|
||||
// base_weight times the score's multiplier: 1 at score 0, rising linearly to the maximum at
|
||||
// score_max and falling to the minimum at score_min.
|
||||
[[nodiscard]] std::uint64_t cellular_offer_weight(std::uint32_t base_weight, std::int32_t score,
|
||||
const CellularOfferRules& rules) noexcept;
|
||||
|
||||
// Each weight's chance: clamp(k x weight, floor, cap) with k chosen so the chances sum to
|
||||
// `options` x 100%, in the weights' order. Nothing when no such allocation exists (too few
|
||||
// mutations for the cap, too many for the floor, or weights too large).
|
||||
[[nodiscard]] std::optional<std::vector<std::uint32_t>>
|
||||
cellular_offer_chances(std::span<const std::uint64_t> weights, const CellularOfferRules& rules);
|
||||
|
||||
// Systematic draw over chances summing to `options` x 100%: the indices at
|
||||
// u, u + 100%, u + 200%, ... along the cumulative chances, `u` in [0, 100%). Distinct by
|
||||
// construction; each index appears with exactly its chance over uniform `u`.
|
||||
[[nodiscard]] std::vector<std::size_t>
|
||||
cellular_systematic_draw(std::span<const std::uint32_t> chances, std::uint32_t options,
|
||||
std::uint32_t u) noexcept;
|
||||
} // namespace evo::biology
|
||||
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include "evo/biology/cellular_catalog.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <compare>
|
||||
#include <cstdint>
|
||||
|
||||
// Behaviour triggers (rules 13, ADR-0179; Cellular_mutation_plan.md section 3): what a cell did
|
||||
// each tick raises or lowers its affinity score for each mutation. Integer state only, advanced
|
||||
// once per tick by the owner, so replay reproduces every score exactly.
|
||||
namespace evo::biology {
|
||||
// One tick of a cell's activity: flags are 0 or 1, amounts are pJ or fg (CellularSignal).
|
||||
struct CellularActivity {
|
||||
std::array<std::uint64_t, cellular_signal_count> signal{};
|
||||
std::uint16_t heading{}; // The cell's heading after the tick (65,536 per turn).
|
||||
[[nodiscard]] std::uint64_t operator[](CellularSignal s) const noexcept {
|
||||
return signal[static_cast<std::size_t>(s)];
|
||||
}
|
||||
std::uint64_t& operator[](CellularSignal s) noexcept {
|
||||
return signal[static_cast<std::size_t>(s)];
|
||||
}
|
||||
};
|
||||
|
||||
// One trigger's progress. Its meaning depends on the trigger kind; all fields start at zero.
|
||||
struct CellularTriggerState {
|
||||
std::uint64_t first{}, second{};
|
||||
std::uint32_t ticks{};
|
||||
std::uint16_t heading{};
|
||||
std::uint8_t running{};
|
||||
auto operator<=>(const CellularTriggerState&) const = default;
|
||||
};
|
||||
|
||||
// Advances one trigger by one tick; true when it fires. `threshold` replaces the spec's tick
|
||||
// threshold (or window) when non-zero.
|
||||
[[nodiscard]] bool cellular_trigger_tick(const CellularTriggerSpec& spec, std::uint32_t threshold,
|
||||
CellularTriggerState& state,
|
||||
const CellularActivity& activity) noexcept;
|
||||
|
||||
// Affinity: every mutation's score, and its increase and decrease triggers' progress.
|
||||
struct CellularAffinity {
|
||||
std::array<std::int16_t, cellular_catalog_size> score{};
|
||||
std::array<CellularTriggerState, cellular_catalog_size> increase{}, decrease{};
|
||||
auto operator<=>(const CellularAffinity&) const = default;
|
||||
};
|
||||
// Bounds of every score.
|
||||
struct CellularScoreBounds {
|
||||
std::int32_t min{-1'000}, max{1'000};
|
||||
auto operator<=>(const CellularScoreBounds&) const = default;
|
||||
};
|
||||
// Advances every mutation's triggers by one tick and applies their points to its score
|
||||
// (`tuning` is indexed like the catalog). Disabled mutations do not move.
|
||||
void cellular_affinity_tick(CellularAffinity& affinity, const CellularActivity& activity,
|
||||
std::span<const CellularMutationTuning> tuning,
|
||||
CellularScoreBounds bounds) noexcept;
|
||||
} // namespace evo::biology
|
||||
@@ -0,0 +1,508 @@
|
||||
#include "evo/biology/cellular_catalog.hpp"
|
||||
|
||||
#include "evo/biology/founder.hpp"
|
||||
#include "evo/biology/mutation.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
|
||||
namespace evo::biology {
|
||||
namespace {
|
||||
using T = CellularTrait;
|
||||
using S = CellularSignal;
|
||||
using K = CellularTriggerKind;
|
||||
using E = CellularEffect;
|
||||
using A = CellularAvailability;
|
||||
using P = CellularSpread;
|
||||
|
||||
constexpr std::uint32_t second = 20; // Ticks.
|
||||
constexpr std::uint32_t minute = 60 * second;
|
||||
constexpr std::uint16_t right_angle = 16'384; // 90 degrees.
|
||||
constexpr std::uint16_t twenty_degrees = 3'641;
|
||||
|
||||
constexpr CellularTriggerSpec accumulate(S signal, std::uint32_t ticks) {
|
||||
return {K::accumulate, signal, ticks};
|
||||
}
|
||||
constexpr CellularTriggerSpec absence(S signal, std::uint32_t ticks) {
|
||||
return {K::absence, signal, ticks};
|
||||
}
|
||||
constexpr CellularTriggerSpec count(S signal, std::uint32_t times, std::uint32_t ticks) {
|
||||
return {K::count, signal, ticks, times};
|
||||
}
|
||||
constexpr CellularTriggerSpec event(S signal) {
|
||||
return {K::event, signal};
|
||||
}
|
||||
constexpr CellularTriggerSpec share(S part, S whole, std::uint32_t ticks) {
|
||||
return {K::share, part, ticks, 0, whole, 500};
|
||||
}
|
||||
constexpr CellularTriggerSpec regular(std::uint32_t gap, std::uint32_t ticks) {
|
||||
return {K::regular, S::thrust, ticks, 0, {}, 0, gap};
|
||||
}
|
||||
constexpr CellularTriggerSpec straight(std::uint32_t ticks) {
|
||||
return {K::straight, S::thrust, ticks, 0, {}, 0, 0, twenty_degrees};
|
||||
}
|
||||
constexpr CellularTriggerSpec turns(std::uint32_t times, std::uint32_t ticks) {
|
||||
return {K::turns, S::thrust, ticks, times, {}, 0, 0, right_angle};
|
||||
}
|
||||
|
||||
struct Entry {
|
||||
std::uint16_t id;
|
||||
std::string_view key, name, pro, con;
|
||||
E effect;
|
||||
T primary;
|
||||
std::int8_t primary_sign;
|
||||
T paired;
|
||||
std::int8_t paired_sign;
|
||||
P spread;
|
||||
A available;
|
||||
CellularTriggerSpec increase, decrease;
|
||||
std::uint32_t base_weight = 100, primary_step = 5;
|
||||
};
|
||||
constexpr CellularMutationSpec spec(const Entry& e) {
|
||||
return {e.id, e.key, e.name, e.pro, e.con, e.effect,
|
||||
e.primary, e.paired, e.primary_sign, e.paired_sign, e.spread, e.available,
|
||||
e.increase, e.decrease, e.base_weight, e.primary_step};
|
||||
}
|
||||
|
||||
// Cellular_mutation_plan.md sections 5-9. Mirrors take their core mutation's triggers swapped.
|
||||
constexpr std::array<CellularMutationSpec, cellular_catalog_size> catalog{
|
||||
// Movement (section 5).
|
||||
spec({1, "strong_propulsion", "Strong Propulsion", "Top speed and acceleration +5%",
|
||||
"More energy per second at full thrust, and per distance", E::adjust, T::move_output, 1,
|
||||
T::move_efficiency, -1, P::none, A::always, accumulate(S::thrust, 10 * second),
|
||||
absence(S::thrust, 30 * minute)}),
|
||||
spec({2, "economical_propulsion", "Economical Propulsion",
|
||||
"Drifts further after letting go; top speed +5%",
|
||||
"Stops more slowly; slides more in turns", E::adjust, T::move_coasting, 1, T::none, 0,
|
||||
P::none, A::always, regular(15 * second, minute), absence(S::thrust, 2 * minute)}),
|
||||
spec({3, "efficient_propulsion", "Efficient Propulsion", "Less energy per distance",
|
||||
"Top speed and acceleration -2%", E::adjust, T::move_efficiency, 1, T::move_output, -1,
|
||||
P::none, A::always, share(S::energy_movement, S::energy_spent, minute),
|
||||
absence(S::thrust, 5 * minute)}),
|
||||
spec({4, "agile_steering", "Agile Steering", "Turns 5% faster",
|
||||
"Top speed and acceleration -2%", E::adjust, T::move_steering, 1, T::move_output, -1,
|
||||
P::none, A::always, turns(5, 20 * second), straight(minute)}),
|
||||
spec({5, "streamlined_body", "Streamlined Body", "Top speed +5% and longer forward glide",
|
||||
"Turns 2% slower", E::adjust, T::move_streamlining, 1, T::move_steering, -1, P::none,
|
||||
A::always, straight(20 * second), turns(5, 20 * second)}),
|
||||
spec({6, "strong_brakes", "Strong Brakes", "Stops and reverses 5% faster",
|
||||
"Top speed -2%; each brake draws more energy", E::adjust, T::move_braking, 1,
|
||||
T::move_output, -1, P::none, A::always, count(S::brake_press, 5, minute),
|
||||
absence(S::brake_press, 10 * minute)}),
|
||||
spec({7, "light_propulsion", "Light Propulsion", "Less energy per second and per distance",
|
||||
"Top speed -5%", E::adjust, T::move_output, -1, T::move_efficiency, 1, P::none, A::always,
|
||||
absence(S::thrust, 30 * minute), accumulate(S::thrust, 10 * second)}),
|
||||
spec({8, "damped_body", "Damped Body", "Stops sooner, slides less",
|
||||
"Top speed -5%; more energy per distance", E::adjust, T::move_coasting, -1, T::none, 0,
|
||||
P::none, A::always, absence(S::thrust, 2 * minute), regular(15 * second, minute)}),
|
||||
spec({9, "overdriven_propulsion", "Overdriven Propulsion", "Top speed +2%",
|
||||
"More energy per second and per distance", E::adjust, T::move_efficiency, -1,
|
||||
T::move_output, 1, P::none, A::always, absence(S::thrust, 5 * minute),
|
||||
share(S::energy_movement, S::energy_spent, minute)}),
|
||||
spec({10, "forward_drive", "Forward Drive", "Top speed +2%", "Turns 5% slower", E::adjust,
|
||||
T::move_steering, -1, T::move_output, 1, P::none, A::always, straight(minute),
|
||||
turns(5, 20 * second)}),
|
||||
spec({11, "rounded_body", "Rounded Body", "Turns 2% faster", "Top speed -5%", E::adjust,
|
||||
T::move_streamlining, -1, T::move_steering, 1, P::none, A::always, turns(5, 20 * second),
|
||||
straight(20 * second)}),
|
||||
spec({12, "light_brakes", "Light Brakes", "Top speed +2%", "Stops 5% slower", E::adjust,
|
||||
T::move_braking, -1, T::move_output, 1, P::none, A::always,
|
||||
absence(S::brake_press, 10 * minute), count(S::brake_press, 5, minute)}),
|
||||
// Feeding (section 6).
|
||||
spec({13, "carbon_gatherer", "Carbon Gatherer", "Collects carbon 5% faster",
|
||||
"Collects N, P and other matter 1% slower each", E::adjust, T::collect_carbon, 1, T::none,
|
||||
0, P::collect_matter, A::always, share(S::collected_carbon, S::collected_matter, minute),
|
||||
absence(S::collected_carbon, 5 * minute)}),
|
||||
spec({14, "nitrogen_gatherer", "Nitrogen Gatherer", "Collects nitrogen 5% faster",
|
||||
"Collects C, P and other matter 1% slower each", E::adjust, T::collect_nitrogen, 1,
|
||||
T::none, 0, P::collect_matter, A::always,
|
||||
share(S::collected_nitrogen, S::collected_matter, minute),
|
||||
absence(S::collected_nitrogen, 5 * minute)}),
|
||||
spec({15, "phosphorus_gatherer", "Phosphorus Gatherer", "Collects phosphorus 5% faster",
|
||||
"Collects C, N and other matter 1% slower each", E::adjust, T::collect_phosphorus, 1,
|
||||
T::none, 0, P::collect_matter, A::always,
|
||||
share(S::collected_phosphorus, S::collected_matter, minute),
|
||||
absence(S::collected_phosphorus, 5 * minute)}),
|
||||
spec({16, "other_matter_gatherer", "Other Matter Gatherer", "Collects other matter 5% faster",
|
||||
"Collects C, N and P 1% slower each", E::adjust, T::collect_other, 1, T::none, 0,
|
||||
P::collect_matter, A::always, share(S::collected_other, S::collected_matter, minute),
|
||||
absence(S::collected_other, 5 * minute)}),
|
||||
spec({17, "big_appetite", "Big Appetite", "Takes in 5% more per second at rich food",
|
||||
"More upkeep", E::adjust, T::intake, 1, T::upkeep_efficiency, -1, P::none, A::always,
|
||||
accumulate(S::intake_limited, 10 * second), absence(S::fed, 5 * minute)}),
|
||||
spec({18, "small_appetite", "Small Appetite", "Less upkeep", "Takes in 5% less at rich food",
|
||||
E::adjust, T::intake, -1, T::upkeep_efficiency, 1, P::none, A::always,
|
||||
absence(S::fed, 5 * minute), accumulate(S::intake_limited, 10 * second)}),
|
||||
// Energy (section 6).
|
||||
spec({19, "thorough_extraction", "Thorough Extraction", "More energy from each energy bubble",
|
||||
"Drains energy bubbles 2% slower", E::adjust, T::energy_kept, 1, T::collect_energy, -1,
|
||||
P::none, A::always, accumulate(S::burned_matter, 30 * second),
|
||||
absence(S::burned_matter, 10 * minute)}),
|
||||
spec({20, "rapid_extraction", "Rapid Extraction", "Drains energy bubbles 5% faster",
|
||||
"Keeps slightly less of each", E::adjust, T::collect_energy, 1, T::energy_kept, -1,
|
||||
P::none, A::always, accumulate(S::energy_limited, 10 * second),
|
||||
absence(S::energy_collected, 5 * minute)}),
|
||||
spec({21, "carbon_metabolism", "Carbon Metabolism", "Carbon covers 5% more energy when burned",
|
||||
"N, P and other matter cover 1% less each", E::adjust, T::burn_carbon, 1, T::none, 0,
|
||||
P::burn, A::always, accumulate(S::burned_carbon, 30 * second),
|
||||
absence(S::burned_carbon, 10 * minute)}),
|
||||
spec({22, "nitrogen_metabolism", "Nitrogen Metabolism", "Nitrogen covers 5% more energy",
|
||||
"C, P and other matter cover 1% less each", E::adjust, T::burn_nitrogen, 1, T::none, 0,
|
||||
P::burn, A::always, accumulate(S::burned_nitrogen, 30 * second),
|
||||
absence(S::burned_nitrogen, 10 * minute)}),
|
||||
spec({23, "phosphorus_metabolism", "Phosphorus Metabolism", "Phosphorus covers 5% more energy",
|
||||
"C, N and other matter cover 1% less each", E::adjust, T::burn_phosphorus, 1, T::none, 0,
|
||||
P::burn, A::always, accumulate(S::burned_phosphorus, 30 * second),
|
||||
absence(S::burned_phosphorus, 10 * minute)}),
|
||||
spec({24, "other_matter_metabolism", "Other Matter Metabolism",
|
||||
"Other matter covers 5% more energy", "C, N and P cover 1% less each", E::adjust,
|
||||
T::burn_other, 1, T::none, 0, P::burn, A::always,
|
||||
accumulate(S::burned_other, 30 * second), absence(S::burned_other, 10 * minute)}),
|
||||
// Photosynthesis (section 6).
|
||||
spec({25, "acquire_photosynthesis", "Acquire Photosynthesis", "Steady energy from light",
|
||||
"Producer tissue adds mass and upkeep; drains energy bubbles slower", E::acquire_light,
|
||||
T::light, 1, T::collect_energy, -1, P::none, A::light_absent_after_carbon,
|
||||
accumulate(S::burned_carbon, 30 * second),
|
||||
accumulate(S::burned_not_carbon, 30 * second)}),
|
||||
spec({26, "expand_photosynthesis", "Expand Photosynthesis", "5% more energy from light",
|
||||
"More tissue mass and upkeep; drains energy bubbles slower", E::adjust, T::light, 1,
|
||||
T::collect_energy, -1, P::none, A::light_present, accumulate(S::drifting, minute),
|
||||
accumulate(S::thrust, 2 * minute)}),
|
||||
spec({27, "reduce_photosynthesis", "Reduce Photosynthesis",
|
||||
"Less tissue, lighter; drains energy bubbles faster", "Less energy from light", E::adjust,
|
||||
T::light, -1, T::collect_energy, 1, P::none, A::light_present,
|
||||
accumulate(S::thrust, 2 * minute), accumulate(S::drifting, minute)}),
|
||||
spec({28, "shed_photosynthesis", "Shed Photosynthesis", "No producer mass or upkeep",
|
||||
"No light energy", E::shed_light, T::light, -1, T::none, 0, P::none, A::light_present,
|
||||
accumulate(S::thrust, 5 * minute), accumulate(S::drifting, minute), 25}),
|
||||
// Predation and defense (section 7).
|
||||
spec({29, "reinforced_membrane", "Reinforced Membrane",
|
||||
"Predators need 5% more mass to engulf you",
|
||||
"More membrane mass and upkeep, slower collection, heavier", E::adjust, T::membrane, 1,
|
||||
T::none, 0, P::none, A::always, accumulate(S::larger_contact, 5 * second),
|
||||
absence(S::larger_contact, 10 * minute)}),
|
||||
spec({30, "thin_membrane", "Thin Membrane", "Faster collection, less mass and upkeep",
|
||||
"Easier to engulf", E::adjust, T::membrane, -1, T::none, 0, P::none, A::always,
|
||||
absence(S::larger_contact, 10 * minute), accumulate(S::larger_contact, 5 * second)}),
|
||||
spec({31, "expanding_chamber", "Expanding Chamber", "Can engulf prey 5% bigger",
|
||||
"Stretched wall: slightly easier to engulf yourself", E::adjust, T::engulf_capacity, 1,
|
||||
T::membrane, -1, P::none, A::always, count(S::engulf_too_large_or_membrane, 3, 0),
|
||||
absence(S::engulf_attempt, 10 * minute)}),
|
||||
spec({32, "small_mouth", "Small Mouth", "Tougher wall", "Only smaller prey", E::adjust,
|
||||
T::engulf_capacity, -1, T::membrane, 1, P::none, A::always,
|
||||
absence(S::engulf_attempt, 10 * minute), count(S::engulf_too_large, 3, 0)}),
|
||||
spec({33, "rapid_digestion", "Rapid Digestion", "Takes 5% more from a kill straight away",
|
||||
"Slower everyday feeding", E::adjust, T::kill_absorption, 1, T::intake, -1, P::none,
|
||||
A::always, event(S::engulf_success), absence(S::engulf_success, 15 * minute)}),
|
||||
spec({34, "grazers_gut", "Grazer's Gut", "Faster everyday feeding", "Takes less from kills",
|
||||
E::adjust, T::kill_absorption, -1, T::intake, 1, P::none, A::always,
|
||||
absence(S::engulf_success, 15 * minute), event(S::engulf_success)}),
|
||||
// Body and maintenance (section 8).
|
||||
spec({35, "larger_body", "Larger Body",
|
||||
"Harder to engulf, can engulf bigger prey, touches more food",
|
||||
"More upkeep, costlier buds and regrowth, heavier and slower", E::adjust, T::size, 1,
|
||||
T::none, 0, P::none, A::always, accumulate(S::reserve_over_body, minute),
|
||||
accumulate(S::burned_matter, 30 * second)}),
|
||||
spec({36, "smaller_body", "Smaller Body", "Less upkeep, cheaper buds and regrowth, quicker",
|
||||
"Easier to engulf, smaller prey only", E::adjust, T::size, -1, T::none, 0, P::none,
|
||||
A::always, accumulate(S::burned_matter, 30 * second),
|
||||
accumulate(S::reserve_over_body, minute)}),
|
||||
spec({37, "carbon_thrifty_body", "Carbon-Thrifty Body",
|
||||
"Building, regrowth and buds need less carbon", "Need more N, P and other matter",
|
||||
E::recipe, T::recipe_carbon, -1, T::none, 0, P::none, A::always,
|
||||
accumulate(S::blocked_carbon, 10 * second),
|
||||
share(S::collected_carbon, S::collected_matter, minute), 100, 20}),
|
||||
spec({38, "nitrogen_thrifty_body", "Nitrogen-Thrifty Body", "Need less nitrogen",
|
||||
"Need more C, P and other matter", E::recipe, T::recipe_nitrogen, -1, T::none, 0, P::none,
|
||||
A::always, accumulate(S::blocked_nitrogen, 10 * second),
|
||||
share(S::collected_nitrogen, S::collected_matter, minute), 100, 20}),
|
||||
spec({39, "phosphorus_thrifty_body", "Phosphorus-Thrifty Body", "Need less phosphorus",
|
||||
"Need more C, N and other matter", E::recipe, T::recipe_phosphorus, -1, T::none, 0,
|
||||
P::none, A::always, accumulate(S::blocked_phosphorus, 10 * second),
|
||||
share(S::collected_phosphorus, S::collected_matter, minute), 100, 20}),
|
||||
spec({40, "other_thrifty_body", "Other-Thrifty Body", "Need less other matter",
|
||||
"Need more C, N and P", E::recipe, T::recipe_other, -1, T::none, 0, P::none, A::always,
|
||||
accumulate(S::blocked_other, 10 * second),
|
||||
share(S::collected_other, S::collected_matter, minute), 100, 20}),
|
||||
spec({41, "efficient_upkeep", "Efficient Upkeep",
|
||||
"Lower upkeep, never below the essential floor", "Regrows 2% slower", E::adjust,
|
||||
T::upkeep_efficiency, 1, T::growth_speed, -1, P::none, A::always,
|
||||
share(S::energy_upkeep, S::energy_spent, 5 * minute), accumulate(S::regrowing, minute)}),
|
||||
spec({42, "rapid_growth", "Rapid Growth", "Regrows 5% faster", "More upkeep", E::adjust,
|
||||
T::growth_speed, 1, T::upkeep_efficiency, -1, P::none, A::always,
|
||||
accumulate(S::regrowing, minute), accumulate(S::fully_grown, 10 * minute)}),
|
||||
spec({43, "dense_tissue", "Dense Tissue",
|
||||
"Smaller footprint for the same mass, lighter membrane, less upkeep",
|
||||
"Less surface: slower collection, touches less food", E::adjust, T::density, 1, T::none,
|
||||
0, P::none, A::always, accumulate(S::larger_contact, 5 * second),
|
||||
accumulate(S::fed, minute)}),
|
||||
spec({44, "light_tissue", "Light Tissue",
|
||||
"Larger surface: faster collection, touches more food",
|
||||
"More membrane mass and upkeep, bigger target", E::adjust, T::density, -1, T::none, 0,
|
||||
P::none, A::always, accumulate(S::fed, minute),
|
||||
accumulate(S::larger_contact, 5 * second)}),
|
||||
// Reproduction (section 9).
|
||||
spec({45, "bigger_buds", "Bigger Buds", "Buds start 5% larger and survive more often",
|
||||
"Each bud costs 5% more", E::adjust, T::bud_share, 1, T::none, 0, P::none, A::always,
|
||||
event(S::bud_lost_early), event(S::bud_refused)}),
|
||||
spec({46, "smaller_buds", "Smaller Buds", "Each bud is cheaper",
|
||||
"Buds start smaller and more vulnerable", E::adjust, T::bud_share, -1, T::none, 0,
|
||||
P::none, A::always, event(S::bud_refused), event(S::bud_lost_early)}),
|
||||
spec({47, "twinning", "Twinning", "More offspring per bud",
|
||||
"Each offspring starts smaller and more vulnerable", E::adjust, T::litter, 1, T::none, 0,
|
||||
P::none, A::always, event(S::bud_with_surplus), event(S::bud_lost_early), 100, 25}),
|
||||
spec({48, "single_bud", "Single Bud", "Each offspring starts larger", "Fewer offspring per bud",
|
||||
E::adjust, T::litter, -1, T::none, 0, P::none, A::always, event(S::bud_lost_early),
|
||||
event(S::bud_with_surplus), 100, 25}),
|
||||
spec({49, "adhesion", "Adhesion",
|
||||
"Become a multicellular organism: the path to emerging into the world",
|
||||
"Permanent: no more splits, one shared reserve, engulfed whole, heavier",
|
||||
E::acquire_adhesion, T::none, 0, T::none, 0, P::none, A::adhesion_absent_after_buds,
|
||||
event(S::bud_done), absence(S::bud_done, 15 * minute), 50}),
|
||||
};
|
||||
|
||||
// The founder's value of a point-based trait over 100: one point in the trait's units.
|
||||
std::uint64_t unit_of(T trait) noexcept {
|
||||
const auto& f = primitive_founder_parameters;
|
||||
switch (trait) {
|
||||
case T::intake:
|
||||
return f.ingestion / 100;
|
||||
case T::energy_kept:
|
||||
return f.energy_conversion / 100;
|
||||
case T::light:
|
||||
return photosynthesis_acquired_start / 100;
|
||||
case T::membrane:
|
||||
return founder_membrane_nanometres / 100;
|
||||
case T::size:
|
||||
return f.target_size / 100;
|
||||
case T::bud_share:
|
||||
return f.reproduction / 100;
|
||||
default:
|
||||
return 1; // Indices are points already; recipe and litter move in their own units.
|
||||
}
|
||||
}
|
||||
std::optional<Capability> capability_of(T trait) noexcept {
|
||||
switch (trait) {
|
||||
case T::intake:
|
||||
return Capability::ingestion;
|
||||
case T::energy_kept:
|
||||
return Capability::energy_conversion;
|
||||
case T::light:
|
||||
return Capability::photosynthesis;
|
||||
case T::membrane:
|
||||
return Capability::membrane;
|
||||
case T::size:
|
||||
return Capability::target_size;
|
||||
case T::bud_share:
|
||||
return Capability::reproduction;
|
||||
default:
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
// The trait's slot in the draft; null when absent (a capability the plan does not carry).
|
||||
std::uint16_t* index_of(GenomeDraft& draft, T trait) noexcept {
|
||||
if (!draft.movement_arrangement || !draft.cellular_traits)
|
||||
return nullptr;
|
||||
auto& a = *draft.movement_arrangement;
|
||||
auto& t = *draft.cellular_traits;
|
||||
switch (trait) {
|
||||
case T::move_output:
|
||||
return &a.output;
|
||||
case T::move_efficiency:
|
||||
return &a.efficiency;
|
||||
case T::move_steering:
|
||||
return &a.steering;
|
||||
case T::move_streamlining:
|
||||
return &a.streamlining;
|
||||
case T::move_braking:
|
||||
return &a.braking;
|
||||
case T::move_coasting:
|
||||
return &a.coasting;
|
||||
case T::collect_energy:
|
||||
return &t.collection[0];
|
||||
case T::collect_carbon:
|
||||
return &t.collection[1];
|
||||
case T::collect_nitrogen:
|
||||
return &t.collection[2];
|
||||
case T::collect_phosphorus:
|
||||
return &t.collection[3];
|
||||
case T::collect_other:
|
||||
return &t.collection[4];
|
||||
case T::burn_carbon:
|
||||
return &t.burn[0];
|
||||
case T::burn_nitrogen:
|
||||
return &t.burn[1];
|
||||
case T::burn_phosphorus:
|
||||
return &t.burn[2];
|
||||
case T::burn_other:
|
||||
return &t.burn[3];
|
||||
case T::engulf_capacity:
|
||||
return &t.engulf_capacity;
|
||||
case T::kill_absorption:
|
||||
return &t.kill_absorption;
|
||||
case T::upkeep_efficiency:
|
||||
return &t.upkeep_efficiency;
|
||||
case T::growth_speed:
|
||||
return &t.growth_speed;
|
||||
case T::density:
|
||||
return &t.density;
|
||||
case T::litter:
|
||||
return &t.litter;
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
// Moves one trait by `units` (its own units) in direction `sign`, within bounds. False when
|
||||
// the trait is absent or the move would leave its bounds.
|
||||
bool move(GenomeDraft& draft, T trait, std::int8_t sign, std::uint64_t units,
|
||||
const CellularCatalogBounds& bounds) {
|
||||
if (trait == T::none || sign == 0 || units == 0)
|
||||
return trait == T::none || units == 0;
|
||||
const auto unit = unit_of(trait);
|
||||
const bool litter = trait == T::litter;
|
||||
const auto lowest = litter ? 0 : std::uint64_t{bounds.min_points} * unit;
|
||||
const auto highest =
|
||||
litter ? std::uint64_t{bounds.max_litter} : std::uint64_t{bounds.max_points} * unit;
|
||||
const auto step = litter ? units : units * unit;
|
||||
const auto shifted = [&](std::uint64_t value) -> std::optional<std::uint64_t> {
|
||||
if (sign > 0)
|
||||
return value > highest || step > highest - value ? std::nullopt
|
||||
: std::optional{value + step};
|
||||
return value < lowest + step ? std::nullopt : std::optional{value - step};
|
||||
};
|
||||
if (const auto capability = capability_of(trait)) {
|
||||
const auto record =
|
||||
std::ranges::find(draft.capabilities, *capability, &CapabilityRecord::kind);
|
||||
if (record == draft.capabilities.end())
|
||||
return false;
|
||||
const auto next = shifted(record->parameter);
|
||||
const auto& limits =
|
||||
(*capability_schema_for(draft.schema_version))[static_cast<std::size_t>(*capability)];
|
||||
if (!next || *next < limits.minimum || *next > limits.maximum)
|
||||
return false;
|
||||
record->parameter = static_cast<std::uint32_t>(*next);
|
||||
return true;
|
||||
}
|
||||
auto* slot = index_of(draft, trait);
|
||||
if (slot == nullptr)
|
||||
return false;
|
||||
const auto next = shifted(*slot);
|
||||
if (!next || *next > std::numeric_limits<std::uint16_t>::max())
|
||||
return false;
|
||||
*slot = static_cast<std::uint16_t>(*next);
|
||||
return true;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::span<const CellularMutationSpec> cellular_catalog() noexcept {
|
||||
return catalog;
|
||||
}
|
||||
const CellularMutationSpec* cellular_mutation(std::uint16_t id) noexcept {
|
||||
const auto found = std::ranges::find(catalog, id, &CellularMutationSpec::id);
|
||||
return found == catalog.end() ? nullptr : &*found;
|
||||
}
|
||||
|
||||
CellularMutationTuning cellular_default_tuning(const CellularMutationSpec& spec) noexcept {
|
||||
CellularMutationTuning tuning{};
|
||||
tuning.base_weight = spec.base_weight;
|
||||
tuning.primary_step = spec.primary_step;
|
||||
return tuning;
|
||||
}
|
||||
|
||||
std::optional<GenomeDraft>
|
||||
apply_cellular_mutation(const Genome& plan, const CellularMutationSpec& spec,
|
||||
const CellularMutationTuning& tuning, const CellularCatalogBounds& bounds,
|
||||
const CellularLineageProgress& progress, std::uint64_t successor_id,
|
||||
std::uint64_t origin_event) {
|
||||
if (plan.schema_version() < genome_schema_v7 || !plan.cellular_traits() ||
|
||||
!plan.movement_arrangement() || !tuning.enabled)
|
||||
return std::nullopt;
|
||||
GenomeDraft draft;
|
||||
draft.schema_version = plan.schema_version();
|
||||
draft.id = successor_id;
|
||||
draft.origin_event = origin_event;
|
||||
draft.development_version = plan.development_version();
|
||||
draft.parents = {plan.id()};
|
||||
draft.capabilities.assign(plan.capabilities().begin(), plan.capabilities().end());
|
||||
draft.movement_arrangement = plan.movement_arrangement();
|
||||
draft.cellular_traits = plan.cellular_traits();
|
||||
if (!draft.cellular_traits)
|
||||
return std::nullopt;
|
||||
auto& traits = *draft.cellular_traits;
|
||||
const bool producer = plan.carries(Capability::photosynthesis);
|
||||
switch (spec.available) {
|
||||
case A::always:
|
||||
break;
|
||||
case A::light_absent_after_carbon:
|
||||
if (producer || progress.carbon_burned < bounds.carbon_for_light)
|
||||
return std::nullopt;
|
||||
break;
|
||||
case A::light_present:
|
||||
if (!producer)
|
||||
return std::nullopt;
|
||||
break;
|
||||
case A::adhesion_absent_after_buds:
|
||||
if (traits.adhesion != 0 || progress.buds < bounds.buds_for_adhesion)
|
||||
return std::nullopt;
|
||||
break;
|
||||
}
|
||||
switch (spec.effect) {
|
||||
case E::adjust:
|
||||
if (!move(draft, spec.primary, spec.primary_sign, tuning.primary_step, bounds) ||
|
||||
!move(draft, spec.paired, spec.paired_sign, tuning.paired_step, bounds))
|
||||
return std::nullopt;
|
||||
if (spec.spread != P::none) {
|
||||
const auto group = spec.spread == P::burn
|
||||
? std::array{T::burn_carbon, T::burn_nitrogen,
|
||||
T::burn_phosphorus, T::burn_other}
|
||||
: std::array{T::collect_carbon, T::collect_nitrogen,
|
||||
T::collect_phosphorus, T::collect_other};
|
||||
for (const auto other : group)
|
||||
if (other != spec.primary &&
|
||||
!move(draft, other, static_cast<std::int8_t>(-spec.primary_sign),
|
||||
tuning.spread_step, bounds))
|
||||
return std::nullopt;
|
||||
}
|
||||
break;
|
||||
case E::recipe: {
|
||||
auto& recipe = traits.recipe;
|
||||
const auto own =
|
||||
static_cast<std::size_t>(spec.primary) - static_cast<std::size_t>(T::recipe_carbon);
|
||||
const auto step = tuning.primary_step;
|
||||
if (own >= recipe.size() || recipe[own] < bounds.recipe_min_share + step)
|
||||
return std::nullopt;
|
||||
recipe[own] = static_cast<std::uint16_t>(recipe[own] - step);
|
||||
// The other three share the step; the first of them takes the remainder.
|
||||
auto share = step / 3;
|
||||
auto remainder = step - share * 3;
|
||||
for (std::size_t c = 0; c < recipe.size(); ++c) {
|
||||
if (c == own)
|
||||
continue;
|
||||
recipe[c] = static_cast<std::uint16_t>(recipe[c] + share + remainder);
|
||||
remainder = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case E::acquire_light:
|
||||
if (producer)
|
||||
return std::nullopt;
|
||||
draft.capabilities.push_back(
|
||||
{Capability::photosynthesis, photosynthesis_acquired_start, 0});
|
||||
if (!move(draft, spec.paired, spec.paired_sign, tuning.paired_step, bounds))
|
||||
return std::nullopt;
|
||||
break;
|
||||
case E::shed_light:
|
||||
std::erase_if(draft.capabilities, [](const CapabilityRecord& record) {
|
||||
return record.kind == Capability::photosynthesis;
|
||||
});
|
||||
break;
|
||||
case E::acquire_adhesion:
|
||||
traits.adhesion = 1;
|
||||
break;
|
||||
}
|
||||
// The genome's own validation (ranges, prerequisites) decides the rest.
|
||||
if (!Genome::create(draft))
|
||||
return std::nullopt;
|
||||
return draft;
|
||||
}
|
||||
} // namespace evo::biology
|
||||
@@ -0,0 +1,118 @@
|
||||
#include "evo/biology/cellular_offers.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <numeric>
|
||||
|
||||
namespace evo::biology {
|
||||
namespace {
|
||||
// Weights above this keep every product below 2^63 (weights x 10^7 targets).
|
||||
constexpr std::uint64_t max_weight = 1'000'000'000;
|
||||
} // namespace
|
||||
|
||||
std::uint64_t cellular_offer_weight(std::uint32_t base_weight, std::int32_t score,
|
||||
const CellularOfferRules& rules) noexcept {
|
||||
std::int64_t multiplier = 1'000;
|
||||
if (score > 0 && rules.score_max > 0)
|
||||
multiplier += (static_cast<std::int64_t>(rules.max_multiplier_per_mille) - 1'000) *
|
||||
std::min(score, rules.score_max) / rules.score_max;
|
||||
if (score < 0 && rules.score_min < 0)
|
||||
multiplier -= (1'000 - static_cast<std::int64_t>(rules.min_multiplier_per_mille)) *
|
||||
std::max(score, rules.score_min) / rules.score_min;
|
||||
multiplier = std::max<std::int64_t>(multiplier, 0);
|
||||
return std::uint64_t{base_weight} * static_cast<std::uint64_t>(multiplier);
|
||||
}
|
||||
|
||||
std::optional<std::vector<std::uint32_t>>
|
||||
cellular_offer_chances(std::span<const std::uint64_t> weights, const CellularOfferRules& rules) {
|
||||
const std::uint64_t m = weights.size();
|
||||
const std::uint64_t target = std::uint64_t{rules.options} * cellular_chance_whole;
|
||||
const std::uint64_t floor = rules.floor_ppm, cap = rules.cap_ppm;
|
||||
if (m == 0 || rules.options == 0 || floor > cap || cap > cellular_chance_whole ||
|
||||
m * floor > target || m * cap < target ||
|
||||
std::ranges::any_of(weights, [](std::uint64_t w) { return w > max_weight; }))
|
||||
return std::nullopt;
|
||||
// Ascending by weight (stable on the given order): the lightest sit at the floor, the
|
||||
// heaviest at the cap, and the middle share what is left in proportion to weight.
|
||||
std::vector<std::size_t> order(weights.size());
|
||||
std::iota(order.begin(), order.end(), 0);
|
||||
std::ranges::stable_sort(order, {}, [&](std::size_t i) { return weights[i]; });
|
||||
std::vector<std::uint64_t> prefix(m + 1, 0);
|
||||
for (std::size_t i = 0; i < m; ++i)
|
||||
prefix[i + 1] = prefix[i] + weights[order[i]];
|
||||
for (std::uint64_t low = 0; low <= m; ++low) {
|
||||
for (std::uint64_t high = 0; low + high <= m; ++high) {
|
||||
const auto fixed = low * floor + high * cap;
|
||||
const auto mid_begin = low, mid_end = m - high;
|
||||
if (mid_begin == mid_end) {
|
||||
if (fixed != target)
|
||||
continue;
|
||||
std::vector<std::uint32_t> chances(m);
|
||||
for (std::size_t i = 0; i < m; ++i)
|
||||
chances[order[i]] = static_cast<std::uint32_t>(i < low ? floor : cap);
|
||||
return chances;
|
||||
}
|
||||
if (fixed > target)
|
||||
continue;
|
||||
const auto num = target - fixed; // <= 10^7 for at most 10 options.
|
||||
const auto den = prefix[mid_end] - prefix[mid_begin];
|
||||
if (den == 0)
|
||||
continue;
|
||||
// Middle within [floor, cap]; the floor group below it and the cap group above.
|
||||
const auto w_first = weights[order[mid_begin]];
|
||||
const auto w_last = weights[order[mid_end - 1]];
|
||||
if (num * w_first < floor * den || num * w_last > cap * den)
|
||||
continue;
|
||||
if (low > 0 && num * weights[order[low - 1]] > floor * den)
|
||||
continue;
|
||||
if (high > 0 && num * weights[order[mid_end]] < cap * den)
|
||||
continue;
|
||||
std::vector<std::uint32_t> chances(m);
|
||||
std::vector<std::uint64_t> remainder(m, 0);
|
||||
std::uint64_t total = fixed;
|
||||
for (std::size_t i = 0; i < m; ++i) {
|
||||
if (i < low || i >= mid_end) {
|
||||
chances[order[i]] = static_cast<std::uint32_t>(i < low ? floor : cap);
|
||||
continue;
|
||||
}
|
||||
const auto product = num * weights[order[i]];
|
||||
chances[order[i]] = static_cast<std::uint32_t>(product / den);
|
||||
remainder[i] = product % den;
|
||||
total += product / den;
|
||||
}
|
||||
// Rounding: the largest remainders (then the lighter order) take one ppm each.
|
||||
std::vector<std::size_t> middle;
|
||||
for (auto i = mid_begin; i < mid_end; ++i)
|
||||
middle.push_back(static_cast<std::size_t>(i));
|
||||
std::ranges::stable_sort(middle, std::ranges::greater{},
|
||||
[&](std::size_t i) { return remainder[i]; });
|
||||
for (std::size_t k = 0; total < target && k < middle.size(); ++k) {
|
||||
auto& chance = chances[order[middle[k]]];
|
||||
if (chance < cap) {
|
||||
++chance;
|
||||
++total;
|
||||
}
|
||||
}
|
||||
if (total != target)
|
||||
continue;
|
||||
return chances;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::vector<std::size_t> cellular_systematic_draw(std::span<const std::uint32_t> chances,
|
||||
std::uint32_t options, std::uint32_t u) noexcept {
|
||||
std::vector<std::size_t> picked;
|
||||
std::uint64_t point = u % cellular_chance_whole;
|
||||
std::uint64_t cumulative = 0;
|
||||
for (std::size_t i = 0; i < chances.size() && picked.size() < options; ++i) {
|
||||
cumulative += chances[i];
|
||||
// A chance of at most 100% can cover at most one point.
|
||||
if (point < cumulative) {
|
||||
picked.push_back(i);
|
||||
point += cellular_chance_whole;
|
||||
}
|
||||
}
|
||||
return picked;
|
||||
}
|
||||
} // namespace evo::biology
|
||||
@@ -0,0 +1,152 @@
|
||||
#include "evo/biology/cellular_triggers.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
|
||||
namespace evo::biology {
|
||||
namespace {
|
||||
// The smaller angle between two headings.
|
||||
std::uint16_t deviation(std::uint16_t a, std::uint16_t b) noexcept {
|
||||
const auto forward = static_cast<std::uint16_t>(a - b);
|
||||
const auto back = static_cast<std::uint16_t>(b - a);
|
||||
return std::min(forward, back);
|
||||
}
|
||||
std::uint64_t saturating_add(std::uint64_t a, std::uint64_t b) noexcept {
|
||||
return b > std::numeric_limits<std::uint64_t>::max() - a
|
||||
? std::numeric_limits<std::uint64_t>::max()
|
||||
: a + b;
|
||||
}
|
||||
// a * 1000 >= b * k, exactly, for a, b below 2^54.
|
||||
bool share_at_least(std::uint64_t part, std::uint64_t whole, std::uint32_t per_thousand) noexcept {
|
||||
constexpr std::uint64_t limit = 1ULL << 54U;
|
||||
if (part >= limit || whole >= limit) {
|
||||
part >>= 10U;
|
||||
whole >>= 10U;
|
||||
}
|
||||
return part * 1'000 >= whole * per_thousand;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
bool cellular_trigger_tick(const CellularTriggerSpec& spec, std::uint32_t threshold,
|
||||
CellularTriggerState& state, const CellularActivity& activity) noexcept {
|
||||
const auto ticks = threshold != 0 ? threshold : spec.ticks;
|
||||
const auto value = activity[spec.signal];
|
||||
const bool present = value != 0;
|
||||
switch (spec.kind) {
|
||||
case CellularTriggerKind::accumulate:
|
||||
if (present && ++state.first >= ticks) {
|
||||
state.first = 0;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
case CellularTriggerKind::absence:
|
||||
state.first = present ? 0 : state.first + 1;
|
||||
if (state.first >= ticks) {
|
||||
state.first = 0;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
case CellularTriggerKind::count:
|
||||
// A window opens at the first counted event; `ticks` 0 keeps it open forever.
|
||||
if (state.first != 0 && ticks != 0 && ++state.ticks >= ticks) {
|
||||
state.first = 0;
|
||||
state.ticks = 0;
|
||||
}
|
||||
if (present) {
|
||||
if (state.first == 0)
|
||||
state.ticks = 0;
|
||||
if (++state.first >= spec.count) {
|
||||
state.first = 0;
|
||||
state.ticks = 0;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
case CellularTriggerKind::event:
|
||||
return present;
|
||||
case CellularTriggerKind::share: {
|
||||
state.first = saturating_add(state.first, value);
|
||||
state.second = saturating_add(state.second, activity[spec.denominator]);
|
||||
if (++state.ticks < ticks)
|
||||
return false;
|
||||
const bool fired =
|
||||
state.second != 0 && share_at_least(state.first, state.second, spec.per_thousand);
|
||||
state = {};
|
||||
return fired;
|
||||
}
|
||||
case CellularTriggerKind::regular:
|
||||
// A run starts at the signal and breaks after a gap longer than `gap`.
|
||||
state.first = present ? 0 : state.first + 1;
|
||||
if (present && state.running == 0) {
|
||||
state.running = 1;
|
||||
state.ticks = 0;
|
||||
}
|
||||
if (state.running == 0)
|
||||
return false;
|
||||
if (state.first > spec.gap) {
|
||||
state.running = 0;
|
||||
return false;
|
||||
}
|
||||
if (++state.ticks >= ticks) {
|
||||
state.ticks = 0;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
case CellularTriggerKind::straight:
|
||||
if (!present)
|
||||
return false;
|
||||
if (state.running == 0 || deviation(activity.heading, state.heading) > spec.angle) {
|
||||
state.running = 1;
|
||||
state.heading = activity.heading;
|
||||
state.first = 0;
|
||||
}
|
||||
if (++state.first >= ticks) {
|
||||
state.first = 0;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
case CellularTriggerKind::turns: {
|
||||
if (state.running == 0) {
|
||||
state.running = 1;
|
||||
state.heading = activity.heading;
|
||||
return false;
|
||||
}
|
||||
if (state.first != 0 && ++state.ticks >= ticks) {
|
||||
state.first = 0;
|
||||
state.ticks = 0;
|
||||
}
|
||||
if (deviation(activity.heading, state.heading) <= spec.angle)
|
||||
return false;
|
||||
state.heading = activity.heading;
|
||||
if (state.first == 0)
|
||||
state.ticks = 0;
|
||||
if (++state.first >= spec.count) {
|
||||
state.first = 0;
|
||||
state.ticks = 0;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void cellular_affinity_tick(CellularAffinity& affinity, const CellularActivity& activity,
|
||||
std::span<const CellularMutationTuning> tuning,
|
||||
CellularScoreBounds bounds) noexcept {
|
||||
const auto catalog = cellular_catalog();
|
||||
for (std::size_t i = 0; i < catalog.size() && i < tuning.size(); ++i) {
|
||||
const auto& t = tuning[i];
|
||||
if (!t.enabled)
|
||||
continue;
|
||||
std::int32_t score = affinity.score[i];
|
||||
if (cellular_trigger_tick(catalog[i].increase, t.increase_ticks, affinity.increase[i],
|
||||
activity))
|
||||
score += t.increase_points;
|
||||
if (cellular_trigger_tick(catalog[i].decrease, t.decrease_ticks, affinity.decrease[i],
|
||||
activity))
|
||||
score -= t.decrease_points;
|
||||
affinity.score[i] = static_cast<std::int16_t>(std::clamp(score, bounds.min, bounds.max));
|
||||
}
|
||||
}
|
||||
} // namespace evo::biology
|
||||
@@ -0,0 +1,207 @@
|
||||
#include "evo/biology/cellular_catalog.hpp"
|
||||
#include "evo/biology/founder.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <set>
|
||||
#include <source_location>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
// The rules-13 named mutation catalog (ADR-0179; Cellular_mutation_plan.md): stable identities,
|
||||
// the eligible pool at birth, each kind of edit, gates and bounds.
|
||||
namespace {
|
||||
using namespace evo;
|
||||
using namespace evo::biology;
|
||||
|
||||
void check(bool condition, std::source_location at = std::source_location::current()) {
|
||||
if (!condition)
|
||||
throw std::runtime_error("cellular catalog line " + std::to_string(at.line()));
|
||||
}
|
||||
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
||||
|
||||
Genome founder() {
|
||||
const auto genome = primitive_founder(1, 1, primitive_founder_v6);
|
||||
check(genome.has_value());
|
||||
return genome.value_or(*primitive_founder(1, 1, primitive_founder_v6));
|
||||
}
|
||||
std::uint64_t next_id = 100;
|
||||
std::optional<Genome> mutate(const Genome& plan, std::uint16_t id,
|
||||
const CellularCatalogBounds& bounds = {},
|
||||
const CellularLineageProgress& progress = {}) {
|
||||
const auto* spec = cellular_mutation(id);
|
||||
check(spec != nullptr);
|
||||
if (spec == nullptr)
|
||||
return std::nullopt;
|
||||
const auto draft = apply_cellular_mutation(plan, *spec, cellular_default_tuning(*spec), bounds,
|
||||
progress, ++next_id, 7);
|
||||
if (!draft)
|
||||
return std::nullopt;
|
||||
const auto made = Genome::create(*draft);
|
||||
check(made.has_value());
|
||||
return made ? std::optional{*made} : std::nullopt;
|
||||
}
|
||||
std::uint32_t parameter(const Genome& genome, Capability kind) {
|
||||
const auto value = genome.parameter(kind);
|
||||
return value ? *value : 0;
|
||||
}
|
||||
CellularTraits traits(const Genome& genome) {
|
||||
return genome.cellular_traits().value_or(CellularTraits{});
|
||||
}
|
||||
MovementArrangement arrangement(const Genome& genome) {
|
||||
return genome.movement_arrangement().value_or(MovementArrangement{});
|
||||
}
|
||||
|
||||
void identities_are_stable_and_unique() {
|
||||
const auto all = cellular_catalog();
|
||||
CHECK(all.size() == cellular_catalog_size);
|
||||
std::set<std::uint16_t> ids;
|
||||
std::set<std::string_view> keys;
|
||||
for (const auto& spec : all) {
|
||||
ids.insert(spec.id);
|
||||
keys.insert(spec.key);
|
||||
CHECK(!spec.name.empty() && !spec.pro.empty() && !spec.con.empty());
|
||||
for (const auto& trigger : {spec.increase, spec.decrease})
|
||||
CHECK(trigger.kind == CellularTriggerKind::event ||
|
||||
trigger.kind == CellularTriggerKind::count || trigger.ticks != 0);
|
||||
CHECK(cellular_mutation(spec.id) == &spec);
|
||||
}
|
||||
CHECK(ids.size() == cellular_catalog_size && *ids.begin() == 1 &&
|
||||
*ids.rbegin() == cellular_catalog_size && keys.size() == cellular_catalog_size);
|
||||
CHECK(cellular_mutation(0) == nullptr && cellular_mutation(50) == nullptr);
|
||||
}
|
||||
|
||||
// 43 can be offered at birth (Cellular_mutation_plan.md section 9): every mutation but the
|
||||
// three needing photosynthesis, Acquire Photosynthesis before any carbon is burned, Single Bud
|
||||
// at litter 0, and Adhesion before five buds.
|
||||
void the_pool_at_birth() {
|
||||
const auto plan = founder();
|
||||
CellularCatalogBounds bounds{};
|
||||
bounds.carbon_for_light = 1'000'000;
|
||||
std::set<std::uint16_t> closed;
|
||||
for (const auto& spec : cellular_catalog())
|
||||
if (!mutate(plan, spec.id, bounds))
|
||||
closed.insert(spec.id);
|
||||
CHECK(closed == std::set<std::uint16_t>{25, 26, 27, 28, 48, 49});
|
||||
CHECK(mutate(plan, 25, bounds, {1'000'000, 0}).has_value());
|
||||
CHECK(mutate(plan, 49, bounds, {0, 5}).has_value());
|
||||
}
|
||||
|
||||
void each_kind_of_edit() {
|
||||
const auto plan = founder();
|
||||
const auto strong = mutate(plan, 1);
|
||||
CHECK(strong && arrangement(*strong).output == 105 && arrangement(*strong).efficiency == 98);
|
||||
CHECK(strong && strong.value().parents().size() == 1 &&
|
||||
strong.value().parents()[0] == plan.id() &&
|
||||
strong.value().schema_version() == genome_schema_v7);
|
||||
const auto light = mutate(plan, 7);
|
||||
CHECK(light && arrangement(*light).output == 95 && arrangement(*light).efficiency == 102);
|
||||
const auto coasting = mutate(plan, 2);
|
||||
CHECK(coasting && arrangement(*coasting).coasting == 105);
|
||||
const auto gatherer = mutate(plan, 13);
|
||||
CHECK(gatherer &&
|
||||
traits(*gatherer).collection == std::array<std::uint16_t, 5>{100, 105, 99, 99, 99});
|
||||
const auto metabolism = mutate(plan, 22);
|
||||
CHECK(metabolism && traits(*metabolism).burn == std::array<std::uint16_t, 4>{99, 105, 99, 99});
|
||||
const auto appetite = mutate(plan, 17);
|
||||
CHECK(appetite && parameter(*appetite, Capability::ingestion) == 574'000 + 28'700 &&
|
||||
traits(*appetite).upkeep_efficiency == 98);
|
||||
const auto thorough = mutate(plan, 19);
|
||||
CHECK(thorough && parameter(*thorough, Capability::energy_conversion) == 420 &&
|
||||
traits(*thorough).collection[0] == 98);
|
||||
const auto reinforced = mutate(plan, 29);
|
||||
CHECK(reinforced && parameter(*reinforced, Capability::membrane) == 1'050);
|
||||
const auto chamber = mutate(plan, 31);
|
||||
CHECK(chamber && traits(*chamber).engulf_capacity == 105 &&
|
||||
parameter(*chamber, Capability::membrane) == 980);
|
||||
const auto larger = mutate(plan, 35);
|
||||
CHECK(larger && parameter(*larger, Capability::target_size) == 8'400'000);
|
||||
const auto thrifty = mutate(plan, 37);
|
||||
CHECK(thrifty && traits(*thrifty).recipe == std::array<std::uint16_t, 4>{480, 158, 56, 306});
|
||||
const auto buds = mutate(plan, 45);
|
||||
CHECK(buds && parameter(*buds, Capability::reproduction) == 525);
|
||||
const auto twins = mutate(plan, 47);
|
||||
CHECK(twins && traits(*twins).litter == 25);
|
||||
const auto single = twins ? mutate(*twins, 48) : std::nullopt;
|
||||
CHECK(single && traits(*single).litter == 0);
|
||||
const auto adhesion = mutate(plan, 49, {}, {0, 5});
|
||||
CHECK(adhesion && traits(*adhesion).adhesion == 1 && !mutate(*adhesion, 49, {}, {0, 5}));
|
||||
}
|
||||
|
||||
void photosynthesis_comes_and_goes() {
|
||||
const auto plan = founder();
|
||||
const auto producer = mutate(plan, 25);
|
||||
CHECK(producer && parameter(*producer, Capability::photosynthesis) == 100 &&
|
||||
traits(*producer).collection[0] == 98 && !mutate(*producer, 25));
|
||||
if (!producer)
|
||||
return;
|
||||
const auto more = mutate(*producer, 26);
|
||||
CHECK(more && parameter(*more, Capability::photosynthesis) == 105 &&
|
||||
traits(*more).collection[0] == 96);
|
||||
const auto less = mutate(*producer, 27);
|
||||
CHECK(less && parameter(*less, Capability::photosynthesis) == 95 &&
|
||||
traits(*less).collection[0] == 100);
|
||||
const auto shed = mutate(*producer, 28);
|
||||
CHECK(shed && !shed->carries(Capability::photosynthesis) && mutate(*shed, 25));
|
||||
}
|
||||
|
||||
void bounds_and_switches() {
|
||||
// Output climbs to the 1,000-point bound and no further; efficiency never below 25.
|
||||
auto plan = founder();
|
||||
for (int i = 0; i < 400; ++i) {
|
||||
const auto next = mutate(plan, 1);
|
||||
if (!next)
|
||||
break;
|
||||
plan = *next;
|
||||
}
|
||||
CHECK(arrangement(plan).output == 1'000 || arrangement(plan).efficiency < 27);
|
||||
CHECK(!mutate(plan, 1));
|
||||
CHECK(arrangement(plan).efficiency >= 25);
|
||||
// A tighter bound closes it sooner.
|
||||
CellularCatalogBounds tight{};
|
||||
tight.max_points = 104;
|
||||
CHECK(!mutate(founder(), 1, tight));
|
||||
// A recipe never takes a channel below its minimum share: phosphorus starts at 50, so one
|
||||
// step of 20 reaches 30 and a second would pass the minimum of 20.
|
||||
const auto once = mutate(founder(), 39);
|
||||
CHECK(once && traits(*once).recipe[2] == 30 && !mutate(*once, 39));
|
||||
CellularCatalogBounds lenient{};
|
||||
lenient.recipe_min_share = 10;
|
||||
CHECK(once && mutate(*once, 39, lenient).has_value());
|
||||
// A disabled mutation is never available.
|
||||
const auto* spec = cellular_mutation(1);
|
||||
CHECK(spec != nullptr);
|
||||
if (spec == nullptr)
|
||||
return;
|
||||
auto off = cellular_default_tuning(*spec);
|
||||
off.enabled = false;
|
||||
CHECK(!apply_cellular_mutation(founder(), *spec, off, {}, {}, 999, 7));
|
||||
// Tuned steps change the edit.
|
||||
auto larger_step = cellular_default_tuning(*spec);
|
||||
larger_step.primary_step = 10;
|
||||
larger_step.paired_step = 0;
|
||||
const auto tuned = apply_cellular_mutation(founder(), *spec, larger_step, {}, {}, 999, 7);
|
||||
const auto tuned_arrangement =
|
||||
tuned ? tuned->movement_arrangement.value_or(MovementArrangement{}) : MovementArrangement{};
|
||||
CHECK(tuned_arrangement.output == 110 && tuned_arrangement.efficiency == 100);
|
||||
// Earlier schemas have no catalog.
|
||||
const auto older = primitive_founder(1, 1, primitive_founder_v5);
|
||||
CHECK(older &&
|
||||
!apply_cellular_mutation(*older, *spec, cellular_default_tuning(*spec), {}, {}, 999, 7));
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
try {
|
||||
identities_are_stable_and_unique();
|
||||
the_pool_at_birth();
|
||||
each_kind_of_edit();
|
||||
photosynthesis_comes_and_goes();
|
||||
bounds_and_switches();
|
||||
} catch (const std::exception& error) {
|
||||
std::fprintf(stderr, "%s\n", error.what());
|
||||
return 1;
|
||||
}
|
||||
std::puts("PASS cellular mutation catalog");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
#include "evo/biology/cellular_offers.hpp"
|
||||
#include "evo/biology/cellular_triggers.hpp"
|
||||
#include "evo/core/random.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstdio>
|
||||
#include <numeric>
|
||||
#include <set>
|
||||
#include <source_location>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// Rules-13 offers (ADR-0179; Cellular_mutation_plan.md section 3): triggers move affinity
|
||||
// scores, scores set weights, weights set floored and capped chances summing to the options,
|
||||
// and a systematic draw picks distinct mutations with exactly those chances.
|
||||
namespace {
|
||||
using namespace evo;
|
||||
using namespace evo::biology;
|
||||
|
||||
void check(bool condition, std::source_location at = std::source_location::current()) {
|
||||
if (!condition)
|
||||
throw std::runtime_error("cellular offers line " + std::to_string(at.line()));
|
||||
}
|
||||
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
||||
|
||||
CellularActivity with(CellularSignal signal, std::uint64_t value = 1, std::uint16_t heading = 0) {
|
||||
CellularActivity activity{};
|
||||
activity[signal] = value;
|
||||
activity.heading = heading;
|
||||
return activity;
|
||||
}
|
||||
// Runs a trigger over `ticks` ticks of `activity`, counting fires.
|
||||
std::uint32_t run(const CellularTriggerSpec& spec, CellularTriggerState& state,
|
||||
const CellularActivity& activity, std::uint32_t ticks) {
|
||||
std::uint32_t fired = 0;
|
||||
for (std::uint32_t t = 0; t < ticks; ++t)
|
||||
fired += cellular_trigger_tick(spec, 0, state, activity) ? 1U : 0U;
|
||||
return fired;
|
||||
}
|
||||
|
||||
void triggers() {
|
||||
using K = CellularTriggerKind;
|
||||
using S = CellularSignal;
|
||||
const auto thrusting = with(S::thrust);
|
||||
const CellularActivity idle{};
|
||||
// Accumulate: every 200 thrust ticks, idle ticks do not count.
|
||||
CellularTriggerState s{};
|
||||
const CellularTriggerSpec thrust10{K::accumulate, S::thrust, 200};
|
||||
CHECK(run(thrust10, s, thrusting, 199) == 0 && run(thrust10, s, idle, 50) == 0 &&
|
||||
run(thrust10, s, thrusting, 1) == 1 && run(thrust10, s, thrusting, 400) == 2);
|
||||
// Absence: fires after 100 idle ticks running; any thrust restarts it.
|
||||
s = {};
|
||||
const CellularTriggerSpec quiet{K::absence, S::thrust, 100};
|
||||
CHECK(run(quiet, s, idle, 99) == 0 && run(quiet, s, thrusting, 1) == 0 &&
|
||||
run(quiet, s, idle, 99) == 0 && run(quiet, s, idle, 1) == 1);
|
||||
// A tuned threshold replaces the spec's.
|
||||
s = {};
|
||||
CHECK(!cellular_trigger_tick(quiet, 2, s, idle) && cellular_trigger_tick(quiet, 2, s, idle));
|
||||
// Count: 3 presses within 10 ticks fire; spread out they do not.
|
||||
s = {};
|
||||
const CellularTriggerSpec presses{K::count, S::brake_press, 10, 3};
|
||||
const auto press = with(S::brake_press);
|
||||
CHECK(run(presses, s, press, 1) == 0 && run(presses, s, idle, 3) == 0 &&
|
||||
run(presses, s, press, 1) == 0 && run(presses, s, idle, 3) == 0 &&
|
||||
run(presses, s, press, 1) == 1); // The third press 8 ticks after the first.
|
||||
s = {};
|
||||
CHECK(run(presses, s, press, 1) + run(presses, s, idle, 12) + run(presses, s, press, 1) +
|
||||
run(presses, s, idle, 12) + run(presses, s, press, 1) ==
|
||||
0);
|
||||
// Count without a window: any 3.
|
||||
s = {};
|
||||
const CellularTriggerSpec failures{K::count, S::engulf_too_large, 0, 3};
|
||||
const auto fail = with(S::engulf_too_large);
|
||||
CHECK(run(failures, s, fail, 1) + run(failures, s, idle, 5'000) + run(failures, s, fail, 2) ==
|
||||
1);
|
||||
// Event: each tick it happens.
|
||||
s = {};
|
||||
CHECK(run({K::event, S::engulf_success}, s, with(S::engulf_success), 3) == 3);
|
||||
// Share: at the end of each 4-tick window, carbon at least half of all matter collected.
|
||||
s = {};
|
||||
const CellularTriggerSpec diet{K::share, S::collected_carbon, 4, 0, S::collected_matter, 500};
|
||||
CellularActivity rich{};
|
||||
rich[S::collected_carbon] = 6;
|
||||
rich[S::collected_matter] = 10;
|
||||
CellularActivity poor{};
|
||||
poor[S::collected_carbon] = 1;
|
||||
poor[S::collected_matter] = 10;
|
||||
CHECK(run(diet, s, rich, 3) == 0 && run(diet, s, rich, 1) == 1 && run(diet, s, poor, 4) == 0 &&
|
||||
run(diet, s, idle, 4) == 0);
|
||||
// Regular: thrust at least every 3 ticks for 10 ticks fires; a longer gap breaks the run.
|
||||
s = {};
|
||||
const CellularTriggerSpec pulsed{K::regular, S::thrust, 10, 0, {}, 0, 3};
|
||||
std::uint32_t fired = 0;
|
||||
for (int t = 0; t < 12; ++t)
|
||||
fired += cellular_trigger_tick(pulsed, 0, s, t % 3 == 0 ? thrusting : idle) ? 1U : 0U;
|
||||
CHECK(fired == 1);
|
||||
s = {};
|
||||
fired = 0;
|
||||
for (int t = 0; t < 30; ++t)
|
||||
fired += cellular_trigger_tick(pulsed, 0, s, t % 5 == 0 ? thrusting : idle) ? 1U : 0U;
|
||||
CHECK(fired == 0);
|
||||
// Straight: 5 thrust ticks within 20 degrees of one heading; a sharp change restarts.
|
||||
s = {};
|
||||
const CellularTriggerSpec line{K::straight, S::thrust, 5, 0, {}, 0, 0, 3'641};
|
||||
CHECK(run(line, s, with(S::thrust, 1, 1'000), 4) == 0 &&
|
||||
run(line, s, with(S::thrust, 1, 3'000), 1) == 1);
|
||||
s = {};
|
||||
CHECK(run(line, s, with(S::thrust, 1, 0), 4) == 0 &&
|
||||
run(line, s, with(S::thrust, 1, 10'000), 4) == 0 &&
|
||||
run(line, s, with(S::thrust, 1, 10'000), 1) == 1);
|
||||
// Turns: 3 turns of more than 90 degrees within 20 ticks.
|
||||
s = {};
|
||||
const CellularTriggerSpec weave{K::turns, S::thrust, 20, 3, {}, 0, 0, 16'384};
|
||||
fired = 0;
|
||||
for (const std::uint16_t heading :
|
||||
std::array<std::uint16_t, 5>{0, 20'000, 40'000, 60'000, 60'100})
|
||||
fired += cellular_trigger_tick(weave, 0, s, with(S::thrust, 1, heading)) ? 1U : 0U;
|
||||
CHECK(fired == 1);
|
||||
// Wrapping: 65,000 to 1,000 is a small turn.
|
||||
s = {};
|
||||
CHECK(!cellular_trigger_tick(weave, 0, s, with(S::thrust, 1, 65'000)) &&
|
||||
!cellular_trigger_tick(weave, 0, s, with(S::thrust, 1, 1'000)) && s.first == 0);
|
||||
}
|
||||
|
||||
void affinity() {
|
||||
std::vector<CellularMutationTuning> tuning;
|
||||
for (const auto& spec : cellular_catalog())
|
||||
tuning.push_back(cellular_default_tuning(spec));
|
||||
CellularAffinity a{};
|
||||
// Ten seconds of thrust raises Strong Propulsion (index 0) and lowers Light Propulsion (6).
|
||||
const auto thrusting = with(CellularSignal::thrust);
|
||||
for (int t = 0; t < 200; ++t)
|
||||
cellular_affinity_tick(a, thrusting, tuning, {});
|
||||
CHECK(a.score[0] == 100 && a.score[6] == -100);
|
||||
// Scores stay within bounds, and a disabled mutation does not move.
|
||||
tuning[0].increase_points = 5'000;
|
||||
tuning[6].enabled = false;
|
||||
for (int t = 0; t < 200; ++t)
|
||||
cellular_affinity_tick(a, thrusting, tuning, {-1'000, 1'000});
|
||||
CHECK(a.score[0] == 1'000 && a.score[6] == -100);
|
||||
}
|
||||
|
||||
void weights() {
|
||||
const CellularOfferRules rules{};
|
||||
CHECK(cellular_offer_weight(100, 0, rules) == 100'000);
|
||||
CHECK(cellular_offer_weight(100, 1'000, rules) == 1'000'000);
|
||||
CHECK(cellular_offer_weight(100, 500, rules) == 550'000);
|
||||
CHECK(cellular_offer_weight(100, -1'000, rules) == 0);
|
||||
CHECK(cellular_offer_weight(100, -500, rules) == 50'000);
|
||||
CHECK(cellular_offer_weight(100, 9'999, rules) == 1'000'000); // Clamped to score_max.
|
||||
}
|
||||
|
||||
std::uint64_t sum(const std::vector<std::uint32_t>& values) {
|
||||
return std::accumulate(values.begin(), values.end(), std::uint64_t{0});
|
||||
}
|
||||
|
||||
void chances() {
|
||||
const CellularOfferRules rules{};
|
||||
// Ten equal weights share 300% equally.
|
||||
const std::vector<std::uint64_t> equal(10, 1'000);
|
||||
const auto even = cellular_offer_chances(equal, rules);
|
||||
CHECK(even && std::ranges::all_of(*even, [](std::uint32_t c) { return c == 300'000; }));
|
||||
// One heavy weight is capped at 70%; one zero weight sits at the floor.
|
||||
std::vector<std::uint64_t> mixed{1'000, 1'000, 1'000, 1'000, 1'000'000, 0};
|
||||
const auto capped = cellular_offer_chances(mixed, rules);
|
||||
CHECK(capped && (*capped)[4] == 700'000 && (*capped)[5] == 10 && sum(*capped) == 3'000'000);
|
||||
CHECK(capped && capped.value()[0] - capped.value()[3] <= 1); // Rounding over four.
|
||||
// Four mutations cannot fill three options at a 70% cap.
|
||||
CHECK(!cellular_offer_chances(std::vector<std::uint64_t>(4, 1'000), rules));
|
||||
// Seed ensemble: every allocation sums exactly and respects the bounds and the order.
|
||||
Random random{11};
|
||||
for (int round = 0; round < 300; ++round) {
|
||||
const auto m = 5 + random.next() % 45;
|
||||
std::vector<std::uint64_t> w(m);
|
||||
for (auto& weight : w)
|
||||
weight =
|
||||
random.next() % 4 == 0 ? random.next() % 1'000'000'000 : random.next() % 200'000;
|
||||
const auto c = cellular_offer_chances(w, rules);
|
||||
CHECK(c.has_value());
|
||||
if (!c)
|
||||
continue;
|
||||
CHECK(sum(*c) == 3'000'000);
|
||||
for (std::size_t i = 0; i < m; ++i) {
|
||||
CHECK((*c)[i] >= 10 && (*c)[i] <= 700'000);
|
||||
for (std::size_t j = 0; j < m; ++j)
|
||||
if (w[i] > w[j])
|
||||
CHECK((*c)[i] + 1 >= (*c)[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void systematic_draw() {
|
||||
const CellularOfferRules rules{};
|
||||
const std::vector<std::uint64_t> w{5, 900'000, 3'000, 3'000, 40'000, 0, 120'000};
|
||||
const auto c = cellular_offer_chances(w, rules);
|
||||
CHECK(c.has_value());
|
||||
if (!c)
|
||||
return;
|
||||
std::vector<std::uint64_t> picked(c->size(), 0);
|
||||
for (std::uint32_t u = 0; u < cellular_chance_whole; ++u) {
|
||||
const auto draw = cellular_systematic_draw(*c, 3, u);
|
||||
CHECK(draw.size() == 3 && std::set<std::size_t>(draw.begin(), draw.end()).size() == 3);
|
||||
for (const auto index : draw)
|
||||
++picked[index];
|
||||
}
|
||||
// Over every u, each mutation appears in exactly its chance's share of draws.
|
||||
for (std::size_t i = 0; i < c->size(); ++i)
|
||||
CHECK(picked[i] == (*c)[i]);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
try {
|
||||
triggers();
|
||||
affinity();
|
||||
weights();
|
||||
chances();
|
||||
systematic_draw();
|
||||
} catch (const std::exception& error) {
|
||||
std::fprintf(stderr, "%s\n", error.what());
|
||||
return 1;
|
||||
}
|
||||
std::puts("PASS cellular triggers, affinity, chances and the systematic draw");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user