Implement M5.5 atomic terrain excavation and debris accounting
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
#include "evo/world/chunks.hpp"
|
||||
#include <optional>
|
||||
namespace evo::world {
|
||||
struct ChunkStore::Entry {
|
||||
ChunkAddress address{};
|
||||
std::optional<CompactCells> cells;
|
||||
std::uint64_t revision{};
|
||||
bool dirty{};
|
||||
};
|
||||
struct ChunkStore::History {
|
||||
std::array<CellCut, 64> cuts{};
|
||||
std::size_t count{};
|
||||
std::uint32_t destination{};
|
||||
EditReceipt receipt{};
|
||||
};
|
||||
} // namespace evo::world
|
||||
+11
-13
@@ -1,4 +1,4 @@
|
||||
#include "evo/world/chunks.hpp"
|
||||
#include "chunk_state.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <limits>
|
||||
@@ -98,25 +98,23 @@ std::expected<CellSample, ChunkError> CompactCells::read(std::uint32_t index) co
|
||||
}
|
||||
return *CellSample::create({static_cast<std::uint16_t>(word & 65535U)}, word >> 16U);
|
||||
}
|
||||
struct ChunkStore::Entry {
|
||||
ChunkAddress address{};
|
||||
std::optional<CompactCells> cells;
|
||||
std::uint64_t revision{};
|
||||
bool dirty{};
|
||||
};
|
||||
ChunkStore::ChunkStore(BaseDescriptor descriptor, std::size_t slots, std::size_t budget,
|
||||
std::size_t bytes)
|
||||
std::size_t bytes, DebrisCapacity debris)
|
||||
: descriptor_{descriptor}, slots_{slots}, budget_{budget}, bytes_{bytes},
|
||||
entries_{std::make_unique<Entry[]>(slots)} {}
|
||||
entries_{std::make_unique<Entry[]>(slots)}, debris_capacity_{debris},
|
||||
history_{std::make_unique<History[]>(retained_commands)} {}
|
||||
ChunkStore::~ChunkStore() = default;
|
||||
std::expected<std::size_t, ChunkError> ChunkStore::minimum_bytes(std::size_t slots) noexcept {
|
||||
if (slots == 0 || slots > max_slots) {
|
||||
return std::unexpected{ChunkError::invalid_config};
|
||||
}
|
||||
return sizeof(ChunkStore) + slots * sizeof(Entry) + scratch_bytes;
|
||||
return sizeof(ChunkStore) + slots * sizeof(Entry) + scratch_bytes +
|
||||
retained_commands * sizeof(History);
|
||||
}
|
||||
std::expected<std::unique_ptr<ChunkStore>, ChunkError>
|
||||
ChunkStore::create(BaseDescriptor descriptor, std::size_t slots, std::size_t budget) {
|
||||
std::expected<std::unique_ptr<ChunkStore>, ChunkError> ChunkStore::create(BaseDescriptor descriptor,
|
||||
std::size_t slots,
|
||||
std::size_t budget,
|
||||
DebrisCapacity debris) {
|
||||
const auto minimum = minimum_bytes(slots);
|
||||
if (!minimum) {
|
||||
return std::unexpected{minimum.error()};
|
||||
@@ -127,7 +125,7 @@ ChunkStore::create(BaseDescriptor descriptor, std::size_t slots, std::size_t bud
|
||||
if (budget < *minimum) {
|
||||
return std::unexpected{ChunkError::memory_budget};
|
||||
}
|
||||
return std::unique_ptr<ChunkStore>{new ChunkStore(descriptor, slots, budget, *minimum)};
|
||||
return std::unique_ptr<ChunkStore>{new ChunkStore(descriptor, slots, budget, *minimum, debris)};
|
||||
}
|
||||
bool ChunkStore::current(ChunkView v) const noexcept {
|
||||
return v.owner_ == this && v.slot_ < slots_ && entries_[v.slot_].cells &&
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
#include "chunk_state.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::world {
|
||||
namespace {
|
||||
void hash_field(std::uint64_t& hash, std::uint64_t value, unsigned bytes) noexcept {
|
||||
for (unsigned i = 0; i < bytes; ++i) {
|
||||
hash = (hash ^ (value & 255U)) * 1099511628211ULL;
|
||||
value >>= 8U;
|
||||
}
|
||||
}
|
||||
std::uint32_t pack_cell(CellSample cell) noexcept {
|
||||
return cell.material().value | (static_cast<std::uint32_t>(cell.fill()) << 16U);
|
||||
}
|
||||
} // namespace
|
||||
std::expected<std::uint64_t, ChunkError> excavation_digest(const ExcavationRequest& r) noexcept {
|
||||
if (r.command == 0 || r.destination != 1 || r.cuts.empty() || r.cuts.size() > 64) {
|
||||
return std::unexpected{ChunkError::invalid_edit};
|
||||
}
|
||||
std::uint64_t hash = 14695981039346656037ULL;
|
||||
hash_field(hash, 1, 4); // request contract version
|
||||
hash_field(hash, r.command, 8);
|
||||
hash_field(hash, r.destination, 4);
|
||||
hash_field(hash, r.cuts.size(), 4);
|
||||
std::size_t chunks = 0;
|
||||
for (std::size_t i = 0; i < r.cuts.size(); ++i) {
|
||||
const auto& c = r.cuts[i];
|
||||
const auto index = ordinal(c.local, base_chunk_edge);
|
||||
if (!index || !join({c.chunk, c.local}, base_chunk_edge) || c.expected_revision == 0 ||
|
||||
c.removed_fill == 0 || c.removed_fill > fill_scale) {
|
||||
return std::unexpected{ChunkError::invalid_edit};
|
||||
}
|
||||
if (i == 0 || c.chunk != r.cuts[i - 1].chunk) {
|
||||
++chunks;
|
||||
}
|
||||
if (chunks > 4) {
|
||||
return std::unexpected{ChunkError::invalid_edit};
|
||||
}
|
||||
if (i > 0) {
|
||||
const auto& previous = r.cuts[i - 1];
|
||||
if (c.chunk < previous.chunk || (c.chunk == previous.chunk &&
|
||||
(*index <= *ordinal(previous.local, base_chunk_edge) ||
|
||||
c.expected_revision != previous.expected_revision))) {
|
||||
return std::unexpected{ChunkError::invalid_edit};
|
||||
}
|
||||
}
|
||||
hash_field(hash, static_cast<std::uint64_t>(c.chunk.x), 8);
|
||||
hash_field(hash, static_cast<std::uint64_t>(c.chunk.y), 8);
|
||||
hash_field(hash, static_cast<std::uint64_t>(c.chunk.z), 8);
|
||||
hash_field(hash, c.local.x, 4);
|
||||
hash_field(hash, c.local.y, 4);
|
||||
hash_field(hash, c.local.z, 4);
|
||||
hash_field(hash, c.expected_revision, 8);
|
||||
hash_field(hash, c.removed_fill, 4);
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
std::expected<VolumeUnits, ChunkError> ChunkStore::debris(MaterialId material) const noexcept {
|
||||
if (material.value < 1 || material.value > 2) {
|
||||
return std::unexpected{ChunkError::invalid_edit};
|
||||
}
|
||||
return debris_[material.value - 1];
|
||||
}
|
||||
std::expected<EditReceipt, ChunkError> ChunkStore::excavate(const ExcavationRequest& r) {
|
||||
const auto digest = excavation_digest(r);
|
||||
if (!digest) {
|
||||
return std::unexpected{digest.error()};
|
||||
}
|
||||
if (*digest != r.digest) {
|
||||
return std::unexpected{ChunkError::digest_mismatch};
|
||||
}
|
||||
if (r.command <= last_command_) {
|
||||
for (std::size_t i = 0; i < retained_commands; ++i) {
|
||||
const auto& old = history_[i];
|
||||
if (old.receipt.command == r.command) {
|
||||
if (old.receipt.digest != r.digest || old.destination != r.destination ||
|
||||
old.count != r.cuts.size() ||
|
||||
!std::equal(r.cuts.begin(), r.cuts.end(), old.cuts.begin())) {
|
||||
return std::unexpected{ChunkError::command_conflict};
|
||||
}
|
||||
return old.receipt;
|
||||
}
|
||||
}
|
||||
return std::unexpected{ChunkError::stale_command};
|
||||
}
|
||||
EditReceipt receipt{};
|
||||
receipt.command = r.command;
|
||||
receipt.digest = r.digest;
|
||||
std::array<std::size_t, 4> slots{};
|
||||
for (const auto& cut : r.cuts) {
|
||||
if (receipt.chunk_count == 0 ||
|
||||
receipt.chunks[receipt.chunk_count - 1].address != cut.chunk) {
|
||||
const auto token = view(cut.chunk);
|
||||
if (!token) {
|
||||
return std::unexpected{token.error()};
|
||||
}
|
||||
if (token->revision() != cut.expected_revision) {
|
||||
return std::unexpected{ChunkError::stale_view};
|
||||
}
|
||||
slots[receipt.chunk_count] = token->slot_;
|
||||
receipt.chunks[receipt.chunk_count++].address = cut.chunk;
|
||||
}
|
||||
}
|
||||
if (revision_ > std::numeric_limits<std::uint64_t>::max() - receipt.chunk_count) {
|
||||
return std::unexpected{ChunkError::revision_exhausted};
|
||||
}
|
||||
std::array<std::optional<CompactCells>, 4> replacements;
|
||||
std::size_t new_bytes = 0, old_bytes = 0, next_cut = 0;
|
||||
for (std::size_t chunk = 0; chunk < receipt.chunk_count; ++chunk) {
|
||||
const auto& entry = entries_[slots[chunk]];
|
||||
if (!entry.cells) {
|
||||
return std::unexpected{ChunkError::missing};
|
||||
}
|
||||
old_bytes += entry.cells->payload_bytes();
|
||||
std::array<std::uint32_t, base_chunk_cells> words{};
|
||||
for (std::uint32_t i = 0; i < base_chunk_cells; ++i) {
|
||||
words[i] = pack_cell(*entry.cells->read(i));
|
||||
}
|
||||
while (next_cut < r.cuts.size() && r.cuts[next_cut].chunk == entry.address) {
|
||||
const auto& cut = r.cuts[next_cut++];
|
||||
const auto index = *ordinal(cut.local, base_chunk_edge);
|
||||
const auto material = words[index] & 65535U;
|
||||
const auto fill = words[index] >> 16U;
|
||||
if (material < 1 || material > 2 || fill < cut.removed_fill) {
|
||||
return std::unexpected{ChunkError::insufficient_material};
|
||||
}
|
||||
const auto remaining = fill - cut.removed_fill;
|
||||
words[index] = remaining == 0 ? 0 : material | (remaining << 16U);
|
||||
receipt.removed[material - 1].value += cut.removed_fill;
|
||||
}
|
||||
for (std::size_t material = 0; material < 2; ++material) {
|
||||
if (receipt.removed[material].value >
|
||||
debris_capacity_.materials[material].value - debris_[material].value) {
|
||||
return std::unexpected{ChunkError::destination_full};
|
||||
}
|
||||
}
|
||||
new_bytes += CompactCells::required_bytes(words);
|
||||
if (new_bytes > budget_ - bytes_) {
|
||||
return std::unexpected{ChunkError::memory_budget};
|
||||
}
|
||||
replacements[chunk].emplace(CompactCells::encode(words));
|
||||
receipt.chunks[chunk].revision = revision_ + chunk + 1;
|
||||
}
|
||||
static_assert(std::is_nothrow_move_assignable_v<std::optional<CompactCells>>);
|
||||
static_assert(std::is_nothrow_copy_assignable_v<History>);
|
||||
// All rejection and allocation precede this single-owner, nonthrowing commit.
|
||||
for (std::size_t chunk = 0; chunk < receipt.chunk_count; ++chunk) {
|
||||
auto& entry = entries_[slots[chunk]];
|
||||
entry.cells = std::move(replacements[chunk]);
|
||||
entry.revision = receipt.chunks[chunk].revision;
|
||||
entry.dirty = true;
|
||||
}
|
||||
bytes_ = bytes_ - old_bytes + new_bytes;
|
||||
revision_ += receipt.chunk_count;
|
||||
for (std::size_t material = 0; material < 2; ++material) {
|
||||
debris_[material].value += receipt.removed[material].value;
|
||||
}
|
||||
auto& record = history_[history_next_];
|
||||
std::copy(r.cuts.begin(), r.cuts.end(), record.cuts.begin());
|
||||
record.count = r.cuts.size();
|
||||
record.destination = r.destination;
|
||||
record.receipt = receipt;
|
||||
history_next_ = (history_next_ + 1) % retained_commands;
|
||||
last_command_ = r.command;
|
||||
return receipt;
|
||||
}
|
||||
} // namespace evo::world
|
||||
Reference in New Issue
Block a user