Files
aevolution/tests/cellular_revamp_tuning_tests.cpp
T
dingoandClaude Opus 5.5 cc2eaf2e05
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m42s
Foundation / checks (checks) (push) Successful in 6s
Foundation / checks (client) (push) Successful in 5s
Foundation / postgres-tools (push) Successful in 31s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / server-image (push) Successful in 4m45s
Foundation / checks (network) (push) Successful in 4s
Foundation / image (push) Successful in 5m31s
feat(cellular): energy cycles back through the spent pool (ADR-0182 section 14)
Energy cells used went to the region's heat store and never returned, so a
region without photosynthesizers drained: region 16's wildlife ate its energy
and starved, and its matter-only drip filled the deposit limit.

Two optional rules-13 tuning keys, adoptable live: spent.heat_recycle_per_mille
moves that share of the heat store to the spent pool at the end of every tick,
before the drip (1,000 closes the loop and returns heat built up before), and
spent.drip_absorb lets a drip at the deposit limit join the first existing bubble
at or after its spot. Both use the reserved word after the offer interval, so
rules without them keep their bytes, digests, checkpoints and replays. The DEV
revamp file turns both on. The region view reports heat and the cycle; the
portal counts heat in the region's energy and ledger.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 10:18:24 -04:00

237 lines
12 KiB
C++

