#include "evo/server/lifecycle/cellular_revamp.hpp" #include "evo/core/random.hpp" #include #include #include namespace evo::server { bool cellular_revamp_offer_rules_valid(const CellularRevampRules& rules) noexcept { const auto& o = rules.offers; const auto& b = rules.bounds; constexpr std::uint64_t most = 1'000'000'000'000'000ULL; if (o.options == 0 || o.options > max_cellular_offer_options || o.floor_ppm > o.cap_ppm || o.cap_ppm > biology::cellular_chance_whole || o.score_min > 0 || o.score_max < 0 || o.score_min < -30'000 || o.score_max > 30'000 || o.max_multiplier_per_mille > 100'000 || o.min_multiplier_per_mille > 1'000 || b.min_points == 0 || b.min_points >= b.max_points || b.max_points > biology::max_trait_points || b.recipe_min_share == 0 || b.recipe_min_share > 250 || b.max_litter > biology::max_litter || rules.offer_interval_ticks == 0 || rules.offer_interval_ticks > 72'000) return false; for (const auto& m : rules.mutations) { if (m.base_weight > 10'000 || m.increase_points < -10'000 || m.increase_points > 10'000 || m.decrease_points < -10'000 || m.decrease_points > 10'000 || m.primary_step > 1'000 || m.paired_step > 1'000 || m.spread_step > 1'000 || m.cost_scale_percent > 100 || m.increase_ticks > 10'000'000 || m.decrease_ticks > 10'000'000 || m.cost.energy.value > most) return false; for (const auto& channel : m.cost.matter) if (channel.value > most) return false; } return true; } bool cellular_revamp_valid(const CellularRevampRules& rules) noexcept { constexpr std::uint64_t most = 1'000'000'000'000'000ULL; const auto& step = rules.step; for (const auto rate : rules.burn_pj_per_kfg) if (rate > 1'000'000'000'000ULL) return false; return rules.body.essential_upkeep_per_thousand <= 1'000 && step.membrane_collection_per_mille_per_point <= 1'000 && step.engulf_ratio_milli >= 1 && step.engulf_ratio_milli <= 1'000'000 && step.engulf_reserve_percent <= 100 && step.kill_bubble_per_thousand <= 1'000 && rules.drip_interval_ticks <= 72'000 && rules.drip_bubble_pj <= most && rules.drip_bubble_fg <= most && (rules.drip_interval_ticks == 0 || (rules.drip_bubble_pj != 0 && rules.drip_bubble_fg != 0)) && rules.reserve_load_per_thousand <= 1'000 && rules.growth_per_tick_centi_mille <= 100'000 && rules.split_share_per_thousand != 0 && rules.split_share_per_thousand < 1'000 && rules.split_min_growth != 0 && rules.split_min_growth <= 1'000 && rules.bud_fee_scale_percent <= 100 && rules.bud_fee.energy.value <= most && std::ranges::all_of( rules.bud_fee.matter, [](const accounting::Femtograms& f) { return f.value <= most; }) && rules.organism_cap != 0 && rules.organism_cap <= biology::max_cellular_tick_lives && cellular_revamp_offer_rules_valid(rules); } biology::CellularHandling cellular_loaded_handling(const biology::CellularHandling& handling, std::uint64_t body_mass, std::uint64_t carried, const CellularRevampRules& rules) noexcept { // Masses stay far below 2^40 fg, so these products fit in 64 bits. const auto load = carried / 1'000 * rules.reserve_load_per_thousand + carried % 1'000 * rules.reserve_load_per_thousand / 1'000; if (load == 0 || body_mass == 0 || body_mass > (1ULL << 40U) || load > (1ULL << 40U)) return handling; const auto total = body_mass + load; const auto slower = [&](std::uint64_t value) { return value * body_mass / total; }; const auto dearer = [&](std::uint64_t value) { return value > (1ULL << 23U) ? value / body_mass * total : (value * total + body_mass - 1) / body_mass; }; auto loaded = handling; loaded.thrust_q10 = static_cast(slower(handling.thrust_q10)); loaded.reverse_thrust_q10 = static_cast(slower(handling.reverse_thrust_q10)); loaded.turn = static_cast(slower(handling.turn)); loaded.thrust_cost.value = dearer(handling.thrust_cost.value); loaded.reverse_cost.value = dearer(handling.reverse_cost.value); return loaded; } 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) { if (rules.drip_interval_ticks == 0 || tick % rules.drip_interval_ticks != 0) return true; Random random{world * 0x9e3779b97f4a7c15ULL ^ region * 0xc2b2ae3d27d4eb4fULL ^ tick}; const auto by_address = [](const biology::CellularPatch& patch) { return patch.address; }; for (std::size_t channel = 0; channel < 5; ++channel) { auto& pool = channel == 0 ? spent.energy.value : spent.matter[channel - 1].value; const auto cap = channel == 0 ? rules.drip_bubble_pj : rules.drip_bubble_fg; // Both draws happen for every channel, so a channel's spot never depends on another's. const auto x = random.next(); const auto y = random.next(); if (pool == 0 || config.patches.size() >= biology::max_cellular_liquid_deposits) continue; biology::CellularPatchAddress address{static_cast(x % config.width_um), static_cast(y % config.height_um)}; auto at = std::ranges::lower_bound(config.patches, address, {}, by_address); while (at != config.patches.end() && at->address == address) { address.x_um = address.x_um + 1 < config.width_um ? address.x_um + 1 : 0; at = std::ranges::lower_bound(config.patches, address, {}, by_address); } if (config.next_deposit == std::numeric_limits::max()) return false; const auto amount = std::min(pool, cap); biology::CellularPatch bubble{address, source, {}, config.next_deposit++, biology::cellular_mote_radius_um, biology::CellularDepositKind::mote}; bubble.merged = 1; if (channel == 0) bubble.stock.energy.value = amount; else bubble.stock.matter[channel - 1].value = amount; pool -= amount; config.patches.insert(at, bubble); } return true; } std::optional 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) { const auto catalog = biology::cellular_catalog(); std::vector eligible; std::vector weights; for (std::size_t i = 0; i < catalog.size(); ++i) { const auto& tuning = rules.mutations.at(i); // A dry run with placeholder identities decides eligibility. if (!biology::apply_cellular_mutation(plan, catalog[i], tuning, rules.bounds, life.progress, plan.id() + 1, 1)) continue; eligible.push_back(catalog[i].id); weights.push_back(biology::cellular_offer_weight(tuning.base_weight, life.affinity.score.at(i), rules.offers)); } const auto chances = biology::cellular_offer_chances(weights, rules.offers); if (!chances) return std::nullopt; const auto event = life.events + 1; Random random{world * 0x9e3779b97f4a7c15ULL ^ region * 0xc2b2ae3d27d4eb4fULL ^ life_id * 0x165667b19e3779f9ULL ^ event}; std::uint64_t draw = random.next(); constexpr auto whole = std::uint64_t{biology::cellular_chance_whole}; while (draw >= std::numeric_limits::max() / whole * whole) draw = random.next(); // Exactly uniform below one whole. const auto picked = biology::cellular_systematic_draw(*chances, rules.offers.options, static_cast(draw % whole)); if (picked.size() != rules.offers.options) return std::nullopt; CellularRevampOffer offer{}; offer.event_index = event; offer.offer_tick = tick; offer.count = static_cast(picked.size()); for (std::size_t k = 0; k < picked.size(); ++k) { offer.ids.at(k) = eligible.at(picked[k]); offer.chances.at(k) = chances->at(picked[k]); } return offer; } accounting::Inventory cellular_mutation_cost(const biology::CellularMutationTuning& t) noexcept { const auto scaled = [&](std::uint64_t amount) { return amount / 100 * t.cost_scale_percent + amount % 100 * t.cost_scale_percent / 100; }; accounting::Inventory cost{}; cost.energy.value = scaled(t.cost.energy.value); for (std::size_t c = 0; c < cost.matter.size(); ++c) cost.matter[c].value = scaled(t.cost.matter[c].value); return cost; } bool cellular_reclaim(biology::CellularHabitatConfig& config, const accounting::Inventory& amount, std::uint64_t seed, biology::CellularPosition from, std::uint64_t source) { Random random{seed}; const auto by_address = [](const biology::CellularPatch& patch) { return patch.address; }; for (std::size_t channel = 0; channel < 5; ++channel) { const auto value = channel == 0 ? amount.energy.value : amount.matter[channel - 1].value; const auto x = random.next(); const auto y = random.next(); if (value == 0) continue; if (config.patches.size() >= biology::max_cellular_liquid_deposits) { // Full: the deposit farthest from the paying cell (lowest address on a tie). const auto distance = [&](const biology::CellularPatch& patch) { const auto dx = static_cast(patch.address.x_um) - static_cast(from.x_um); const auto dy = static_cast(patch.address.y_um) - static_cast(from.y_um); return static_cast(dx * dx) + static_cast(dy * dy); }; auto farthest = config.patches.begin(); for (auto it = config.patches.begin(); it != config.patches.end(); ++it) if (distance(*it) > distance(*farthest)) farthest = it; if (farthest == config.patches.end()) return false; auto& target = channel == 0 ? farthest->stock.energy.value : farthest->stock.matter[channel - 1].value; if (value > std::numeric_limits::max() - target) return false; target += value; continue; } biology::CellularPatchAddress address{static_cast(x % config.width_um), static_cast(y % config.height_um)}; auto at = std::ranges::lower_bound(config.patches, address, {}, by_address); while (at != config.patches.end() && at->address == address) { address.x_um = address.x_um + 1 < config.width_um ? address.x_um + 1 : 0; at = std::ranges::lower_bound(config.patches, address, {}, by_address); } if (config.next_deposit == std::numeric_limits::max()) return false; biology::CellularPatch bubble{address, source, {}, config.next_deposit++, biology::cellular_mote_radius_um, biology::CellularDepositKind::mote}; bubble.merged = 1; if (channel == 0) bubble.stock.energy.value = value; else bubble.stock.matter[channel - 1].value = value; config.patches.insert(at, bubble); } return true; } CellularRevampRules cellular_revamp_rules(const biology::CellularRevampTuning& t) { const auto u32 = [](std::int64_t value) { return static_cast(value); }; const auto u64 = [](std::int64_t value) { return static_cast(value); }; return {.body = {.essential_upkeep_per_thousand = u32(t.body_essential_upkeep_per_mille)}, .burn_pj_per_kfg = {u64(t.energy_burn_carbon_pj_per_kfg), u64(t.energy_burn_nitrogen_pj_per_kfg), u64(t.energy_burn_phosphorus_pj_per_kfg), u64(t.energy_burn_other_pj_per_kfg)}, .step = {.membrane_collection_per_mille_per_point = u32(t.feeding_membrane_collection_per_mille_per_point), .engulf_ratio_milli = u32(t.predation_engulf_ratio_milli), .engulf_reserve_percent = u32(t.predation_engulf_reserve_percent), .kill_bubble_per_thousand = u32(t.predation_kill_bubble_per_mille)}, .drip_interval_ticks = u32(t.spent_drip_interval_ticks), .drip_bubble_pj = u64(t.spent_drip_bubble_pj), .drip_bubble_fg = u64(t.spent_drip_bubble_fg), .reserve_load_per_thousand = u32(t.body_reserve_load_per_mille), .growth_per_tick_centi_mille = u32(t.body_growth_per_tick_centi_mille), .offers = {.options = u32(t.mutation_options), .floor_ppm = u32(t.mutation_floor_ppm), .cap_ppm = u32(t.mutation_cap_ppm), .score_min = static_cast(t.mutation_score_min), .score_max = static_cast(t.mutation_score_max), .max_multiplier_per_mille = u32(t.mutation_max_multiplier_per_mille), .min_multiplier_per_mille = u32(t.mutation_min_multiplier_per_mille)}, .bounds = {.min_points = u32(t.mutation_min_points), .max_points = u32(t.mutation_max_points), .recipe_min_share = u32(t.mutation_recipe_min_share), .max_litter = (u32(t.mutation_max_offspring) - 1) * 100, .carbon_for_light = u64(t.mutation_carbon_for_light_fg), .buds_for_adhesion = u32(t.mutation_buds_for_adhesion)}, .offer_interval_ticks = u32(t.mutation_offer_interval_ticks), .mutations = t.mutations, .split_share_per_thousand = u32(t.reproduction_split_share_per_mille), .split_min_growth = u32(t.reproduction_split_min_growth_per_mille), .bud_fee = {{u64(t.reproduction_bud_fee_e_pj)}, {accounting::Femtograms{u64(t.reproduction_bud_fee_c_fg)}, accounting::Femtograms{u64(t.reproduction_bud_fee_n_fg)}, accounting::Femtograms{u64(t.reproduction_bud_fee_p_fg)}, accounting::Femtograms{u64(t.reproduction_bud_fee_o_fg)}}}, .bud_fee_scale_percent = u32(t.reproduction_bud_fee_scale_percent), .organism_cap = u32(t.reproduction_organism_cap)}; } } // namespace evo::server