18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 19:57:06 +01:00
Files
maszyna/tests/unit/test_eu07m_edit.cpp
T
maj00r 6828e52a45 feat(editor): the EU07M models as a field of work of their own, made for those who don't know the format
The window of an EU07M model becomes the field of work "Models" (F7) beside the surroundings, the tracks,
the roads, the terrain and the catenary. The node bank says what is edited and lists the vehicles, the tool
options hold the page of the tool chosen in the toolbar (Lights, Parts of the vehicle, All parts,
Animations), and the inspector the settings of the part picked, in plain words; the fields of the format
are kept under Advanced.

- A click on the model picks its part (the smallest box of geometry of a part shown holding the point);
  a light is added where the model is clicked, a spot shining towards the camera.
- A light which nothing else points at is taken out (eu07m::node_removable / remove_node, with
  TModel3d::UnlinkEditorSubmodel / LinkEditorSubmodel on the model the game has).
- Ctrl+Z / Ctrl+Y take the changes of the model back and make them again, a light added or taken out
  among them; another model asked for with changes not saved asks first.
- "Import to EU07M" writes <model>.eu07m beside the .e3d or .t3d of a model without one, made of the
  model as the game loaded it (eu07m::import_model), so a lamp post or a cab gets its lights here.
- "Put a model in the scenery" puts a model of the folder of the models in front of the camera, "Whole
  model" moves the scenery model with the gizmo of the editor, "Put a vehicle on a track..." opens the
  Trainset tool.
- The inspector of a scenery model with an .eu07m file offers "Edit this model (F7)"; the floating window
  is gone.
- Review: the model is written to its .tmp, closed and checked before it replaces the file, and a failed
  .tmp is removed; play_clip counts From only for a positive pace again, so a pace of 0 holds the clip
  and a negative one runs it backwards.

New icons at the end of editor_icons.png (the cells before them are unchanged), the strings in
lang/editor_pl.po, new self-test commands (modelmode, modeltool, modeladdlight, modelundo, modelredo,
modelremove, modelplace, modelimport) and unit tests of remove_node. No asset in the SVN is needed or
changed; the import writes an .eu07m file into the game data only when the user asks for it.
2026-11-29 22:17:14 +01:00

