Implement M6.5c persisted environmental community and bounded writers
This commit is contained in:
@@ -5,9 +5,9 @@
|
||||
namespace evo::world {
|
||||
namespace {
|
||||
constexpr std::uint64_t magic = 0x31504c43564f4541ULL;
|
||||
void put(WorldBytes& b, std::uint64_t v, unsigned n) {
|
||||
void put(WorldBytes& b, std::size_t& at, std::uint64_t v, unsigned n) {
|
||||
for (unsigned i = 0; i < n; ++i) {
|
||||
b.push_back(static_cast<std::uint8_t>(v & 255U));
|
||||
b.at(at++) = static_cast<std::uint8_t>(v & 255U);
|
||||
v >>= 8U;
|
||||
}
|
||||
}
|
||||
@@ -59,43 +59,47 @@ std::expected<WorldBytes, TransferError> EnvironmentalTransfers::snapshot() cons
|
||||
length += 20 + 42 * ordered[i]->receipt.legs;
|
||||
if (length > max_snapshot_bytes)
|
||||
return std::unexpected{TransferError::budget};
|
||||
WorldBytes out;
|
||||
out.reserve(length);
|
||||
put(out, magic, 8);
|
||||
put(out, 1, 4);
|
||||
put(out, length, 4);
|
||||
put(out, fields_->identity().world, 8);
|
||||
put(out, fields_->identity().frame, 8);
|
||||
put(out, adoption_command_, 8);
|
||||
put(out, count_, 4);
|
||||
put(out, count, 4);
|
||||
put(out, terrain->size(), 4);
|
||||
put(out, fields.size(), 4);
|
||||
out.insert(out.end(), terrain->begin(), terrain->end());
|
||||
out.insert(out.end(), fields.begin(), fields.end());
|
||||
WorldBytes out(length);
|
||||
std::size_t at{};
|
||||
put(out, at, magic, 8);
|
||||
put(out, at, 1, 4);
|
||||
put(out, at, length, 4);
|
||||
put(out, at, fields_->identity().world, 8);
|
||||
put(out, at, fields_->identity().frame, 8);
|
||||
put(out, at, adoption_command_, 8);
|
||||
put(out, at, count_, 4);
|
||||
put(out, at, count, 4);
|
||||
put(out, at, terrain->size(), 4);
|
||||
put(out, at, fields.size(), 4);
|
||||
std::copy(terrain->begin(), terrain->end(), out.begin() + static_cast<std::ptrdiff_t>(at));
|
||||
at += terrain->size();
|
||||
std::copy(fields.begin(), fields.end(), out.begin() + static_cast<std::ptrdiff_t>(at));
|
||||
at += fields.size();
|
||||
for (const auto& a : organisms()) {
|
||||
put(out, a.id.value, 8);
|
||||
put(out, at, a.id.value, 8);
|
||||
for (auto v : a.balances)
|
||||
put(out, v, 8);
|
||||
put(out, at, v, 8);
|
||||
for (auto v : a.capacities)
|
||||
put(out, v, 8);
|
||||
put(out, at, v, 8);
|
||||
}
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
const auto& h = *ordered[i];
|
||||
put(out, h.receipt.command, 8);
|
||||
put(out, h.receipt.tick, 8);
|
||||
put(out, h.receipt.legs, 4);
|
||||
put(out, at, h.receipt.command, 8);
|
||||
put(out, at, h.receipt.tick, 8);
|
||||
put(out, at, h.receipt.legs, 4);
|
||||
for (const auto& l : std::span{h.legs}.first(h.receipt.legs)) {
|
||||
put(out, std::bit_cast<std::uint64_t>(l.voxel.x), 8);
|
||||
put(out, std::bit_cast<std::uint64_t>(l.voxel.y), 8);
|
||||
put(out, std::bit_cast<std::uint64_t>(l.voxel.z), 8);
|
||||
put(out, l.organism.value, 8);
|
||||
put(out, static_cast<std::uint8_t>(l.field), 1);
|
||||
put(out, static_cast<std::uint8_t>(l.direction), 1);
|
||||
put(out, l.amount, 8);
|
||||
put(out, at, std::bit_cast<std::uint64_t>(l.voxel.x), 8);
|
||||
put(out, at, std::bit_cast<std::uint64_t>(l.voxel.y), 8);
|
||||
put(out, at, std::bit_cast<std::uint64_t>(l.voxel.z), 8);
|
||||
put(out, at, l.organism.value, 8);
|
||||
put(out, at, static_cast<std::uint8_t>(l.field), 1);
|
||||
put(out, at, static_cast<std::uint8_t>(l.direction), 1);
|
||||
put(out, at, l.amount, 8);
|
||||
}
|
||||
}
|
||||
put(out, hash(out), 8);
|
||||
put(out, at, hash(std::span{out}.first(length - 8)), 8);
|
||||
if (at != length)
|
||||
return std::unexpected{TransferError::format};
|
||||
return out;
|
||||
}
|
||||
std::expected<std::unique_ptr<EnvironmentalTransfers>, TransferError>
|
||||
|
||||
Reference in New Issue
Block a user