Files
aevolution/src/biology/cellular_habitat.cpp
T
dingo 1787718383
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m56s
Foundation / checks (postgres) (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 2m39s
Foundation / checks (client) (push) Canceled after 2m38s
Foundation / checks (network) (push) Canceled after 36s
Add finite cellular habitat snapshot
2026-10-10 21:13:51 -04:00

213 lines
8.3 KiB
C++

#include "evo/biology/cellular_habitat.hpp"
#include <algorithm>
#include <array>
#include <limits>
namespace evo::biology {
namespace {
constexpr std::uint64_t habitat_magic = 0x314B5441504F5645ULL; // EVOPATK1
constexpr std::uint32_t habitat_rules = 1;
bool add(std::uint64_t value, std::uint64_t& total) noexcept {
if (value > std::numeric_limits<std::uint64_t>::max() - total)
return false;
total += value;
return true;
}
bool add(accounting::Inventory value, accounting::Inventory& total) noexcept {
if (!add(value.energy.value, total.energy.value))
return false;
for (std::size_t channel = 0; channel < value.matter.size(); ++channel)
if (!add(value.matter[channel].value, total.matter[channel].value))
return false;
return true;
}
std::uint64_t checksum(std::span<const std::uint8_t> bytes) noexcept {
std::uint64_t value = 14695981039346656037ULL;
for (const auto byte : bytes)
value = (value ^ byte) * 1099511628211ULL;
return value;
}
class Writer {
public:
explicit Writer(std::vector<std::uint8_t>& bytes) : bytes_{bytes} {}
void put(std::uint64_t value, unsigned width = 8) {
for (unsigned i = 0; i < width; ++i, value >>= 8U)
bytes_.push_back(static_cast<std::uint8_t>(value & 255U));
}
void inventory(accounting::Inventory value) {
put(value.energy.value);
for (const auto matter : value.matter)
put(matter.value);
}
private:
std::vector<std::uint8_t>& bytes_;
};
class Reader {
public:
explicit Reader(std::span<const std::uint8_t> bytes) : bytes_{bytes} {}
std::uint64_t get(unsigned width = 8) {
std::uint64_t value{};
for (unsigned i = 0; i < width; ++i)
value |= std::uint64_t{bytes_[at_++]} << (8U * i);
return value;
}
accounting::Inventory inventory() {
accounting::Inventory value{};
value.energy.value = get();
for (auto& matter : value.matter)
matter.value = get();
return value;
}
private:
std::span<const std::uint8_t> bytes_;
std::size_t at_{};
};
} // namespace
CellularHabitat::~CellularHabitat() = default;
std::expected<std::unique_ptr<CellularHabitat>, CellularHabitatError>
CellularHabitat::create(CellularHabitatConfig config) {
if (config.world == 0 || config.region == 0 || config.frame == 0 || config.width_um == 0 ||
config.height_um == 0 || config.width_um > cellular_motion_axis_limit_um ||
config.height_um > cellular_motion_axis_limit_um ||
config.width_um % cellular_patch_pitch_um != 0 ||
config.height_um % cellular_patch_pitch_um != 0 ||
config.patches.size() > max_cellular_patches)
return std::unexpected{CellularHabitatError::invalid_config};
accounting::Inventory totals{};
for (std::size_t index = 0; index < config.patches.size(); ++index) {
const auto& patch = config.patches[index];
if (patch.source == 0 || patch.address.x_um >= config.width_um ||
patch.address.y_um >= config.height_um ||
patch.address.x_um % cellular_patch_pitch_um != 0 ||
patch.address.y_um % cellular_patch_pitch_um != 0)
return std::unexpected{CellularHabitatError::invalid_patch};
if (index != 0 && !(config.patches[index - 1].address < patch.address))
return std::unexpected{config.patches[index - 1].address == patch.address
? CellularHabitatError::duplicate_patch
: CellularHabitatError::invalid_patch};
if (!add(patch.stock, totals))
return std::unexpected{CellularHabitatError::overflow};
}
return std::unique_ptr<CellularHabitat>{new CellularHabitat{std::move(config), totals}};
}
const CellularHabitatConfig& CellularHabitat::config() const noexcept {
return config_;
}
std::span<const CellularPatch> CellularHabitat::patches() const noexcept {
return config_.patches;
}
accounting::Inventory CellularHabitat::totals() const noexcept {
return totals_;
}
std::optional<std::size_t> CellularHabitat::find(CellularPatchAddress address) const noexcept {
const auto found = std::lower_bound(
config_.patches.begin(), config_.patches.end(), address,
[](const CellularPatch& patch, CellularPatchAddress key) { return patch.address < key; });
if (found == config_.patches.end() || found->address != address)
return std::nullopt;
return static_cast<std::size_t>(found - config_.patches.begin());
}
std::expected<CellularPatchUptakeProposal, CellularHabitatError>
CellularHabitat::propose_uptake(CellularPatchAddress address, accounting::Inventory reserve,
const MetabolicBudget& budget) const noexcept {
const auto index = find(address);
if (!index)
return std::unexpected{CellularHabitatError::missing_patch};
const auto& patch = config_.patches[*index];
const auto uptake = propose_cellular_uptake(patch.stock, reserve, budget);
if (!uptake)
return std::unexpected{CellularHabitatError::uptake};
return CellularPatchUptakeProposal{address, *uptake};
}
std::vector<std::uint8_t> CellularHabitat::snapshot() const {
std::vector<std::uint8_t> bytes;
bytes.reserve(cellular_habitat_header_bytes +
cellular_patch_record_bytes * config_.patches.size() + 8);
Writer out{bytes};
out.put(habitat_magic);
out.put(cellular_habitat_format, 4);
out.put(habitat_rules, 4);
out.put(config_.world);
out.put(config_.region);
out.put(config_.frame);
out.put(config_.width_um, 4);
out.put(config_.height_um, 4);
out.put(cellular_patch_pitch_um, 4);
out.put(config_.patches.size(), 4);
out.put(0); // Reserved.
out.put(0); // Reserved.
for (const auto& patch : config_.patches) {
out.put(patch.address.x_um, 4);
out.put(patch.address.y_um, 4);
out.put(patch.source);
out.inventory(patch.stock);
}
out.put(checksum(bytes));
return bytes;
}
std::expected<std::unique_ptr<CellularHabitat>, CellularHabitatError>
CellularHabitat::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
std::uint64_t region, std::uint64_t frame) {
if (bytes.size() < cellular_habitat_header_bytes + 8 ||
bytes.size() > max_cellular_habitat_bytes)
return std::unexpected{CellularHabitatError::format};
Reader tail{bytes.last(8)};
if (checksum(bytes.first(bytes.size() - 8)) != tail.get())
return std::unexpected{CellularHabitatError::checksum};
Reader in{bytes};
if (in.get() != habitat_magic)
return std::unexpected{CellularHabitatError::format};
if (in.get(4) != cellular_habitat_format || in.get(4) != habitat_rules)
return std::unexpected{CellularHabitatError::version};
CellularHabitatConfig config;
config.world = in.get();
config.region = in.get();
config.frame = in.get();
if (config.world != world || config.region != region || config.frame != frame)
return std::unexpected{CellularHabitatError::identity};
config.width_um = static_cast<std::uint32_t>(in.get(4));
config.height_um = static_cast<std::uint32_t>(in.get(4));
if (in.get(4) != cellular_patch_pitch_um)
return std::unexpected{CellularHabitatError::version};
const auto count = in.get(4);
if (in.get() != 0 || in.get() != 0 || count > max_cellular_patches ||
bytes.size() != cellular_habitat_header_bytes + cellular_patch_record_bytes * count + 8)
return std::unexpected{CellularHabitatError::format};
config.patches.reserve(static_cast<std::size_t>(count));
for (std::uint64_t index = 0; index < count; ++index) {
CellularPatch patch{};
patch.address.x_um = static_cast<std::uint32_t>(in.get(4));
patch.address.y_um = static_cast<std::uint32_t>(in.get(4));
patch.source = in.get();
patch.stock = in.inventory();
config.patches.push_back(patch);
}
auto restored = create(std::move(config));
if (!restored)
return std::unexpected{restored.error()};
const auto canonical = (*restored)->snapshot();
if (canonical.size() != bytes.size() ||
!std::equal(canonical.begin(), canonical.end(), bytes.begin()))
return std::unexpected{CellularHabitatError::format};
return std::move(*restored);
}
} // namespace evo::biology