Files
aevolution/include/evo/server/lifecycle/cellular_revamp.hpp
T

135 lines
7.6 KiB
C++

#pragma once
#include "evo/biology/cellular_catalog.hpp"
#include "evo/biology/cellular_habitat.hpp"
#include "evo/biology/cellular_motion.hpp"
#include "evo/biology/cellular_offers.hpp"
#include "evo/biology/cellular_region_step.hpp"
#include "evo/biology/cellular_revamp_tuning.hpp"
#include "evo/biology/cellular_triggers.hpp"
#include "evo/biology/development.hpp"
#include <array>
#include <compare>
#include <cstdint>
#include <optional>
// Rules 13 (ADR-0179, Cellular_Revamp_Build_Plan.md): what a cellular owner declares when it
// upgrades to owner format 11. Taken from the Git tuning file when the region opens and
// persisted with the owner, so replay and restore never read the file. Later build phases add
// the feeding, mutation and reproduction tunables here.
namespace evo::server {
struct CellularRevampRules {
biology::BodyTuning body{};
// Picojoules 1,000 fg of C, N, P and other matter stand in for at burn efficiency 100.
std::array<std::uint64_t, 4> burn_pj_per_kfg{};
biology::CellularRevampStepRules step{};
// The spent pool drips back as bubbles every `drip_interval_ticks` ticks (0: never), one
// bubble per non-empty channel of at most these amounts, at seeded random spots.
std::uint32_t drip_interval_ticks{};
std::uint64_t drip_bubble_pj{}, drip_bubble_fg{};
// Share of reserve matter carried as load, per mille: it slows thrust and turning and makes
// thrust costlier, in proportion to the body's mass. Upkeep stays on the body.
std::uint32_t reserve_load_per_thousand{};
// Growth per tick in hundredths of a per mille at growth speed 100 (trial 500: 5 per mille).
std::uint32_t growth_per_tick_centi_mille{500};
// Mutation offers: the draw, the catalog's bounds and gates, the cadence in active ticks,
// and every catalog mutation's tunables in catalog order.
biology::CellularOfferRules offers{};
biology::CellularCatalogBounds bounds{};
std::uint32_t offer_interval_ticks{500};
std::array<biology::CellularMutationTuning, biology::cellular_catalog_size> mutations{};
// Reproduction: the offspring's share of a split and the growth a cell needs to split; the
// on-demand bud's fee (amounts times scale, reclaimed); the region's organism cap.
std::uint32_t split_share_per_thousand{500}, split_min_growth{1'000};
accounting::Inventory bud_fee{};
std::uint32_t bud_fee_scale_percent{};
std::uint32_t organism_cap{256};
// Multicellular organisms (reproduction plan section 6): the share of the extra members'
// mass carried as load on one cell's thrust, and the emergence threshold.
std::uint32_t organism_load_per_thousand{500};
std::uint32_t emerge_members{8};
std::uint64_t emerge_mass_fg{}, emerge_reserve_pj{};
// ADR-0181: 0 preserves historical behavior. Revision 1 makes split_share
// the controlled cell's share, returns bud fees to that cell, launches mutation
// offspring, and protects a growth fuel buffer for every organism.
std::uint32_t safety_version{}, growth_reserve_seconds{}, split_launch_um_per_s{};
auto operator<=>(const CellularRevampRules&) const = default;
};
// A rules-13 offer: the drawn mutations (catalog ids) and their chances, in catalog order.
inline constexpr std::size_t max_cellular_offer_options = 10;
struct CellularRevampOffer {
std::uint64_t event_index{}, offer_tick{};
std::uint8_t count{};
std::array<std::uint16_t, max_cellular_offer_options> ids{};
std::array<std::uint32_t, max_cellular_offer_options> chances{};
auto operator<=>(const CellularRevampOffer&) const = default;
};
// A rules-13 life's mutation state: affinity, lineage progress, the cadence counter, the pending
// offer (count 0: none), the previous tick's brake (for presses) and the growth carry.
struct CellularRevampLife {
biology::CellularAffinity affinity{};
biology::CellularLineageProgress progress{};
std::uint32_t active_ticks{};
std::uint8_t last_brake{};
std::uint16_t members{1}; // Cells in the organism: 1 for a single cell.
std::uint32_t growth_carry{};
std::uint64_t events{}; // Offers made so far; the next offer's event index is events + 1.
CellularRevampOffer offer{};
auto operator<=>(const CellularRevampLife&) const = default;
};
// The rules' offer settings as the catalog and offer code take them.
[[nodiscard]] bool cellular_revamp_offer_rules_valid(const CellularRevampRules& rules) noexcept;
// Draws an offer from `plan`'s eligible mutations at its affinity, or nothing when fewer
// mutations are eligible than the cap can fill. Seeded by world, region, life and event, so a
// retry draws the same offer.
[[nodiscard]] std::optional<CellularRevampOffer>
cellular_draw_offer(const biology::Genome& plan, const CellularRevampLife& life,
const CellularRevampRules& rules, std::uint64_t world, std::uint64_t region,
std::uint64_t life_id, std::uint64_t tick);
// How many offspring one bud makes from `litter` (hundredths of an extra offspring) and a roll:
// one, plus every whole hundred, plus one more when roll % 100 falls below the remainder; at most
// max_litter / 100 + 1.
[[nodiscard]] constexpr std::uint32_t
cellular_litter_count(std::uint32_t litter, std::uint64_t roll, std::uint32_t max_litter) noexcept {
const auto count = 1 + litter / 100 + (roll % 100 < litter % 100 ? 1U : 0U);
return count < max_litter / 100 + 1 ? count : max_litter / 100 + 1;
}
// What choosing a mutation costs: its amounts times its scale.
[[nodiscard]] accounting::Inventory
cellular_mutation_cost(const biology::CellularMutationTuning& tuning) noexcept;
// Returns `amount` to the region as bubbles at seeded random spots, one per non-empty channel;
// a habitat at its deposit limit merges each into the existing deposit farthest from `from`.
// False only on identity overflow.
[[nodiscard]] bool cellular_reclaim(biology::CellularHabitatConfig& config,
const accounting::Inventory& amount, std::uint64_t seed,
biology::CellularPosition from, std::uint64_t source);
[[nodiscard]] bool cellular_revamp_valid(const CellularRevampRules& rules) noexcept;
// The spent pool's drip for the tick ending at `tick`: on every `drip_interval_ticks`-th tick,
// one bubble per non-empty channel of at most the declared size, taken from `spent`, at a
// seeded random spot (nudged east past taken addresses) with the next deposit id and `source`.
// A habitat at its deposit limit keeps the rest waiting. False only on identity overflow, with
// `config` and `spent` then unusable.
[[nodiscard]] bool cellular_drip_spent(biology::CellularHabitatConfig& config,
accounting::Inventory& spent,
const CellularRevampRules& rules, std::uint64_t world,
std::uint64_t region, std::uint64_t tick,
std::uint64_t source);
// A body's handling carrying reserve matter: thrust, reverse thrust and turning scale by
// body / (body + load), full-output costs by (body + load) / body, where load is the declared
// share of `carried` matter. Drag is unchanged. No load leaves the handling as it is.
[[nodiscard]] biology::CellularHandling
cellular_loaded_handling(const biology::CellularHandling& handling, std::uint64_t body_mass,
std::uint64_t carried, const CellularRevampRules& rules,
std::uint64_t extra_load = 0) noexcept;
// Rules from the revamp tuning file.
[[nodiscard]] CellularRevampRules
cellular_revamp_rules(const biology::CellularRevampTuning& tuning);
} // namespace evo::server