#include "evo/biology/cellular_revamp_tuning.hpp"
#include <cstdio>
#include <fstream>
#include <iterator>
#include <source_location>
#include <stdexcept>
#include <string>
// The rules-13 revamp tuning file (ADR-0179): same format and refusals as the rules-12 file,
// with its own keys.
namespace {
using namespace evo::biology;
void check(bool condition, std::source_location at = std::source_location::current()) {
if (!condition)
throw std::runtime_error("cellular revamp tuning line " + std::to_string(at.line()));
}
#define CHECK(expression) check(static_cast<bool>(expression))
CellularRevampTuning trial(bool safety = true) {
CellularRevampTuning t{.version = 1,
.playtest_starting_wildlife_cells = 100,
.feeding_collection_percent = 130,
.feeding_split_evenly = 1,
.body_essential_upkeep_per_mille = 250,
.body_reserve_load_per_mille = 1'000,
.body_growth_per_tick_centi_mille = 625,
.energy_burn_carbon_pj_per_kfg = 2'000,
.energy_burn_nitrogen_pj_per_kfg = 2'000,
.energy_burn_phosphorus_pj_per_kfg = 2'000,
.energy_burn_other_pj_per_kfg = 2'000,
.feeding_membrane_collection_per_mille_per_point = 2,
.predation_engulf_ratio_milli = 1'500,
.predation_engulf_reserve_percent = 100,
.predation_kill_bubble_per_mille = 500,
.spent_drip_interval_ticks = 20,
.spent_drip_bubble_pj = 20'000'000,
.spent_drip_bubble_fg = 1'000'000,
.mutation_options = 3,
.mutation_floor_ppm = 10,
.mutation_cap_ppm = 700'000,
.mutation_score_min = -1'000,
.mutation_score_max = 1'000,
.mutation_max_multiplier_per_mille = 10'000,
.mutation_min_multiplier_per_mille = 0,
.mutation_offer_interval_ticks = 500,
.mutation_min_points = 25,
.mutation_max_points = 1'000,
.mutation_recipe_min_share = 20,
.mutation_max_offspring = 10,
.mutation_carbon_for_light_fg = 20'000'000,
.mutation_buds_for_adhesion = 5,
.reproduction_split_share_per_mille = 800,
.reproduction_split_min_growth_per_mille = 500,
.reproduction_bud_fee_e_pj = 250'000,
.reproduction_bud_fee_c_fg = 100'000,
.reproduction_bud_fee_n_fg = 30'000,
.reproduction_bud_fee_p_fg = 10'000,
.reproduction_bud_fee_o_fg = 60'000,
.reproduction_bud_fee_scale_percent = 50,
.reproduction_organism_cap = 256,
.organism_load_per_mille = 500,
.organism_emerge_members = 8,
.organism_emerge_mass_fg = 100'000'000,
.organism_emerge_reserve_pj = 0,
.reproduction_safety_version = 1,
.body_growth_reserve_seconds = 60,
.reproduction_split_launch_um_per_s = 800};
const auto catalog = cellular_catalog();
for (std::size_t i = 0; i < catalog.size(); ++i)
t.mutations.at(i) = cellular_default_tuning(catalog[i]);
if (!safety) {
t.reproduction_split_share_per_mille = 500;
t.reproduction_safety_version = 0;
t.body_growth_reserve_seconds = 0;
t.reproduction_split_launch_um_per_s = 0;
// The DEV revamp file cycles energy (ADR-0182 section 14).
t.spent_heat_recycle_per_mille = 1'000;
t.spent_drip_absorb = 1;
}
return t;
}
std::string with(std::string text, const std::string& key, const std::string& value) {
const auto at = text.find(key + " = ");
check(at != std::string::npos);
const auto end = text.find('\n', at);
return text.replace(at, end - at, key + " = " + value);
}
CellularTuningErrorKind error_of(const std::string& text) {
const auto parsed = parse_cellular_revamp_tuning(text);
check(!parsed.has_value());
return parsed.error().kind;
}
// Per-mutation overrides are optional, change only their field, and refuse unknown names,
// unknown fields, duplicates and out-of-range values.
void mutation_overrides() {
const auto base = encode_cellular_revamp_tuning(trial());
// The canonical text lists every override; a file may omit them all.
std::string bare;
for (std::size_t at = 0; at < base.size();) {
const auto end = base.find('\n', at);
const auto line = base.substr(at, end - at);
if (!line.starts_with("mutation.") || line.find('.', 9) == std::string::npos ||
line.find('.', 9) > line.find(' '))
bare += line + "\n";
at = end + 1;
}
CHECK(parse_cellular_revamp_tuning(bare) == trial());
const auto tuned =
parse_cellular_revamp_tuning(bare + "mutation.strong_propulsion.cost_e_pj = 1_000\n"
"mutation.strong_propulsion.cost_scale_percent = 50\n"
"mutation.adhesion.enabled = 0\n");
CHECK(tuned.has_value());
if (!tuned)
return;
auto expected = trial();
expected.mutations.at(0).cost.energy.value = 1'000;
expected.mutations.at(0).cost_scale_percent = 50;
expected.mutations.at(48).enabled = false;
CHECK(*tuned == expected);
CHECK(error_of(bare + "mutation.unknown_thing.enabled = 1\n") ==
CellularTuningErrorKind::unknown_key);
CHECK(error_of(bare + "mutation.adhesion.colour = 1\n") ==
CellularTuningErrorKind::unknown_key);
CHECK(error_of(bare + "mutation.adhesion.enabled = 1\nmutation.adhesion.enabled = 0\n") ==
CellularTuningErrorKind::duplicate_key);
CHECK(error_of(bare + "mutation.adhesion.cost_scale_percent = 101\n") ==
CellularTuningErrorKind::out_of_range);
CHECK(error_of(with(bare, "mutation.floor_ppm", "800000")) ==
CellularTuningErrorKind::inconsistent);
}
void canonical_round_trip() {
const auto text = encode_cellular_revamp_tuning(trial());
const auto parsed = parse_cellular_revamp_tuning(text);
CHECK(parsed && *parsed == trial());
CHECK(encode_cellular_revamp_tuning(*parsed) == text);
const auto commented =
"# heading\n\n" + with(text, "body.essential_upkeep_per_mille", "2_50 # c");
CHECK(parse_cellular_revamp_tuning(commented) == trial());
}
void refusals() {
const auto base = encode_cellular_revamp_tuning(trial());
CHECK(error_of(with(base, "reproduction.safety_version", "2")) ==
CellularTuningErrorKind::out_of_range);
CHECK(error_of(with(base, "reproduction.safety_version", "0")) ==
CellularTuningErrorKind::inconsistent);
CHECK(error_of(with(base, "body.growth_reserve_seconds", "0")) ==
CellularTuningErrorKind::inconsistent);
auto historical = base;
for (const auto* key : {"reproduction.safety_version", "body.growth_reserve_seconds",
"reproduction.split_launch_um_per_s"}) {
const auto at = historical.find(key);
historical.erase(at, historical.find('\n', at) - at + 1);
}
CHECK(parse_cellular_revamp_tuning(historical)->reproduction_safety_version == 0);
// The energy cycle keys (ADR-0182 section 14) are optional and bounded.
auto uncycled = base;
for (const auto* key : {"spent.heat_recycle_per_mille", "spent.drip_absorb"}) {
const auto at = uncycled.find(key);
uncycled.erase(at, uncycled.find('\n', at) - at + 1);
}
CHECK(parse_cellular_revamp_tuning(uncycled) == trial());
const auto cycled = parse_cellular_revamp_tuning(
with(with(base, "spent.heat_recycle_per_mille", "1_000"), "spent.drip_absorb", "1"));
CHECK(cycled && cycled->spent_heat_recycle_per_mille == 1'000 &&
cycled->spent_drip_absorb == 1);
CHECK(error_of(with(base, "spent.heat_recycle_per_mille", "1001")) ==
CellularTuningErrorKind::out_of_range);
CHECK(error_of(with(base, "spent.drip_absorb", "2")) == CellularTuningErrorKind::out_of_range);
CHECK(error_of(with(base, "playtest.starting_wildlife_cells", "256")) ==
CellularTuningErrorKind::out_of_range);
CHECK(error_of(with(base, "playtest.starting_wildlife_cells", "-1")) ==
CellularTuningErrorKind::out_of_range);
auto legacy = base;
const auto start = legacy.find("playtest.starting_wildlife_cells");
legacy.erase(start, legacy.find('\n', start) - start + 1);
CHECK(parse_cellular_revamp_tuning(legacy)->playtest_starting_wildlife_cells == 0);
CHECK(error_of(base + "unknown.key = 1\n") == CellularTuningErrorKind::unknown_key);
CHECK(error_of(base + "tuning.version = 1\n") == CellularTuningErrorKind::duplicate_key);
CHECK(error_of(base + "no equals sign\n") == CellularTuningErrorKind::syntax);
CHECK(error_of(with(base, "body.essential_upkeep_per_mille", "1001")) ==
CellularTuningErrorKind::out_of_range);
CHECK(error_of(with(base, "body.essential_upkeep_per_mille", "x")) ==
CellularTuningErrorKind::invalid_value);
CHECK(error_of(with(base, "tuning.version", "2")) ==
CellularTuningErrorKind::unsupported_version);
auto missing = base;
missing.erase(missing.find("body.essential_upkeep_per_mille"));
const auto parsed = parse_cellular_revamp_tuning(missing);
CHECK(!parsed && parsed.error().kind == CellularTuningErrorKind::missing_key &&
parsed.error().key == "body.essential_upkeep_per_mille");
CHECK(error_of(with(base, "spent.drip_bubble_fg", "0")) ==
CellularTuningErrorKind::inconsistent);
CHECK(error_of(with(base, "predation.engulf_reserve_percent", "101")) ==
CellularTuningErrorKind::out_of_range);
// Rules-12 keys belong in the other file.
CHECK(error_of(base + "region.width_um = 60000\n") == CellularTuningErrorKind::unknown_key);
CHECK(error_of(std::string(max_cellular_revamp_tuning_bytes + 1, '#')) ==
CellularTuningErrorKind::too_large);
}
// The checked-in DEV revamp tuning file must parse; CTest passes its path when it is present.
void repository_file(const char* path, bool safety) {
std::ifstream file(path, std::ios::binary);
CHECK(file.good());
const std::string text{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()};
const auto parsed = parse_cellular_revamp_tuning(text);
if (!parsed)
std::fprintf(stderr, "%s: line %zu %s: %s\n", path, parsed.error().line,
parsed.error().key.c_str(),
std::string{describe(parsed.error().kind)}.c_str());
CHECK(parsed && *parsed == trial(safety));
}
} // namespace
int main(int argc, char** argv) {
try {
canonical_round_trip();
refusals();
mutation_overrides();
if (argc > 1)
repository_file(argv[1], false);
if (argc > 2)
repository_file(argv[2], true);
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
return 1;
}
std::puts("PASS cellular revamp tuning file");
return 0;
}