292 lines
12 KiB
C++
292 lines
12 KiB
C++
#include "evo/world/fields.hpp"
|
|
#include <algorithm>
|
|
#include <limits>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace evo::world {
|
|
namespace {
|
|
constexpr auto ceiling = std::numeric_limits<std::uint64_t>::max();
|
|
constexpr std::array<std::size_t, field_count> conserved_channel{4, 2, 3, 1, 2, 3, 4, 0, 0, 0};
|
|
std::size_t index(Field field) noexcept {
|
|
return static_cast<std::size_t>(field);
|
|
}
|
|
std::uint64_t value_at(std::span<const std::uint64_t> values, std::size_t i) noexcept {
|
|
return values[values.size() == 1 ? 0 : i];
|
|
}
|
|
} // namespace
|
|
std::uint64_t FieldStore::Channel::get(std::size_t site) const noexcept {
|
|
const auto& page = pages[site / 64];
|
|
return page ? (*page)[site % 64] : baseline;
|
|
}
|
|
std::size_t FieldStore::Channel::bytes() const noexcept {
|
|
std::size_t total{};
|
|
for (const auto& page : pages)
|
|
if (page)
|
|
total += sizeof(Page);
|
|
return total;
|
|
}
|
|
std::size_t FieldStore::page_bytes(std::span<const std::uint64_t> values) noexcept {
|
|
std::size_t total{};
|
|
for (std::size_t start = 0; start < values.size(); start += 64) {
|
|
const auto end = std::min(start + 64, values.size());
|
|
for (auto i = start; i < end; ++i) {
|
|
if (values[i] != values[0]) {
|
|
total += sizeof(Page);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return total;
|
|
}
|
|
FieldStore::Channel FieldStore::encode(std::span<const std::uint64_t> values) {
|
|
Channel out;
|
|
out.present = true;
|
|
out.baseline = values[0];
|
|
for (std::size_t start = 0; start < values.size(); start += 64) {
|
|
const auto end = std::min(start + 64, values.size());
|
|
bool changed{};
|
|
for (auto i = start; i < end; ++i)
|
|
changed |= values[i] != out.baseline;
|
|
if (!changed)
|
|
continue;
|
|
auto page = std::make_unique<Page>();
|
|
for (auto i = start; i < end; ++i)
|
|
(*page)[i - start] = values[i];
|
|
out.pages[start / 64] = std::move(page);
|
|
}
|
|
return out;
|
|
}
|
|
std::expected<std::unique_ptr<FieldStore>, FieldError>
|
|
FieldStore::create(WorldIdentity identity, std::uint64_t opening_id, FieldDefaults defaults,
|
|
std::span<const FieldSite> sites, std::span<const FieldOpening> fields,
|
|
std::size_t byte_budget) {
|
|
if (identity.world == 0 || identity.frame == 0 || opening_id == 0 || sites.empty() ||
|
|
sites.size() > max_sites || fields.size() > field_count)
|
|
return std::unexpected{FieldError::config};
|
|
for (std::size_t i = 0; i < sites.size(); ++i) {
|
|
const auto& s = sites[i];
|
|
if (!split(s.voxel, base_chunk_edge) || !measure_habitat(s.box) ||
|
|
s.substrate.value >= defaults.water_capacity.size() ||
|
|
(i != 0 && !(sites[i - 1].voxel < s.voxel)))
|
|
return std::unexpected{FieldError::mapping};
|
|
}
|
|
std::size_t bytes = sizeof(FieldStore) + sites.size() * sizeof(FieldSite);
|
|
std::array<std::uint64_t, 5> totals{};
|
|
std::size_t prior{};
|
|
bool first = true;
|
|
for (const auto& f : fields) {
|
|
const auto k = index(f.field);
|
|
if (k >= field_count || (!first && k <= prior) ||
|
|
(f.values.size() != 1 && f.values.size() != sites.size()))
|
|
return std::unexpected{FieldError::channel};
|
|
first = false;
|
|
prior = k;
|
|
bytes += page_bytes(f.values);
|
|
for (std::size_t i = 0; i < sites.size(); ++i) {
|
|
const auto value = value_at(f.values, i);
|
|
if (f.field == Field::water &&
|
|
value > defaults.water_capacity[sites[i].substrate.value].value)
|
|
return std::unexpected{FieldError::capacity};
|
|
auto& total = totals[conserved_channel[k]];
|
|
if (value > ceiling - total)
|
|
return std::unexpected{FieldError::overflow};
|
|
total += value;
|
|
}
|
|
}
|
|
if (bytes > byte_budget || scratch_bytes > byte_budget - bytes)
|
|
return std::unexpected{FieldError::budget};
|
|
auto out = std::unique_ptr<FieldStore>(new FieldStore);
|
|
out->identity_ = identity;
|
|
out->opening_ = opening_id;
|
|
out->defaults_ = defaults;
|
|
out->count_ = sites.size();
|
|
out->budget_ = byte_budget;
|
|
out->bytes_ = bytes;
|
|
out->sites_ = std::make_unique<FieldSite[]>(sites.size());
|
|
std::copy(sites.begin(), sites.end(), out->sites_.get());
|
|
for (const auto& f : fields)
|
|
out->fields_[index(f.field)] = encode(f.values);
|
|
return out;
|
|
}
|
|
std::expected<std::uint64_t, FieldError> FieldStore::read(std::size_t site,
|
|
Field field) const noexcept {
|
|
if (site >= count_)
|
|
return std::unexpected{FieldError::site};
|
|
if (index(field) >= field_count)
|
|
return std::unexpected{FieldError::channel};
|
|
if (!fields_[index(field)].present)
|
|
return std::unexpected{FieldError::missing};
|
|
return fields_[index(field)].get(site);
|
|
}
|
|
std::expected<void, FieldError> FieldStore::transfer(std::uint64_t command, Field field,
|
|
std::size_t from, std::size_t to,
|
|
std::uint64_t amount) {
|
|
if (command == 0 || command <= command_)
|
|
return std::unexpected{FieldError::stale_command};
|
|
if (amount == 0 || from == to)
|
|
return std::unexpected{FieldError::invalid_transfer};
|
|
const auto source = read(from, field), destination = read(to, field);
|
|
if (!source)
|
|
return std::unexpected{source.error()};
|
|
if (!destination)
|
|
return std::unexpected{destination.error()};
|
|
if (amount > *source)
|
|
return std::unexpected{FieldError::insufficient};
|
|
if (amount > ceiling - *destination)
|
|
return std::unexpected{FieldError::overflow};
|
|
if (field == Field::water &&
|
|
*destination + amount > defaults_.water_capacity[sites_[to].substrate.value].value)
|
|
return std::unexpected{FieldError::capacity};
|
|
std::array<std::uint64_t, max_sites> values{};
|
|
auto& channel = fields_[index(field)];
|
|
for (std::size_t i = 0; i < count_; ++i)
|
|
values[i] = channel.get(i);
|
|
values[from] -= amount;
|
|
values[to] += amount;
|
|
const auto logical = std::span{values}.first(count_);
|
|
const auto needed = page_bytes(logical);
|
|
if (needed > budget_ - bytes_ - scratch_bytes)
|
|
return std::unexpected{FieldError::budget};
|
|
auto replacement = encode(logical);
|
|
bytes_ = bytes_ - channel.bytes() + needed;
|
|
channel = std::move(replacement);
|
|
command_ = command;
|
|
return {};
|
|
}
|
|
|
|
namespace {
|
|
constexpr std::uint64_t magic = 0x31444c464f564541ULL; // AEVOFLD1
|
|
std::uint64_t checksum(std::span<const std::uint8_t> bytes) noexcept {
|
|
std::uint64_t h = 14695981039346656037ULL;
|
|
for (auto b : bytes)
|
|
h = (h ^ b) * 1099511628211ULL;
|
|
return h;
|
|
}
|
|
void put(WorldBytes& out, std::uint64_t value, unsigned width) {
|
|
for (unsigned i = 0; i < width; ++i) {
|
|
out.push_back(static_cast<std::uint8_t>(value & 255U));
|
|
value >>= 8U;
|
|
}
|
|
}
|
|
struct Reader {
|
|
std::span<const std::uint8_t> bytes;
|
|
std::size_t at{};
|
|
bool ok{true};
|
|
std::uint64_t get(unsigned width) noexcept {
|
|
if (!ok || width > bytes.size() - at) {
|
|
ok = false;
|
|
return 0;
|
|
}
|
|
std::uint64_t value{};
|
|
for (unsigned i = 0; i < width; ++i)
|
|
value |= std::uint64_t{bytes[at++]} << (8U * i);
|
|
return value;
|
|
}
|
|
std::int64_t signed64() noexcept {
|
|
const auto n = get(8);
|
|
return n <= static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max())
|
|
? static_cast<std::int64_t>(n)
|
|
: -1 - static_cast<std::int64_t>(~n);
|
|
}
|
|
};
|
|
} // namespace
|
|
WorldBytes FieldStore::snapshot() const {
|
|
std::uint32_t mask{};
|
|
std::size_t present{};
|
|
for (std::size_t k = 0; k < field_count; ++k)
|
|
if (fields_[k].present) {
|
|
mask |= 1U << k;
|
|
++present;
|
|
}
|
|
const auto length = 96 + count_ * (74 + 8 * present);
|
|
WorldBytes out;
|
|
out.reserve(length);
|
|
put(out, magic, 8);
|
|
put(out, 1, 4);
|
|
put(out, length, 4);
|
|
put(out, identity_.world, 8);
|
|
put(out, identity_.frame, 8);
|
|
put(out, environmental_units_version, 4);
|
|
put(out, 1, 4);
|
|
put(out, opening_, 8);
|
|
put(out, command_, 8);
|
|
put(out, count_, 4);
|
|
put(out, mask, 4);
|
|
for (const auto cap : defaults_.water_capacity)
|
|
put(out, cap.value, 8);
|
|
for (const auto& site : sites()) {
|
|
put(out, static_cast<std::uint64_t>(site.voxel.x), 8);
|
|
put(out, static_cast<std::uint64_t>(site.voxel.y), 8);
|
|
put(out, static_cast<std::uint64_t>(site.voxel.z), 8);
|
|
for (const auto value :
|
|
{site.box.x, site.box.y, site.box.z, site.box.width, site.box.height, site.box.depth})
|
|
put(out, value.value, 8);
|
|
put(out, site.substrate.value, 2);
|
|
}
|
|
for (const auto& field : fields_)
|
|
if (field.present)
|
|
for (std::size_t i = 0; i < count_; ++i)
|
|
put(out, field.get(i), 8);
|
|
put(out, checksum(out), 8);
|
|
return out;
|
|
}
|
|
std::expected<std::unique_ptr<FieldStore>, FieldError>
|
|
FieldStore::restore(std::span<const std::uint8_t> bytes, WorldIdentity identity,
|
|
std::size_t byte_budget) {
|
|
if (bytes.size() < 96 || bytes.size() > max_snapshot_bytes)
|
|
return std::unexpected{FieldError::format};
|
|
Reader r{bytes.first(bytes.size() - 8)};
|
|
if (r.get(8) != magic)
|
|
return std::unexpected{FieldError::format};
|
|
if (r.get(4) != 1)
|
|
return std::unexpected{FieldError::version};
|
|
if (r.get(4) != bytes.size())
|
|
return std::unexpected{FieldError::format};
|
|
Reader tail{bytes.last(8)};
|
|
if (checksum(r.bytes) != tail.get(8))
|
|
return std::unexpected{FieldError::checksum};
|
|
const WorldIdentity stored{r.get(8), r.get(8)};
|
|
if (identity.world == 0 || identity.frame == 0 || identity != stored)
|
|
return std::unexpected{FieldError::identity};
|
|
if (r.get(4) != environmental_units_version || r.get(4) != 1)
|
|
return std::unexpected{FieldError::version};
|
|
const auto opening = r.get(8), command = r.get(8), count = r.get(4), mask = r.get(4);
|
|
if (count == 0 || count > max_sites || mask >= (1U << field_count))
|
|
return std::unexpected{FieldError::format};
|
|
std::size_t present{};
|
|
for (std::size_t k = 0; k < field_count; ++k)
|
|
if ((mask & (std::uint64_t{1} << k)) != 0)
|
|
++present;
|
|
if (bytes.size() != 96 + count * (74 + 8 * present))
|
|
return std::unexpected{FieldError::format};
|
|
FieldDefaults defaults;
|
|
for (auto& cap : defaults.water_capacity)
|
|
cap.value = r.get(8);
|
|
std::vector<FieldSite> sites(static_cast<std::size_t>(count));
|
|
for (auto& site : sites) {
|
|
site.voxel = {r.signed64(), r.signed64(), r.signed64()};
|
|
site.box = {{r.get(8)}, {r.get(8)}, {r.get(8)}, {r.get(8)}, {r.get(8)}, {r.get(8)}};
|
|
site.substrate = {static_cast<std::uint16_t>(r.get(2))};
|
|
}
|
|
std::array<std::vector<std::uint64_t>, field_count> data;
|
|
std::vector<FieldOpening> fields;
|
|
fields.reserve(present);
|
|
for (std::size_t k = 0; k < field_count; ++k) {
|
|
if ((mask & (std::uint64_t{1} << k)) == 0)
|
|
continue;
|
|
data[k].resize(static_cast<std::size_t>(count));
|
|
for (auto& v : data[k])
|
|
v = r.get(8);
|
|
fields.push_back({static_cast<Field>(k), data[k]});
|
|
}
|
|
if (!r.ok || r.at != r.bytes.size())
|
|
return std::unexpected{FieldError::format};
|
|
auto out = create(identity, opening, defaults, sites, fields, byte_budget);
|
|
if (!out)
|
|
return out;
|
|
(*out)->command_ = command;
|
|
return out;
|
|
}
|
|
} // namespace evo::world
|