Implement M5.5 atomic terrain excavation and debris accounting
This commit is contained in:
@@ -0,0 +1,241 @@
|
||||
#include "evo/world/chunks.hpp"
|
||||
#include <array>
|
||||
#include <cstdio>
|
||||
#include <limits>
|
||||
#include <source_location>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
using namespace evo::world;
|
||||
void check(bool condition, std::source_location at = std::source_location::current()) {
|
||||
if (!condition) {
|
||||
throw std::runtime_error("world edit line " + std::to_string(at.line()));
|
||||
}
|
||||
}
|
||||
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
||||
template <typename T> bool refused(const std::expected<T, ChunkError>& r, ChunkError e) {
|
||||
return !r && r.error() == e;
|
||||
}
|
||||
std::unique_ptr<ChunkStore> store(std::uint64_t capacity = 1000000, std::size_t extra = 200000) {
|
||||
const auto minimum = ChunkStore::minimum_bytes(4);
|
||||
CHECK(minimum);
|
||||
const auto cap = DebrisCapacity{{VolumeUnits{capacity}, VolumeUnits{capacity}}};
|
||||
auto result = ChunkStore::create({}, 4, *minimum + extra, cap);
|
||||
CHECK(result);
|
||||
return std::move(*result);
|
||||
}
|
||||
ExcavationRequest request(std::uint64_t id, std::span<const CellCut> cuts) {
|
||||
ExcavationRequest r{id, 1, cuts, 0};
|
||||
const auto hash = excavation_digest(r);
|
||||
CHECK(hash);
|
||||
r.digest = *hash;
|
||||
return r;
|
||||
}
|
||||
std::array<CellCut, 2> seam(ChunkStore& s, std::uint32_t amount = 65535) {
|
||||
const auto a = s.load({-1, 0, -2});
|
||||
const auto b = s.load({0, 0, -2});
|
||||
CHECK(a && b);
|
||||
return {{{{-1, 0, -2}, {15, 0, 0}, a->revision(), amount},
|
||||
{{0, 0, -2}, {0, 0, 0}, b->revision(), amount}}};
|
||||
}
|
||||
std::uint64_t volume(ChunkStore& s, ChunkAddress address, std::uint16_t material) {
|
||||
const auto view = s.view(address);
|
||||
CHECK(view);
|
||||
std::uint64_t total = 0;
|
||||
for (std::uint32_t i = 0; i < 4096; ++i) {
|
||||
const auto c = s.read(*view, *local_from_ordinal(i, 16));
|
||||
CHECK(c);
|
||||
if (c->material().value == material) {
|
||||
total += c->fill();
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
void conservation_and_retry() {
|
||||
auto s = store();
|
||||
const auto cuts = seam(*s);
|
||||
const auto before = volume(*s, cuts[0].chunk, 1) + volume(*s, cuts[1].chunk, 1);
|
||||
const auto old = s->view(cuts[0].chunk);
|
||||
CHECK(old);
|
||||
const auto r = request(1, cuts);
|
||||
const auto result = s->excavate(r);
|
||||
CHECK(result);
|
||||
CHECK(result->chunk_count == 2 && result->removed[0].value == 131070 &&
|
||||
result->removed[1].value == 0);
|
||||
CHECK(volume(*s, cuts[0].chunk, 1) + volume(*s, cuts[1].chunk, 1) + s->debris({1})->value ==
|
||||
before);
|
||||
CHECK(refused(s->read(*old, {}), ChunkError::stale_view));
|
||||
for (const auto& c : cuts) {
|
||||
const auto current = s->view(c.chunk);
|
||||
CHECK(current);
|
||||
const auto value = s->read(*current, c.local);
|
||||
CHECK(value && value->material().value == 0 && value->fill() == 0);
|
||||
CHECK(refused(s->evict(*current), ChunkError::dirty));
|
||||
}
|
||||
const auto used = s->accounted_bytes();
|
||||
const auto repeated = s->excavate(r);
|
||||
CHECK(repeated && *repeated == *result);
|
||||
CHECK(s->debris({1})->value == 131070 && s->accounted_bytes() == used);
|
||||
auto changed = cuts;
|
||||
changed[0].removed_fill = 1;
|
||||
CHECK(refused(s->excavate(request(1, changed)), ChunkError::command_conflict));
|
||||
auto invalid = r;
|
||||
invalid.digest ^= 1;
|
||||
CHECK(refused(s->excavate(invalid), ChunkError::digest_mismatch));
|
||||
CHECK(refused(s->excavate(request(2, cuts)), ChunkError::stale_view));
|
||||
auto fresh = cuts;
|
||||
for (auto& c : fresh) {
|
||||
c.expected_revision = s->view(c.chunk)->revision();
|
||||
}
|
||||
CHECK(refused(s->excavate(request(2, fresh)), ChunkError::insufficient_material));
|
||||
// Failed ID 2 is reusable; change to a populated cell and partial excavation.
|
||||
fresh[0].local = {14, 0, 0};
|
||||
fresh[0].removed_fill = 100;
|
||||
fresh[1].local = {1, 0, 0};
|
||||
fresh[1].removed_fill = 200;
|
||||
const auto partial = s->excavate(request(2, fresh));
|
||||
CHECK(partial);
|
||||
CHECK(s->read(*s->view(fresh[0].chunk), fresh[0].local)->fill() == 65435);
|
||||
CHECK(s->read(*s->view(fresh[1].chunk), fresh[1].local)->fill() == 65335);
|
||||
CHECK(volume(*s, cuts[0].chunk, 1) + volume(*s, cuts[1].chunk, 1) + s->debris({1})->value ==
|
||||
before);
|
||||
CHECK(*s->excavate(r) == *result); // receipt is historical even after newer edits
|
||||
}
|
||||
void atomic_rejection() {
|
||||
for (const auto memory_limited : {false, true}) {
|
||||
auto s = store(memory_limited ? 1000000 : 100000, memory_limited ? 4104 : 200000);
|
||||
const auto cuts = seam(*s);
|
||||
const auto before = s->accounted_bytes();
|
||||
const auto old = s->view(cuts[0].chunk);
|
||||
CHECK(old);
|
||||
CHECK(refused(s->excavate(request(1, cuts)),
|
||||
memory_limited ? ChunkError::memory_budget : ChunkError::destination_full));
|
||||
CHECK(s->accounted_bytes() == before && s->debris({1})->value == 0);
|
||||
for (const auto& c : cuts) {
|
||||
const auto view = s->view(c.chunk);
|
||||
CHECK(view && view->revision() == c.expected_revision);
|
||||
CHECK(s->read(*view, c.local)->fill() == 65535);
|
||||
}
|
||||
CHECK(s->read(*old, {}));
|
||||
CHECK(s->evict(*old)); // rejection did not set dirty
|
||||
}
|
||||
auto s = store();
|
||||
auto cuts = seam(*s);
|
||||
cuts[1].chunk = {1, 0, -2};
|
||||
CHECK(refused(s->excavate(request(1, cuts)), ChunkError::missing));
|
||||
CHECK(s->debris({1})->value == 0);
|
||||
}
|
||||
void late_source_failure() {
|
||||
auto s = store();
|
||||
auto cuts = seam(*s);
|
||||
std::array<CellCut, 1> precondition{cuts[1]};
|
||||
precondition[0].removed_fill = 65534;
|
||||
CHECK(s->excavate(request(1, precondition)));
|
||||
cuts = seam(*s, 2);
|
||||
const auto bytes = s->accounted_bytes();
|
||||
CHECK(refused(s->excavate(request(2, cuts)), ChunkError::insufficient_material));
|
||||
CHECK(s->accounted_bytes() == bytes && s->debris({1})->value == 65534);
|
||||
for (const auto& c : cuts) {
|
||||
CHECK(s->view(c.chunk)->revision() == c.expected_revision);
|
||||
}
|
||||
CHECK(s->read(*s->view(cuts[0].chunk), cuts[0].local)->fill() == 65535);
|
||||
CHECK(s->read(*s->view(cuts[1].chunk), cuts[1].local)->fill() == 1);
|
||||
CHECK(s->evict(*s->view(cuts[0].chunk)));
|
||||
}
|
||||
void shape_and_retention() {
|
||||
auto s = store();
|
||||
auto cuts = seam(*s, 1);
|
||||
auto r = request(1, cuts);
|
||||
r.destination = 2;
|
||||
CHECK(refused(s->excavate(r), ChunkError::invalid_edit));
|
||||
r = request(1, cuts);
|
||||
r.command = 0;
|
||||
CHECK(refused(s->excavate(r), ChunkError::invalid_edit));
|
||||
CHECK(refused(excavation_digest({1, 1, {}, 0}), ChunkError::invalid_edit));
|
||||
auto bad = cuts;
|
||||
bad[1] = bad[0];
|
||||
CHECK(refused(excavation_digest({1, 1, bad, 0}), ChunkError::invalid_edit));
|
||||
bad = {cuts[1], cuts[0]};
|
||||
CHECK(refused(excavation_digest({1, 1, bad, 0}), ChunkError::invalid_edit));
|
||||
for (const auto amount : {0U, 65536U, std::numeric_limits<std::uint32_t>::max()}) {
|
||||
bad = cuts;
|
||||
bad[0].removed_fill = amount;
|
||||
CHECK(refused(excavation_digest({1, 1, bad, 0}), ChunkError::invalid_edit));
|
||||
}
|
||||
bad = cuts;
|
||||
bad[0].local.x = 16;
|
||||
CHECK(refused(excavation_digest({1, 1, bad, 0}), ChunkError::invalid_edit));
|
||||
bad = cuts;
|
||||
bad[0].chunk.x = std::numeric_limits<std::int64_t>::min();
|
||||
CHECK(refused(excavation_digest({1, 1, bad, 0}), ChunkError::invalid_edit));
|
||||
std::vector<CellCut> oversized(65, cuts[0]);
|
||||
CHECK(refused(excavation_digest({1, 1, oversized, 0}), ChunkError::invalid_edit));
|
||||
CHECK(refused(s->debris({0}), ChunkError::invalid_edit));
|
||||
const auto original = request(1, cuts);
|
||||
CHECK(s->excavate(original));
|
||||
for (std::uint64_t id = 2; id <= 65; ++id) {
|
||||
auto current = cuts;
|
||||
for (auto& c : current) {
|
||||
c.expected_revision = s->view(c.chunk)->revision();
|
||||
}
|
||||
const auto command = request(id, current);
|
||||
const auto done = s->excavate(command);
|
||||
CHECK(done);
|
||||
CHECK(*s->excavate(command) == *done);
|
||||
}
|
||||
CHECK(refused(s->excavate(original), ChunkError::stale_command));
|
||||
CHECK(s->debris({1})->value == 130);
|
||||
}
|
||||
void four_chunks_and_materials() {
|
||||
auto s = store();
|
||||
std::array<CellCut, 64> cuts{};
|
||||
for (std::size_t chunk = 0; chunk < 4; ++chunk) {
|
||||
const ChunkAddress address{static_cast<std::int64_t>(chunk), 0, -2};
|
||||
const auto v = s->load(address);
|
||||
CHECK(v);
|
||||
for (std::size_t i = 0; i < 16; ++i) {
|
||||
cuts[chunk * 16 + i] = {
|
||||
address, {static_cast<std::uint32_t>(i), 0, 0}, v->revision(), 1};
|
||||
}
|
||||
}
|
||||
const auto done = s->excavate(request(1, cuts));
|
||||
CHECK(done && done->chunk_count == 4 && done->removed[0].value == 64);
|
||||
std::array<CellCut, 5> excessive{};
|
||||
for (std::size_t i = 0; i < 5; ++i) {
|
||||
excessive[i] = {{static_cast<std::int64_t>(i), 0, -2}, {}, 1, 1};
|
||||
}
|
||||
CHECK(refused(excavation_digest({2, 1, excessive, 0}), ChunkError::invalid_edit));
|
||||
auto regolith = store();
|
||||
const auto v = regolith->load({0, 0, -1});
|
||||
CHECK(v);
|
||||
for (std::uint32_t i = 0; i < 4096; ++i) {
|
||||
const auto local = *local_from_ordinal(i, 16);
|
||||
const auto c = regolith->read(*v, local);
|
||||
CHECK(c);
|
||||
if (c->material().value == 2) {
|
||||
const std::array<CellCut, 1> cut{{{{0, 0, -1}, local, v->revision(), 123}}};
|
||||
const auto before = volume(*regolith, {0, 0, -1}, 2);
|
||||
CHECK(regolith->excavate(request(1, cut)));
|
||||
CHECK(regolith->debris({1})->value == 0 && regolith->debris({2})->value == 123);
|
||||
CHECK(volume(*regolith, {0, 0, -1}, 2) + 123 == before);
|
||||
return;
|
||||
}
|
||||
}
|
||||
CHECK(false);
|
||||
}
|
||||
} // namespace
|
||||
int main() try {
|
||||
conservation_and_retry();
|
||||
atomic_rejection();
|
||||
late_source_failure();
|
||||
shape_and_retention();
|
||||
four_chunks_and_materials();
|
||||
return 0;
|
||||
} catch (const std::exception& e) {
|
||||
std::fprintf(stderr, "%s\n", e.what());
|
||||
return 1;
|
||||
} catch (...) {
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user