160 lines
6.2 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "model/eu07m/eu07m_build.h"
#include "model/eu07m/eu07m_io.h"
#include <cstring>
#include <vector>
namespace {
// root (0) { lamp (1), door (2) }, rear (3); a light slot of the root lists the door and the rear node, a joint is aimed at the
// rear node and a clip moves it
eu07m::model lamp_model() {
eu07m::model model;
auto const node = [&model](std::string_view const Name, std::int32_t const Parent, std::uint32_t const End) {
eu07m::node_record record;
record.name = model.intern(Name);
record.parent = Parent;
record.subtree_end = End;
model.nodes.push_back(record);
};
node("root", -1, 3);
node("lamp", 0, 2);
node("door", 0, 3);
node("rear", -1, 4);
eu07m::payload::light_slot slot;
slot.on_count = 1;
slot.off_count = 1;
std::vector<std::byte> data(sizeof(slot) + 2 * sizeof(std::uint32_t));
std::memcpy(data.data(), &slot, sizeof(slot));
std::uint32_t const lit{2};
std::uint32_t const dark{3};
std::memcpy(data.data() + sizeof(slot), &lit, sizeof(lit));
std::memcpy(data.data() + sizeof(slot) + sizeof(lit), &dark, sizeof(dark));
eu07m::add_component(model, eu07m::component::light_slot, eu07m::target_kind::node, 0, data);
eu07m::payload::joint joint;
std::vector<std::byte> jointdata(sizeof(joint));
std::memcpy(jointdata.data(), &joint, sizeof(joint));
eu07m::add_component(model, eu07m::component::joint, eu07m::target_kind::node, 3, jointdata);
eu07m::clip_record clip;
clip.name = model.intern("open");
clip.track_first = 0;
clip.track_count = 1;
model.clips.push_back(clip);
eu07m::track_record track;
track.node = 3;
track.key_count = 1;
model.clip_tracks.push_back(track);
model.key_times.push_back(0.f);
model.key_vectors.push_back({0.f, 0.f, 0.f});
eu07m::build_name_index(model);
return model;
}
std::uint32_t listed_node(eu07m::model const &Model, std::uint32_t const Component, std::size_t const Item) {
std::uint32_t node{0};
std::memcpy(&node, Model.component_data.data() + Model.components[Component].data_offset + sizeof(eu07m::payload::light_slot) + Item * sizeof(node), sizeof(node));
return node;
}
} // namespace
TEST_CASE("eu07m: a node put in under a parent moves the references to the nodes after it") {
auto model{lamp_model()};
REQUIRE(eu07m::validate(model).empty());
eu07m::node_record light;
light.name = model.intern("light_1");
auto const index{eu07m::insert_node(model, 0, light)};
CHECK(index == 3);
REQUIRE(model.nodes.size() == 5);
// the root holds it, the lamp and the door keep their subtrees, the rear node moves on
CHECK(model.nodes[0].subtree_end == 4);
CHECK(model.nodes[1].subtree_end == 2);
CHECK(model.nodes[2].subtree_end == 3);
CHECK(model.nodes[3].parent == 0);
CHECK(model.nodes[3].subtree_end == 4);
CHECK(model.string(model.nodes[4].name) == "rear");
CHECK(model.nodes[4].parent == -1);
CHECK(model.nodes[4].subtree_end == 5);
// the joint, the track and the light slot follow the rear node; the door stays
CHECK(model.components[1].target == 4);
CHECK(model.clip_tracks[0].node == 4);
CHECK(listed_node(model, 0, 0) == 2);
CHECK(listed_node(model, 0, 1) == 4);
CHECK(eu07m::find_nodes(model, "light_1") == std::vector<std::uint32_t>{3});
CHECK(eu07m::find_nodes(model, "rear") == std::vector<std::uint32_t>{4});
CHECK(eu07m::validate(model).empty());
}
TEST_CASE("eu07m: a node put in without a parent goes last, and a component added is listed by it") {
auto model{lamp_model()};
eu07m::node_record light;
light.name = model.intern("light_2");
auto const index{eu07m::insert_node(model, -1, light)};
CHECK(index == 4);
CHECK(model.nodes[3].subtree_end == 4);
CHECK(model.nodes[4].subtree_end == 5);
eu07m::payload::light payload;
std::vector<std::byte> data(sizeof(payload));
std::memcpy(data.data(), &payload, sizeof(payload));
auto const component{eu07m::add_component(model, eu07m::component::light, eu07m::target_kind::node, index, data)};
CHECK(model.nodes[index].component_first == component);
CHECK(model.nodes[index].component_count == 1);
CHECK(model.components[component].data_offset % 4 == 0);
CHECK(eu07m::validate(model).empty());
// the file written and read again holds the same
eu07m::model read;
REQUIRE(eu07m::read(eu07m::write(model), read).ok);
CHECK(read.nodes.size() == 5);
CHECK(read.components.size() == 3);
}
TEST_CASE("eu07m: a light put in and taken out again leaves the model as it was") {
auto model{lamp_model()};
auto const before{model.nodes};
eu07m::node_record light;
light.name = model.intern("light_3");
auto const index{eu07m::insert_node(model, 0, light)};
eu07m::payload::light payload;
std::vector<std::byte> data(sizeof(payload));
std::memcpy(data.data(), &payload, sizeof(payload));
eu07m::add_component(model, eu07m::component::light, eu07m::target_kind::node, index, data);
REQUIRE(eu07m::node_removable(model, index));
REQUIRE(eu07m::remove_node(model, index));
REQUIRE(model.nodes.size() == before.size());
for (std::size_t idx = 0; idx < before.size(); ++idx) {
CHECK(model.nodes[idx].parent == before[idx].parent);
CHECK(model.nodes[idx].subtree_end == before[idx].subtree_end);
CHECK(model.nodes[idx].name == before[idx].name);
}
// the components of the light are gone, the others point where they did
REQUIRE(model.components.size() == 2);
CHECK(model.components[1].target == 3);
CHECK(model.clip_tracks[0].node == 3);
CHECK(listed_node(model, 0, 0) == 2);
CHECK(listed_node(model, 0, 1) == 3);
CHECK(eu07m::find_nodes(model, "rear") == std::vector<std::uint32_t>{3});
CHECK(eu07m::validate(model).empty());
}
TEST_CASE("eu07m: a node with children, or one something points at, isn't taken out") {
auto model{lamp_model()};
auto const nodes{model.nodes.size()};
// the root has children, the door is listed by the light slot, the rear node is moved by a clip
CHECK_FALSE(eu07m::remove_node(model, 0));
CHECK_FALSE(eu07m::remove_node(model, 2));
CHECK_FALSE(eu07m::remove_node(model, 3));
CHECK_FALSE(eu07m::remove_node(model, 9));
CHECK(model.nodes.size() == nodes);
// the lamp is a leaf nothing points at
CHECK(eu07m::remove_node(model, 1));
CHECK(model.nodes.size() == nodes - 1);
CHECK(model.nodes[0].subtree_end == 2);
CHECK(listed_node(model, 0, 0) == 1);
CHECK(listed_node(model, 0, 1) == 2);
CHECK(eu07m::validate(model).empty());
}