feat(lifecycle): rules 13 behaviour-weighted mutation offers in the owner
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
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 4m34s
Foundation / postgres-tools (push) Successful in 22s
Foundation / server-image (push) Successful in 4m16s
Foundation / image (push) Successful in 4m47s
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
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 4m34s
Foundation / postgres-tools (push) Successful in 22s
Foundation / server-image (push) Successful in 4m16s
Foundation / image (push) Successful in 4m47s
Phase 3 of the cellular revamp build plan (ADR-0179), server half. - Every owned, active rules-13 life gathers its activity each tick from every phase (thrust and drift, brake presses, feeding and the collection limits, matter burned by channel, energy by use, contact with larger cells, engulf outcomes, growth blocked by a channel, buds made, refused or lost young, the reserve against the body) and advances its 98 triggers and 49 affinity scores. - Active play earns an offer every interval: eligible catalog mutations of the life's plan, weighted by affinity, drawn with the floored and capped chances and a seeded systematic draw (a retry draws the same offer). Wildlife, newborns and frozen cells earn nothing; a plan without schema-7 traits has no catalog. - A choice must name the life's own pending offer. It edits the offspring plan (phase 4 turns this into the split), pays the mutation's tuned cost from the reserve (matter may stand in for its energy, excluded channels excepted) and reclaims it as seeded random bubbles, merging into the farthest deposit at the deposit limit. Unaffordable choices keep the offer; declines clear it. - Offspring inherit affinity scores and lineage progress. Growth speed scales the growth step with a carried fraction. - Format 11 persists the offer tunables and each life's mutation state; the revamp tuning file gains the offer keys and optional per-mutation overrides (mutation.<key>.<field>), defaulting to the catalog with zero costs. Tests cover per-mutation overrides and refusals, the collection-limit flags, offers from active play, founder-5 plans never offered, exact restore of a pending offer, a choice editing only the plan and reclaiming its cost with closure, unaffordable choices, declines, foreign choices, and thrust moving affinity. Refreshes the DEV session window. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -272,6 +272,12 @@ propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
|
||||
cellular_pool_yield_per_thousand / 1000);
|
||||
auto& reserve = channel_of(result.reserve_after, channel);
|
||||
const auto take = std::min({stock, allowance, u64_max - reserve});
|
||||
if (stock > take) {
|
||||
if (channel == 0)
|
||||
result.energy_limited = true;
|
||||
else
|
||||
result.matter_limited = true;
|
||||
}
|
||||
if (take == 0)
|
||||
continue;
|
||||
stock -= take;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "evo/biology/cellular_revamp_tuning.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <set>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::biology {
|
||||
namespace {
|
||||
@@ -20,6 +22,8 @@ constexpr std::array fields{
|
||||
0, 1'000},
|
||||
Field{"body.reserve_load_per_mille", &CellularRevampTuning::body_reserve_load_per_mille, 0,
|
||||
1'000},
|
||||
Field{"body.growth_per_tick_centi_mille",
|
||||
&CellularRevampTuning::body_growth_per_tick_centi_mille, 1, 100'000},
|
||||
Field{"energy.burn_carbon_pj_per_kfg", &CellularRevampTuning::energy_burn_carbon_pj_per_kfg, 0,
|
||||
rate_limit},
|
||||
Field{"energy.burn_nitrogen_pj_per_kfg", &CellularRevampTuning::energy_burn_nitrogen_pj_per_kfg,
|
||||
@@ -39,8 +43,134 @@ constexpr std::array fields{
|
||||
Field{"spent.drip_interval_ticks", &CellularRevampTuning::spent_drip_interval_ticks, 0, 72'000},
|
||||
Field{"spent.drip_bubble_pj", &CellularRevampTuning::spent_drip_bubble_pj, 0, amount_limit},
|
||||
Field{"spent.drip_bubble_fg", &CellularRevampTuning::spent_drip_bubble_fg, 0, amount_limit},
|
||||
Field{"mutation.options", &CellularRevampTuning::mutation_options, 1, 10},
|
||||
Field{"mutation.floor_ppm", &CellularRevampTuning::mutation_floor_ppm, 0, 1'000'000},
|
||||
Field{"mutation.cap_ppm", &CellularRevampTuning::mutation_cap_ppm, 1, 1'000'000},
|
||||
Field{"mutation.score_min", &CellularRevampTuning::mutation_score_min, -30'000, 0},
|
||||
Field{"mutation.score_max", &CellularRevampTuning::mutation_score_max, 0, 30'000},
|
||||
Field{"mutation.max_multiplier_per_mille",
|
||||
&CellularRevampTuning::mutation_max_multiplier_per_mille, 1'000, 100'000},
|
||||
Field{"mutation.min_multiplier_per_mille",
|
||||
&CellularRevampTuning::mutation_min_multiplier_per_mille, 0, 1'000},
|
||||
Field{"mutation.offer_interval_ticks", &CellularRevampTuning::mutation_offer_interval_ticks, 1,
|
||||
72'000},
|
||||
Field{"mutation.min_points", &CellularRevampTuning::mutation_min_points, 1, 1'000},
|
||||
Field{"mutation.max_points", &CellularRevampTuning::mutation_max_points, 100, 1'000},
|
||||
Field{"mutation.recipe_min_share", &CellularRevampTuning::mutation_recipe_min_share, 1, 250},
|
||||
Field{"mutation.max_offspring", &CellularRevampTuning::mutation_max_offspring, 1, 100},
|
||||
Field{"mutation.carbon_for_light_fg", &CellularRevampTuning::mutation_carbon_for_light_fg, 0,
|
||||
amount_limit},
|
||||
Field{"mutation.buds_for_adhesion", &CellularRevampTuning::mutation_buds_for_adhesion, 0,
|
||||
1'000},
|
||||
};
|
||||
|
||||
// A per-mutation field's bounds, in cellular_mutation_tuning_fields order.
|
||||
constexpr std::array<std::pair<std::int64_t, std::int64_t>, 15> mutation_field_bounds{{
|
||||
{0, 1},
|
||||
{0, 10'000},
|
||||
{-10'000, 10'000},
|
||||
{-10'000, 10'000},
|
||||
{0, 1'000},
|
||||
{0, 1'000},
|
||||
{0, 1'000},
|
||||
{0, 10'000'000},
|
||||
{0, 10'000'000},
|
||||
{0, amount_limit},
|
||||
{0, amount_limit},
|
||||
{0, amount_limit},
|
||||
{0, amount_limit},
|
||||
{0, amount_limit},
|
||||
{0, 100},
|
||||
}};
|
||||
std::int64_t get_field(const CellularMutationTuning& t, std::size_t field) {
|
||||
switch (field) {
|
||||
case 0:
|
||||
return t.enabled ? 1 : 0;
|
||||
case 1:
|
||||
return t.base_weight;
|
||||
case 2:
|
||||
return t.increase_points;
|
||||
case 3:
|
||||
return t.decrease_points;
|
||||
case 4:
|
||||
return t.primary_step;
|
||||
case 5:
|
||||
return t.paired_step;
|
||||
case 6:
|
||||
return t.spread_step;
|
||||
case 7:
|
||||
return t.increase_ticks;
|
||||
case 8:
|
||||
return t.decrease_ticks;
|
||||
case 9:
|
||||
return static_cast<std::int64_t>(t.cost.energy.value);
|
||||
case 14:
|
||||
return t.cost_scale_percent;
|
||||
default:
|
||||
return static_cast<std::int64_t>(t.cost.matter[field - 10].value);
|
||||
}
|
||||
}
|
||||
void set_field(CellularMutationTuning& t, std::size_t field, std::int64_t value) {
|
||||
const auto u32 = static_cast<std::uint32_t>(value);
|
||||
switch (field) {
|
||||
case 0:
|
||||
t.enabled = value != 0;
|
||||
break;
|
||||
case 1:
|
||||
t.base_weight = u32;
|
||||
break;
|
||||
case 2:
|
||||
t.increase_points = static_cast<std::int32_t>(value);
|
||||
break;
|
||||
case 3:
|
||||
t.decrease_points = static_cast<std::int32_t>(value);
|
||||
break;
|
||||
case 4:
|
||||
t.primary_step = u32;
|
||||
break;
|
||||
case 5:
|
||||
t.paired_step = u32;
|
||||
break;
|
||||
case 6:
|
||||
t.spread_step = u32;
|
||||
break;
|
||||
case 7:
|
||||
t.increase_ticks = u32;
|
||||
break;
|
||||
case 8:
|
||||
t.decrease_ticks = u32;
|
||||
break;
|
||||
case 9:
|
||||
t.cost.energy.value = static_cast<std::uint64_t>(value);
|
||||
break;
|
||||
case 14:
|
||||
t.cost_scale_percent = u32;
|
||||
break;
|
||||
default:
|
||||
t.cost.matter[field - 10].value = static_cast<std::uint64_t>(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// `mutation.<key>.<field>`: the catalog index and field index, or nothing.
|
||||
std::optional<std::pair<std::size_t, std::size_t>> mutation_key(std::string_view key) {
|
||||
constexpr std::string_view prefix = "mutation.";
|
||||
if (!key.starts_with(prefix))
|
||||
return std::nullopt;
|
||||
const auto rest = key.substr(prefix.size());
|
||||
const auto dot = rest.find('.');
|
||||
if (dot == std::string_view::npos)
|
||||
return std::nullopt;
|
||||
const auto name = rest.substr(0, dot);
|
||||
const auto field = rest.substr(dot + 1);
|
||||
const auto catalog = cellular_catalog();
|
||||
const auto spec = std::ranges::find(catalog, name, &CellularMutationSpec::key);
|
||||
const auto slot = std::ranges::find(cellular_mutation_tuning_fields, field);
|
||||
if (spec == catalog.end() || slot == cellular_mutation_tuning_fields.end())
|
||||
return std::nullopt;
|
||||
return std::pair{static_cast<std::size_t>(spec - catalog.begin()),
|
||||
static_cast<std::size_t>(slot - cellular_mutation_tuning_fields.begin())};
|
||||
}
|
||||
|
||||
CellularTuningError error(CellularTuningErrorKind kind, std::size_t line, std::string_view key) {
|
||||
return {kind, line, std::string{key}};
|
||||
}
|
||||
@@ -52,8 +182,28 @@ parse_cellular_revamp_tuning(std::string_view text) {
|
||||
if (!lines)
|
||||
return std::unexpected{lines.error()};
|
||||
CellularRevampTuning tuning{};
|
||||
const auto catalog = cellular_catalog();
|
||||
for (std::size_t i = 0; i < catalog.size(); ++i)
|
||||
tuning.mutations.at(i) = cellular_default_tuning(catalog[i]);
|
||||
std::array<bool, fields.size()> seen{};
|
||||
std::set<std::pair<std::size_t, std::size_t>> overridden;
|
||||
for (const auto& [line_number, key, value_text] : *lines) {
|
||||
// Optional per-mutation overrides; an unknown mutation or field is an unknown key.
|
||||
if (const auto target = mutation_key(key)) {
|
||||
if (!overridden.insert(*target).second)
|
||||
return std::unexpected{
|
||||
error(CellularTuningErrorKind::duplicate_key, line_number, key)};
|
||||
const auto value = parse_tuning_integer(value_text);
|
||||
if (!value)
|
||||
return std::unexpected{
|
||||
error(CellularTuningErrorKind::invalid_value, line_number, key)};
|
||||
const auto [low, high] = mutation_field_bounds.at(target->second);
|
||||
if (*value < low || *value > high)
|
||||
return std::unexpected{
|
||||
error(CellularTuningErrorKind::out_of_range, line_number, key)};
|
||||
set_field(tuning.mutations.at(target->first), target->second, *value);
|
||||
continue;
|
||||
}
|
||||
std::size_t index{};
|
||||
while (index < fields.size() && fields.at(index).key != key)
|
||||
++index;
|
||||
@@ -77,6 +227,10 @@ parse_cellular_revamp_tuning(std::string_view text) {
|
||||
if (tuning.version != cellular_revamp_tuning_version)
|
||||
return std::unexpected{
|
||||
error(CellularTuningErrorKind::unsupported_version, 0, "tuning.version")};
|
||||
if (tuning.mutation_floor_ppm > tuning.mutation_cap_ppm ||
|
||||
tuning.mutation_min_points >= tuning.mutation_max_points)
|
||||
return std::unexpected{
|
||||
error(CellularTuningErrorKind::inconsistent, 0, "mutation.floor_ppm")};
|
||||
// A drip needs a bubble size on both kinds of channel.
|
||||
if (tuning.spent_drip_interval_ticks != 0 &&
|
||||
(tuning.spent_drip_bubble_pj == 0 || tuning.spent_drip_bubble_fg == 0))
|
||||
@@ -93,6 +247,17 @@ std::string encode_cellular_revamp_tuning(const CellularRevampTuning& tuning) {
|
||||
text.append(std::to_string(tuning.*field.member));
|
||||
text.push_back('\n');
|
||||
}
|
||||
const auto catalog = cellular_catalog();
|
||||
for (std::size_t i = 0; i < catalog.size(); ++i)
|
||||
for (std::size_t f = 0; f < cellular_mutation_tuning_fields.size(); ++f) {
|
||||
text.append("mutation.");
|
||||
text.append(catalog[i].key);
|
||||
text.push_back('.');
|
||||
text.append(cellular_mutation_tuning_fields.at(f));
|
||||
text.append(" = ");
|
||||
text.append(std::to_string(get_field(tuning.mutations.at(i), f)));
|
||||
text.push_back('\n');
|
||||
}
|
||||
return text;
|
||||
}
|
||||
} // namespace evo::biology
|
||||
|
||||
@@ -58,7 +58,19 @@ constexpr std::size_t cellular_life_motion_bytes = 20;
|
||||
// energy and matter (16), and the spent pool (40): 120 bytes. The word after the drip interval
|
||||
// is the reserve load share. Later build phases extend this
|
||||
// block before format 11 is deployed.
|
||||
constexpr std::size_t cellular_lives_v11_header_bytes = cellular_lives_v10_header_bytes + 120;
|
||||
// Then the growth rate and a reserved zero (8), the offer rules (seven 4-byte fields and a
|
||||
// reserved zero, 32), the catalog bounds (five 4-byte fields, a reserved zero and the carbon
|
||||
// gate, 32), the offer interval and a reserved zero (8), and 49 mutation tunables of 80 bytes.
|
||||
constexpr std::size_t cellular_revamp_mutation_bytes = 80;
|
||||
constexpr std::size_t cellular_lives_v11_header_bytes =
|
||||
cellular_lives_v10_header_bytes + 120 + 8 + 32 + 32 + 8 +
|
||||
biology::cellular_catalog_size * cellular_revamp_mutation_bytes;
|
||||
// Each format-11 life adds its mutation state: 49 scores and a reserved zero (100), 98 trigger
|
||||
// states of 24 bytes (2,352), lineage progress and the cadence counter (16), the last brake,
|
||||
// three zeros and the growth carry (8), the offers made (8), and the pending offer (88).
|
||||
constexpr std::size_t cellular_trigger_state_bytes = 24;
|
||||
constexpr std::size_t cellular_life_revamp_bytes =
|
||||
100 + 2 * biology::cellular_catalog_size * cellular_trigger_state_bytes + 16 + 8 + 8 + 88;
|
||||
|
||||
bool valid_motion(const biology::CellularMotionState& m) noexcept {
|
||||
constexpr std::int64_t carry = biology::cellular_velocity_q10_per_um_s * 1'000'000;
|
||||
@@ -180,6 +192,184 @@ bool subtract_inventory(accounting::Inventory& total, const accounting::Inventor
|
||||
return true;
|
||||
}
|
||||
|
||||
// Format 11: the revamp rules' offer settings and a life's mutation state.
|
||||
void write_trigger(Writer& out, const biology::CellularTriggerState& s) {
|
||||
out.put(s.first);
|
||||
out.put(s.second);
|
||||
out.put(s.ticks, 4);
|
||||
out.put(s.heading, 2);
|
||||
out.put(s.running, 1);
|
||||
out.put(0, 1);
|
||||
}
|
||||
std::optional<biology::CellularTriggerState> read_trigger(Reader& in) {
|
||||
biology::CellularTriggerState s{};
|
||||
s.first = in.get();
|
||||
s.second = in.get();
|
||||
s.ticks = static_cast<std::uint32_t>(in.get(4));
|
||||
s.heading = static_cast<std::uint16_t>(in.get(2));
|
||||
const auto running = in.get(1);
|
||||
if (running > 1 || in.get(1) != 0)
|
||||
return std::nullopt;
|
||||
s.running = static_cast<std::uint8_t>(running);
|
||||
return s;
|
||||
}
|
||||
void write_revamp_offers(Writer& out, const CellularRevampRules& r) {
|
||||
out.put(r.growth_per_tick_centi_mille, 4);
|
||||
out.put(0, 4);
|
||||
const auto& o = r.offers;
|
||||
for (const auto value : {o.options, o.floor_ppm, o.cap_ppm})
|
||||
out.put(value, 4);
|
||||
out.put(static_cast<std::uint32_t>(o.score_min), 4);
|
||||
out.put(static_cast<std::uint32_t>(o.score_max), 4);
|
||||
out.put(o.max_multiplier_per_mille, 4);
|
||||
out.put(o.min_multiplier_per_mille, 4);
|
||||
out.put(0, 4);
|
||||
const auto& b = r.bounds;
|
||||
for (const auto value :
|
||||
{b.min_points, b.max_points, b.recipe_min_share, b.max_litter, b.buds_for_adhesion})
|
||||
out.put(value, 4);
|
||||
out.put(0, 4);
|
||||
out.put(b.carbon_for_light);
|
||||
out.put(r.offer_interval_ticks, 4);
|
||||
out.put(0, 4);
|
||||
for (const auto& m : r.mutations) {
|
||||
out.put(m.enabled ? 1 : 0, 1);
|
||||
out.zeros(3);
|
||||
out.put(m.base_weight, 4);
|
||||
out.put(static_cast<std::uint32_t>(m.increase_points), 4);
|
||||
out.put(static_cast<std::uint32_t>(m.decrease_points), 4);
|
||||
for (const auto value : {m.primary_step, m.paired_step, m.spread_step, m.increase_ticks,
|
||||
m.decrease_ticks, m.cost_scale_percent})
|
||||
out.put(value, 4);
|
||||
out.inventory(m.cost);
|
||||
}
|
||||
}
|
||||
bool read_revamp_offers(Reader& in, CellularRevampRules& r) {
|
||||
const auto u32 = [&in] { return static_cast<std::uint32_t>(in.get(4)); };
|
||||
const auto i32 = [&in] {
|
||||
return static_cast<std::int32_t>(static_cast<std::uint32_t>(in.get(4)));
|
||||
};
|
||||
bool zero = true;
|
||||
r.growth_per_tick_centi_mille = u32();
|
||||
zero = zero && in.get(4) == 0;
|
||||
auto& o = r.offers;
|
||||
o.options = u32();
|
||||
o.floor_ppm = u32();
|
||||
o.cap_ppm = u32();
|
||||
o.score_min = i32();
|
||||
o.score_max = i32();
|
||||
o.max_multiplier_per_mille = u32();
|
||||
o.min_multiplier_per_mille = u32();
|
||||
zero = zero && in.get(4) == 0;
|
||||
auto& b = r.bounds;
|
||||
b.min_points = u32();
|
||||
b.max_points = u32();
|
||||
b.recipe_min_share = u32();
|
||||
b.max_litter = u32();
|
||||
b.buds_for_adhesion = u32();
|
||||
zero = zero && in.get(4) == 0;
|
||||
b.carbon_for_light = in.get();
|
||||
r.offer_interval_ticks = u32();
|
||||
zero = zero && in.get(4) == 0;
|
||||
for (auto& m : r.mutations) {
|
||||
const auto enabled = in.get(1);
|
||||
zero = zero && enabled <= 1 && in.get(3) == 0;
|
||||
m.enabled = enabled == 1;
|
||||
m.base_weight = u32();
|
||||
m.increase_points = i32();
|
||||
m.decrease_points = i32();
|
||||
m.primary_step = u32();
|
||||
m.paired_step = u32();
|
||||
m.spread_step = u32();
|
||||
m.increase_ticks = u32();
|
||||
m.decrease_ticks = u32();
|
||||
m.cost_scale_percent = u32();
|
||||
m.cost = in.inventory();
|
||||
}
|
||||
return zero;
|
||||
}
|
||||
void write_revamp_life(Writer& out, const CellularRevampLife& life) {
|
||||
for (const auto score : life.affinity.score)
|
||||
out.put(static_cast<std::uint16_t>(score), 2);
|
||||
out.put(0, 2);
|
||||
for (const auto& s : life.affinity.increase)
|
||||
write_trigger(out, s);
|
||||
for (const auto& s : life.affinity.decrease)
|
||||
write_trigger(out, s);
|
||||
out.put(life.progress.carbon_burned);
|
||||
out.put(life.progress.buds, 4);
|
||||
out.put(life.active_ticks, 4);
|
||||
out.put(life.last_brake, 1);
|
||||
out.zeros(3);
|
||||
out.put(life.growth_carry, 4);
|
||||
out.put(life.events);
|
||||
const auto& offer = life.offer;
|
||||
out.put(offer.event_index);
|
||||
out.put(offer.offer_tick);
|
||||
out.put(offer.count, 1);
|
||||
out.zeros(7);
|
||||
for (const auto id : offer.ids)
|
||||
out.put(id, 2);
|
||||
for (const auto chance : offer.chances)
|
||||
out.put(chance, 4);
|
||||
out.zeros(4);
|
||||
}
|
||||
std::optional<CellularRevampLife> read_revamp_life(Reader& in) {
|
||||
CellularRevampLife life{};
|
||||
for (auto& score : life.affinity.score)
|
||||
score = static_cast<std::int16_t>(static_cast<std::uint16_t>(in.get(2)));
|
||||
bool zero = in.get(2) == 0;
|
||||
for (auto& s : life.affinity.increase) {
|
||||
const auto read = read_trigger(in);
|
||||
if (!read)
|
||||
return std::nullopt;
|
||||
s = *read;
|
||||
}
|
||||
for (auto& s : life.affinity.decrease) {
|
||||
const auto read = read_trigger(in);
|
||||
if (!read)
|
||||
return std::nullopt;
|
||||
s = *read;
|
||||
}
|
||||
life.progress.carbon_burned = in.get();
|
||||
life.progress.buds = static_cast<std::uint32_t>(in.get(4));
|
||||
life.active_ticks = static_cast<std::uint32_t>(in.get(4));
|
||||
const auto brake = in.get(1);
|
||||
zero = zero && brake <= 1 && in.get(3) == 0;
|
||||
life.last_brake = static_cast<std::uint8_t>(brake);
|
||||
life.growth_carry = static_cast<std::uint32_t>(in.get(4));
|
||||
life.events = in.get();
|
||||
auto& offer = life.offer;
|
||||
offer.event_index = in.get();
|
||||
offer.offer_tick = in.get();
|
||||
const auto count = in.get(1);
|
||||
zero = zero && in.get(7) == 0 && count <= max_cellular_offer_options;
|
||||
offer.count = static_cast<std::uint8_t>(count);
|
||||
for (auto& id : offer.ids)
|
||||
id = static_cast<std::uint16_t>(in.get(2));
|
||||
for (auto& chance : offer.chances)
|
||||
chance = static_cast<std::uint32_t>(in.get(4));
|
||||
zero = zero && in.get(4) == 0;
|
||||
// A pending offer names catalog mutations once each, with chances, and nothing after them;
|
||||
// no offer leaves every slot zero.
|
||||
for (std::size_t k = 0; k < max_cellular_offer_options; ++k) {
|
||||
const bool used = k < offer.count;
|
||||
if (used != (offer.ids.at(k) != 0) ||
|
||||
(used &&
|
||||
(biology::cellular_mutation(offer.ids.at(k)) == nullptr || offer.chances.at(k) == 0 ||
|
||||
offer.chances.at(k) > biology::cellular_chance_whole)) ||
|
||||
(!used && offer.chances.at(k) != 0))
|
||||
return std::nullopt;
|
||||
for (std::size_t j = 0; j < k && used; ++j)
|
||||
if (offer.ids.at(j) == offer.ids.at(k))
|
||||
return std::nullopt;
|
||||
}
|
||||
if (!zero || (offer.count == 0 && (offer.event_index != 0 || offer.offer_tick != 0)) ||
|
||||
(offer.count != 0 && offer.event_index != life.events) || life.growth_carry >= 100)
|
||||
return std::nullopt;
|
||||
return life;
|
||||
}
|
||||
|
||||
std::expected<biology::MetabolicBudget, CellularLivesError>
|
||||
budget_for(const biology::LineageArchive& archive, const CellularOwnedLife& life,
|
||||
std::uint32_t temperature,
|
||||
@@ -1050,13 +1240,15 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
// Format 11 is format 10 with the revamp rules.
|
||||
const bool revamp = state_->format_version == cellular_lives_revamp_format;
|
||||
const bool liquid = revamp || state_->format_version == cellular_lives_liquid_format;
|
||||
const auto life_bytes = liquid ? cellular_life_v6_bytes + 8 + cellular_life_motion_bytes
|
||||
: inheritance ? cellular_life_v6_bytes + 8
|
||||
: state_->format_version == cellular_lives_legacy_format
|
||||
? cellular_life_v1_bytes
|
||||
: presence ? cellular_life_v6_bytes
|
||||
: parents ? cellular_life_v5_bytes
|
||||
: cellular_life_v2_bytes;
|
||||
const auto life_bytes =
|
||||
revamp
|
||||
? cellular_life_v6_bytes + 8 + cellular_life_motion_bytes + cellular_life_revamp_bytes
|
||||
: liquid ? cellular_life_v6_bytes + 8 + cellular_life_motion_bytes
|
||||
: inheritance ? cellular_life_v6_bytes + 8
|
||||
: state_->format_version == cellular_lives_legacy_format ? cellular_life_v1_bytes
|
||||
: presence ? cellular_life_v6_bytes
|
||||
: parents ? cellular_life_v5_bytes
|
||||
: cellular_life_v2_bytes;
|
||||
const bool causes = state_->format_version >= cellular_lives_engulf_format;
|
||||
const auto corpse_bytes = inheritance ? cellular_corpse_v5_bytes + 8
|
||||
: parents ? cellular_corpse_v5_bytes
|
||||
@@ -1176,6 +1368,7 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
out.put(r.drip_bubble_pj);
|
||||
out.put(r.drip_bubble_fg);
|
||||
out.inventory(state_->spent);
|
||||
write_revamp_offers(out, r);
|
||||
}
|
||||
for (const auto site : state_->nursery_config.sites)
|
||||
out.site(site);
|
||||
@@ -1245,6 +1438,8 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
out.put(static_cast<std::uint64_t>(life.motion.remainder_x), 4);
|
||||
out.put(static_cast<std::uint64_t>(life.motion.remainder_y), 4);
|
||||
}
|
||||
if (revamp)
|
||||
write_revamp_life(out, life.revamp);
|
||||
}
|
||||
for (const auto& corpse : state_->corpses) {
|
||||
out.put(corpse.life_id);
|
||||
@@ -1700,15 +1895,18 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
revamp.drip_bubble_pj = in.get();
|
||||
revamp.drip_bubble_fg = in.get();
|
||||
spent = in.inventory();
|
||||
if (reserved != 0 || !cellular_revamp_valid(revamp))
|
||||
if (!read_revamp_offers(in, revamp) || reserved != 0 || !cellular_revamp_valid(revamp))
|
||||
return std::unexpected{CellularLivesError::format};
|
||||
}
|
||||
const auto life_bytes = liquid_version ? cellular_life_v6_bytes + 8 + cellular_life_motion_bytes
|
||||
: inheritance_version ? cellular_life_v6_bytes + 8
|
||||
: legacy ? cellular_life_v1_bytes
|
||||
: presence_version ? cellular_life_v6_bytes
|
||||
: bud_version ? cellular_life_v5_bytes
|
||||
: cellular_life_v2_bytes;
|
||||
const auto life_bytes =
|
||||
revamp_version
|
||||
? cellular_life_v6_bytes + 8 + cellular_life_motion_bytes + cellular_life_revamp_bytes
|
||||
: liquid_version ? cellular_life_v6_bytes + 8 + cellular_life_motion_bytes
|
||||
: inheritance_version ? cellular_life_v6_bytes + 8
|
||||
: legacy ? cellular_life_v1_bytes
|
||||
: presence_version ? cellular_life_v6_bytes
|
||||
: bud_version ? cellular_life_v5_bytes
|
||||
: cellular_life_v2_bytes;
|
||||
const auto corpse_bytes = inheritance_version ? cellular_corpse_v5_bytes + 8
|
||||
: bud_version ? cellular_corpse_v5_bytes
|
||||
: engulf_version ? cellular_corpse_v4_bytes
|
||||
@@ -1820,6 +2018,12 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
if (padding != 0 || !valid_motion(life.motion))
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
}
|
||||
if (revamp_version) {
|
||||
const auto state = read_revamp_life(in);
|
||||
if (!state)
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
life.revamp = *state;
|
||||
}
|
||||
lives.push_back(life);
|
||||
}
|
||||
|
||||
@@ -2014,6 +2218,21 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
return std::unexpected{CellularLivesError::mutation};
|
||||
continue;
|
||||
}
|
||||
if (revamp_rules) {
|
||||
// Rules 13: a choice names the life's own pending offer and one of its mutations.
|
||||
const auto& pending = life->revamp.offer;
|
||||
const auto ids = std::span{pending.ids}.first(pending.count);
|
||||
if (pending.count == 0 || pending.event_index != selection.event_index ||
|
||||
pending.offer_tick != selection.offer_tick ||
|
||||
(selection.operator_id != 0 &&
|
||||
std::ranges::find(ids, selection.operator_id) == ids.end()) ||
|
||||
state_->archive.find(selection.successor_id))
|
||||
return std::unexpected{CellularLivesError::mutation};
|
||||
for (std::size_t j = 0; j < i; ++j)
|
||||
if (selections[j].successor_id == selection.successor_id)
|
||||
return std::unexpected{CellularLivesError::identity};
|
||||
continue;
|
||||
}
|
||||
if (life->mutation.event_index != selection.event_index ||
|
||||
life->mutation.offer_tick != selection.offer_tick)
|
||||
return std::unexpected{CellularLivesError::mutation};
|
||||
@@ -2118,6 +2337,17 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
if (!add_inventory(next_spent, proposal->spent))
|
||||
return std::unexpected{CellularLivesError::overflow};
|
||||
auto habitat_config = state_->habitat->config();
|
||||
// Rules 13: each owned life's activity this tick, gathered from every phase below and
|
||||
// applied to its affinity at the end. Paid mutation costs wait to be reclaimed.
|
||||
std::vector<std::pair<std::uint64_t, biology::CellularActivity>> activity;
|
||||
const auto activity_of = [&activity](std::uint64_t life_id) -> biology::CellularActivity& {
|
||||
const auto found = std::ranges::find(
|
||||
activity, life_id, &std::pair<std::uint64_t, biology::CellularActivity>::first);
|
||||
if (found != activity.end())
|
||||
return found->second;
|
||||
return activity.emplace_back(life_id, biology::CellularActivity{}).second;
|
||||
};
|
||||
std::vector<std::pair<accounting::Inventory, biology::CellularPosition>> reclaims;
|
||||
for (const auto& result : proposal->lives) {
|
||||
const auto found = std::ranges::lower_bound(
|
||||
next_lives, result.life_id, {}, [](const auto& life) { return life.identity.life; });
|
||||
@@ -2129,9 +2359,80 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
found->motion = result.candidate.motion;
|
||||
if (!add_inventory(next_heat, result.candidate.heat_after))
|
||||
return std::unexpected{CellularLivesError::overflow};
|
||||
if (revamp_rules && found->account != LifeAccount{}) {
|
||||
using S = biology::CellularSignal;
|
||||
const auto& c = result.candidate;
|
||||
auto& a = activity_of(result.life_id);
|
||||
const bool thrust = c.movement_status == biology::CellularMovementStatus::moved ||
|
||||
c.movement_status == biology::CellularMovementStatus::blocked;
|
||||
a[S::thrust] = thrust ? 1 : 0;
|
||||
a[S::drifting] = thrust ? 0 : 1;
|
||||
const auto request =
|
||||
std::ranges::lower_bound(ordered, result.life_id, {}, &CellularLifeIntent::life_id);
|
||||
const bool brake = request != ordered.end() && request->life_id == result.life_id &&
|
||||
request->intent.brake;
|
||||
a[S::brake_press] = brake && found->revamp.last_brake == 0 ? 1 : 0;
|
||||
found->revamp.last_brake = brake ? 1 : 0;
|
||||
const auto acquired = c.intake ? c.intake->transfer.acquired : accounting::Inventory{};
|
||||
std::uint64_t collected{}, burned{};
|
||||
for (const auto& channel : acquired.matter)
|
||||
collected += channel.value;
|
||||
for (const auto& channel : c.spent.matter)
|
||||
burned += channel.value;
|
||||
a[S::fed] = collected != 0 || acquired.energy.value != 0 ? 1 : 0;
|
||||
a[S::intake_limited] = c.matter_limited ? 1 : 0;
|
||||
a[S::energy_limited] = c.energy_limited ? 1 : 0;
|
||||
a[S::energy_collected] = acquired.energy.value;
|
||||
a[S::collected_carbon] = acquired.matter[0].value;
|
||||
a[S::collected_nitrogen] = acquired.matter[1].value;
|
||||
a[S::collected_phosphorus] = acquired.matter[2].value;
|
||||
a[S::collected_other] = acquired.matter[3].value;
|
||||
a[S::collected_matter] = collected;
|
||||
a[S::burned_carbon] = c.spent.matter[0].value;
|
||||
a[S::burned_nitrogen] = c.spent.matter[1].value;
|
||||
a[S::burned_phosphorus] = c.spent.matter[2].value;
|
||||
a[S::burned_other] = c.spent.matter[3].value;
|
||||
a[S::burned_matter] = burned;
|
||||
a[S::burned_not_carbon] = burned - c.spent.matter[0].value;
|
||||
a[S::energy_upkeep] = c.maintenance_cost.value;
|
||||
a[S::energy_movement] = c.movement_cost.value;
|
||||
a[S::energy_spent] = c.maintenance_cost.value + c.movement_cost.value;
|
||||
const bool asked = request != ordered.end() && request->life_id == result.life_id &&
|
||||
request->intent.engulf;
|
||||
a[S::engulf_attempt] = asked ? 1 : 0;
|
||||
a[S::engulf_success] = result.engulf == biology::CellularEngulfStatus::engulfed ? 1 : 0;
|
||||
const bool too_large = result.engulf == biology::CellularEngulfStatus::prey_too_large;
|
||||
a[S::engulf_too_large] = too_large ? 1 : 0;
|
||||
a[S::engulf_too_large_or_membrane] =
|
||||
too_large || result.engulf == biology::CellularEngulfStatus::membrane_too_thick ? 1
|
||||
: 0;
|
||||
a.heading = c.motion.heading;
|
||||
found->revamp.progress.carbon_burned += c.spent.matter[0].value;
|
||||
// Touching a cell over 1.5 times its own mass.
|
||||
const auto own = std::ranges::lower_bound(inputs, result.life_id, {},
|
||||
&biology::CellularLifeStepInput::life_id);
|
||||
if (own != inputs.end() && own->life_id == result.life_id)
|
||||
for (const auto& other : proposal->lives) {
|
||||
if (other.life_id == result.life_id)
|
||||
continue;
|
||||
const auto input = std::ranges::lower_bound(
|
||||
inputs, other.life_id, {}, &biology::CellularLifeStepInput::life_id);
|
||||
if (input == inputs.end() || input->life_id != other.life_id ||
|
||||
input->budget.phenotype.total_mass * 2 <=
|
||||
own->budget.phenotype.total_mass * 3)
|
||||
continue;
|
||||
if (biology::cellular_footprints_contact(c.position, own->extent_um,
|
||||
other.candidate.position,
|
||||
input->extent_um)) {
|
||||
a[S::larger_contact] = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Only owned lives earn mutation offers; wildlife never mutates in M11 (ADR-0098).
|
||||
// Rules 13 earn them from active play instead (below).
|
||||
if (state_->format_version >= cellular_lives_format && result.candidate.intake &&
|
||||
found->account != LifeAccount{}) {
|
||||
found->account != LifeAccount{} && !revamp_rules) {
|
||||
const auto acquired = result.candidate.intake->transfer.acquired.energy.value;
|
||||
if (acquired >
|
||||
std::numeric_limits<std::uint64_t>::max() - found->mutation.fed_energy_credit_pj)
|
||||
@@ -2188,6 +2489,10 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
state_->revamp.body);
|
||||
if (!corpse_budget)
|
||||
return std::unexpected{corpse_budget.error()};
|
||||
if (revamp_rules && dead.cause == biology::CellularDeathCause::engulfed &&
|
||||
found->parent_life != 0 &&
|
||||
proposal->end_tick - found->identity.born.value <= cellular_bud_early_ticks)
|
||||
activity_of(found->parent_life)[biology::CellularSignal::bud_lost_early] += 1;
|
||||
next_corpses.push_back(
|
||||
{found->identity.life, found->identity.organism, found->source, proposal->end_tick,
|
||||
dead.position, found->growth, corpse_budget->phenotype.extent, dead.inventory,
|
||||
@@ -2206,6 +2511,72 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
{selection.life_id, selection.successor_id, CellularMutationResult::life_died});
|
||||
continue;
|
||||
}
|
||||
if (revamp_rules) {
|
||||
auto& pending = found->revamp.offer;
|
||||
const auto plan = next_archive.find(cellular_offspring_genome(*found));
|
||||
const auto expressed = next_archive.find(found->identity.genome);
|
||||
if (pending.count == 0 || !plan || !expressed)
|
||||
return std::unexpected{CellularLivesError::mutation};
|
||||
const auto* spec = biology::cellular_mutation(selection.operator_id);
|
||||
const auto draft = spec == nullptr
|
||||
? std::nullopt
|
||||
: biology::apply_cellular_mutation(
|
||||
*plan, *spec, state_->revamp.mutations.at(spec->id - 1u),
|
||||
state_->revamp.bounds, found->revamp.progress,
|
||||
selection.successor_id, selection.successor_id);
|
||||
if (selection.operator_id == 0 || !draft) {
|
||||
// A decline, or a mutation no longer available, consumes the offer.
|
||||
pending = {};
|
||||
mutation_results.push_back(
|
||||
{selection.life_id, selection.successor_id, CellularMutationResult::declined});
|
||||
continue;
|
||||
}
|
||||
// The cost comes from the reserve: matter channels directly, energy with matter
|
||||
// standing in (except excluded channels). Unaffordable: the offer stays.
|
||||
const auto cost = cellular_mutation_cost(state_->revamp.mutations.at(spec->id - 1u));
|
||||
auto reserve = found->reserve;
|
||||
bool affordable = true;
|
||||
for (std::size_t c = 0; c < reserve.matter.size(); ++c) {
|
||||
if (reserve.matter[c].value < cost.matter[c].value)
|
||||
affordable = false;
|
||||
else
|
||||
reserve.matter[c].value -= cost.matter[c].value;
|
||||
}
|
||||
const auto request = std::ranges::lower_bound(ordered, selection.life_id, {},
|
||||
&CellularLifeIntent::life_id);
|
||||
const auto exclude = request != ordered.end() && request->life_id == selection.life_id
|
||||
? request->intent.exclude
|
||||
: std::uint8_t{0};
|
||||
const auto& traits = expressed->cellular_traits();
|
||||
const auto burn = biology::cellular_burn(
|
||||
state_->revamp.burn_pj_per_kfg,
|
||||
traits ? traits->burn : std::array<std::uint16_t, 4>{100, 100, 100, 100}, exclude);
|
||||
const auto payment =
|
||||
affordable ? biology::cellular_pay(reserve, cost.energy.value, burn, false)
|
||||
: std::nullopt;
|
||||
if (!payment) {
|
||||
mutation_results.push_back({selection.life_id, selection.successor_id,
|
||||
CellularMutationResult::insufficient_funding});
|
||||
continue;
|
||||
}
|
||||
const auto successor = biology::Genome::create(*draft);
|
||||
if (!successor || next_offspring_revisions.size() >= biology::max_archived_genomes ||
|
||||
!next_archive.admit(*successor))
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
next_offspring_revisions.push_back({selection.life_id, found->offspring_genome,
|
||||
selection.successor_id, proposal->end_tick});
|
||||
found->offspring_genome = selection.successor_id;
|
||||
found->reserve = reserve;
|
||||
if (!add_inventory(next_spent, payment->spent))
|
||||
return std::unexpected{CellularLivesError::overflow};
|
||||
auto returned = cost;
|
||||
returned.energy.value = payment->energy;
|
||||
reclaims.emplace_back(returned, found->position);
|
||||
pending = {};
|
||||
mutation_results.push_back({selection.life_id, selection.successor_id,
|
||||
CellularMutationResult::offspring_plan_applied});
|
||||
continue;
|
||||
}
|
||||
const auto parent = next_archive.find(cellular_offspring_genome(*found));
|
||||
if (!parent || !found->mutation.offer.has_value())
|
||||
return std::unexpected{CellularLivesError::mutation};
|
||||
@@ -2317,8 +2688,28 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
budget_for(next_archive, *parent, temperature, state_->revamp.body);
|
||||
if (!genome || !expressed || !parent_budget)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
// Rules 13: a bud made with the reserve covering twice its cost.
|
||||
bool surplus = false;
|
||||
if (revamp_rules && !twin) {
|
||||
const auto cost = biology::offspring_cost(*expressed, *genome, temperature);
|
||||
surplus =
|
||||
cost.has_value() && parent->reserve.energy.value / 2 >= cost->energy.value;
|
||||
for (std::size_t c = 0; surplus && c < cost->matter.size(); ++c)
|
||||
surplus = parent->reserve.matter[c].value / 2 >= cost->matter[c].value;
|
||||
}
|
||||
const auto transition = biology::fund_offspring(
|
||||
*expressed, *genome, parent->growth, parent->reserve, temperature, twins ? 2U : 1U);
|
||||
if (revamp_rules && !twin && parent->account != LifeAccount{}) {
|
||||
using S = biology::CellularSignal;
|
||||
auto& a = activity_of(request.life_id);
|
||||
if (transition) {
|
||||
a[S::bud_done] += 1;
|
||||
a[S::bud_with_surplus] += surplus ? 1 : 0;
|
||||
parent->revamp.progress.buds += 1;
|
||||
} else if (transition.error() == biology::BuddingError::insufficient_funding) {
|
||||
a[S::bud_refused] += 1;
|
||||
}
|
||||
}
|
||||
if (!transition) {
|
||||
if (transition.error() == biology::BuddingError::development)
|
||||
return std::unexpected{CellularLivesError::biology};
|
||||
@@ -2361,6 +2752,11 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
bud.parent_life = parent->identity.life;
|
||||
if (inheritance_rules)
|
||||
bud.offspring_genome = genome->id();
|
||||
if (revamp_rules) {
|
||||
// Offspring start with the parent's scores and lineage progress.
|
||||
bud.revamp.affinity.score = parent->revamp.affinity.score;
|
||||
bud.revamp.progress = parent->revamp.progress;
|
||||
}
|
||||
// Foreign residents can have a higher origin-region namespace. Keep
|
||||
// the sequence sorted even before the next parent's lookup/twin.
|
||||
const auto slot = std::ranges::lower_bound(
|
||||
@@ -2433,15 +2829,48 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
const auto genome = next_archive.find(life.identity.genome);
|
||||
if (!genome)
|
||||
return std::unexpected(CellularLivesError::archive);
|
||||
// Rules 13: growth speed scales the per-tick step; the fraction carries over.
|
||||
auto step = cellular_growth_per_tick;
|
||||
const auto& traits = genome->cellular_traits();
|
||||
if (revamp_rules && traits) {
|
||||
const auto centi =
|
||||
life.revamp.growth_carry +
|
||||
state_->revamp.growth_per_tick_centi_mille * traits->growth_speed / 100;
|
||||
step = centi / 100;
|
||||
life.revamp.growth_carry = centi % 100;
|
||||
if (step == 0)
|
||||
continue;
|
||||
}
|
||||
const auto budget = biology::metabolic_budget(
|
||||
*genome, life.growth,
|
||||
{state_->config.ambient_temperature_millikelvin, life.reserve},
|
||||
cellular_growth_per_tick);
|
||||
{state_->config.ambient_temperature_millikelvin, life.reserve}, step,
|
||||
state_->revamp.body);
|
||||
if (!budget || budget->body != life.body)
|
||||
return std::unexpected(CellularLivesError::biology);
|
||||
const auto& move = budget->construction;
|
||||
if (move.after == life.growth)
|
||||
if (move.after == life.growth) {
|
||||
if (revamp_rules && life.account != LifeAccount{}) {
|
||||
// Which channels held the step back: what an ample reserve's step needs.
|
||||
accounting::Inventory ample{{1ULL << 50U}, {}};
|
||||
for (auto& channel : ample.matter)
|
||||
channel.value = 1ULL << 50U;
|
||||
const auto full = biology::metabolic_budget(
|
||||
*genome, life.growth,
|
||||
{state_->config.ambient_temperature_millikelvin, ample}, step,
|
||||
state_->revamp.body);
|
||||
if (full) {
|
||||
using S = biology::CellularSignal;
|
||||
constexpr std::array blocked{S::blocked_carbon, S::blocked_nitrogen,
|
||||
S::blocked_phosphorus, S::blocked_other};
|
||||
auto& a = activity_of(life.identity.life);
|
||||
for (std::size_t c = 0; c < blocked.size(); ++c)
|
||||
if (full->construction.intake.matter[c].value >
|
||||
life.reserve.matter[c].value)
|
||||
a[blocked.at(c)] = 1;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
const auto grown = biology::metabolic_budget(
|
||||
*genome, move.after, {state_->config.ambient_temperature_millikelvin, {}});
|
||||
if (!grown)
|
||||
@@ -2477,6 +2906,44 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
life.body = grown->body;
|
||||
}
|
||||
}
|
||||
// Rules 13: each owned, active life's activity moves its affinity; active play earns offers.
|
||||
if (revamp_rules) {
|
||||
using S = biology::CellularSignal;
|
||||
for (auto& life : next_lives) {
|
||||
const auto request = std::ranges::lower_bound(ordered, life.identity.life, {},
|
||||
&CellularLifeIntent::life_id);
|
||||
if (life.account == LifeAccount{} || request == ordered.end() ||
|
||||
request->life_id != life.identity.life || request->intent.frozen ||
|
||||
life.presence != CellularPresence::active)
|
||||
continue; // Wildlife, newborns, and frozen or absent cells.
|
||||
auto a = activity_of(life.identity.life);
|
||||
std::uint64_t reserve{}, body{};
|
||||
for (std::size_t c = 0; c < life.body.matter.size(); ++c) {
|
||||
reserve += life.reserve.matter[c].value;
|
||||
body += life.body.matter[c].value;
|
||||
}
|
||||
a[S::reserve_over_body] = reserve > body ? 1 : 0;
|
||||
a[S::regrowing] = life.growth.realized_per_thousand < 1000 ? 1 : 0;
|
||||
a[S::fully_grown] = life.growth.realized_per_thousand == 1000 ? 1 : 0;
|
||||
biology::cellular_affinity_tick(
|
||||
life.revamp.affinity, a, state_->revamp.mutations,
|
||||
{state_->revamp.offers.score_min, state_->revamp.offers.score_max});
|
||||
if (life.revamp.offer.count != 0 ||
|
||||
++life.revamp.active_ticks < state_->revamp.offer_interval_ticks)
|
||||
continue;
|
||||
life.revamp.active_ticks = 0;
|
||||
const auto plan = next_archive.find(cellular_offspring_genome(life));
|
||||
if (!plan)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto offer =
|
||||
cellular_draw_offer(*plan, life.revamp, state_->revamp, state_->world,
|
||||
state_->region, life.identity.life, proposal->end_tick);
|
||||
if (offer) {
|
||||
life.revamp.offer = *offer;
|
||||
life.revamp.events = offer->event_index;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::ranges::sort(next_corpses, {}, &CellularOwnedCorpse::organism);
|
||||
if (proposal->heat_released.value >
|
||||
std::numeric_limits<std::uint64_t>::max() - next_heat.energy.value)
|
||||
@@ -2512,6 +2979,13 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
!cellular_drip_spent(habitat_config, next_spent, state_->revamp, state_->world,
|
||||
state_->region, proposal->end_tick, state_->nursery_config.source))
|
||||
return std::unexpected{CellularLivesError::overflow};
|
||||
// Rules 13: paid mutation costs return as bubbles at seeded random spots.
|
||||
for (std::size_t k = 0; k < reclaims.size(); ++k)
|
||||
if (!cellular_reclaim(habitat_config, reclaims[k].first,
|
||||
state_->world * 0x9e3779b97f4a7c15ULL ^
|
||||
proposal->end_tick * 0x94d049bbULL ^ k,
|
||||
reclaims[k].second, state_->nursery_config.source))
|
||||
return std::unexpected{CellularLivesError::overflow};
|
||||
if (deposits)
|
||||
std::erase_if(habitat_config.patches, [](const biology::CellularPatch& patch) {
|
||||
return patch.stock == accounting::Inventory{};
|
||||
|
||||
@@ -4,8 +4,35 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
|
||||
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;
|
||||
@@ -19,7 +46,8 @@ bool cellular_revamp_valid(const CellularRevampRules& rules) noexcept {
|
||||
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.reserve_load_per_thousand <= 1'000 &&
|
||||
rules.growth_per_tick_centi_mille <= 100'000 && cellular_revamp_offer_rules_valid(rules);
|
||||
}
|
||||
|
||||
biology::CellularHandling cellular_loaded_handling(const biology::CellularHandling& handling,
|
||||
@@ -87,6 +115,117 @@ bool cellular_drip_spent(biology::CellularHabitatConfig& config, accounting::Inv
|
||||
return true;
|
||||
}
|
||||
|
||||
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) {
|
||||
const auto catalog = biology::cellular_catalog();
|
||||
std::vector<std::uint16_t> eligible;
|
||||
std::vector<std::uint64_t> 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<std::uint64_t>::max() / whole * whole)
|
||||
draw = random.next(); // Exactly uniform below one whole.
|
||||
const auto picked = biology::cellular_systematic_draw(*chances, rules.offers.options,
|
||||
static_cast<std::uint32_t>(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<std::uint8_t>(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<std::int64_t>(patch.address.x_um) -
|
||||
static_cast<std::int64_t>(from.x_um);
|
||||
const auto dy = static_cast<std::int64_t>(patch.address.y_um) -
|
||||
static_cast<std::int64_t>(from.y_um);
|
||||
return static_cast<std::uint64_t>(dx * dx) + static_cast<std::uint64_t>(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<std::uint64_t>::max() - target)
|
||||
return false;
|
||||
target += value;
|
||||
continue;
|
||||
}
|
||||
biology::CellularPatchAddress address{static_cast<std::uint32_t>(x % config.width_um),
|
||||
static_cast<std::uint32_t>(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<std::uint32_t>::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<std::uint32_t>(value); };
|
||||
const auto u64 = [](std::int64_t value) { return static_cast<std::uint64_t>(value); };
|
||||
@@ -103,6 +242,22 @@ CellularRevampRules cellular_revamp_rules(const biology::CellularRevampTuning& t
|
||||
.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)};
|
||||
.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<std::int32_t>(t.mutation_score_min),
|
||||
.score_max = static_cast<std::int32_t>(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};
|
||||
}
|
||||
} // namespace evo::server
|
||||
|
||||
Reference in New Issue
Block a user