Implement M6.6 retained corpses and versioned bounded decay

This commit is contained in:
dingo
2026-09-27 18:37:36 -04:00
parent 6fd553c84b
commit 6dd0e49e45
12 changed files with 392 additions and 27 deletions
+35 -18
View File
@@ -38,7 +38,7 @@ struct Reader {
return v;
}
};
template <class T, class IO> void config_io(T& c, IO& io) {
template <class T, class IO> void config_io(T& c, IO& io, bool extended) {
io(c.rules);
io(c.world);
io(c.frame);
@@ -62,29 +62,37 @@ template <class T, class IO> void config_io(T& c, IO& io) {
io(c.producer_max_mK);
io(c.consumer_min_mK);
io(c.consumer_max_mK);
if (extended)
for (auto& v : c.decay)
io(v);
}
template <class T, class IO> void progress_io(T& p, IO& io) {
template <class T, class IO> void progress_io(T& p, IO& io, bool extended) {
io(p.tick);
io(p.source_remainder);
io(p.last_funded);
io(p.efficiency_remainder);
io(p.growth_remainder);
io(p.producer_terminal);
if (extended)
io(p.consumer_terminal);
}
template <class T, class IO> void state_io(T& s, IO& io) {
template <class T, class IO> void state_io(T& s, IO& io, bool extended) {
for (auto& v : s.environment)
io(v);
for (auto& v : s.producer)
io(v);
for (auto& v : s.consumer)
io(v);
progress_io(s.progress, io);
progress_io(s.progress, io, extended);
}
template <class T, class IO> void history_io(T& h, IO& io) {
template <class T, class IO> void history_io(T& h, IO& io, bool extended) {
io(h.input.temperature_mK);
io(h.input.source_enabled);
for (auto& v : h.input.bite)
io(v);
if (extended)
for (auto& v : h.input.terminal)
io(v);
auto& r = h.report;
io(r.tick);
io(r.captured);
@@ -98,27 +106,32 @@ template <class T, class IO> void history_io(T& h, IO& io) {
for (auto& v : r.waste)
io(v);
io(r.terminal_transition);
if (extended) {
io(r.consumer_transition);
for (auto& v : r.decomposed)
io(v);
}
}
} // namespace
std::expected<world::WorldBytes, CommunityError> EnvironmentalCommunity::snapshot() const {
auto resource = resources_->snapshot();
if (!resource)
return std::unexpected{CommunityError::resources};
const auto length =
24 + 29 * 8 + 6 * 8 + 36 * 8 + 8 + count_ * 25 * 8 + 8 + resource->size() + 8;
const bool extended = config_.rules == 2;
const auto length = (extended ? 712 : 616) + count_ * (extended ? 304 : 200) + resource->size();
if (length > max_snapshot_bytes)
return std::unexpected{CommunityError::budget};
Writer w;
w.bytes.resize(length);
w(magic);
w(1);
w(config_.rules);
w(length);
config_io(config_, w);
progress_io(progress_, w);
state_io(anchor_, w);
config_io(config_, w, extended);
progress_io(progress_, w, extended);
state_io(anchor_, w, extended);
w(count_);
for (std::size_t i = 0; i < count_; ++i)
history_io(history_[(next_ + history_limit - count_ + i) % history_limit], w);
history_io(history_[(next_ + history_limit - count_ + i) % history_limit], w, extended);
w(resource->size());
std::copy(resource->begin(), resource->end(),
w.bytes.begin() + static_cast<std::ptrdiff_t>(w.at));
@@ -136,7 +149,9 @@ EnvironmentalCommunity::restore(std::span<const std::uint8_t> bytes, world::Worl
Reader r{bytes.first(bytes.size() - 8)}, tail{bytes.last(8)};
if (r.get() != magic)
return std::unexpected{CommunityError::format};
if (r.get() != 1)
const auto version = r.get();
const bool extended = version == 2;
if (version != 1 && version != 2)
return std::unexpected{CommunityError::version};
if (r.get() != bytes.size())
return std::unexpected{CommunityError::format};
@@ -145,16 +160,18 @@ EnvironmentalCommunity::restore(std::span<const std::uint8_t> bytes, world::Worl
if (budget < sizeof(EnvironmentalCommunity) + scratch_bytes)
return std::unexpected{CommunityError::budget};
auto o = std::unique_ptr<EnvironmentalCommunity>(new EnvironmentalCommunity);
config_io(o->config_, r);
progress_io(o->progress_, r);
state_io(o->anchor_, r);
config_io(o->config_, r, extended);
if (o->config_.rules != version)
return std::unexpected{CommunityError::version};
progress_io(o->progress_, r, extended);
state_io(o->anchor_, r, extended);
const auto count = r.get();
if (!r.ok || count != std::min(o->progress_.tick, std::uint64_t{history_limit}) ||
o->anchor_.progress.tick != o->progress_.tick - count ||
count * 200 > r.bytes.size() - r.at)
count * (extended ? 304 : 200) > r.bytes.size() - r.at)
return std::unexpected{CommunityError::format};
for (std::size_t i = 0; i < count; ++i) {
history_io(o->history_[i], r);
history_io(o->history_[i], r, extended);
if (o->history_[i].report.tick != o->anchor_.progress.tick + i + 1)
return std::unexpected{CommunityError::format};
}
+40 -5
View File
@@ -15,7 +15,9 @@ std::uint64_t portion(std::uint64_t x, std::uint64_t a) {
} // namespace
bool EnvironmentalCommunity::configuration(const EnvironmentalConfig& c,
const world::EnvironmentalTransfers& r) noexcept {
if (c.rules != 1 || c.world == 0 || c.frame == 0 ||
if ((c.rules != 1 && c.rules != 2) ||
(c.rules == 1 && std::ranges::any_of(c.decay, [](auto v) { return v != 0; })) ||
c.world == 0 || c.frame == 0 ||
r.fields().identity() != world::WorldIdentity{c.world, c.frame} || c.source == 0 ||
c.tick_microseconds == 0 || c.area_um2 == 0 || c.irradiance_denominator == 0 ||
c.growth_denominator == 0 || c.efficiency > 1000 || c.assimilation > 1000 ||
@@ -51,10 +53,12 @@ bool EnvironmentalCommunity::valid(const State& s) const noexcept {
if ((p.last_funded == 0 && (p.source_remainder != 0 || p.efficiency_remainder != 0)) ||
p.source_remainder >= config_.irradiance_denominator ||
p.growth_remainder >= config_.growth_denominator || p.efficiency_remainder >= 1000 ||
p.last_funded > p.tick || p.producer_terminal > 1 ||
p.last_funded > p.tick || p.producer_terminal > 1 || p.consumer_terminal > 1 ||
(config_.rules == 1 && p.consumer_terminal != 0) ||
(p.tick == 0 && p != EnvironmentalProgress{}))
return false;
if (p.producer_terminal && std::ranges::any_of(s.producer, [](auto v) { return v != 0; }))
if (config_.rules == 1 && p.producer_terminal &&
std::ranges::any_of(s.producer, [](auto v) { return v != 0; }))
return false;
if (s.environment[0] > resources_->fields().water_capacity(0)->value)
return false;
@@ -98,6 +102,18 @@ EnvironmentalCommunity::step(State& s, std::uint64_t tick,
const auto& c = config_;
EnvironmentalReport report{};
report.tick = tick;
if (input.terminal[0] > 1 || input.terminal[1] > 1 ||
(c.rules == 1 && (input.terminal[0] || input.terminal[1])) ||
(input.terminal[0] && p.producer_terminal) || (input.terminal[1] && p.consumer_terminal))
return std::unexpected{CommunityError::input};
if (input.terminal[0]) {
p.producer_terminal = 1;
report.terminal_transition = 1;
}
if (input.terminal[1]) {
p.consumer_terminal = 1;
report.consumer_transition = 1;
}
const bool eligible = !p.producer_terminal && input.temperature_mK >= c.producer_min_mK &&
input.temperature_mK <= c.producer_max_mK;
if (eligible && input.source_enabled) {
@@ -150,8 +166,8 @@ EnvironmentalCommunity::step(State& s, std::uint64_t tick,
report.unmet = quota->amount.value - grown;
}
if (any(input.bite)) {
if (p.producer_terminal || input.temperature_mK < c.consumer_min_mK ||
input.temperature_mK > c.consumer_max_mK)
if (p.producer_terminal || p.consumer_terminal ||
input.temperature_mK < c.consumer_min_mK || input.temperature_mK > c.consumer_max_mK)
return std::unexpected{CommunityError::input};
for (std::size_t j = 0; j < 5; ++j) {
const auto amount = input.bite[j], k = food[j];
@@ -173,6 +189,25 @@ EnvironmentalCommunity::step(State& s, std::uint64_t tick,
report.terminal_transition = 1;
}
}
if (c.rules == 2) {
constexpr std::array<std::size_t, 10> destination{0, 1, 2, 3, 1, 2, 6, 9, 8, 9};
auto remaining = c.decay;
for (std::size_t actor = 0; actor < 2; ++actor) {
if (!(actor == 0 ? p.producer_terminal : p.consumer_terminal))
continue;
auto& corpse = actor == 0 ? s.producer : s.consumer;
for (std::size_t k = 0; k < 10; ++k) {
auto amount = std::min(remaining[k], corpse[k]);
if (k == 0)
amount = std::min(amount, resources_->fields().water_capacity(0)->value -
s.environment[0]);
corpse[k] -= amount;
s.environment[destination[k]] += amount;
remaining[k] -= amount;
report.decomposed[k] += amount;
}
}
}
p.tick = tick;
if (!valid(s))
return std::unexpected{CommunityError::resources};