#include "evo/world/fields.hpp" #include #include #include #include namespace evo::world { namespace { constexpr auto ceiling = std::numeric_limits::max(); constexpr std::array conserved_channel{4, 2, 3, 1, 2, 3, 4, 0, 0, 0}; std::size_t index(Field field) noexcept { return static_cast(field); } std::uint64_t value_at(std::span 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 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 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(); for (auto i = start; i < end; ++i) (*page)[i - start] = values[i]; out.pages[start / 64] = std::move(page); } return out; } std::expected, FieldError> FieldStore::create(WorldIdentity identity, std::uint64_t opening_id, FieldDefaults defaults, std::span sites, std::span 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 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(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(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 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 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 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 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(value & 255U)); value >>= 8U; } } struct Reader { std::span 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::numeric_limits::max()) ? static_cast(n) : -1 - static_cast(~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(site.voxel.x), 8); put(out, static_cast(site.voxel.y), 8); put(out, static_cast(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, FieldError> FieldStore::restore(std::span 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 sites(static_cast(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(r.get(2))}; } std::array, field_count> data; std::vector 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(count)); for (auto& v : data[k]) v = r.get(8); fields.push_back({static_cast(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