18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 10:37:07 +01:00
Files
maszyna/tests/unit/test_eu07m_edit.cpp
T
maj00r 3f206105b8 fix(editor): a part taken out keeps the references after it; holds and the cab end with the editor
After review of #551:

- The components the data of others points at, which a removal moves back, are all of them now: besides the bindings of a
  channel and the light sources of a light slot, the sockets of a spring (socket_a, socket_b), of the ends of a chain (end_a,
  end_b) and of a model reference, and the properties whose value is a component. A node whose component one of these points
  at isn't taken out; what the node's own components point at among themselves doesn't keep it.
- The items of the vertex groups of other meshes, absolute vertex indices, move back over the vertices cut out with a part
  (the first vertex of each pair of a group of ranges).
- A component aimed at a mesh moves back with the meshes after a removed one, and a mesh a component is aimed at keeps its node.
- The holds of the model panel on the lamps, the hooks and the pantographs are let go of when the editor is left and when the
  panel turns to another vehicle: a pantograph held down no longer cuts the power of the vehicle driven after F11.
- "Get in the cab" no longer changes the vehicle the player drives: the one driven before is driven again as the editor is left.
- The window of a model that can be imported says that a later update of the .e3d or .t3d doesn't reach the game while the
  .eu07m is there.
- A model file with a space in its name isn't put in: the scenery line would read it as another.

Unit tests for the vertex groups, the mesh targets and the sockets in the data of the components.
2026-11-30 21:27:36 +01:00

