diff --git a/application/editormode.cpp b/application/editormode.cpp index 7531d5d2b..2afaaa5cb 100644 --- a/application/editormode.cpp +++ b/application/editormode.cpp @@ -5866,6 +5866,8 @@ bool editor_mode::enter_cab(TDynamicObject &Vehicle) } simulation::Trains.insert(train); } + if (false == m_driven_before_cab.has_value()) + m_driven_before_cab = (simulation::Train != nullptr) ? simulation::Train->Dynamic()->name() : std::string{}; simulation::Train = train; if ((Vehicle.mdKabina == nullptr) || (Vehicle.mdKabina == Vehicle.mdModel)) return false; @@ -5888,6 +5890,12 @@ void editor_mode::place_model_file(std::string const &File, std::string const &P glm::dvec3 ahead{-view[0][2], 0.0, -view[2][2]}; ahead = glm::length(ahead) > 1e-6 ? glm::normalize(ahead) : glm::dvec3{0.0, 0.0, 1.0}; auto const where{placement_on_ground(Camera.Pos + ahead * 12.0)}; + // a name in a line of the scenery ends at a space: a file with one would be read as another + if (File.find_first_of(" \t") != std::string::npos) + { + ui()->set_status(format(STR_C("The model %s can't be put in: a scenery doesn't take a space in the name of a file"), File.c_str()), true); + return; + } auto *placed{simulation::State.create_model("node -1 0 none model 0 0 0 0 " + File + " none " + (Parameters.empty() ? std::string{} : Parameters + " ") + "endmodel", "editor_", where)}; if (placed == nullptr) { @@ -7807,6 +7815,14 @@ void editor_mode::enter() void editor_mode::exit() { + // a pantograph held down by the model panel would cut the power of the vehicle driven next, one held up would rise without air + ui()->model_panel().release_holds(); + // the vehicle driven before a cab was entered in the editor is the player's again + if (m_driven_before_cab.has_value()) + { + simulation::Train = m_driven_before_cab->empty() ? nullptr : simulation::Trains.find(*m_driven_before_cab); + m_driven_before_cab.reset(); + } m_ride.leave(); track_sleepers::lay_in_background(true); // the driver's view has no menu to switch the drawn cursor off: the system one there diff --git a/application/editormode.h b/application/editormode.h index 0b6620e1b..498f58ba3 100644 --- a/application/editormode.h +++ b/application/editormode.h @@ -876,6 +876,9 @@ class editor_mode : public application_mode, private editor_track::observer, pri // a model of the folder of the models put in the scenery and selected; Parameters go after its skin, such as "lights 3" void place_model_file(std::string const &File, std::string const &Parameters = {}); bool enter_cab(TDynamicObject &Vehicle); // the cab of the vehicle loaded as by getting in, the camera in the seat of the driver + // the vehicle driven before the first "Get in the cab" of the editor (its name; empty when none was), driven again as the editor + // is left: the cab entered is the editor's, not the player's + std::optional m_driven_before_cab; bool m_cabview{false}; // the cab of the vehicle driven is drawn, as from the seat of the driver float m_terrain_spacing{2.0f}; // point spacing of the chunks made and of the spacing tool float m_terrain_baseheight{0.0f}; // height of chunks made where there's no ground diff --git a/application/editoruimodel.cpp b/application/editoruimodel.cpp index f71da8046..b1d0bd79e 100644 --- a/application/editoruimodel.cpp +++ b/application/editoruimodel.cpp @@ -907,8 +907,21 @@ eu07m_model_panel::target eu07m_model_panel::wanted(scene::basic_node *Selected) return result; } +void eu07m_model_panel::release_holds() const +{ + if ((m_target.vehicle != nullptr) && vehicle_exists(m_target.vehicle)) + { + m_target.vehicle->release_holds(); + } +} + void eu07m_model_panel::open(target const &Target) { + // the vehicle shown before has its parts and pantographs back: a hold is only for the vehicle in the panel + if (Target.vehicle != m_target.vehicle) + { + release_holds(); + } m_target = Target; m_pending.reset(); m_kept.reset(); @@ -1287,6 +1300,11 @@ void eu07m_model_panel::render_header() { ImGui::SetTooltip("%s", STR_C("Writes an .eu07m file beside the model, made of the model as the game has it. From then on the game loads the model from it, in every scenery")); } + // the game doesn't compare the .eu07m with the file it was made of: an update of that one is shadowed by it + if (m_importable) + { + ImGui::TextDisabled("%s", STR_C("While the .eu07m is there, a later update of the .e3d or .t3d doesn't reach the game: import it again, or delete the .eu07m")); + } if (false == m_message.empty()) { ImGui::TextWrapped("%s", m_message.c_str()); diff --git a/application/editoruimodel.h b/application/editoruimodel.h index 4aa4e2c68..b92f065a4 100644 --- a/application/editoruimodel.h +++ b/application/editoruimodel.h @@ -72,6 +72,9 @@ class eu07m_model_panel tool current_tool() const { return m_tool; } void choose_tool(tool Tool); bool has_vehicle() const { return m_target.vehicle != nullptr; } + // the parts and the pantographs held on the vehicle shown let go of: the editor is left, and the simulation goes on with the + // vehicle as it sets them itself + void release_holds() const; bool has_animations() const; bool marks_shown() const { return m_marks; } void show_marks(bool Shown) { m_marks = Shown; } diff --git a/lang/editor_pl.po b/lang/editor_pl.po index e9673d1b0..b51015ea3 100644 --- a/lang/editor_pl.po +++ b/lang/editor_pl.po @@ -17089,3 +17089,9 @@ msgstr "Tej części nie da się usunąć: ma pod sobą inne części albo coś msgid "Removed {}. Ctrl+Z brings it back" msgstr "Usunięto {}. Ctrl+Z ją przywróci" + +msgid "While the .eu07m is there, a later update of the .e3d or .t3d doesn't reach the game: import it again, or delete the .eu07m" +msgstr "Dopóki jest plik .eu07m, późniejsza aktualizacja pliku .e3d lub .t3d nie dociera do gry: zaimportuj model ponownie albo usuń plik .eu07m" + +msgid "The model %s can't be put in: a scenery doesn't take a space in the name of a file" +msgstr "Nie można wstawić modelu %s: sceneria nie przyjmuje spacji w nazwie pliku" diff --git a/model/eu07m/eu07m_build.cpp b/model/eu07m/eu07m_build.cpp index 07a6e8010..2dab0498d 100644 --- a/model/eu07m/eu07m_build.cpp +++ b/model/eu07m/eu07m_build.cpp @@ -363,7 +363,8 @@ template void visit_index_list(std::span const Data } } -template void visit_properties(std::span const Data, Visit_ const &Visit) { +// the values of the properties of the type (a node, a component), each given to Visit, which may change it +template void visit_properties(std::span const Data, payload::value_type const Type, Visit_ const &Visit) { std::uint32_t count{0}; if (false == read_at(Data, 0, count)) return; @@ -372,7 +373,7 @@ template void visit_properties(std::span const Data payload::property property; if (false == read_at(Data, offset, property)) return; - if (property.type != payload::value_type::node) + if (property.type != Type) continue; Visit(property.value[0]); write_at(Data, offset, property); @@ -421,7 +422,7 @@ std::span payload_of(model &Model, component_record const &Record) { template void visit_payload_nodes(model &Model, component_record const &Record, Visit_ const &Visit) { auto const data{payload_of(Model, Record)}; switch (Record.type) { - case component::properties: visit_properties(data, Visit); break; + case component::properties: visit_properties(data, payload::value_type::node, Visit); break; case component::spring: visit_spring(data, Visit); break; case component::chain: visit_chain(data, Visit); break; case component::variants: visit_variants(data, Visit); break; @@ -430,27 +431,71 @@ template void visit_payload_nodes(model &Model, component_reco } } -// the components the data of the component points at, each given to Visit, which may change it: the targets of the bindings of a -// channel other than clips, and the light sources of a light slot -template void visit_payload_components(model &Model, component_record const &Record, Visit_ const &Visit) { - auto const data{payload_of(Model, Record)}; - if (Record.type == component::light_slot) { - if (payload::light_slot slot; read_at(data, 0, slot)) - visit_index_list(data, sizeof(slot) + (std::size_t{slot.on_count} + slot.off_count) * sizeof(std::uint32_t), slot.light_count, Visit); +// the light sources of a light slot +template void visit_slot_lights(std::span const Data, Visit_ const &Visit) { + if (payload::light_slot slot; read_at(Data, 0, slot)) + visit_index_list(Data, sizeof(slot) + (std::size_t{slot.on_count} + slot.off_count) * sizeof(std::uint32_t), slot.light_count, Visit); +} + +// the sockets of the two ends of a spring +template void visit_spring_sockets(std::span const Data, Visit_ const &Visit) { + payload::spring spring; + if (false == read_at(Data, 0, spring)) return; - } + Visit(spring.socket_a); + Visit(spring.socket_b); + write_at(Data, 0, spring); +} + +// the sockets of the two ends of a chain +template void visit_chain_ends(std::span const Data, Visit_ const &Visit) { + payload::chain chain; + if (false == read_at(Data, 0, chain)) + return; + Visit(chain.end_a); + Visit(chain.end_b); + write_at(Data, 0, chain); +} + +// the socket a model reference is put in at +template void visit_reference_socket(std::span const Data, Visit_ const &Visit) { + payload::model_reference reference; + if (false == read_at(Data, 0, reference)) + return; + Visit(reference.socket); + write_at(Data, 0, reference); +} + +// the targets of the bindings of a channel, those of the clips aside +template void visit_channel_targets(std::span const Data, Visit_ const &Visit) { payload::channel channel; - if ((Record.type != component::channel) || (false == read_at(data, 0, channel))) + if (false == read_at(Data, 0, channel)) return; for (std::uint16_t idx = 0; idx < channel.binding_count; ++idx) { auto const offset{sizeof(channel) + idx * sizeof(payload::channel_binding)}; payload::channel_binding binding; - if (false == read_at(data, offset, binding)) + if (false == read_at(Data, offset, binding)) return; if (binding.kind == payload::binding_kind::clip) continue; Visit(binding.target); - write_at(data, offset, binding); + write_at(Data, offset, binding); + } +} + +// the components the data of the component points at, each given to Visit, which may change it: the targets of the bindings of a +// channel other than clips, the light sources of a light slot, the sockets of a spring, of the ends of a chain and of a model +// reference, and the properties whose value is a component. no_index (none) is given too +template void visit_payload_components(model &Model, component_record const &Record, Visit_ const &Visit) { + auto const data{payload_of(Model, Record)}; + switch (Record.type) { + case component::light_slot: visit_slot_lights(data, Visit); break; + case component::channel: visit_channel_targets(data, Visit); break; + case component::spring: visit_spring_sockets(data, Visit); break; + case component::chain: visit_chain_ends(data, Visit); break; + case component::model_reference: visit_reference_socket(data, Visit); break; + case component::properties: visit_properties(data, payload::value_type::component, Visit); break; + default: break; } } @@ -527,15 +572,39 @@ bool node_referenced(model &Model, std::uint32_t const Node) { return found; } -// the data of another component points at the component: a binding of a channel, the light sources of a light slot -bool component_referenced(model &Model, std::uint32_t const Component) { +// the data of a component other than those Leaving with it points at the component (visit_payload_components()) +bool component_referenced(model &Model, std::uint32_t const Component, std::span const Leaving) { auto found{false}; auto const check = [Component, &found](std::uint32_t const &Index) { found |= (Index == Component); }; - for (auto const &record : Model.components) - visit_payload_components(Model, record, check); + for (std::uint32_t idx = 0; idx < Model.components.size(); ++idx) { + if (std::ranges::find(Leaving, idx) == Leaving.end()) + visit_payload_components(Model, Model.components[idx], check); + } return found; } +// a component aimed at the mesh: it would aim at another one once the mesh is taken out +bool mesh_targeted(model const &Model, std::uint32_t const Mesh) { + return std::ranges::any_of(Model.components, [Mesh](component_record const &Record) { return (Record.kind == target_kind::mesh) && (Record.target == Mesh); }); +} + +// the items of the vertex groups past the vertices taken out, Count from First, moved back over them: they are vertices of the +// streams of the file. the groups of the mesh the vertices were of go with it +void shift_vertex_groups(model &Model, std::size_t const First, std::size_t const Count) { + auto const end{First + Count}; + for (auto const &group : Model.vertex_groups) { + auto const ranges{(group.flags & vertex_group_flags::ranges) != 0}; + // an item of ranges is a pair (first, count) in two places: only its first vertex moves + auto const step{ranges ? std::size_t{2} : std::size_t{1}}; + auto const itemend{std::min(std::size_t{group.item_first} + std::size_t{group.item_count} * step, Model.vertex_group_items.size())}; + for (auto item{std::size_t{group.item_first}}; item < itemend; item += step) { + auto &vertex{Model.vertex_group_items[item]}; + if (vertex >= end) + vertex -= static_cast(Count); + } + } +} + // a range of components after the component taken out of the table void close_range(std::uint32_t &First, std::uint32_t &Count, std::uint32_t const Component) { if (First > Component) @@ -593,7 +662,10 @@ void cut_geometry(model &Model, std::uint32_t const Primitive) { return; auto const removed{Model.primitives[Primitive]}; auto const vertex_end{std::size_t{removed.base_vertex} + removed.vertex_count}; + auto const vertices{Model.vertex_count()}; cut_vertices(Model, removed.base_vertex, removed.vertex_count); + if (Model.vertex_count() < vertices) + shift_vertex_groups(Model, removed.base_vertex, removed.vertex_count); auto const indexed{(removed.flags & primitive_flags::indexed) != 0}; auto const wide{(removed.flags & primitive_flags::index32) != 0}; auto const index_end{std::size_t{removed.first_index} + removed.index_count}; @@ -639,6 +711,11 @@ void erase_mesh(model &Model, std::uint32_t const Mesh) { if (group.mesh > Mesh) --group.mesh; } + // a component aimed at a mesh after it moves back with it; none is aimed at this one (node_removable()) + for (auto &record : Model.components) { + if ((record.kind == target_kind::mesh) && (record.target > Mesh)) + --record.target; + } } } // namespace @@ -668,8 +745,12 @@ bool node_removable(model &Model, std::uint32_t const Node) { // geometry which another node draws too stays, and with it the node if ((Model.nodes[Node].mesh >= 0) && (false == own_mesh(Model, Node).has_value())) return false; + // so does a mesh a component is aimed at + if (auto const mesh{own_mesh(Model, Node)}; mesh && mesh_targeted(Model, *mesh)) + return false; + // its components go with it: what they point at among themselves doesn't keep it auto const components{components_of(Model, Node)}; - return std::ranges::none_of(components, [&Model](std::uint32_t const Component) { return component_referenced(Model, Component); }); + return std::ranges::none_of(components, [&Model, &components](std::uint32_t const Component) { return component_referenced(Model, Component, components); }); } bool remove_node(model &Model, std::uint32_t const Node) { diff --git a/model/eu07m/eu07m_build.h b/model/eu07m/eu07m_build.h index ab7139302..5ed6add15 100644 --- a/model/eu07m/eu07m_build.h +++ b/model/eu07m/eu07m_build.h @@ -58,8 +58,9 @@ void compute_bounds(model &Model); // in their data. the index of the names and the bounds, where the model has them, are built again. returns the index of the node std::uint32_t insert_node(model &Model, std::int32_t Parent, node_record Node); // the node can be taken out: a leaf which nothing points at but what goes with it (its components, its transform, its own mesh), whose -// mesh no other node draws and has no vertex groups, and whose components aren't pointed at by the data of others (the bindings of a -// channel, the light sources of a light slot) +// mesh no other node draws, has no vertex groups and no component aimed at it, and whose components aren't pointed at by the data of +// others (the bindings of a channel, the light sources of a light slot, the sockets of a spring, a chain or a model reference, a +// property whose value is a component) bool node_removable(model &Model, std::uint32_t Node); // takes out the node, which node_removable() allows, with its components, its transform and its mesh with the primitives; their // vertices and indices, where no other primitive shares them, leave the streams. each reference to the nodes, the meshes, the diff --git a/tests/unit/test_eu07m_edit.cpp b/tests/unit/test_eu07m_edit.cpp index aa83aff1b..59693bb4c 100644 --- a/tests/unit/test_eu07m_edit.cpp +++ b/tests/unit/test_eu07m_edit.cpp @@ -233,3 +233,76 @@ TEST_CASE("eu07m: a part whose mesh another part draws too isn't taken out") { 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{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](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()); +}