mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-21 06:14:58 +01:00
* feat(editor): Fields in the Terrain work mode, the ground under them made wavy, natural slopes A Fields tool beside Fill: LMB outlines a field, which gets the material chosen in the palette and is kept in a list (terrain/<name>/patches.txt, or in the manifest while there are few), where its material, repeat, angle, transition and grass are changed later. - painted into the terrain (the paint of the chunks, its edge blended over a transition and shifted by a noise, the 3D grass under it left, taken away or of another kind), or - a surface of its own: the ground cut away within the outline and the field drawn over the hole as a mesh following the heights of the terrain, made around the camera as the water is. It doesn't take one of the 8 materials of a chunk; its edge is sharp. The ground under a field can be made wavy before: rolling hills, furrows, bumps, long waves, a hollow or a mound, hummocks in a grid, strips of fields; with a profile (rounded, sharp ridges, sharp valleys, terraces, flat tops), roughness, variation and a fade from the edge. The chunks under it are made finer where the waves need it. The formation modifiers get an optional natural shape (natural: wander, scale, gentler, soften, roughness in the manifest): the formation and the edges of the track bed stay, the slopes and their foot wander, get gentler and meet the ground softly. Without it the modifiers shape the ground as before. Formats: new optional entries patches, patches_file and natural of the manifest, and the patches file; a terrain without them reads and saves as before, and the release doesn't read them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ * Fields: one noise for the edges of the fields and the natural slopes, one filling of the geometry of the water Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ * Fields: the vertices handed to the geometry of the water by value Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ * fix(editor): whole-number loop counters instead of float ones (Sonar cpp:S2193) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ * fix(editor): Fields: the edits of a field stick, the natural slopes stay when the formations are laid again, no holes far off or by a missing chunk The copy of the field the widgets edit is kept while one of them is held, so a drag ends with what it was dragged to. Laying the formations again keeps the natural shape of the slopes of each. The surfaces of the fields are made as far as the chunks are drawn, so their holes never show without them; a field over ground with no chunk is made of what has ground, and waits only for chunks which are in the files but not loaded yet. Outline it again puts the points on the ground and the cut changes that field. The holes of the fields are marked (254) and giving one back closes only its own, the holes cut by hand stay. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ * fix(editor): Fields: a separate field is built after loading with the camera still; a cleared outline or another field chosen forgets the one to outline again; giving back a field keeps the holes of the others over it - waits_for_ground counts a chunk as still coming while its pack isn't read yet (right after opening the scenery none is), and as not coming only when the pack is missing or read without that chunk. Before, the first build after opening left a saved separate field a hole until the camera moved. - Clear the outline (Fields and the area fill) and choosing another field in the list forget the field put back by Outline it again, so the next cut is a new field and not the old one moved. - All holes of separate fields share one mark, so closing one closed it under an overlapping field too; the separate fields left over the closed outline are cut again after it (when a field is given back or outlined again). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ * chore(editor): Fields: one identifier a statement, contains() for the sets (Sonar cpp:S1659, cpp:S6171) Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ * ci(windows): build lua51.dll for the unit tests (cherry picked from commitb3e6e02779) * fix(terrain): the fields made again only when one comes or goes; patches_file a plain name; the hand holes leave the fields' After the review of jerrrrycho atedb5160a: - update() no longer rebuilds all the water and the separate fields every 200 m the camera moves. Past patch_move it compares the separate (not painted) fields within the reach with the ones made, and rebuilds only when one came within the reach or left it; otherwise only the centre moves. A terrain with only painted fields never rebuilds for a move, also in driving mode. The fields waiting for their ground are made again as before. - patches_file of a manifest is taken only as a plain file name in the folder of the terrain. A path with folders, "..", or a root is logged ("Bad terrain: ...") and patches.txt is used instead, so a downloaded manifest can't have the patches read from, or saved over, a file elsewhere. - The holes cut or closed by hand (the Holes tool, "Close the hole" of the Fill section) leave the hole samples of the fields (kFieldHole) as they are, so a field's mesh no longer stands over closed ground and z-fights with it, and "Give back" still finds the field's hole. The unmarked fallback of cut_field (the fields cut before the mark) thereby closes only unmarked holes. - The field picked with "Outline it again" is forgotten when the Fill tool uses the outline up, and when another terrain tool than the fields is chosen, so the next field drawn is a new one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QRgQ1K1Qu7TCsa74zsNURd --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Hirek193 <polomarket73@gmail.com>
1861 lines
68 KiB
C++
1861 lines
68 KiB
C++
/*
|
|
This Source Code Form is subject to the
|
|
terms of the Mozilla Public License, v.
|
|
2.0. If a copy of the MPL was not
|
|
distributed with this file, You can
|
|
obtain one at
|
|
http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
#include "scene/heightmapterrain.h"
|
|
|
|
#include "scene/sceneterrain.h"
|
|
#include "utilities/Logs.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstring>
|
|
#include <filesystem>
|
|
#include <format>
|
|
#include <fstream>
|
|
#include <limits>
|
|
#include <sstream>
|
|
#include <string_view>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
|
|
#include <yaml-cpp/yaml.h>
|
|
#include "zstd.h"
|
|
|
|
namespace heightmap
|
|
{
|
|
|
|
namespace
|
|
{
|
|
|
|
std::uint32_t constexpr fourcc(char const A, char const B, char const C, char const D)
|
|
{
|
|
return static_cast<std::uint32_t>(static_cast<std::uint8_t>(A)) | (static_cast<std::uint32_t>(static_cast<std::uint8_t>(B)) << 8) |
|
|
(static_cast<std::uint32_t>(static_cast<std::uint8_t>(C)) << 16) | (static_cast<std::uint32_t>(static_cast<std::uint8_t>(D)) << 24);
|
|
}
|
|
|
|
std::uint32_t constexpr tag_chunk{fourcc('C', 'H', 'N', 'K')};
|
|
std::uint32_t constexpr tag_heights{fourcc('H', 'G', 'H', 'T')};
|
|
std::uint32_t constexpr tag_base{fourcc('H', 'B', 'A', 'S')};
|
|
std::uint32_t constexpr tag_paint{fourcc('S', 'P', 'L', 'T')};
|
|
std::uint32_t constexpr tag_adjust{fourcc('H', 'A', 'D', 'J')};
|
|
std::uint32_t constexpr tag_holes{fourcc('H', 'O', 'L', 'E')};
|
|
std::uint32_t constexpr tag_grass{fourcc('G', 'R', 'A', 'S')};
|
|
|
|
std::uint32_t constexpr pack_magic{fourcc('T', 'C', 'H', '1')};
|
|
std::uint32_t constexpr pack_version{1};
|
|
std::size_t constexpr header_size{64};
|
|
std::size_t constexpr index_entry_size{16};
|
|
std::size_t constexpr slice_entry_size{32};
|
|
std::uint32_t constexpr codec_stored{0};
|
|
std::uint32_t constexpr codec_zstd{1};
|
|
int constexpr zstd_level{3}; // light and fast, the data is simple
|
|
|
|
// the slices are small, a slice larger than this is taken for a damaged file
|
|
std::uint32_t constexpr max_slice_size{256u * 1024u * 1024u};
|
|
|
|
template <typename Type_> void put(std::vector<std::uint8_t> &Out, Type_ const Value)
|
|
{
|
|
auto const offset{Out.size()};
|
|
Out.resize(offset + sizeof(Type_));
|
|
std::memcpy(Out.data() + offset, &Value, sizeof(Type_));
|
|
}
|
|
|
|
template <typename Type_> void put_at(std::vector<std::uint8_t> &Out, std::size_t const Offset, Type_ const Value)
|
|
{
|
|
std::memcpy(Out.data() + Offset, &Value, sizeof(Type_));
|
|
}
|
|
|
|
// reads values from a block of data, without going past its end
|
|
class reader
|
|
{
|
|
public:
|
|
reader(std::uint8_t const *Data, std::size_t const Size) : m_data(Data), m_size(Size) {}
|
|
template <typename Type_> bool get(Type_ &Value)
|
|
{
|
|
if (m_offset + sizeof(Type_) > m_size)
|
|
return false;
|
|
std::memcpy(&Value, m_data + m_offset, sizeof(Type_));
|
|
m_offset += sizeof(Type_);
|
|
return true;
|
|
}
|
|
bool get(void *Out, std::size_t const Size)
|
|
{
|
|
if (m_offset + Size > m_size)
|
|
return false;
|
|
std::memcpy(Out, m_data + m_offset, Size);
|
|
m_offset += Size;
|
|
return true;
|
|
}
|
|
std::size_t left() const { return m_size - m_offset; }
|
|
|
|
private:
|
|
std::uint8_t const *m_data;
|
|
std::size_t m_size;
|
|
std::size_t m_offset{0};
|
|
};
|
|
|
|
void encode_heights(std::vector<float> const &Heights, std::vector<std::uint8_t> &Out)
|
|
{
|
|
if (Heights.empty())
|
|
return;
|
|
auto const [low, high]{std::minmax_element(Heights.begin(), Heights.end())};
|
|
auto const base{static_cast<std::int64_t>(std::floor(*low / height_step))};
|
|
auto const top{static_cast<std::int64_t>(std::ceil(*high / height_step))};
|
|
if (top - base > 0xffff || base < std::numeric_limits<std::int32_t>::min() || top > std::numeric_limits<std::int32_t>::max())
|
|
{
|
|
// too high a range for the steps: kept as they are
|
|
put<std::uint8_t>(Out, 1);
|
|
for (auto const height : Heights)
|
|
put<float>(Out, height);
|
|
return;
|
|
}
|
|
put<std::uint8_t>(Out, 0);
|
|
put<std::int32_t>(Out, static_cast<std::int32_t>(base));
|
|
// differences of the neighbouring points are small and repeat a lot, which compresses well
|
|
std::uint16_t previous{0};
|
|
for (auto const height : Heights)
|
|
{
|
|
auto const steps{static_cast<std::uint16_t>(std::clamp<std::int64_t>(std::llround(height / height_step) - base, 0, 0xffff))};
|
|
put<std::uint16_t>(Out, static_cast<std::uint16_t>(steps - previous));
|
|
previous = steps;
|
|
}
|
|
}
|
|
|
|
bool decode_heights(std::vector<std::uint8_t> const &Data, std::size_t const Count, std::vector<float> &Out)
|
|
{
|
|
reader input{Data.data(), Data.size()};
|
|
std::uint8_t encoding{0};
|
|
if (false == input.get(encoding))
|
|
return false;
|
|
Out.resize(Count);
|
|
if (encoding == 1)
|
|
{
|
|
if (input.left() != Count * sizeof(float))
|
|
return false;
|
|
return input.get(Out.data(), Count * sizeof(float));
|
|
}
|
|
if (encoding != 0)
|
|
return false;
|
|
std::int32_t base{0};
|
|
if (false == input.get(base) || input.left() != Count * sizeof(std::uint16_t))
|
|
return false;
|
|
std::uint16_t value{0};
|
|
for (std::size_t i = 0; i < Count; ++i)
|
|
{
|
|
std::uint16_t delta{0};
|
|
input.get(delta);
|
|
value = static_cast<std::uint16_t>(value + delta);
|
|
Out[i] = static_cast<float>((static_cast<std::int64_t>(base) + value) * height_step);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::uint32_t checksum(std::vector<std::uint8_t> const &Data)
|
|
{
|
|
return scene::terrain_file::checksum(reinterpret_cast<char const *>(Data.data()), Data.size());
|
|
}
|
|
|
|
// reads the data of a slice of a pack file, unpacked and checked
|
|
template <typename Slice_> bool load_slice(std::ifstream &File, Slice_ const &Slice, std::vector<std::uint8_t> &Out)
|
|
{
|
|
File.clear();
|
|
std::vector<std::uint8_t> stored(Slice.stored);
|
|
File.seekg(static_cast<std::streamoff>(Slice.offset));
|
|
File.read(reinterpret_cast<char *>(stored.data()), stored.size());
|
|
if (File.gcount() != static_cast<std::streamsize>(stored.size()))
|
|
return false;
|
|
if (Slice.codec == codec_stored)
|
|
Out = std::move(stored);
|
|
else if (Slice.codec == codec_zstd)
|
|
{
|
|
Out.resize(Slice.size);
|
|
auto const result{ZSTD_decompress(Out.data(), Out.size(), stored.data(), stored.size())};
|
|
if (ZSTD_isError(result) || result != Slice.size)
|
|
return false;
|
|
}
|
|
else
|
|
return false;
|
|
return Out.size() == Slice.size && checksum(Out) == Slice.crc;
|
|
}
|
|
|
|
int floor_div(int const Value, int const Divisor)
|
|
{
|
|
return (Value >= 0 ? Value / Divisor : -((-Value + Divisor - 1) / Divisor));
|
|
}
|
|
|
|
// the larger of two values, the corner between them rounded off over K
|
|
double soft_max(double const A, double const B, double const K)
|
|
{
|
|
if (K <= 1e-6)
|
|
return std::max(A, B);
|
|
auto const h{std::max(K - std::abs(A - B), 0.0) / K};
|
|
return std::max(A, B) + h * h * K * 0.25;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
double natural_noise(double const X, double const Z, double const Scale, int const Channel)
|
|
{
|
|
auto const hash = [](std::int64_t const Ix, std::int64_t const Iz) {
|
|
auto h{static_cast<std::uint64_t>(Ix) * 0x9E3779B97F4A7C15ull ^ static_cast<std::uint64_t>(Iz) * 0xC2B2AE3D27D4EB4Full};
|
|
h ^= h >> 31;
|
|
h *= 0xBF58476D1CE4E5B9ull;
|
|
h ^= h >> 29;
|
|
return static_cast<double>(h & 0xffffff) / static_cast<double>(0xffffff) * 2.0 - 1.0;
|
|
};
|
|
auto const octave = [&](double const U, double const V) {
|
|
auto const iu{std::floor(U)};
|
|
auto const iv{std::floor(V)};
|
|
auto const fu{U - iu};
|
|
auto const fv{V - iv};
|
|
auto const su{fu * fu * (3.0 - 2.0 * fu)};
|
|
auto const sv{fv * fv * (3.0 - 2.0 * fv)};
|
|
auto const x{static_cast<std::int64_t>(iu)};
|
|
auto const z{static_cast<std::int64_t>(iv)};
|
|
auto const a{hash(x, z) + (hash(x + 1, z) - hash(x, z)) * su};
|
|
auto const b{hash(x, z + 1) + (hash(x + 1, z + 1) - hash(x, z + 1)) * su};
|
|
return a + (b - a) * sv;
|
|
};
|
|
auto const scale{std::max(0.5, Scale)};
|
|
auto const u{X / scale + Channel * 101.7};
|
|
auto const v{Z / scale - Channel * 57.3};
|
|
return std::clamp(0.7 * octave(u, v) + 0.3 * octave(u * 2.7 + 17.3, v * 2.7 - 9.1), -1.0, 1.0);
|
|
}
|
|
|
|
int cells_for(float const Spacing)
|
|
{
|
|
return static_cast<int>(std::lround(chunk_size / valid_spacing(Spacing)));
|
|
}
|
|
|
|
float valid_spacing(float const Spacing)
|
|
{
|
|
float best{default_spacing};
|
|
float distance{std::numeric_limits<float>::max()};
|
|
for (auto const spacing : spacings)
|
|
if (std::abs(spacing - Spacing) < distance)
|
|
{
|
|
distance = std::abs(spacing - Spacing);
|
|
best = spacing;
|
|
}
|
|
return best;
|
|
}
|
|
|
|
int detail_samples(int const Cells)
|
|
{
|
|
return std::max(default_paint_samples, Cells);
|
|
}
|
|
|
|
chunk_key chunk_at(double const X, double const Z)
|
|
{
|
|
return {static_cast<int>(std::floor(X / chunk_size)), static_cast<int>(std::floor(Z / chunk_size))};
|
|
}
|
|
|
|
glm::dvec2 chunk_corner(chunk_key const &Key)
|
|
{
|
|
return {Key.first * chunk_size, Key.second * chunk_size};
|
|
}
|
|
|
|
pack_key pack_of(chunk_key const &Key)
|
|
{
|
|
return {floor_div(Key.first, pack_side), floor_div(Key.second, pack_side)};
|
|
}
|
|
|
|
glm::dvec2 pack_corner(pack_key const &Key)
|
|
{
|
|
return {Key.first * pack_side * chunk_size, Key.second * pack_side * chunk_size};
|
|
}
|
|
|
|
std::string pack_file_name(pack_key const &Key)
|
|
{
|
|
return std::to_string(Key.first) + "_" + std::to_string(Key.second) + ".tch";
|
|
}
|
|
|
|
// chunk_data
|
|
|
|
std::size_t chunk_data::layer_count() const
|
|
{
|
|
std::size_t count{0};
|
|
while (count < layers.size() && layers[count] != no_layer)
|
|
++count;
|
|
return count;
|
|
}
|
|
|
|
bool chunk_data::contains(double const X, double const Z) const
|
|
{
|
|
auto const low{corner()};
|
|
return X >= low.x && X <= low.x + chunk_size && Z >= low.y && Z <= low.y + chunk_size;
|
|
}
|
|
|
|
double chunk_data::height_at(double const X, double const Z) const
|
|
{
|
|
return height_in(heights, X, Z);
|
|
}
|
|
|
|
double chunk_data::height_in(std::vector<float> const &Heights, double const X, double const Z) const
|
|
{
|
|
if (cells <= 0 || Heights.size() != static_cast<std::size_t>(side()) * side())
|
|
return 0.0;
|
|
auto const low{corner()};
|
|
auto const fx{(X - low.x) / spacing()};
|
|
auto const fz{(Z - low.y) / spacing()};
|
|
auto const ix{std::clamp(static_cast<int>(std::floor(fx)), 0, cells - 1)};
|
|
auto const iz{std::clamp(static_cast<int>(std::floor(fz)), 0, cells - 1)};
|
|
auto const tx{std::clamp(fx - ix, 0.0, 1.0)};
|
|
auto const tz{std::clamp(fz - iz, 0.0, 1.0)};
|
|
auto const at = [&](int const X_, int const Z_) { return static_cast<double>(Heights[static_cast<std::size_t>(Z_) * side() + X_]); };
|
|
auto const h00{at(ix, iz)}, h10{at(ix + 1, iz)}, h01{at(ix, iz + 1)}, h11{at(ix + 1, iz + 1)};
|
|
// the quads are cut along the diagonal from (x0, z1) to (x1, z0), as the mesh is made
|
|
if (tx + tz <= 1.0)
|
|
return h00 + tx * (h10 - h00) + tz * (h01 - h00);
|
|
return h11 + (1.0 - tx) * (h01 - h11) + (1.0 - tz) * (h10 - h11);
|
|
}
|
|
|
|
std::shared_ptr<chunk_data> chunk_data::make_flat(chunk_key const &Key, float const Spacing, float const Height)
|
|
{
|
|
auto chunk{std::make_shared<chunk_data>()};
|
|
chunk->key = Key;
|
|
chunk->cells = cells_for(Spacing);
|
|
chunk->heights.assign(static_cast<std::size_t>(chunk->side()) * chunk->side(), Height);
|
|
chunk->layers[0] = 0;
|
|
return chunk;
|
|
}
|
|
|
|
std::shared_ptr<chunk_data> chunk_data::resampled(float const Spacing, int const Paint) const
|
|
{
|
|
auto chunk{std::make_shared<chunk_data>(*this)};
|
|
chunk->cells = cells_for(Spacing);
|
|
if (chunk->cells != cells)
|
|
{
|
|
auto const low{corner()};
|
|
auto const step{chunk->spacing()};
|
|
// a coarser grid would otherwise pick single points of the finer one: each point inside the chunk takes the average of
|
|
// the ground around it (a tent of the size of its cell). the points of the edges are the ground itself, as they're
|
|
// shared with the neighbours, which keep theirs
|
|
auto const reach{chunk->cells < cells ? step * 0.5 : 0.0};
|
|
auto const taps{reach > 0.0 ? std::max(1, static_cast<int>(std::ceil(reach / spacing()))) : 0};
|
|
auto const resample = [&](std::vector<float> const &Source, std::vector<float> &Target) {
|
|
Target.resize(static_cast<std::size_t>(chunk->side()) * chunk->side());
|
|
for (int iz = 0; iz <= chunk->cells; ++iz)
|
|
for (int ix = 0; ix <= chunk->cells; ++ix)
|
|
{
|
|
auto const x{low.x + ix * step}, z{low.y + iz * step};
|
|
auto value{height_in(Source, x, z)};
|
|
if (taps > 0 && ix > 0 && iz > 0 && ix < chunk->cells && iz < chunk->cells)
|
|
{
|
|
double sum{0.0}, weights{0.0};
|
|
for (int dz = -taps; dz <= taps; ++dz)
|
|
for (int dx = -taps; dx <= taps; ++dx)
|
|
{
|
|
auto const weight{static_cast<double>((taps + 1 - std::abs(dx)) * (taps + 1 - std::abs(dz)))};
|
|
sum += weight * height_in(Source, x + dx * reach / taps, z + dz * reach / taps);
|
|
weights += weight;
|
|
}
|
|
value = sum / weights;
|
|
}
|
|
Target[static_cast<std::size_t>(iz) * chunk->side() + ix] = static_cast<float>(value);
|
|
}
|
|
};
|
|
resample(heights, chunk->heights);
|
|
if (false == base.empty())
|
|
resample(base, chunk->base);
|
|
if (false == adjust.empty())
|
|
resample(adjust, chunk->adjust);
|
|
}
|
|
if (Paint > 0 && paint > 0 && Paint != paint)
|
|
{
|
|
// the weights of the samples between the old ones blended from them, and brought back to 255 a sample
|
|
auto const count{layer_count()};
|
|
auto const oldside{static_cast<std::size_t>(paint + 1)};
|
|
auto const newside{static_cast<std::size_t>(Paint + 1)};
|
|
chunk->paint = Paint;
|
|
chunk->weights.assign(newside * newside * count, 0);
|
|
std::vector<double> values(count);
|
|
for (std::size_t iz = 0; iz < newside; ++iz)
|
|
for (std::size_t ix = 0; ix < newside; ++ix)
|
|
{
|
|
auto const fx{static_cast<double>(ix) * paint / Paint}, fz{static_cast<double>(iz) * paint / Paint};
|
|
auto const x0{std::min(static_cast<std::size_t>(fx), oldside - 2)}, z0{std::min(static_cast<std::size_t>(fz), oldside - 2)};
|
|
auto const tx{fx - x0}, tz{fz - z0};
|
|
double total{0.0};
|
|
for (std::size_t slot = 0; slot < count; ++slot)
|
|
{
|
|
auto const at = [&](std::size_t const X, std::size_t const Z) { return static_cast<double>(weights[slot * oldside * oldside + Z * oldside + X]); };
|
|
values[slot] = (at(x0, z0) * (1.0 - tx) + at(x0 + 1, z0) * tx) * (1.0 - tz) + (at(x0, z0 + 1) * (1.0 - tx) + at(x0 + 1, z0 + 1) * tx) * tz;
|
|
total += values[slot];
|
|
}
|
|
// rounded down, what's left of 255 goes to the heaviest
|
|
int sum{0};
|
|
std::size_t heaviest{0};
|
|
for (std::size_t slot = 0; slot < count; ++slot)
|
|
{
|
|
auto const weight{total > 0.0 ? static_cast<int>(values[slot] * 255.0 / total) : (slot == 0 ? 255 : 0)};
|
|
chunk->weights[slot * newside * newside + iz * newside + ix] = static_cast<std::uint8_t>(weight);
|
|
sum += weight;
|
|
if (values[slot] > values[heaviest])
|
|
heaviest = slot;
|
|
}
|
|
chunk->weights[heaviest * newside * newside + iz * newside + ix] += static_cast<std::uint8_t>(255 - sum);
|
|
}
|
|
}
|
|
return chunk;
|
|
}
|
|
|
|
std::uint8_t chunk_data::weight(std::size_t const Slot, int const Ix, int const Iz) const
|
|
{
|
|
if (paint <= 0)
|
|
return Slot == 0 ? 255 : 0;
|
|
auto const samples{static_cast<std::size_t>(paint + 1) * (paint + 1)};
|
|
auto const index{Slot * samples + static_cast<std::size_t>(Iz) * (paint + 1) + Ix};
|
|
return index < weights.size() ? weights[index] : 0;
|
|
}
|
|
|
|
int chunk_data::slot_of(std::uint16_t const Layer) const
|
|
{
|
|
for (std::size_t slot = 0; slot < layers.size(); ++slot)
|
|
if (layers[slot] == Layer)
|
|
return static_cast<int>(slot);
|
|
return -1;
|
|
}
|
|
|
|
std::size_t chunk_data::dominant_slot() const
|
|
{
|
|
auto const count{layer_count()};
|
|
if (paint <= 0 || count < 2)
|
|
return 0;
|
|
auto const samples{static_cast<std::size_t>(paint + 1) * (paint + 1)};
|
|
std::size_t best{0};
|
|
std::uint64_t bestsum{0};
|
|
for (std::size_t slot = 0; slot < count; ++slot)
|
|
{
|
|
std::uint64_t sum{0};
|
|
for (std::size_t i = 0; i < samples && slot * samples + i < weights.size(); ++i)
|
|
sum += weights[slot * samples + i];
|
|
if (sum > bestsum)
|
|
{
|
|
bestsum = sum;
|
|
best = slot;
|
|
}
|
|
}
|
|
return best;
|
|
}
|
|
|
|
void chunk_data::ensure_paint(int const Samples)
|
|
{
|
|
if (paint > 0)
|
|
return;
|
|
if (layers[0] == no_layer)
|
|
layers[0] = 0;
|
|
paint = std::max(1, Samples);
|
|
auto const samples{static_cast<std::size_t>(paint + 1) * (paint + 1)};
|
|
weights.assign(samples * layer_count(), 0);
|
|
std::fill(weights.begin(), weights.begin() + samples, 255);
|
|
}
|
|
|
|
void chunk_data::compact_layers()
|
|
{
|
|
if (paint <= 0)
|
|
return;
|
|
auto const samples{static_cast<std::size_t>(paint + 1) * (paint + 1)};
|
|
auto const count{layer_count()};
|
|
std::vector<std::uint8_t> kept;
|
|
std::array<std::uint16_t, max_chunk_layers> keptlayers;
|
|
keptlayers.fill(no_layer);
|
|
std::size_t used{0};
|
|
for (std::size_t slot = 0; slot < count; ++slot)
|
|
{
|
|
auto const begin{weights.begin() + slot * samples};
|
|
if (std::all_of(begin, begin + samples, [](std::uint8_t const Weight) { return Weight == 0; }))
|
|
continue;
|
|
kept.insert(kept.end(), begin, begin + samples);
|
|
keptlayers[used++] = layers[slot];
|
|
}
|
|
if (used == 0)
|
|
{
|
|
// nothing painted at all; shouldn't happen, but the chunk can't be left without a material
|
|
keptlayers[0] = layers[0] == no_layer ? 0 : layers[0];
|
|
used = 1;
|
|
kept.clear();
|
|
}
|
|
layers = keptlayers;
|
|
if (used == 1)
|
|
{
|
|
paint = 0;
|
|
weights.clear();
|
|
return;
|
|
}
|
|
weights = std::move(kept);
|
|
}
|
|
|
|
float chunk_data::hole_value(double const X, double const Z) const
|
|
{
|
|
if (false == has_holes())
|
|
return 0.f;
|
|
auto const low{corner()};
|
|
auto const step{chunk_size / holes};
|
|
auto const fx{std::clamp((X - low.x) / step, 0.0, static_cast<double>(holes))};
|
|
auto const fz{std::clamp((Z - low.y) / step, 0.0, static_cast<double>(holes))};
|
|
auto const ix{std::min(static_cast<int>(fx), holes - 1)};
|
|
auto const iz{std::min(static_cast<int>(fz), holes - 1)};
|
|
auto const tx{fx - ix}, tz{fz - iz};
|
|
auto const side{static_cast<std::size_t>(holes + 1)};
|
|
auto const at = [&](int const X_, int const Z_) { return hole_mask[static_cast<std::size_t>(Z_) * side + X_] / 255.0; };
|
|
return static_cast<float>((at(ix, iz) * (1.0 - tx) + at(ix + 1, iz) * tx) * (1.0 - tz) + (at(ix, iz + 1) * (1.0 - tx) + at(ix + 1, iz + 1) * tx) * tz);
|
|
}
|
|
|
|
bool chunk_data::cut_cells(int const Ix0, int const Iz0, int const Ix1, int const Iz1) const
|
|
{
|
|
if (false == has_holes() || cells <= 0)
|
|
return false;
|
|
// every sample the surface of the cells is led from has to be cut, then all of it is
|
|
auto const ratio{static_cast<double>(holes) / cells};
|
|
auto const range = [&](int const From, int const To) {
|
|
return std::make_pair(std::clamp(static_cast<int>(std::floor(From * ratio + 1e-9)), 0, holes), std::clamp(static_cast<int>(std::ceil(To * ratio - 1e-9)), 0, holes));
|
|
};
|
|
auto const [x0, x1]{range(Ix0, Ix1)};
|
|
auto const [z0, z1]{range(Iz0, Iz1)};
|
|
auto const side{static_cast<std::size_t>(holes + 1)};
|
|
for (int z = z0; z <= z1; ++z)
|
|
for (int x = x0; x <= x1; ++x)
|
|
if (hole_mask[static_cast<std::size_t>(z) * side + x] < 128)
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
void chunk_data::ensure_holes()
|
|
{
|
|
if (has_holes())
|
|
return;
|
|
holes = detail_samples(cells);
|
|
hole_mask.assign(static_cast<std::size_t>(holes + 1) * (holes + 1), 0);
|
|
}
|
|
|
|
void chunk_data::compact_holes()
|
|
{
|
|
if (has_holes() && std::any_of(hole_mask.begin(), hole_mask.end(), [](std::uint8_t const Value) { return Value != 0; }))
|
|
return;
|
|
holes = 0;
|
|
hole_mask.clear();
|
|
}
|
|
|
|
std::size_t chunk_data::grass_count() const
|
|
{
|
|
if (grass <= 0)
|
|
return 0;
|
|
std::size_t count{0};
|
|
while (count < grass_kinds.size() && grass_kinds[count] != no_layer)
|
|
++count;
|
|
return count;
|
|
}
|
|
|
|
int chunk_data::grass_slot_of(std::uint16_t const Kind) const
|
|
{
|
|
for (std::size_t slot = 0; slot < grass_count(); ++slot)
|
|
if (grass_kinds[slot] == Kind)
|
|
return static_cast<int>(slot);
|
|
return -1;
|
|
}
|
|
|
|
void chunk_data::ensure_grass()
|
|
{
|
|
if (grass > 0)
|
|
return;
|
|
grass = default_paint_samples;
|
|
grass_kinds.fill(no_layer);
|
|
grass_density.clear();
|
|
}
|
|
|
|
void chunk_data::compact_grass()
|
|
{
|
|
if (grass <= 0)
|
|
return;
|
|
auto const samples{static_cast<std::size_t>(grass + 1) * (grass + 1)};
|
|
auto const count{grass_count()};
|
|
std::vector<std::uint8_t> kept;
|
|
std::array<std::uint16_t, max_chunk_grass> keptkinds;
|
|
keptkinds.fill(no_layer);
|
|
std::size_t used{0};
|
|
for (std::size_t slot = 0; slot < count; ++slot)
|
|
{
|
|
auto const begin{grass_density.begin() + slot * samples};
|
|
if (slot * samples + samples > grass_density.size() || std::all_of(begin, begin + samples, [](std::uint8_t const Value) { return Value == 0; }))
|
|
continue;
|
|
kept.insert(kept.end(), begin, begin + samples);
|
|
keptkinds[used++] = grass_kinds[slot];
|
|
}
|
|
grass_kinds = keptkinds;
|
|
grass_density = std::move(kept);
|
|
if (used == 0)
|
|
{
|
|
grass = 0;
|
|
grass_density.clear();
|
|
}
|
|
}
|
|
|
|
// modifiers
|
|
|
|
std::pair<glm::dvec2, glm::dvec2> modifier::bounds() const
|
|
{
|
|
glm::dvec2 low{std::numeric_limits<double>::max()}, high{-std::numeric_limits<double>::max()};
|
|
double widest{0.0};
|
|
for (auto const &point : points)
|
|
{
|
|
low = glm::min(low, point.position);
|
|
high = glm::max(high, point.position);
|
|
widest = std::max(widest, point.half_width);
|
|
}
|
|
if (points.empty())
|
|
return {glm::dvec2{0.0}, glm::dvec2{0.0}};
|
|
auto const margin{widest + reach};
|
|
return {low - glm::dvec2{margin}, high + glm::dvec2{margin}};
|
|
}
|
|
|
|
namespace
|
|
{
|
|
// the file of the patches a manifest names: a plain file name in the folder of the terrain. A path with folders, "..", or a root
|
|
// would have the patches read from any file the user can read, and written over any file the user can write as the terrain is
|
|
// saved, so it is taken as the default name instead
|
|
std::string patches_file_name(std::string_view const Name)
|
|
{
|
|
std::filesystem::path const name{Name};
|
|
if (false == Name.empty() && name.filename() == name && Name != "." && Name != "..")
|
|
return std::string{Name};
|
|
ErrorLog(std::format("Bad terrain: patches_file \"{}\" isn't a plain file name, patches.txt used instead", Name), logtype::file);
|
|
return "patches.txt";
|
|
}
|
|
|
|
std::uint64_t grid_key(int const X, int const Z)
|
|
{
|
|
return (static_cast<std::uint64_t>(static_cast<std::uint32_t>(X)) << 32) | static_cast<std::uint32_t>(Z);
|
|
}
|
|
} // namespace
|
|
|
|
modifier_shape::modifier_shape(modifier const &Modifier) : m_modifier(Modifier)
|
|
{
|
|
for (auto const &point : Modifier.points)
|
|
m_widest = std::max(m_widest, point.half_width);
|
|
auto const bounds{Modifier.bounds()};
|
|
m_min = bounds.first;
|
|
m_max = bounds.second;
|
|
auto const &points{Modifier.points};
|
|
for (std::size_t i = 0; i + 1 < points.size(); ++i)
|
|
{
|
|
auto const &a{points[i].position};
|
|
auto const &b{points[i + 1].position};
|
|
auto const x0{static_cast<int>(std::floor(std::min(a.x, b.x) / m_cell))}, x1{static_cast<int>(std::floor(std::max(a.x, b.x) / m_cell))};
|
|
auto const z0{static_cast<int>(std::floor(std::min(a.y, b.y) / m_cell))}, z1{static_cast<int>(std::floor(std::max(a.y, b.y) / m_cell))};
|
|
for (auto x = x0; x <= x1; ++x)
|
|
for (auto z = z0; z <= z1; ++z)
|
|
m_grid.emplace_back(grid_key(x, z), static_cast<std::uint32_t>(i));
|
|
}
|
|
std::sort(m_grid.begin(), m_grid.end());
|
|
}
|
|
|
|
bool modifier_shape::reaches(glm::dvec2 const &Min, glm::dvec2 const &Max) const
|
|
{
|
|
return false == m_grid.empty() && Max.x >= m_min.x && Min.x <= m_max.x && Max.y >= m_min.y && Min.y <= m_max.y;
|
|
}
|
|
|
|
bool modifier_shape::apply(double const X, double const Z, float &Height) const
|
|
{
|
|
auto const result{evaluate(X, Z, Height)};
|
|
if (false == result.has_value() || std::abs(result->height - Height) < 0.001)
|
|
return false;
|
|
Height = static_cast<float>(result->height);
|
|
return true;
|
|
}
|
|
|
|
std::optional<modifier_shape::effect> modifier_shape::evaluate(double const X, double const Z, double const Ground) const
|
|
{
|
|
if (m_grid.empty() || X < m_min.x || X > m_max.x || Z < m_min.y || Z > m_max.y)
|
|
return std::nullopt;
|
|
auto const &points{m_modifier.points};
|
|
auto const reach{m_widest + m_modifier.reach};
|
|
auto distance{reach};
|
|
double formation{0.0}, halfwidth{0.0};
|
|
bool found{false};
|
|
auto const x0{static_cast<int>(std::floor((X - reach) / m_cell))}, x1{static_cast<int>(std::floor((X + reach) / m_cell))};
|
|
auto const z0{static_cast<int>(std::floor((Z - reach) / m_cell))}, z1{static_cast<int>(std::floor((Z + reach) / m_cell))};
|
|
glm::dvec2 const point{X, Z};
|
|
for (auto x = x0; x <= x1; ++x)
|
|
for (auto z = z0; z <= z1; ++z)
|
|
{
|
|
auto const key{grid_key(x, z)};
|
|
auto it{std::lower_bound(m_grid.begin(), m_grid.end(), std::make_pair(key, std::uint32_t{0}))};
|
|
for (; it != m_grid.end() && it->first == key; ++it)
|
|
{
|
|
auto const i{it->second};
|
|
auto const &a{points[i].position};
|
|
auto const &b{points[i + 1].position};
|
|
auto const run{b - a};
|
|
auto const length{glm::dot(run, run)};
|
|
auto const along{length > 1e-12 ? glm::dot(point - a, run) / length : 0.0};
|
|
if ((m_modifier.open_start && i == 0 && along < 0.0) || (m_modifier.open_end && i + 2 == points.size() && along > 1.0))
|
|
continue;
|
|
auto const t{std::clamp(along, 0.0, 1.0)};
|
|
auto const offset{glm::length(point - (a + run * t))};
|
|
if (offset < distance)
|
|
{
|
|
distance = offset;
|
|
formation = points[i].formation + (points[i + 1].formation - points[i].formation) * t;
|
|
halfwidth = points[i].half_width + (points[i + 1].half_width - points[i].half_width) * t;
|
|
found = true;
|
|
}
|
|
}
|
|
}
|
|
if (false == found)
|
|
return std::nullopt;
|
|
// the formation under the axis, slopes from its edges down or up to the ground, the bends rounded off
|
|
auto slope{std::max(0.1, m_modifier.slope)};
|
|
auto const rounding{std::max(0.0, m_modifier.rounding)};
|
|
auto const ground{Ground};
|
|
auto beyond{soft_max(0.0, distance - halfwidth, rounding)};
|
|
// the bends of the slopes round off only away from the formation: under it the ground is the formation itself, a cut or a
|
|
// fill as shallow as the bend isn't let to sink or rise it
|
|
auto bend{rounding / slope * std::clamp(beyond / std::max(rounding, 1e-6), 0.0, 1.0)};
|
|
auto const &natural{m_modifier.natural};
|
|
// away from the edge of the formation only: the track bed keeps its width and its edges
|
|
auto const fade{natural.any() ? std::clamp(beyond / 3.0, 0.0, 1.0) : 0.0};
|
|
if (fade > 0.0)
|
|
{
|
|
auto const smooth{fade * fade * (3.0 - 2.0 * fade)};
|
|
beyond = std::max(0.0, beyond - natural.wander * natural_noise(X, Z, natural.scale, 1) * smooth);
|
|
slope *= 1.0 + std::max(0.0, natural.gentler) * (0.5 + 0.5 * natural_noise(X, Z, natural.scale * 0.7, 2)) * smooth;
|
|
bend += std::max(0.0, natural.soften) * std::min(std::abs(ground - formation), 6.0) * smooth;
|
|
}
|
|
auto target{ground > formation ? -soft_max(-ground, -(formation + beyond / slope), bend) : soft_max(ground, formation - beyond / slope, bend)};
|
|
if (fade > 0.0 && natural.roughness > 0.0)
|
|
{
|
|
// bumps on the face of the slope, none where it meets the ground
|
|
auto const face{std::clamp(std::abs(target - ground) / 0.5, 0.0, 1.0)};
|
|
target += natural.roughness * natural_noise(X, Z, 3.0, 3) * face * fade;
|
|
}
|
|
return effect{target, distance - halfwidth, distance};
|
|
}
|
|
|
|
// manifest
|
|
|
|
std::vector<grass_def> const &grass_presets()
|
|
{
|
|
static std::vector<grass_def> const presets{
|
|
// name, root, tip, height, variation, width, density, bend, dried out, follows the seasons
|
|
{"meadow", {0.22f, 0.33f, 0.09f}, {0.58f, 0.68f, 0.27f}, 0.25f, 0.45f, 0.020f, 90.f, 0.30f, {0.74f, 0.66f, 0.40f}, 1.f},
|
|
{"lawn", {0.18f, 0.30f, 0.08f}, {0.42f, 0.58f, 0.20f}, 0.07f, 0.30f, 0.012f, 200.f, 0.15f, {0.62f, 0.60f, 0.36f}, 0.6f},
|
|
{"dry grass", {0.36f, 0.32f, 0.16f}, {0.80f, 0.72f, 0.45f}, 0.30f, 0.45f, 0.016f, 70.f, 0.40f, {0.82f, 0.74f, 0.50f}, 0.5f},
|
|
{"tall grass", {0.18f, 0.27f, 0.07f}, {0.60f, 0.66f, 0.30f}, 0.70f, 0.35f, 0.022f, 40.f, 0.45f, {0.76f, 0.66f, 0.40f}, 1.f},
|
|
{"weeds", {0.16f, 0.25f, 0.08f}, {0.45f, 0.55f, 0.22f}, 0.16f, 0.60f, 0.030f, 30.f, 0.25f, {0.52f, 0.44f, 0.28f}, 1.f},
|
|
};
|
|
return presets;
|
|
}
|
|
|
|
bool load_manifest(std::string const &Path, manifest &Manifest, std::string *Error)
|
|
{
|
|
try
|
|
{
|
|
auto const document{YAML::LoadFile(Path)};
|
|
manifest result;
|
|
result.layers.clear();
|
|
if (auto const node{document["version"]})
|
|
result.version = node.as<int>();
|
|
if (auto const node{document["compress"]})
|
|
result.compress = node.as<bool>();
|
|
if (auto const node{document["spacing"]})
|
|
result.spacing = valid_spacing(node.as<float>());
|
|
if (auto const node{document["water_material"]})
|
|
result.water_material = node.as<std::string>();
|
|
if (auto const node{document["layers"]}; node && node.IsSequence())
|
|
for (auto const &layer : node)
|
|
{
|
|
// a material name, or a map with the material, the size its textures repeat at and the angle they're turned by
|
|
if (layer.IsMap())
|
|
{
|
|
layer_def entry;
|
|
if (auto const material{layer["material"]})
|
|
entry.material = material.as<std::string>();
|
|
if (auto const size{layer["size"]})
|
|
entry.size = std::max(0.f, size.as<float>());
|
|
if (auto const rotation{layer["rotation"]})
|
|
entry.rotation = rotation.as<float>();
|
|
if (false == entry.material.empty())
|
|
result.layers.push_back(entry);
|
|
}
|
|
else
|
|
result.layers.push_back({layer.as<std::string>(), 0.f});
|
|
}
|
|
if (result.layers.empty())
|
|
result.layers.push_back({"grass", 0.f});
|
|
if (auto const node{document["water"]}; node && node.IsSequence())
|
|
for (auto const &entry : node)
|
|
{
|
|
water_body water;
|
|
if (auto const name{entry["name"]})
|
|
water.name = name.as<std::string>();
|
|
if (auto const level{entry["level"]})
|
|
water.level = level.as<double>();
|
|
if (auto const material{entry["material"]})
|
|
water.material = material.as<std::string>();
|
|
if (auto const size{entry["size"]})
|
|
water.size = std::max(0.f, size.as<float>());
|
|
if (auto const outline{entry["outline"]}; outline && outline.IsSequence())
|
|
for (auto const &point : outline)
|
|
if (point.IsSequence() && point.size() >= 2)
|
|
water.outline.emplace_back(point[0].as<double>(), point[1].as<double>());
|
|
if (water.outline.size() >= 3)
|
|
result.water.push_back(std::move(water));
|
|
}
|
|
if (auto const node{document["patches"]}; node && node.IsSequence())
|
|
for (auto const &entry : node)
|
|
{
|
|
surface_patch patch;
|
|
if (auto const name{entry["name"]})
|
|
patch.name = name.as<std::string>();
|
|
if (auto const material{entry["material"]})
|
|
patch.material = material.as<std::string>();
|
|
if (auto const size{entry["size"]})
|
|
patch.size = std::max(0.f, size.as<float>());
|
|
if (auto const rotation{entry["rotation"]})
|
|
patch.rotation = rotation.as<float>();
|
|
if (auto const lift{entry["lift"]})
|
|
patch.lift = lift.as<float>();
|
|
if (auto const outline{entry["outline"]}; outline && outline.IsSequence())
|
|
for (auto const &point : outline)
|
|
if (point.IsSequence() && point.size() >= 2)
|
|
patch.outline.emplace_back(point[0].as<double>(), point[1].as<double>());
|
|
if (auto const grid{entry["grid"]}; grid && grid.IsSequence() && grid.size() >= 5)
|
|
{
|
|
patch.origin = {grid[0].as<double>(), grid[1].as<double>()};
|
|
patch.spacing = std::max(0.1, grid[2].as<double>());
|
|
patch.columns = std::max(0, grid[3].as<int>());
|
|
patch.rows = std::max(0, grid[4].as<int>());
|
|
}
|
|
if (auto const heights{entry["heights"]}; heights && heights.IsSequence())
|
|
{
|
|
patch.heights.reserve(heights.size());
|
|
for (auto const &height : heights)
|
|
patch.heights.push_back(height.as<float>());
|
|
}
|
|
if (auto const follow{entry["follow"]})
|
|
patch.follow = follow.as<bool>();
|
|
if (auto const paint{entry["paint"]})
|
|
{
|
|
patch.paint = paint.as<bool>();
|
|
patch.follow = patch.follow || patch.paint;
|
|
}
|
|
if (auto const node{entry["feather"]})
|
|
patch.feather = std::max(0.f, node.as<float>());
|
|
if (auto const node{entry["noise"]})
|
|
patch.noise = std::max(0.f, node.as<float>());
|
|
if (auto const node{entry["noise_scale"]})
|
|
patch.noise_scale = std::max(0.5f, node.as<float>());
|
|
if (auto const node{entry["shape"]})
|
|
patch.shape = node.as<std::string>();
|
|
if (auto const node{entry["grass"]})
|
|
patch.grass = std::max(-2, node.as<int>());
|
|
if (patch.follow && !entry["grid"] && !entry["spacing"])
|
|
patch.spacing = 2.0;
|
|
if (auto const spacing{entry["spacing"]})
|
|
patch.spacing = std::max(0.1, spacing.as<double>());
|
|
if (patch.outline.size() >= 3 && (patch.follow || (patch.columns >= 2 && patch.rows >= 2 && patch.heights.size() == static_cast<std::size_t>(patch.columns) * patch.rows)))
|
|
result.patches.push_back(std::move(patch));
|
|
}
|
|
if (auto const node{document["patches_file"]})
|
|
{
|
|
result.patches_file = patches_file_name(node.as<std::string>());
|
|
auto const folder{std::filesystem::path(Path).parent_path()};
|
|
load_patches((folder / result.patches_file).string(), result.patches);
|
|
}
|
|
if (auto const node{document["modifiers"]}; node && node.IsSequence())
|
|
for (auto const &entry : node)
|
|
{
|
|
modifier item;
|
|
if (auto const name{entry["name"]})
|
|
item.name = name.as<std::string>();
|
|
if (auto const slope{entry["slope"]})
|
|
item.slope = slope.as<double>();
|
|
if (auto const reach{entry["reach"]})
|
|
item.reach = reach.as<double>();
|
|
if (auto const rounding{entry["rounding"]})
|
|
item.rounding = rounding.as<double>();
|
|
// natural: {wander: , scale: , gentler: , soften: , roughness: }
|
|
if (auto const natural{entry["natural"]}; natural && natural.IsMap())
|
|
{
|
|
auto const take = [&](char const *Key, double &Value) {
|
|
if (auto const node{natural[Key]})
|
|
Value = std::max(0.0, node.as<double>());
|
|
};
|
|
take("wander", item.natural.wander);
|
|
take("scale", item.natural.scale);
|
|
take("gentler", item.natural.gentler);
|
|
take("soften", item.natural.soften);
|
|
take("roughness", item.natural.roughness);
|
|
item.natural.scale = std::max(0.5, item.natural.scale);
|
|
}
|
|
if (auto const open{entry["open_start"]})
|
|
item.open_start = open.as<bool>();
|
|
if (auto const open{entry["open_end"]})
|
|
item.open_end = open.as<bool>();
|
|
if (auto const points{entry["points"]}; points && points.IsSequence())
|
|
for (auto const &point : points)
|
|
if (point.IsSequence() && point.size() >= 4)
|
|
item.points.push_back({{point[0].as<double>(), point[1].as<double>()}, point[2].as<double>(), point[3].as<double>()});
|
|
if (item.points.size() >= 2)
|
|
result.modifiers.push_back(std::move(item));
|
|
}
|
|
if (auto const node{document["grass"]}; node && node.IsSequence())
|
|
for (auto const &entry : node)
|
|
{
|
|
grass_def kind;
|
|
auto const colour = [](YAML::Node const &Node, glm::vec3 &Colour) {
|
|
if (Node && Node.IsSequence() && Node.size() >= 3)
|
|
Colour = glm::clamp(glm::vec3{Node[0].as<float>(), Node[1].as<float>(), Node[2].as<float>()}, glm::vec3{0.f}, glm::vec3{1.f});
|
|
};
|
|
if (auto const name{entry["name"]})
|
|
kind.name = name.as<std::string>();
|
|
colour(entry["base"], kind.base);
|
|
colour(entry["tip"], kind.tip);
|
|
if (auto const height{entry["height"]})
|
|
kind.height = std::max(0.01f, height.as<float>());
|
|
if (auto const variation{entry["variation"]})
|
|
kind.variation = std::clamp(variation.as<float>(), 0.f, 1.f);
|
|
if (auto const width{entry["width"]})
|
|
kind.width = std::max(0.001f, width.as<float>());
|
|
if (auto const density{entry["density"]})
|
|
kind.density = std::max(0.f, density.as<float>());
|
|
if (auto const bend{entry["bend"]})
|
|
kind.bend = std::clamp(bend.as<float>(), 0.f, 1.f);
|
|
colour(entry["dry"], kind.dry);
|
|
if (auto const seasonal{entry["seasonal"]})
|
|
kind.seasonal = std::clamp(seasonal.as<float>(), 0.f, 1.f);
|
|
result.grass.push_back(std::move(kind));
|
|
}
|
|
Manifest = std::move(result);
|
|
return true;
|
|
}
|
|
catch (YAML::Exception const &exception)
|
|
{
|
|
if (Error != nullptr)
|
|
*Error = exception.what();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool save_manifest(std::string const &Path, manifest const &Manifest, std::string *Error)
|
|
{
|
|
YAML::Emitter out;
|
|
out.SetDoublePrecision(12);
|
|
out.SetFloatPrecision(7);
|
|
out << YAML::Comment("MaSzyna heightmap terrain; the chunks are in the .tch files next to this one");
|
|
out << YAML::BeginMap;
|
|
out << YAML::Key << "version" << YAML::Value << Manifest.version;
|
|
out << YAML::Key << "compress" << YAML::Value << Manifest.compress;
|
|
out << YAML::Key << "spacing" << YAML::Value << Manifest.spacing;
|
|
out << YAML::Key << "water_material" << YAML::Value << Manifest.water_material;
|
|
out << YAML::Key << "layers" << YAML::Value << YAML::BeginSeq;
|
|
for (auto const &layer : Manifest.layers)
|
|
{
|
|
if (layer.size > 0.f || layer.rotation != 0.f)
|
|
{
|
|
out << YAML::Flow << YAML::BeginMap << YAML::Key << "material" << YAML::Value << layer.material << YAML::Key << "size" << YAML::Value << layer.size;
|
|
if (layer.rotation != 0.f)
|
|
out << YAML::Key << "rotation" << YAML::Value << layer.rotation;
|
|
out << YAML::EndMap;
|
|
}
|
|
else
|
|
out << layer.material;
|
|
}
|
|
out << YAML::EndSeq;
|
|
if (false == Manifest.water.empty())
|
|
{
|
|
out << YAML::Key << "water" << YAML::Value << YAML::BeginSeq;
|
|
for (auto const &water : Manifest.water)
|
|
{
|
|
out << YAML::BeginMap;
|
|
out << YAML::Key << "name" << YAML::Value << water.name;
|
|
out << YAML::Key << "level" << YAML::Value << water.level;
|
|
if (false == water.material.empty())
|
|
out << YAML::Key << "material" << YAML::Value << water.material;
|
|
if (water.size > 0.f)
|
|
out << YAML::Key << "size" << YAML::Value << water.size;
|
|
out << YAML::Key << "outline" << YAML::Value << YAML::BeginSeq;
|
|
for (auto const &point : water.outline)
|
|
out << YAML::Flow << YAML::BeginSeq << point.x << point.y << YAML::EndSeq;
|
|
out << YAML::EndSeq;
|
|
out << YAML::EndMap;
|
|
}
|
|
out << YAML::EndSeq;
|
|
}
|
|
auto const sidecar{Manifest.patches_file.empty() && Manifest.patches.size() > patches_inline ? std::string{"patches.txt"} : Manifest.patches_file};
|
|
if (false == sidecar.empty())
|
|
{
|
|
out << YAML::Key << "patches_file" << YAML::Value << sidecar;
|
|
auto const folder{std::filesystem::path(Path).parent_path()};
|
|
if (false == save_patches((folder / sidecar).string(), Manifest.patches))
|
|
{
|
|
if (Error != nullptr)
|
|
*Error = "can't write " + sidecar;
|
|
return false;
|
|
}
|
|
}
|
|
// with the file, only the patches with heights of their own stay here
|
|
auto const inline_patch = [&](surface_patch const &Patch) { return sidecar.empty() || false == Patch.follow; };
|
|
if (std::any_of(Manifest.patches.begin(), Manifest.patches.end(), inline_patch))
|
|
{
|
|
out << YAML::Key << "patches" << YAML::Value << YAML::BeginSeq;
|
|
for (auto const &patch : Manifest.patches)
|
|
{
|
|
if (false == inline_patch(patch))
|
|
continue;
|
|
out << YAML::BeginMap;
|
|
out << YAML::Key << "name" << YAML::Value << patch.name;
|
|
if (false == patch.material.empty())
|
|
out << YAML::Key << "material" << YAML::Value << patch.material;
|
|
if (patch.size > 0.f)
|
|
out << YAML::Key << "size" << YAML::Value << patch.size;
|
|
if (patch.rotation != 0.f)
|
|
out << YAML::Key << "rotation" << YAML::Value << patch.rotation;
|
|
if (patch.lift != 0.f)
|
|
out << YAML::Key << "lift" << YAML::Value << patch.lift;
|
|
out << YAML::Key << "outline" << YAML::Value << YAML::BeginSeq;
|
|
for (auto const &point : patch.outline)
|
|
out << YAML::Flow << YAML::BeginSeq << point.x << point.y << YAML::EndSeq;
|
|
out << YAML::EndSeq;
|
|
if (false == patch.shape.empty())
|
|
out << YAML::Key << "shape" << YAML::Value << patch.shape;
|
|
if (patch.grass != -2)
|
|
out << YAML::Key << "grass" << YAML::Value << patch.grass;
|
|
if (patch.paint)
|
|
{
|
|
out << YAML::Key << "paint" << YAML::Value << true;
|
|
out << YAML::Key << "feather" << YAML::Value << patch.feather;
|
|
out << YAML::Key << "noise" << YAML::Value << patch.noise;
|
|
out << YAML::Key << "noise_scale" << YAML::Value << patch.noise_scale;
|
|
}
|
|
else if (patch.follow)
|
|
{
|
|
out << YAML::Key << "follow" << YAML::Value << true;
|
|
out << YAML::Key << "spacing" << YAML::Value << patch.spacing;
|
|
}
|
|
else
|
|
{
|
|
out << YAML::Key << "grid" << YAML::Value << YAML::Flow << YAML::BeginSeq << patch.origin.x << patch.origin.y << patch.spacing << patch.columns << patch.rows << YAML::EndSeq;
|
|
out << YAML::Key << "heights" << YAML::Value << YAML::Flow << YAML::BeginSeq;
|
|
for (auto const height : patch.heights)
|
|
out << std::round(height * 1000.f) / 1000.f;
|
|
out << YAML::EndSeq;
|
|
}
|
|
out << YAML::EndMap;
|
|
}
|
|
out << YAML::EndSeq;
|
|
}
|
|
if (false == Manifest.modifiers.empty())
|
|
{
|
|
out << YAML::Key << "modifiers" << YAML::Value << YAML::BeginSeq;
|
|
for (auto const &item : Manifest.modifiers)
|
|
{
|
|
out << YAML::BeginMap;
|
|
out << YAML::Key << "name" << YAML::Value << item.name;
|
|
out << YAML::Key << "slope" << YAML::Value << item.slope;
|
|
out << YAML::Key << "reach" << YAML::Value << item.reach;
|
|
out << YAML::Key << "rounding" << YAML::Value << item.rounding;
|
|
if (item.natural.any())
|
|
{
|
|
out << YAML::Key << "natural" << YAML::Value << YAML::Flow << YAML::BeginMap;
|
|
out << YAML::Key << "wander" << YAML::Value << item.natural.wander;
|
|
out << YAML::Key << "scale" << YAML::Value << item.natural.scale;
|
|
out << YAML::Key << "gentler" << YAML::Value << item.natural.gentler;
|
|
out << YAML::Key << "soften" << YAML::Value << item.natural.soften;
|
|
out << YAML::Key << "roughness" << YAML::Value << item.natural.roughness;
|
|
out << YAML::EndMap;
|
|
}
|
|
if (item.open_start)
|
|
out << YAML::Key << "open_start" << YAML::Value << true;
|
|
if (item.open_end)
|
|
out << YAML::Key << "open_end" << YAML::Value << true;
|
|
out << YAML::Key << "points" << YAML::Value << YAML::BeginSeq;
|
|
for (auto const &point : item.points)
|
|
out << YAML::Flow << YAML::BeginSeq << point.position.x << point.position.y << point.formation << point.half_width << YAML::EndSeq;
|
|
out << YAML::EndSeq;
|
|
out << YAML::EndMap;
|
|
}
|
|
out << YAML::EndSeq;
|
|
}
|
|
if (false == Manifest.grass.empty())
|
|
{
|
|
out << YAML::Key << "grass" << YAML::Value << YAML::BeginSeq;
|
|
for (auto const &kind : Manifest.grass)
|
|
{
|
|
out << YAML::BeginMap;
|
|
out << YAML::Key << "name" << YAML::Value << kind.name;
|
|
out << YAML::Key << "base" << YAML::Value << YAML::Flow << YAML::BeginSeq << kind.base.r << kind.base.g << kind.base.b << YAML::EndSeq;
|
|
out << YAML::Key << "tip" << YAML::Value << YAML::Flow << YAML::BeginSeq << kind.tip.r << kind.tip.g << kind.tip.b << YAML::EndSeq;
|
|
out << YAML::Key << "height" << YAML::Value << kind.height;
|
|
out << YAML::Key << "variation" << YAML::Value << kind.variation;
|
|
out << YAML::Key << "width" << YAML::Value << kind.width;
|
|
out << YAML::Key << "density" << YAML::Value << kind.density;
|
|
out << YAML::Key << "bend" << YAML::Value << kind.bend;
|
|
out << YAML::Key << "dry" << YAML::Value << YAML::Flow << YAML::BeginSeq << kind.dry.r << kind.dry.g << kind.dry.b << YAML::EndSeq;
|
|
out << YAML::Key << "seasonal" << YAML::Value << kind.seasonal;
|
|
out << YAML::EndMap;
|
|
}
|
|
out << YAML::EndSeq;
|
|
}
|
|
out << YAML::EndMap;
|
|
if (false == out.good())
|
|
{
|
|
if (Error != nullptr)
|
|
*Error = out.GetLastError();
|
|
return false;
|
|
}
|
|
std::error_code ec;
|
|
std::filesystem::create_directories(std::filesystem::path(Path).parent_path(), ec);
|
|
std::ofstream file{Path, std::ios::binary | std::ios::trunc};
|
|
file << out.c_str() << '\n';
|
|
if (false == file.good())
|
|
{
|
|
if (Error != nullptr)
|
|
*Error = "can't write " + Path;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// pack files
|
|
|
|
bool pack_file::open(std::string const &Path, std::string *Error)
|
|
{
|
|
auto const fail = [&](std::string const &Message) {
|
|
if (Error != nullptr)
|
|
*Error = Path + ": " + Message;
|
|
return false;
|
|
};
|
|
m_path = Path;
|
|
for (auto &slices : m_slices)
|
|
slices.clear();
|
|
std::ifstream file{Path, std::ios::binary};
|
|
if (false == file.good())
|
|
return fail("can't open the file");
|
|
std::vector<std::uint8_t> header(header_size);
|
|
file.read(reinterpret_cast<char *>(header.data()), header.size());
|
|
if (file.gcount() != static_cast<std::streamsize>(header.size()))
|
|
return fail("the file is too short");
|
|
reader input{header.data(), header.size()};
|
|
std::uint32_t magic{0}, version{0}, side{0};
|
|
std::int32_t px{0}, pz{0};
|
|
float size{0.f};
|
|
std::uint64_t indexoffset{0};
|
|
input.get(magic);
|
|
input.get(version);
|
|
input.get(px);
|
|
input.get(pz);
|
|
input.get(side);
|
|
input.get(size);
|
|
input.get(indexoffset);
|
|
if (magic != pack_magic)
|
|
return fail("not a terrain chunks file");
|
|
if (version != pack_version)
|
|
return fail("unsupported version " + std::to_string(version));
|
|
if (side != pack_side || std::abs(size - chunk_size) > 1e-3)
|
|
return fail("unsupported chunk layout");
|
|
m_key = {px, pz};
|
|
|
|
std::vector<std::uint8_t> index(pack_side * pack_side * index_entry_size);
|
|
file.seekg(static_cast<std::streamoff>(indexoffset));
|
|
file.read(reinterpret_cast<char *>(index.data()), index.size());
|
|
if (file.gcount() != static_cast<std::streamsize>(index.size()))
|
|
return fail("damaged chunk index");
|
|
reader entries{index.data(), index.size()};
|
|
for (auto &slices : m_slices)
|
|
{
|
|
std::uint64_t tableoffset{0};
|
|
std::uint32_t count{0}, reserved{0};
|
|
entries.get(tableoffset);
|
|
entries.get(count);
|
|
entries.get(reserved);
|
|
if (tableoffset == 0 || count == 0)
|
|
continue;
|
|
if (count > 64)
|
|
return fail("damaged chunk index");
|
|
std::vector<std::uint8_t> table(count * slice_entry_size);
|
|
file.seekg(static_cast<std::streamoff>(tableoffset));
|
|
file.read(reinterpret_cast<char *>(table.data()), table.size());
|
|
if (file.gcount() != static_cast<std::streamsize>(table.size()))
|
|
return fail("damaged slice table");
|
|
reader tableinput{table.data(), table.size()};
|
|
slices.resize(count);
|
|
for (auto &slice : slices)
|
|
{
|
|
std::uint32_t reserved2{0};
|
|
tableinput.get(slice.tag);
|
|
tableinput.get(slice.codec);
|
|
tableinput.get(slice.offset);
|
|
tableinput.get(slice.stored);
|
|
tableinput.get(slice.size);
|
|
tableinput.get(slice.crc);
|
|
tableinput.get(reserved2);
|
|
if (slice.stored > max_slice_size || slice.size > max_slice_size)
|
|
return fail("damaged slice table");
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool pack_file::has(chunk_key const &Key) const
|
|
{
|
|
if (pack_of(Key) != m_key)
|
|
return false;
|
|
auto const x{Key.first - m_key.first * pack_side};
|
|
auto const z{Key.second - m_key.second * pack_side};
|
|
return false == m_slices[static_cast<std::size_t>(z) * pack_side + x].empty();
|
|
}
|
|
|
|
std::vector<chunk_key> pack_file::chunks() const
|
|
{
|
|
std::vector<chunk_key> result;
|
|
for (int z = 0; z < pack_side; ++z)
|
|
for (int x = 0; x < pack_side; ++x)
|
|
if (false == m_slices[static_cast<std::size_t>(z) * pack_side + x].empty())
|
|
result.emplace_back(m_key.first * pack_side + x, m_key.second * pack_side + z);
|
|
return result;
|
|
}
|
|
|
|
std::vector<std::pair<chunk_key, int>> pack_file::chunk_cells() const
|
|
{
|
|
std::vector<std::pair<chunk_key, int>> result;
|
|
std::ifstream file{m_path, std::ios::binary};
|
|
for (int z = 0; z < pack_side; ++z)
|
|
for (int x = 0; x < pack_side; ++x)
|
|
{
|
|
auto const &slices{m_slices[static_cast<std::size_t>(z) * pack_side + x]};
|
|
if (slices.empty())
|
|
continue;
|
|
int cells{0};
|
|
for (auto const &slice : slices)
|
|
{
|
|
if (slice.tag != tag_chunk)
|
|
continue;
|
|
std::vector<std::uint8_t> data;
|
|
std::uint16_t count{0};
|
|
if (file.is_open() && load_slice(file, slice, data))
|
|
{
|
|
reader input{data.data(), data.size()};
|
|
if (input.get(count))
|
|
cells = count;
|
|
}
|
|
break;
|
|
}
|
|
result.emplace_back(chunk_key{m_key.first * pack_side + x, m_key.second * pack_side + z}, cells);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
std::shared_ptr<chunk_data> pack_file::read(chunk_key const &Key, std::string *Error) const
|
|
{
|
|
auto const fail = [&](std::string const &Message) -> std::shared_ptr<chunk_data> {
|
|
if (Error != nullptr)
|
|
*Error = m_path + ": chunk " + std::to_string(Key.first) + "," + std::to_string(Key.second) + ": " + Message;
|
|
return nullptr;
|
|
};
|
|
if (false == has(Key))
|
|
return fail("not in the file");
|
|
auto const &slices{m_slices[static_cast<std::size_t>(Key.second - m_key.second * pack_side) * pack_side + (Key.first - m_key.first * pack_side)]};
|
|
std::ifstream file{m_path, std::ios::binary};
|
|
if (false == file.good())
|
|
return fail("can't open the file");
|
|
|
|
auto const load = [&](slice const &Slice, std::vector<std::uint8_t> &Out) { return load_slice(file, Slice, Out); };
|
|
|
|
auto chunk{std::make_shared<chunk_data>()};
|
|
chunk->key = Key;
|
|
slice const *heights{nullptr}, *base{nullptr}, *adjust{nullptr}, *paint{nullptr}, *holes{nullptr}, *grass{nullptr};
|
|
bool header{false};
|
|
for (auto const &slice : slices)
|
|
{
|
|
if (slice.tag == tag_chunk)
|
|
{
|
|
std::vector<std::uint8_t> data;
|
|
if (false == load(slice, data))
|
|
return fail("damaged chunk header");
|
|
reader input{data.data(), data.size()};
|
|
std::uint16_t cells{0}, samples{0};
|
|
input.get(cells);
|
|
input.get(samples);
|
|
for (auto &layer : chunk->layers)
|
|
input.get(layer);
|
|
chunk->cells = cells;
|
|
chunk->paint = samples;
|
|
header = true;
|
|
}
|
|
else if (slice.tag == tag_heights)
|
|
heights = &slice;
|
|
else if (slice.tag == tag_base)
|
|
base = &slice;
|
|
else if (slice.tag == tag_adjust)
|
|
adjust = &slice;
|
|
else if (slice.tag == tag_paint)
|
|
paint = &slice;
|
|
else if (slice.tag == tag_holes)
|
|
holes = &slice;
|
|
else if (slice.tag == tag_grass)
|
|
grass = &slice;
|
|
// slices of unknown kinds are left for newer versions of the simulator
|
|
}
|
|
if (false == header || heights == nullptr || chunk->cells <= 0 || chunk->cells > 4096)
|
|
return fail("incomplete chunk");
|
|
if (chunk->layers[0] == no_layer)
|
|
chunk->layers[0] = 0;
|
|
auto const count{static_cast<std::size_t>(chunk->side()) * chunk->side()};
|
|
std::vector<std::uint8_t> data;
|
|
if (false == load(*heights, data) || false == decode_heights(data, count, chunk->heights))
|
|
return fail("damaged heights");
|
|
if (base != nullptr && (false == load(*base, data) || false == decode_heights(data, count, chunk->base)))
|
|
return fail("damaged base heights");
|
|
// touch-ups go with the base heights only
|
|
if (adjust != nullptr && false == chunk->base.empty() && (false == load(*adjust, data) || false == decode_heights(data, count, chunk->adjust)))
|
|
return fail("damaged touch-ups");
|
|
if (chunk->paint > 0)
|
|
{
|
|
auto const expected{static_cast<std::size_t>(chunk->paint + 1) * (chunk->paint + 1) * chunk->layer_count()};
|
|
if (paint == nullptr || false == load(*paint, chunk->weights) || chunk->weights.size() != expected)
|
|
return fail("damaged paint");
|
|
}
|
|
if (holes != nullptr)
|
|
{
|
|
if (false == load(*holes, data))
|
|
return fail("damaged holes");
|
|
reader input{data.data(), data.size()};
|
|
std::uint16_t samples{0};
|
|
input.get(samples);
|
|
auto const count{static_cast<std::size_t>(samples + 1) * (samples + 1)};
|
|
chunk->holes = samples;
|
|
chunk->hole_mask.resize(count);
|
|
if (samples == 0 || input.left() != count || false == input.get(chunk->hole_mask.data(), count))
|
|
return fail("damaged holes");
|
|
}
|
|
if (grass != nullptr)
|
|
{
|
|
if (false == load(*grass, data))
|
|
return fail("damaged grass");
|
|
reader input{data.data(), data.size()};
|
|
std::uint16_t samples{0};
|
|
input.get(samples);
|
|
for (auto &kind : chunk->grass_kinds)
|
|
input.get(kind);
|
|
chunk->grass = samples;
|
|
auto const count{static_cast<std::size_t>(samples + 1) * (samples + 1) * chunk->grass_count()};
|
|
chunk->grass_density.resize(count);
|
|
if (samples == 0 || input.left() != count || false == input.get(chunk->grass_density.data(), count))
|
|
return fail("damaged grass");
|
|
}
|
|
return chunk;
|
|
}
|
|
|
|
bool write_pack(std::string const &Path, pack_key const &Key, std::vector<chunk_ptr> const &Chunks, bool const Compress, std::string *Error)
|
|
{
|
|
struct slice_record
|
|
{
|
|
std::uint32_t tag;
|
|
std::uint32_t codec;
|
|
std::uint64_t offset;
|
|
std::uint32_t stored;
|
|
std::uint32_t size;
|
|
std::uint32_t crc;
|
|
};
|
|
std::array<std::vector<slice_record>, pack_side * pack_side> tables;
|
|
std::vector<std::uint8_t> payload; // slice data, placed after the header
|
|
|
|
auto const add = [&](std::vector<slice_record> &Table, std::uint32_t const Tag, std::vector<std::uint8_t> const &Data) {
|
|
slice_record record{Tag, codec_stored, header_size + payload.size(), static_cast<std::uint32_t>(Data.size()), static_cast<std::uint32_t>(Data.size()), checksum(Data)};
|
|
if (Compress && Data.size() > 64)
|
|
{
|
|
std::vector<std::uint8_t> packed(ZSTD_compressBound(Data.size()));
|
|
auto const result{ZSTD_compress(packed.data(), packed.size(), Data.data(), Data.size(), zstd_level)};
|
|
if (false == ZSTD_isError(result) && result < Data.size())
|
|
{
|
|
packed.resize(result);
|
|
record.codec = codec_zstd;
|
|
record.stored = static_cast<std::uint32_t>(packed.size());
|
|
payload.insert(payload.end(), packed.begin(), packed.end());
|
|
Table.push_back(record);
|
|
return;
|
|
}
|
|
}
|
|
payload.insert(payload.end(), Data.begin(), Data.end());
|
|
Table.push_back(record);
|
|
};
|
|
|
|
for (auto const &chunk : Chunks)
|
|
{
|
|
if (chunk == nullptr || false == chunk->valid() || pack_of(chunk->key) != Key)
|
|
continue;
|
|
auto &table{tables[static_cast<std::size_t>(chunk->key.second - Key.second * pack_side) * pack_side + (chunk->key.first - Key.first * pack_side)]};
|
|
table.clear();
|
|
std::vector<std::uint8_t> data;
|
|
put<std::uint16_t>(data, static_cast<std::uint16_t>(chunk->cells));
|
|
put<std::uint16_t>(data, static_cast<std::uint16_t>(chunk->layer_count() > 1 ? chunk->paint : 0));
|
|
for (auto const layer : chunk->layers)
|
|
put<std::uint16_t>(data, layer);
|
|
add(table, tag_chunk, data);
|
|
data.clear();
|
|
encode_heights(chunk->heights, data);
|
|
add(table, tag_heights, data);
|
|
if (chunk->base.size() == chunk->heights.size())
|
|
{
|
|
data.clear();
|
|
encode_heights(chunk->base, data);
|
|
add(table, tag_base, data);
|
|
if (chunk->adjust.size() == chunk->heights.size())
|
|
{
|
|
data.clear();
|
|
encode_heights(chunk->adjust, data);
|
|
add(table, tag_adjust, data);
|
|
}
|
|
}
|
|
if (chunk->paint > 0 && chunk->layer_count() > 1)
|
|
add(table, tag_paint, chunk->weights);
|
|
if (chunk->has_holes())
|
|
{
|
|
data.clear();
|
|
put<std::uint16_t>(data, static_cast<std::uint16_t>(chunk->holes));
|
|
data.insert(data.end(), chunk->hole_mask.begin(), chunk->hole_mask.end());
|
|
add(table, tag_holes, data);
|
|
}
|
|
if (auto const kinds{chunk->grass_count()}; kinds > 0 && chunk->grass_density.size() == static_cast<std::size_t>(chunk->grass + 1) * (chunk->grass + 1) * kinds)
|
|
{
|
|
data.clear();
|
|
put<std::uint16_t>(data, static_cast<std::uint16_t>(chunk->grass));
|
|
for (auto const kind : chunk->grass_kinds)
|
|
put<std::uint16_t>(data, kind);
|
|
data.insert(data.end(), chunk->grass_density.begin(), chunk->grass_density.end());
|
|
add(table, tag_grass, data);
|
|
}
|
|
}
|
|
|
|
std::vector<std::uint8_t> file;
|
|
file.reserve(header_size + payload.size() + 4096);
|
|
put<std::uint32_t>(file, pack_magic);
|
|
put<std::uint32_t>(file, pack_version);
|
|
put<std::int32_t>(file, Key.first);
|
|
put<std::int32_t>(file, Key.second);
|
|
put<std::uint32_t>(file, pack_side);
|
|
put<float>(file, static_cast<float>(chunk_size));
|
|
auto const indexfield{file.size()};
|
|
put<std::uint64_t>(file, 0);
|
|
file.resize(header_size, 0);
|
|
file.insert(file.end(), payload.begin(), payload.end());
|
|
// slice tables
|
|
std::array<std::uint64_t, pack_side * pack_side> tableoffsets{};
|
|
for (std::size_t i = 0; i < tables.size(); ++i)
|
|
{
|
|
if (tables[i].empty())
|
|
continue;
|
|
tableoffsets[i] = file.size();
|
|
for (auto const &record : tables[i])
|
|
{
|
|
put<std::uint32_t>(file, record.tag);
|
|
put<std::uint32_t>(file, record.codec);
|
|
put<std::uint64_t>(file, record.offset);
|
|
put<std::uint32_t>(file, record.stored);
|
|
put<std::uint32_t>(file, record.size);
|
|
put<std::uint32_t>(file, record.crc);
|
|
put<std::uint32_t>(file, 0);
|
|
}
|
|
}
|
|
// chunk index
|
|
put_at<std::uint64_t>(file, indexfield, file.size());
|
|
for (std::size_t i = 0; i < tables.size(); ++i)
|
|
{
|
|
put<std::uint64_t>(file, tableoffsets[i]);
|
|
put<std::uint32_t>(file, static_cast<std::uint32_t>(tables[i].size()));
|
|
put<std::uint32_t>(file, 0);
|
|
}
|
|
|
|
// written next to the old file first, so a failed write doesn't take the old content with it
|
|
std::error_code ec;
|
|
std::filesystem::create_directories(std::filesystem::path(Path).parent_path(), ec);
|
|
auto const temporary{Path + ".tmp"};
|
|
{
|
|
std::ofstream out{temporary, std::ios::binary | std::ios::trunc};
|
|
out.write(reinterpret_cast<char const *>(file.data()), file.size());
|
|
if (false == out.good())
|
|
{
|
|
if (Error != nullptr)
|
|
*Error = "can't write " + temporary;
|
|
return false;
|
|
}
|
|
}
|
|
std::filesystem::rename(temporary, Path, ec);
|
|
if (ec)
|
|
{
|
|
// some systems won't replace an existing file with rename
|
|
std::filesystem::remove(Path, ec);
|
|
std::filesystem::rename(temporary, Path, ec);
|
|
if (ec)
|
|
{
|
|
if (Error != nullptr)
|
|
*Error = "can't replace " + Path + ": " + ec.message();
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void surface_patch::bounds(glm::dvec2 &Low, glm::dvec2 &High) const
|
|
{
|
|
Low = glm::dvec2{std::numeric_limits<double>::max()};
|
|
High = glm::dvec2{-std::numeric_limits<double>::max()};
|
|
for (auto const &point : outline)
|
|
{
|
|
Low = glm::min(Low, point);
|
|
High = glm::max(High, point);
|
|
}
|
|
}
|
|
|
|
// the patches file: a line a patch, its fields apart by tabs: name, material, repeat, rotation, lift, spacing, kind (paint or
|
|
// surface), transition, noise, the waves of the noise, then the outline as x z pairs apart by spaces. Lines starting with # are
|
|
// left out. The patches of it follow the terrain. The files written before the kind came have the outline right after the spacing
|
|
bool load_patches(std::string const &Path, std::vector<surface_patch> &Patches)
|
|
{
|
|
std::ifstream file{Path};
|
|
if (false == file.is_open())
|
|
return false;
|
|
std::string line;
|
|
while (std::getline(file, line))
|
|
{
|
|
if (line.empty() || line[0] == '#')
|
|
continue;
|
|
if (line.back() == '\r')
|
|
line.pop_back();
|
|
std::vector<std::string> fields;
|
|
std::size_t start{0};
|
|
for (;;)
|
|
{
|
|
auto const tab{line.find('\t', start)};
|
|
fields.push_back(line.substr(start, tab == std::string::npos ? std::string::npos : tab - start));
|
|
if (tab == std::string::npos)
|
|
break;
|
|
start = tab + 1;
|
|
}
|
|
if (fields.size() < 7)
|
|
continue;
|
|
auto const kinded{fields.size() >= 11 && (fields[6] == "paint" || fields[6] == "surface")};
|
|
surface_patch patch;
|
|
patch.follow = true;
|
|
patch.name = fields[0];
|
|
patch.material = fields[1];
|
|
patch.size = std::max(0.f, static_cast<float>(std::atof(fields[2].c_str())));
|
|
patch.rotation = static_cast<float>(std::atof(fields[3].c_str()));
|
|
patch.lift = static_cast<float>(std::atof(fields[4].c_str()));
|
|
patch.spacing = std::max(0.1, std::atof(fields[5].c_str()));
|
|
if (kinded)
|
|
{
|
|
patch.paint = fields[6] == "paint";
|
|
patch.feather = std::max(0.f, static_cast<float>(std::atof(fields[7].c_str())));
|
|
patch.noise = std::max(0.f, static_cast<float>(std::atof(fields[8].c_str())));
|
|
patch.noise_scale = std::max(0.5f, static_cast<float>(std::atof(fields[9].c_str())));
|
|
}
|
|
std::istringstream points{fields[kinded ? 10 : 6]};
|
|
if (kinded && fields.size() >= 12)
|
|
patch.shape = fields[11];
|
|
if (kinded && fields.size() >= 13)
|
|
patch.grass = std::max(-2, std::atoi(fields[12].c_str()));
|
|
double x{0.0};
|
|
double z{0.0};
|
|
while (points >> x >> z)
|
|
patch.outline.emplace_back(x, z);
|
|
if (patch.outline.size() >= 3)
|
|
Patches.push_back(std::move(patch));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool save_patches(std::string const &Path, std::vector<surface_patch> const &Patches)
|
|
{
|
|
std::ofstream file{Path, std::ios::trunc};
|
|
if (false == file.is_open())
|
|
return false;
|
|
file << "# surfaces cut out of the terrain: name, material, repeat, rotation, lift, spacing, kind (paint/surface), transition, noise, waves of the noise, outline x z ... (tabs between the fields)\n";
|
|
char number[64];
|
|
for (auto const &patch : Patches)
|
|
{
|
|
if (false == patch.follow)
|
|
continue; // the ones with heights of their own stay in the manifest
|
|
file << patch.name << '\t' << patch.material << '\t' << patch.size << '\t' << patch.rotation << '\t' << patch.lift << '\t' << patch.spacing << '\t' << (patch.paint ? "paint" : "surface") << '\t'
|
|
<< patch.feather << '\t' << patch.noise << '\t' << patch.noise_scale << '\t';
|
|
for (std::size_t i = 0; i < patch.outline.size(); ++i)
|
|
{
|
|
std::snprintf(number, sizeof(number), "%s%.2f %.2f", i > 0 ? " " : "", patch.outline[i].x, patch.outline[i].y);
|
|
file << number;
|
|
}
|
|
if (false == patch.shape.empty() || patch.grass != -2)
|
|
file << '\t' << patch.shape;
|
|
if (patch.grass != -2)
|
|
file << '\t' << patch.grass;
|
|
file << '\n';
|
|
}
|
|
return static_cast<bool>(file);
|
|
}
|
|
|
|
bool surface_patch::height(double const X, double const Z, double &Height) const
|
|
{
|
|
if (columns < 2 || rows < 2 || heights.size() != static_cast<std::size_t>(columns) * rows)
|
|
return false;
|
|
auto const u{(X - origin.x) / spacing};
|
|
auto const v{(Z - origin.y) / spacing};
|
|
if (u < -1e-6 || v < -1e-6 || u > columns - 1 + 1e-6 || v > rows - 1 + 1e-6)
|
|
return false;
|
|
auto const ix{std::clamp(static_cast<int>(std::floor(u)), 0, columns - 2)};
|
|
auto const iz{std::clamp(static_cast<int>(std::floor(v)), 0, rows - 2)};
|
|
auto const fx{std::clamp(u - ix, 0.0, 1.0)};
|
|
auto const fz{std::clamp(v - iz, 0.0, 1.0)};
|
|
auto const at = [&](int const Column, int const Row) { return static_cast<double>(heights[static_cast<std::size_t>(Row) * columns + Column]); };
|
|
Height = (at(ix, iz) * (1.0 - fx) + at(ix + 1, iz) * fx) * (1.0 - fz) + (at(ix, iz + 1) * (1.0 - fx) + at(ix + 1, iz + 1) * fx) * fz + lift;
|
|
return true;
|
|
}
|
|
|
|
std::vector<std::uint32_t> triangulate(std::vector<glm::dvec2> const &Outline)
|
|
{
|
|
std::vector<std::uint32_t> result;
|
|
auto const count{Outline.size()};
|
|
if (count < 3)
|
|
return result;
|
|
// ear clipping, the outline turned counterclockwise first
|
|
double area{0.0};
|
|
for (std::size_t i = 0; i < count; ++i)
|
|
{
|
|
auto const &a{Outline[i]};
|
|
auto const &b{Outline[(i + 1) % count]};
|
|
area += a.x * b.y - b.x * a.y;
|
|
}
|
|
std::vector<std::uint32_t> ring(count);
|
|
for (std::size_t i = 0; i < count; ++i)
|
|
ring[i] = static_cast<std::uint32_t>(area >= 0.0 ? i : count - 1 - i);
|
|
auto const cross = [](glm::dvec2 const &O, glm::dvec2 const &A, glm::dvec2 const &B) { return (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x); };
|
|
auto const inside = [&](glm::dvec2 const &P, glm::dvec2 const &A, glm::dvec2 const &B, glm::dvec2 const &C) {
|
|
return cross(A, B, P) >= 0.0 && cross(B, C, P) >= 0.0 && cross(C, A, P) >= 0.0;
|
|
};
|
|
std::size_t guard{0};
|
|
while (ring.size() > 3 && guard < count * count)
|
|
{
|
|
++guard;
|
|
bool clipped{false};
|
|
for (std::size_t i = 0; i < ring.size(); ++i)
|
|
{
|
|
auto const prev{ring[(i + ring.size() - 1) % ring.size()]};
|
|
auto const curr{ring[i]};
|
|
auto const next{ring[(i + 1) % ring.size()]};
|
|
auto const &a{Outline[prev]};
|
|
auto const &b{Outline[curr]};
|
|
auto const &c{Outline[next]};
|
|
if (cross(a, b, c) <= 1e-12)
|
|
continue; // reflex or degenerate corner
|
|
bool ear{true};
|
|
for (auto const other : ring)
|
|
{
|
|
if (other == prev || other == curr || other == next)
|
|
continue;
|
|
if (inside(Outline[other], a, b, c))
|
|
{
|
|
ear = false;
|
|
break;
|
|
}
|
|
}
|
|
if (false == ear)
|
|
continue;
|
|
result.insert(result.end(), {prev, curr, next});
|
|
ring.erase(ring.begin() + i);
|
|
clipped = true;
|
|
break;
|
|
}
|
|
if (false == clipped)
|
|
break; // a self-crossing outline; what's left goes as a fan
|
|
}
|
|
for (std::size_t i = 1; i + 1 < ring.size(); ++i)
|
|
result.insert(result.end(), {ring[0], ring[i], ring[i + 1]});
|
|
return result;
|
|
}
|
|
|
|
// meshes
|
|
|
|
int max_stride(int const Cells)
|
|
{
|
|
int stride{1};
|
|
while (stride * 4 <= Cells)
|
|
stride *= 2;
|
|
return stride;
|
|
}
|
|
|
|
namespace
|
|
{
|
|
int segments(int const Cells, int const Stride)
|
|
{
|
|
return (Cells + Stride - 1) / Stride;
|
|
}
|
|
} // namespace
|
|
|
|
std::size_t mesh_vertex_count(int const Cells, int const Stride)
|
|
{
|
|
auto const n{static_cast<std::size_t>(segments(Cells, Stride))};
|
|
return (n + 1) * (n + 1) + 4 * (n + 1);
|
|
}
|
|
|
|
void build_indices(int const Cells, int const Stride, gfx::index_array &Out)
|
|
{
|
|
auto const n{static_cast<gfx::basic_index>(segments(Cells, Stride))};
|
|
auto const row{n + 1};
|
|
auto const grid{row * row};
|
|
Out.clear();
|
|
Out.reserve(static_cast<std::size_t>(n) * n * 6 + static_cast<std::size_t>(n) * 24);
|
|
auto const at = [&](gfx::basic_index const I, gfx::basic_index const J) { return J * row + I; };
|
|
for (gfx::basic_index j = 0; j < n; ++j)
|
|
for (gfx::basic_index i = 0; i < n; ++i)
|
|
{
|
|
auto const v00{at(i, j)}, v10{at(i + 1, j)}, v01{at(i, j + 1)}, v11{at(i + 1, j + 1)};
|
|
Out.insert(Out.end(), {v00, v01, v10, v11, v10, v01});
|
|
}
|
|
// skirts: south (low z) and east face the way the edge is walked turns them, north and west are turned around
|
|
auto const skirt = [&](gfx::basic_index const Skirt, auto const &Edge, bool const Flip) {
|
|
for (gfx::basic_index k = 0; k < n; ++k)
|
|
{
|
|
auto const a{Edge(k)}, b{Edge(k + 1)}, la{Skirt + k}, lb{Skirt + k + 1};
|
|
if (Flip)
|
|
Out.insert(Out.end(), {la, b, a, la, lb, b});
|
|
else
|
|
Out.insert(Out.end(), {a, b, la, b, lb, la});
|
|
}
|
|
};
|
|
skirt(grid, [&](gfx::basic_index const K) { return at(K, 0); }, false);
|
|
skirt(grid + row, [&](gfx::basic_index const K) { return at(K, n); }, true);
|
|
skirt(grid + 2 * row, [&](gfx::basic_index const K) { return at(0, K); }, true);
|
|
skirt(grid + 3 * row, [&](gfx::basic_index const K) { return at(n, K); }, false);
|
|
}
|
|
|
|
void cut_indices(chunk_data const &Chunk, int const Stride, gfx::index_array &Indices)
|
|
{
|
|
if (false == Chunk.has_holes())
|
|
return;
|
|
auto const cells{Chunk.cells};
|
|
auto const stride{std::clamp(Stride, 1, max_stride(cells))};
|
|
auto const n{segments(cells, stride)};
|
|
auto const grid{static_cast<std::size_t>(n) * n * 6};
|
|
if (Indices.size() != grid + static_cast<std::size_t>(n) * 24)
|
|
return;
|
|
auto const coordinate = [&](int const I) { return std::min(I * stride, cells); };
|
|
// the triangles of a cell cut out entirely become empty ones, so the count of the indices stays
|
|
std::vector<bool> cut(static_cast<std::size_t>(n) * n, false);
|
|
for (int j = 0; j < n; ++j)
|
|
for (int i = 0; i < n; ++i)
|
|
{
|
|
if (false == Chunk.cut_cells(coordinate(i), coordinate(j), coordinate(i + 1), coordinate(j + 1)))
|
|
continue;
|
|
cut[static_cast<std::size_t>(j) * n + i] = true;
|
|
std::fill_n(Indices.begin() + (static_cast<std::size_t>(j) * n + i) * 6, 6, Indices[(static_cast<std::size_t>(j) * n + i) * 6]);
|
|
}
|
|
// the skirts along the cells cut out would hang in the opening
|
|
auto const skirt = [&](std::size_t const Skirt, auto const &Cell) {
|
|
for (int k = 0; k < n; ++k)
|
|
if (Cell(k))
|
|
{
|
|
auto const first{grid + (Skirt * n + k) * 6};
|
|
std::fill_n(Indices.begin() + first, 6, Indices[first]);
|
|
}
|
|
};
|
|
skirt(0, [&](int const K) { return cut[K]; });
|
|
skirt(1, [&](int const K) { return cut[static_cast<std::size_t>(n - 1) * n + K]; });
|
|
skirt(2, [&](int const K) { return cut[static_cast<std::size_t>(K) * n]; });
|
|
skirt(3, [&](int const K) { return cut[static_cast<std::size_t>(K) * n + n - 1]; });
|
|
}
|
|
|
|
void build_mesh(chunk_data const &Chunk, int const Stride, glm::dvec3 const &Origin, height_source const &Outside, mesh &Out)
|
|
{
|
|
auto const cells{Chunk.cells};
|
|
auto const stride{std::clamp(Stride, 1, max_stride(cells))};
|
|
auto const n{segments(cells, stride)};
|
|
auto const spacing{Chunk.spacing()};
|
|
auto const low{Chunk.corner()};
|
|
auto const texturebase{pack_corner(pack_of(Chunk.key))};
|
|
auto const coordinate = [&](int const I) { return std::min(I * stride, cells); };
|
|
|
|
// heights of the grid points, the neighbouring chunks asked about the points past the edges
|
|
auto const sample = [&](int const Ix, int const Iz) -> double {
|
|
if (Ix >= 0 && Ix <= cells && Iz >= 0 && Iz <= cells)
|
|
return Chunk.height(Ix, Iz);
|
|
double height;
|
|
if (Outside && Outside(low.x + Ix * spacing, low.y + Iz * spacing, height))
|
|
return height;
|
|
return Chunk.height(std::clamp(Ix, 0, cells), std::clamp(Iz, 0, cells));
|
|
};
|
|
// normals from the differences over the stride, so the distant, coarse grid is lit as it's drawn
|
|
auto const normal = [&](int const Ix, int const Iz) {
|
|
auto const dx{(sample(Ix + stride, Iz) - sample(Ix - stride, Iz)) / (2.0 * stride * spacing)};
|
|
auto const dz{(sample(Ix, Iz + stride) - sample(Ix, Iz - stride)) / (2.0 * stride * spacing)};
|
|
return glm::normalize(glm::vec3(static_cast<float>(-dx), 1.f, static_cast<float>(-dz)));
|
|
};
|
|
|
|
// the skirt has to reach below the edge of a neighbour drawn with a coarser grid
|
|
double deviation{0.0};
|
|
{
|
|
auto const coarse{std::min(stride * 4, cells)};
|
|
auto const edge = [&](auto const &Height) {
|
|
for (int start = 0; start < cells; start += coarse)
|
|
{
|
|
auto const end{std::min(start + coarse, cells)};
|
|
auto const a{Height(start)}, b{Height(end)};
|
|
for (int k = start + 1; k < end; ++k)
|
|
deviation = std::max(deviation, std::abs(Height(k) - (a + (b - a) * (k - start) / double(end - start))));
|
|
}
|
|
};
|
|
edge([&](int const K) { return static_cast<double>(Chunk.height(K, 0)); });
|
|
edge([&](int const K) { return static_cast<double>(Chunk.height(K, cells)); });
|
|
edge([&](int const K) { return static_cast<double>(Chunk.height(0, K)); });
|
|
edge([&](int const K) { return static_cast<double>(Chunk.height(cells, K)); });
|
|
}
|
|
auto const skirtdepth{static_cast<float>(0.5 + 2.0 * deviation + 0.05 * stride * spacing)};
|
|
|
|
Out.vertices.clear();
|
|
Out.vertices.reserve(mesh_vertex_count(cells, stride));
|
|
glm::dvec3 lowest{std::numeric_limits<double>::max()}, highest{-std::numeric_limits<double>::max()};
|
|
auto const vertex = [&](int const Ix, int const Iz, float const Drop) {
|
|
gfx::basic_vertex v;
|
|
glm::dvec3 const world{low.x + Ix * spacing, Chunk.height(Ix, Iz), low.y + Iz * spacing};
|
|
lowest = glm::min(lowest, world);
|
|
highest = glm::max(highest, world);
|
|
v.position = glm::vec3(world - Origin);
|
|
v.position.y -= Drop;
|
|
v.normal = normal(Ix, Iz);
|
|
// texture coordinates in metres from the corner of the pack: small enough for floats, and seamless for any
|
|
// texture repeating a whole number of times over the pack. v runs along -z, the way the tangent space expects it
|
|
v.texture = glm::vec2(static_cast<float>(world.x - texturebase.x), static_cast<float>(texturebase.y - world.z));
|
|
auto const tangent{glm::vec3(1.f, 0.f, 0.f) - v.normal * v.normal.x};
|
|
v.tangent = glm::vec4(glm::length(tangent) > 1e-6f ? glm::normalize(tangent) : glm::vec3(1.f, 0.f, 0.f), 1.f);
|
|
Out.vertices.push_back(v);
|
|
};
|
|
for (int j = 0; j <= n; ++j)
|
|
for (int i = 0; i <= n; ++i)
|
|
vertex(coordinate(i), coordinate(j), 0.f);
|
|
for (int k = 0; k <= n; ++k)
|
|
vertex(coordinate(k), 0, skirtdepth);
|
|
for (int k = 0; k <= n; ++k)
|
|
vertex(coordinate(k), cells, skirtdepth);
|
|
for (int k = 0; k <= n; ++k)
|
|
vertex(0, coordinate(k), skirtdepth);
|
|
for (int k = 0; k <= n; ++k)
|
|
vertex(cells, coordinate(k), skirtdepth);
|
|
|
|
build_indices(cells, stride, Out.indices);
|
|
Out.cut = Chunk.has_holes();
|
|
if (Out.cut)
|
|
cut_indices(Chunk, stride, Out.indices);
|
|
Out.centre = (lowest + highest) * 0.5;
|
|
Out.radius = static_cast<float>(glm::length(highest - lowest) * 0.5 + skirtdepth);
|
|
Out.low = lowest - glm::dvec3{0.0, skirtdepth, 0.0};
|
|
Out.high = highest;
|
|
}
|
|
|
|
} // namespace heightmap
|