18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 17:37:09 +01:00

test(editor): golden round trips of the tracks and of the //$ lines, the cant at the joints

The safety net of the track editor before its domain model changes (E0 of
editor-tracks-tests.md): what the editor reads from the scenery files and
writes back is read, written and read again in tests/unit, with no game data.

Tracks (test_track_text.cpp, data/tracks/). Entries written the way the game
writes them, read by TTrack::Load() and written by export_as_text(), come out
byte for byte the same, and read again give the same track field by field:
plain tracks with all their attributes (events, speed, isolated, overhead,
vradius, rail profile, friction cell), a curve with cant, radius, uneven,
buckle, cwr and sleeper models, a bridge, switches with trackbed, frogs,
movable nose, blades, uneven of both paths and timbers, a turntable, paths of
pieces, the speeds +40, -40, 0.5, 0 and none of tracks, switches and
turntables, a river, a tributary, a road and a crossing. What the game writes
otherwise than given is checked as it is (table as turn, mountain as
mountains, the length of the path, a switch heard from 10 m, the events in
order, names in lower case, model paths with forward slashes, the texture
length, rolls as floats, no overhead and no vradius left out).

The seam: TTrack built from text with no scene. The engine stubs of the tests
had none of it, so the tests link the few sources reading and writing a track
takes (world/Track.cpp, Segment.cpp, TrackUneven.cpp, MemCell.cpp,
scene/scenenode.cpp, utilities/Timer.cpp, rendering/geometrybank.cpp) and
track_stubs.cpp stands in for the rest: a renderer keeping the materials by
their names, and the scene, events, vehicles and the geometry of the rails,
which reading and writing never reach and which stop the test with their name
if one does. It tests the game's own Load() and export, where a parser of its
own would have meant rewriting Load(), and changes no code of the game. The
stub of basic_node::radius_() goes, scenenode.cpp brings the real one. The
rails don't buckle in the tests (TrackBuckle.cpp needs the environment), the
buckle declared is read and written either way; material names go through a
stand-in, the game's material manager isn't tested here.

The //$ lines (test_editor_records.cpp, data/records/). Their readers and
writers were members of editor_mode, srk_tool and the stores, with the scene
around them; they move as they were into editor/editorRecords.cpp, the one
place each kind is read and written, which the stores call (and which the
migration to the project file of E3 will need apart anyway). The methods of
scene::layer_context move into scene/scenelayercontext.cpp so it links alone.
Each kind, h r k p t w b s c o z, read from golden lines and written back comes
out the same, in the coordinates of the file and of a file placed at an
origin; //$e, the descriptions of the templates, read and saved by
editor_includes in a template in a scratch folder, leaves the file as it was.

Cant (test_cant.cpp). The paths an alignment lays roll by asin(cant/gauge),
signed with the turn, back to the cant of the line at every end; a curve the
other way rolls the other way. At a joint editor_track::match_rolls() gives
the joined track the roll for the travel across it, turned where the paths
face or stand back to back, and the track written keeps it. The inspector's
own conversion on 1500 mm stays in its lambdas here; named, as
editor_mode::cant_of_roll()/roll_of_cant(), it comes with the quick fixes.

Known bugs found, tests of the change each (doctest::should_fail, passing
until fixed, then reported to drop the decorator), data/tracks/known_lost.scn:
- angle1/angle2 are read and dropped: not written back.
- fouling1/fouling2 are read and dropped: not written back.
- name@circuit: the circuit is taken out of the name with the letter before
  the @ ("tor1@iso1" becomes track "tor"; substr(0, i - 1)).
- the attributes of the entry (gauge, friction, texture sizes, speed...) are
  written to 6 significant digits, a float has 9: more digits are lost on the
  first save.
None in the //$ lines.

