18
0
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
This commit is contained in:
Claude
2026-11-10 10:14:56 +01:00
parent 19fba58976
commit 60941f2d92
10 changed files with 2035 additions and 12 deletions

View File

@@ -18,6 +18,9 @@ http://mozilla.org/MPL/2.0/.
#include <filesystem>
#include <fstream>
#include <limits>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <yaml-cpp/yaml.h>
#include "zstd.h"
@@ -184,6 +187,35 @@ int floor_div(int const Value, int const Divisor)
return (Value >= 0 ? Value / Divisor : -((-Value + Divisor - 1) / Divisor));
}
// smooth noise of -1..1 in waves of about Scale metres, the same at the same place every time; Channel gives another one
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);
}
// 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)
{
@@ -666,14 +698,30 @@ std::optional<modifier_shape::effect> modifier_shape::evaluate(double const X, d
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 const slope{std::max(0.1, m_modifier.slope)};
auto slope{std::max(0.1, m_modifier.slope)};
auto const rounding{std::max(0.0, m_modifier.rounding)};
auto const ground{Ground};
auto const beyond{soft_max(0.0, distance - halfwidth, rounding)};
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 const bend{rounding / slope * std::clamp(beyond / std::max(rounding, 1e-6), 0.0, 1.0)};
auto const target{ground > formation ? -soft_max(-ground, -(formation + beyond / slope), bend) : soft_max(ground, formation - beyond / slope, bend)};
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};
}
@@ -747,6 +795,67 @@ bool load_manifest(std::string const &Path, manifest &Manifest, std::string *Err
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 = 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)
{
@@ -759,6 +868,20 @@ bool load_manifest(std::string const &Path, manifest &Manifest, std::string *Err
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"]})
@@ -853,6 +976,69 @@ bool save_manifest(std::string const &Path, manifest const &Manifest, std::strin
}
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;
@@ -863,6 +1049,16 @@ bool save_manifest(std::string const &Path, manifest const &Manifest, std::strin
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)
@@ -1295,6 +1491,119 @@ bool write_pack(std::string const &Path, pack_key const &Key, std::vector<chunk_
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;

View File

@@ -155,6 +155,43 @@ struct water_body
std::vector<glm::dvec2> outline; // x, z
};
// piece of the ground cut out of the terrain and drawn on its own with a material of its own (a yard, a platform of beaten earth, a
// field of other crops): the terrain is cut away within the outline, the heights it had there are kept on a grid of the piece, so it
// lies where the ground was and stays as it was cut whatever happens to the terrain later
struct surface_patch
{
std::string name;
std::string material; // empty: the first material of the palette
float size{0.f}; // metres the texture repeats at; 0: the size given by the material, or 8 m
float rotation{0.f}; // degrees the texture is turned by, counterclockwise seen from above
float lift{0.f}; // metres the surface lies over the heights kept
std::vector<glm::dvec2> outline; // x, z
glm::dvec2 origin{0.0}; // x, z of the first point of the grid
double spacing{1.0};
int columns{0}; // points along x
int rows{0}; // points along z
std::vector<float> heights; // rows of growing z
// the surface follows the terrain under it (its heights, holes or not) instead of heights of its own, on a grid of the spacing
// laid from 0; the grid and heights above are unused
bool follow{false};
// painted into the terrain instead (follow is true too): no hole and no mesh of its own, the material laid into the paint of
// the chunks, blending over Feather metres across the outline, the edge shifted by up to Noise metres in waves of Noise_scale
bool paint{false};
float feather{1.f};
float noise{0.f};
float noise_scale{4.f};
// the points it was drawn with and the options of its outline (curves, handles, along a track), for it to be outlined again as
// it was drawn; empty: the outline itself
std::string shape;
// the 3D grass under a painted surface: -2 left as it grows, -1 taken away, 0 or more: that kind of the grass palette grown there
int grass{-2};
// height kept at specified point, interpolated between the points of the grid; false out of the grid
bool height(double X, double Z, double &Height) const;
// x, z of the corners of the box around the outline
void bounds(glm::dvec2 &Low, glm::dvec2 &High) const;
};
// modifier of the terrain: a corridor along a line (axis of a track or a road) with a formation of specified
// height and width, and slopes from its edges to the ground
struct corridor_point
@@ -174,6 +211,18 @@ struct modifier
// portal of a tunnel; otherwise they go round it
bool open_start{false};
bool open_end{false};
// the slopes not drawn with a ruler: the formation and the edges of the track bed stay as designed, the slopes and their foot
// wander and roll as the earth does. All 0: the slopes as designed
struct natural_shape
{
double wander{0.0}; // metres the foot of the slopes wanders in and out by
double scale{15.0}; // about how long the waves of it are along the line, metres
double gentler{0.0}; // up to how much gentler the slopes get than designed, 0.2: up to 1:1.8 of 1:1.5 (never steeper)
double soften{0.0}; // the foot rounded off more, metres of the bend a metre of the height of the slope
double roughness{0.0}; // metres the face of the slope is bumpy by
bool any() const { return wander > 0.0 || gentler > 0.0 || soften > 0.0 || roughness > 0.0; }
};
natural_shape natural;
std::vector<corridor_point> points;
// rectangle (x, z) the modifier can change the ground in
@@ -242,10 +291,18 @@ struct manifest
bool compress{true}; // pack files store their data compressed with zstd
float spacing{default_spacing}; // point spacing of new chunks
std::vector<water_body> water;
std::vector<surface_patch> patches;
// file next to the manifest the patches are kept in, a line each (many of them, as cut along tracks, load faster so); empty:
// in the manifest, unless there are more than patches_inline
std::string patches_file;
std::vector<modifier> modifiers;
std::vector<grass_def> grass; // the grass palette
};
// past this many the patches are saved to their own file (manifest::patches_file)
std::size_t constexpr patches_inline{200};
bool load_patches(std::string const &Path, std::vector<surface_patch> &Patches);
bool save_patches(std::string const &Path, std::vector<surface_patch> const &Patches);
bool load_manifest(std::string const &Path, manifest &Manifest, std::string *Error = nullptr);
bool save_manifest(std::string const &Path, manifest const &Manifest, std::string *Error = nullptr);