309 lines
13 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());
}
namespace {
// root (0) { decal (1), lamp (2) }: the decal and the lamp have a triangle of their own each, the lamp's after the decal's in the
// streams; a vertex has a position and its attributes
eu07m::model geometry_model() {
eu07m::model model;
auto const node = [&model](std::string_view const Name, std::int32_t const Parent, std::uint32_t const End, std::int32_t const Mesh) {
eu07m::node_record record;
record.name = model.intern(Name);
record.parent = Parent;
record.subtree_end = End;
record.mesh = Mesh;
model.nodes.push_back(record);
};
node("root", -1, 3, -1);
node("decal", 0, 2, 0);
node("lamp", 0, 3, 1);
for (std::uint32_t mesh = 0; mesh < 2; ++mesh) {
eu07m::mesh_record record;
record.primitive_first = mesh;
record.primitive_count = 1;
model.meshes.push_back(record);
eu07m::primitive_record primitive;
primitive.first_index = mesh * 3;
primitive.index_count = 3;
primitive.base_vertex = mesh * 3;
primitive.vertex_count = 3;
primitive.node = mesh + 1;
primitive.flags = static_cast<std::uint32_t>(eu07m::topology::triangles) | eu07m::primitive_flags::indexed | eu07m::primitive_flags::opaque;
model.primitives.push_back(primitive);
for (std::uint16_t vertex = 0; vertex < 3; ++vertex) {
model.positions.push_back({static_cast<float>(mesh), static_cast<float>(vertex), 0.f});
model.vertex_attributes.emplace_back();
model.indices16.push_back(vertex);
}
}
eu07m::build_name_index(model);
return model;
}
} // namespace
TEST_CASE("eu07m: a part taken out takes its geometry, and the parts after it move into its place") {
auto model{geometry_model()};
REQUIRE(eu07m::validate(model).empty());
REQUIRE(eu07m::node_removable(model, 1));
REQUIRE(eu07m::remove_node(model, 1));
REQUIRE(model.nodes.size() == 2);
CHECK(model.string(model.nodes[1].name) == "lamp");
CHECK(model.nodes[0].subtree_end == 2);
REQUIRE(model.meshes.size() == 1);
CHECK(model.nodes[1].mesh == 0);
REQUIRE(model.primitives.size() == 1);
CHECK(model.meshes[0].primitive_first == 0);
CHECK(model.primitives[0].base_vertex == 0);
CHECK(model.primitives[0].first_index == 0);
CHECK(model.primitives[0].node == 1);
// the lamp's vertices are the ones left
REQUIRE(model.positions.size() == 3);
CHECK(model.positions[0][0] == 1.f);
CHECK(model.vertex_attributes.size() == 3);
CHECK(model.indices16.size() == 3);
CHECK(eu07m::validate(model).empty());
eu07m::model read;
REQUIRE(eu07m::read(eu07m::write(model), read).ok);
CHECK(read.primitives.size() == 1);
}
TEST_CASE("eu07m: a part whose mesh another part draws too isn't taken out") {
auto model{geometry_model()};
model.nodes[2].mesh = 0;
CHECK_FALSE(eu07m::node_removable(model, 1));
CHECK_FALSE(eu07m::remove_node(model, 1));
CHECK(model.primitives.size() == 2);
}
TEST_CASE("eu07m: the vertex groups of a mesh after a part taken out follow its vertices") {
auto model{geometry_model()};
// two groups of the lamp's mesh: vertices 3 and 5, and the range of its three vertices from 3
model.vertex_group_items = {3, 5, 3, 3};
model.vertex_groups.push_back({model.intern("tip"), 1, 0, 2, 0, 0, 0, 0});
model.vertex_groups.push_back({model.intern("whole"), 1, 2, 1, eu07m::vertex_group_flags::ranges, 0, 0, 0});
REQUIRE(eu07m::validate(model).empty());
REQUIRE(eu07m::remove_node(model, 1));
REQUIRE(model.positions.size() == 3);
CHECK(model.vertex_groups[0].mesh == 0);
CHECK(model.vertex_group_items == std::vector<std::uint32_t>{0, 2, 0, 3});
CHECK(eu07m::validate(model).empty());
}
TEST_CASE("eu07m: a component aimed at a mesh keeps its mesh, and keeps it from being taken out") {
auto model{geometry_model()};
auto const collision{eu07m::add_component(model, eu07m::component::collision, eu07m::target_kind::mesh, 1, eu07m::payload_writer{}.put(eu07m::payload::collision{}).data())};
// the lamp's mesh is aimed at: the lamp stays
CHECK_FALSE(eu07m::node_removable(model, 2));
// the decal's mesh goes, the one aimed at moves back into its place
REQUIRE(eu07m::remove_node(model, 1));
CHECK(model.components[collision].target == 0);
CHECK(model.nodes[1].mesh == 0);
CHECK(eu07m::validate(model).empty());
}
TEST_CASE("eu07m: the sockets the data of the components points at follow a component taken out before them") {
auto model{geometry_model()};
// the decal has a light (component 0), the lamp a socket (1); a spring, a chain, a model reference and a property of the root
// point at the socket
eu07m::add_component(model, eu07m::component::light, eu07m::target_kind::node, 1, eu07m::payload_writer{}.put(eu07m::payload::light{}).data());
auto const socket{eu07m::add_component(model, eu07m::component::socket, eu07m::target_kind::node, 2, eu07m::payload_writer{}.put(eu07m::payload::socket{}).data())};
REQUIRE(socket == 1);
eu07m::payload::spring spring;
spring.anchor_a = 0;
spring.socket_a = socket;
spring.anchor_b = 0;
auto const springs{eu07m::add_component(model, eu07m::component::spring, eu07m::target_kind::node, 0, eu07m::payload_writer{}.put(spring).data())};
eu07m::payload::chain chain;
chain.end_b = socket;
auto const chains{eu07m::add_component(model, eu07m::component::chain, eu07m::target_kind::model, 0, eu07m::payload_writer{}.put(chain).data())};
eu07m::payload::model_reference reference;
reference.path = model.intern("dynamic/x/hose");
reference.socket = socket;
auto const references{eu07m::add_component(model, eu07m::component::model_reference, eu07m::target_kind::model, 0, eu07m::payload_writer{}.put(reference).data())};
eu07m::payload::property property;
property.key = model.intern("plug");
property.type = eu07m::payload::value_type::component;
property.value[0] = socket;
auto const properties{eu07m::add_component(model, eu07m::component::properties, eu07m::target_kind::model, 0, eu07m::payload_writer{}.put(std::uint32_t{1}).put(property).data())};
REQUIRE(eu07m::validate(model).empty());
// the lamp's socket is pointed at: the lamp stays
CHECK_FALSE(eu07m::node_removable(model, 2));
// the decal goes with its light: the socket is component 0 now, and so is what points at it
REQUIRE(eu07m::remove_node(model, 1));
auto const read = [&model]<typename Payload_>(std::uint32_t const Component, Payload_ &Payload) {
auto const &record{model.components[Component]};
std::memcpy(&Payload, model.component_data.data() + record.data_offset, sizeof(Payload));
};
read(springs - 1, spring);
CHECK(spring.socket_a == 0);
CHECK(spring.socket_b == eu07m::no_index);
read(chains - 1, chain);
CHECK(chain.end_a == eu07m::no_index);
CHECK(chain.end_b == 0);
read(references - 1, reference);
CHECK(reference.socket == 0);
eu07m::payload::property moved;
std::memcpy(&moved, model.component_data.data() + model.components[properties - 1].data_offset + sizeof(std::uint32_t), sizeof(moved));
CHECK(moved.value[0] == 0);
CHECK(eu07m::validate(model).empty());
}