Unit tests: 957 cases before, 974 after, all passing.
This commit is contained in:
maj00r
2026-11-23 09:09:21 +01:00
parent c7afa3ca10
commit e27d3b7e73
42 changed files with 2378 additions and 832 deletions
+25 -33
View File
@@ -9,6 +9,7 @@ http://mozilla.org/MPL/2.0/.
#include "stdafx.h"
#include "application/editormode.h"
#include "editor/editorRecords.hpp"
#include "application/editorprojection.h"
#include "application/editormodeshared.h"
#include "editor/editorFormat.hpp"
@@ -660,27 +661,22 @@ struct bindable_objects
}
};
// format of the lines, coordinates in the space of the file:
// <kind> <category> <x y z of the object> <file or name, - if none> <turns 0/1> <anchor count> { <path> <start x y z> <end x y z> }
// the object is the model, the include (position from its parameters), the traction piece (its first point), the memory
// cell or the event launcher found at the location; the anchors name the paths by their ends
// the object of the line, the model, the include (position from its parameters), the traction piece (its first point), the memory
// cell or the event launcher found at the location, bound to the paths named by their ends; the lines and their format:
// editor_records::read_binding() and binding_line()
std::optional<infra::binding> read_binding(std::string const &Text, scene::layer_context const &Context, bindable_objects const &Objects)
{
double const tolerance{0.05};
std::istringstream line{Text};
std::string kindname;
std::string key;
int group{0};
int turns{0};
int count{0};
glm::dvec3 location;
line >> kindname >> group >> location.x >> location.y >> location.z >> key >> turns >> count;
if (line.fail() || count < 1 || count > 4)
auto const read{editor_records::read_binding(Text, Context)};
if (false == read.has_value())
return std::nullopt;
location = Context.to_world(location);
auto const &kindname{read->kind};
auto const &key{read->key};
auto const location{read->location};
auto const count{static_cast<int>(read->anchors.size())};
infra::binding binding;
binding.group = static_cast<infra::category>(std::clamp(group, 0, static_cast<int>(infra::category::environment)));
binding.turns = (turns != 0);
binding.group = static_cast<infra::category>(std::clamp(read->category, 0, static_cast<int>(infra::category::environment)));
binding.turns = read->turns;
auto found{false};
if (kindname == infra::name(infra::kind::model))
{
@@ -719,18 +715,15 @@ std::optional<infra::binding> read_binding(std::string const &Text, scene::layer
return std::nullopt;
for (int i = 0; i < count && found; ++i)
{
int path{0};
glm::dvec3 start;
glm::dvec3 end;
line >> path >> start.x >> start.y >> start.z >> end.x >> end.y >> end.z;
start = Context.to_world(start);
end = Context.to_world(end);
auto const path{read->anchors[i].path};
auto const &start{read->anchors[i].start};
auto const &end{read->anchors[i].end};
auto const track{Objects.paths.find(start, tolerance, [&](TTrack *Track) {
return path >= 0 && path < static_cast<int>(Track->m_paths.size()) &&
glm::distance(Track->m_paths[path].points[segment_data::point::start], start) <= tolerance &&
glm::distance(Track->m_paths[path].points[segment_data::point::end], end) <= tolerance;
})};
if (line.fail() || false == track.has_value())
if (false == track.has_value())
{
found = false;
break;
@@ -816,24 +809,23 @@ void editor_mode::infra_store()
layer = scene::Layers.active();
if (false == scene::Layers.valid(layer))
layer = 1;
auto const &context{scene::Layers.layer(layer).context_insert()};
auto const local = [&](glm::dvec3 const &Point) {
auto const point{context.to_local(Point)};
return format("%.4f %.4f %.4f", point.x, point.y, point.z);
};
auto text{std::string{infra::name(binding.type)} + ' ' + std::to_string(static_cast<int>(binding.group)) + ' ' + local(points.front()) + ' ' + object_key(binding) + ' ' +
(binding.turns ? "1 " : "0 ") + std::to_string(binding.anchors.size())};
editor_records::binding line;
line.kind = infra::name(binding.type);
line.category = static_cast<int>(binding.group);
line.location = points.front();
line.key = object_key(binding);
line.turns = binding.turns;
for (auto const &anchor : binding.anchors)
{
auto const &path{anchor.at.track->m_paths[anchor.at.path]};
text += ' ' + std::to_string(anchor.at.path) + ' ' + local(path.points[segment_data::point::start]) + ' ' + local(path.points[segment_data::point::end]);
line.anchors.push_back({anchor.at.path, path.points[segment_data::point::start], path.points[segment_data::point::end]});
}
lines[layer].push_back(std::move(text));
lines[layer].push_back(editor_records::binding_line(line, scene::Layers.layer(layer).context_insert()));
}
for (auto &entry : lines)
{
if (false == entry.second.empty())
entry.second.insert(entry.second.begin(), "# objects bound to the paths in the track editor, they follow changes of the track; the simulation skips these lines");
entry.second.insert(entry.second.begin(), editor_records::binding_heading());
scene::Layers.mark(entry.first, kBindingMark, std::move(entry.second));
}
}