18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 11:15:23 +01:00
Files
maszyna/tests/unit/test_door_wings.cpp
T
maj00r 13e9191427 fix(doors): folding doors move as the coach ones do, with an optional hold at half way on closing
Folding doors (DoorOpenMethod=Fold) turned at a constant speed. Their speed now changes along the way, in the same
time as before (range / OpenSpeed, range / CloseSpeed):
- opening is fastest at the closed door, slows down around half way and creeps to a stop at the fully open one;
- closing swings to half way and then shuts, each part speeding up from its start and slowing down to its end.

The new optional key DoorFoldHold=<s> in the Doors: line of the .fiz holds a closing door at half way for that
many seconds, with the wings rocking a little towards the closed door, a bit differently each time. Without it
there is no hold, and the random numbers are drawn only for a hold, so vehicles without the key keep theirs.

The position of a folding door now follows its wings, so such a door counts as open or closed only when its wings
are; with a hold, the AI departs after it. Other kinds of doors move as before. The motion is in
vehicle/doorwings.h (fold_times_of, fold_begin, fold_advance) with unit tests; the rocking is applied to the
animation only.

Game data (SVN pctga), changed in the working copy, to be committed there with this change: the Doors: line of
the 45 coach .fiz files with DoorOpenMethod=Fold in dynamic/pkp (11xa_v2, 14xa_v1, 110a_v2, 111arow_v1,
11xa-techniczne_v1, bdhinu_v1) goes from OpenSpeed=70 CloseSpeed=400 to OpenSpeed=32 CloseSpeed=53 and gets
DoorFoldHold=3.3 (2.5 s opening, 1.5 s closing and a 3.3 s hold, as in a recording of an IC coach). The road
vehicles with folding doors are left as they are, without a hold. Either part works without the other: this code
on the old data opens and closes in the old times without a hold; the new data on an older build only makes the
doors slower, the older build ignoring DoorFoldHold.
2026-12-02 20:46:55 +01:00

357 lines
16 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "vehicle/doorwings.h"
#include "utilities/parser.h"
#include <algorithm>
#include <cmath>
#include <vector>
// the poses of the animated door wings (eu07/maszyna#512): a wing of the main model and its namesake in the low
// poly interior get the same pose, worked out once from the door position of the mover
TEST_CASE("door wings: only the door animations move wings") {
CHECK(door_wings::is_wing(0x010)); // TAnim::TypeSet for the doors
CHECK_FALSE(door_wings::is_wing(0x000)); // axles
CHECK_FALSE(door_wings::is_wing(0x055)); // pantographs, five submodels
CHECK_FALSE(door_wings::is_wing(0x070)); // doorsteps
CHECK_FALSE(door_wings::is_wing(0x080)); // mirrors
CHECK(door_wings::is_wing(0x210)); // the parameter count in the upper bits doesn't matter
}
TEST_CASE("door wings: odd wings are on the left, even ones on the right") {
CHECK_FALSE(door_wings::on_right_side(1));
CHECK(door_wings::on_right_side(2));
CHECK_FALSE(door_wings::on_right_side(3));
CHECK(door_wings::on_right_side(4));
}
TEST_CASE("door wings: a sliding wing shifts along the vehicle by the door position") {
CHECK(door_wings::slide(0.f) == glm::vec3(0.f, 0.f, 0.f));
CHECK(door_wings::slide(0.65f) == glm::vec3(0.f, 0.f, 0.65f));
}
TEST_CASE("door wings: a plug wing goes out first, then slides") {
auto const rangeout{0.1f};
// closed
CHECK(door_wings::plug(0.f, rangeout) == glm::vec3(0.f, 0.f, 0.f));
// still going out, and not sliding yet
auto const out{door_wings::plug(0.02f, rangeout)};
CHECK(out.x == doctest::Approx(0.04f));
CHECK(out.z == 0.f);
// out as far as it goes, sliding by what the position has beyond half of the way out
auto const open{door_wings::plug(0.7f, rangeout)};
CHECK(open.x == rangeout);
CHECK(open.y == 0.f);
CHECK(open.z == doctest::Approx(0.65f));
}
TEST_CASE("door wings: a folding wing turns one way and its larger wing twice as far the other way") {
auto const angles{door_wings::fold(40.f)};
CHECK(angles.smaller == 40.f);
CHECK(angles.larger == -80.f);
CHECK(angles.step == 40.f);
auto const closed{door_wings::fold(0.f)};
CHECK(closed.smaller == 0.f);
CHECK(closed.larger == 0.f);
CHECK(closed.step == 0.f);
}
// a wing with its own kind of animation (eu07/maszyna#70): animdoor: in the .mmd, for vehicles whose doors on one side
// aren't all alike, e.g. 111Ainw with folding doors at one end and wider ones at the other
namespace {
// the spec of the .fiz the entries start from: folding doors which turn by 80 degrees
door_wings::wing_spec const fizdoors{0, door_wings::fold_type, 80.f, 0.f};
// reads the entry after animdoor: from the text, starting from the spec of the .fiz
std::string read(std::string const &Entry, door_wings::wing_spec &Spec) {
Spec = fizdoors;
cParser parser(Entry, cParser::buffer_TEXT);
return door_wings::read_spec(parser, Spec);
}
} // namespace
TEST_CASE("door wings: the kinds of animation are named as DoorOpenMethod of the .fiz") {
CHECK(door_wings::type_from_name("Shift") == door_wings::shift_type);
CHECK(door_wings::type_from_name("Rotate") == door_wings::rotate_type);
CHECK(door_wings::type_from_name("Fold") == door_wings::fold_type);
CHECK(door_wings::type_from_name("Plug") == door_wings::plug_type);
// in any letter case
CHECK(door_wings::type_from_name("plug") == door_wings::plug_type);
CHECK(door_wings::type_from_name("SHIFT") == door_wings::shift_type);
// the same numbers as TMoverParameters::door_data::type
CHECK(door_wings::shift_type == 1);
CHECK(door_wings::rotate_type == 2);
CHECK(door_wings::fold_type == 3);
CHECK(door_wings::plug_type == 4);
CHECK_FALSE(door_wings::type_from_name("Roll").has_value());
CHECK_FALSE(door_wings::type_from_name("").has_value());
}
TEST_CASE("door wings: an animdoor entry gives the wing its own kind, range and plug shift") {
door_wings::wing_spec spec;
CHECK(read("3 { type: Plug range: 0.9 rangeout: 0.06 } endmodels", spec).empty());
CHECK(spec.number == 3);
CHECK(spec.type == door_wings::plug_type);
CHECK(spec.range == doctest::Approx(0.9f));
CHECK(spec.range_out == doctest::Approx(0.06f));
}
TEST_CASE("door wings: the keys an animdoor entry leaves out keep the values of the .fiz") {
door_wings::wing_spec spec;
SUBCASE("only the kind") {
CHECK(read("2 { type: Rotate }", spec).empty());
CHECK(spec.number == 2);
CHECK(spec.type == door_wings::rotate_type);
CHECK(spec.range == 80.f);
CHECK(spec.range_out == 0.f);
}
SUBCASE("only the range, keys in any letter case and order") {
CHECK(read("1 { Range: -95 }", spec).empty());
CHECK(spec.type == door_wings::fold_type);
CHECK(spec.range == -95.f);
}
SUBCASE("nothing") {
CHECK(read("4 { }", spec).empty());
CHECK(spec.number == 4);
CHECK(spec.type == fizdoors.type);
CHECK(spec.range == fizdoors.range);
}
}
TEST_CASE("door wings: a faulty animdoor entry says what is wrong with it") {
door_wings::wing_spec spec;
CHECK(read("3 type: Shift range: 0.65", spec) == "no { after the wing number");
CHECK(read("3 { type: Roll range: 0.65 }", spec) == "unknown door type \"Roll\"");
CHECK(read("3 { type: Shift range: 0,65 }", spec) == "bad number \"0,65\" for \"range:\"");
CHECK(read("3 { type: Shift range: wide }", spec) == "bad number \"wide\" for \"range:\"");
CHECK(read("3 { kind: Shift }", spec) == "no } after the keys, \"kind:\" is none of them");
CHECK(read("3 { type: Shift range: }", spec) == "no value for \"range:\"");
CHECK(read("3 { type: Shift range: 0.65", spec) == "no } after the keys");
}
TEST_CASE("door wings: an animdoor entry without its } leaves the entries after it to the .mmd") {
// the first token that is neither a key nor the value of one ends the entry, and is read next by the caller
auto const next{[](std::string const &Text) {
door_wings::wing_spec spec{fizdoors};
cParser parser(Text, cParser::buffer_TEXT);
auto const error{door_wings::read_spec(parser, spec)};
std::vector<std::string> rest;
std::string token;
do {
token.clear();
parser.getTokens(1, false);
parser >> token;
if (false == token.empty()) {
rest.emplace_back(token);
}
} while (false == token.empty());
return std::pair{error, rest};
}};
{
auto const [error, rest]{next("3 { type: Plug range: 0.9\nsounds: horn.wav\nlights: 1\nendmodels")};
CHECK(error == "no } after the keys, \"sounds:\" is none of them");
CHECK(rest == std::vector<std::string>{"sounds:", "horn.wav", "lights:", "1", "endmodels"});
}
{
auto const [error, rest]{next("3 { type: Plug range: endmodels")};
CHECK(error == "no } after the keys, \"endmodels\" is none of them");
CHECK(rest == std::vector<std::string>{"endmodels"});
}
{
// a whole entry leaves the rest as it is
auto const [error, rest]{next("3 { type: Plug range: 0.9 } sounds: horn.wav endmodels")};
CHECK(error.empty());
CHECK(rest == std::vector<std::string>{"sounds:", "horn.wav", "endmodels"});
}
}
TEST_CASE("door wings: the parsed keys go into the spec, pairs of a key and its value") {
door_wings::wing_spec spec{fizdoors};
CHECK(door_wings::parse_spec({"type:", "Shift", "range:", "0.65"}, spec).empty());
CHECK(spec.type == door_wings::shift_type);
CHECK(spec.range == doctest::Approx(0.65f));
CHECK(door_wings::parse_spec({"rangeout:"}, spec) == "no value for \"rangeout:\"");
CHECK(door_wings::parse_spec({}, spec).empty());
}
TEST_CASE("door wings: only the wings with an entry animate in their own way, the latest entry counts") {
std::vector<door_wings::wing_spec> specs;
CHECK(door_wings::find(specs, 1) == nullptr);
door_wings::store(specs, {3, door_wings::shift_type, 0.65f, 0.f});
door_wings::store(specs, {4, door_wings::shift_type, 0.65f, 0.f});
door_wings::store(specs, {3, door_wings::plug_type, 0.9f, 0.06f});
CHECK(specs.size() == 2);
CHECK(door_wings::find(specs, 1) == nullptr);
CHECK(door_wings::find(specs, 2) == nullptr);
REQUIRE(door_wings::find(specs, 3) != nullptr);
CHECK(door_wings::find(specs, 3)->type == door_wings::plug_type);
CHECK(door_wings::find(specs, 3)->range == 0.9f);
REQUIRE(door_wings::find(specs, 4) != nullptr);
CHECK(door_wings::find(specs, 4)->type == door_wings::shift_type);
}
TEST_CASE("door wings: a wing with its own range is as far along it as the door of its side") {
// folding doors of the .fiz turn by 80 degrees; the wide wing slides by 0.9 m
CHECK(door_wings::wing_position(0.f, 80.f, 0.9f) == 0.f);
CHECK(door_wings::wing_position(40.f, 80.f, 0.9f) == doctest::Approx(0.45f));
CHECK(door_wings::wing_position(80.f, 80.f, 0.9f) == doctest::Approx(0.9f));
// a wing turning the other way
CHECK(door_wings::wing_position(20.f, 80.f, -100.f) == doctest::Approx(-25.f));
// no doors in the .fiz: the wing stays closed
CHECK(door_wings::wing_position(0.f, 0.f, 0.9f) == 0.f);
// the pose of the plug wing at the end of its range
auto const open{door_wings::plug(door_wings::wing_position(80.f, 80.f, 0.9f), 0.06f)};
CHECK(open.x == doctest::Approx(0.06f));
CHECK(open.z == doctest::Approx(0.87f));
}
namespace {
// a folding door of 80 degrees, its hold at 40 degrees
constexpr float fold_range{80.f};
constexpr float fold_hold{40.f};
// the coaches: OpenSpeed=32 CloseSpeed=53.3 DoorFoldHold=3.3 over 80 degrees
door_wings::fold_times const fold_coach{door_wings::fold_times_of(fold_range, 32.f, 80.f / 1.5f, 3.3f)};
// the time a folding door takes from Position to the end of its move, in steps of Deltatime
float fold_move_time(float const Position, bool const Opening, float const Deltatime) {
door_wings::fold_state state;
door_wings::fold_begin(state, Position, fold_range, Opening, fold_coach, {});
auto position{Position};
auto const end{Opening ? fold_range : 0.f};
auto time{0.f};
while (std::abs(position - end) > 0.f && time < 100.f) {
position = door_wings::fold_advance(state, position, fold_range, Deltatime);
time += Deltatime;
}
return time;
}
// the speed of the folding door at the position, in degrees a second
float fold_speed(float const Position, bool const Opening) {
return door_wings::fold_speed(Position, fold_range, Opening, fold_coach);
}
// a folding door moved in steps of 1/60 s, which records where it was at each step and how much its wings rocked
struct fold_run {
std::vector<float> positions;
std::vector<float> rocks;
fold_run(float const Position, bool const Opening, door_wings::fold_rocking const &Rocking, float const Seconds) {
door_wings::fold_state state;
door_wings::fold_begin(state, Position, fold_range, Opening, fold_coach, Rocking);
auto position{Position};
for (auto step = 0; step < static_cast<int>(Seconds * 60.f); ++step) {
position = door_wings::fold_advance(state, position, fold_range, 1.f / 60.f);
positions.emplace_back(position);
rocks.emplace_back(state.rock);
}
}
// the number of steps the door stood at the hold
int held() const {
return static_cast<int>(std::ranges::count_if(positions, [](float const Position) { return std::abs(Position - fold_hold) < 1e-4f; }));
}
};
} // namespace
TEST_CASE("door wings: a folding door opens in 2.5 s and closes in 1.5 s and its hold, whatever the step") {
for (auto const step : {1.f / 60.f, 1.f / 15.f}) {
CHECK(fold_move_time(0.f, true, step) == doctest::Approx(2.5f).epsilon(0.03));
CHECK(fold_move_time(fold_range, false, step) == doctest::Approx(4.8f).epsilon(0.02));
}
CHECK(fold_move_time(0.f, true, 0.5f) == doctest::Approx(2.5f).epsilon(0.2));
CHECK(fold_move_time(fold_range, false, 0.5f) == doctest::Approx(4.8f).epsilon(0.1));
}
TEST_CASE("door wings: a closing folding door holds at 50% with its wings rocking, an opening one slows down there") {
door_wings::fold_rocking const rocking{2.f, 0.4f, 0.5f};
// closing: 3.3 s at 40 degrees, the wings rocking by up to the amplitude and still again after it
fold_run const closing{fold_range, false, rocking, 7.f};
CHECK(closing.held() == doctest::Approx(198).epsilon(0.02));
// towards the closed door only, so it never looks like opening again
CHECK(std::ranges::all_of(closing.rocks, [](float const Rock) { return Rock <= 0.f; }));
CHECK(std::ranges::min(closing.rocks) < -1.f);
CHECK(std::ranges::min(closing.rocks) >= -2.f);
CHECK(closing.rocks.back() == 0.f);
CHECK(closing.positions.back() == 0.f);
// opening: no hold, the wings still, but at 50% at a quarter of the speed a little before and after it
fold_run const opening{0.f, true, rocking, 4.f};
CHECK(opening.held() < 3);
CHECK(std::ranges::all_of(opening.rocks, [](float const Rock) { return Rock == 0.f; }));
CHECK(opening.positions.back() == fold_range);
CHECK(fold_speed(40.f, true) < 0.4f * fold_speed(28.f, true));
CHECK(fold_speed(40.f, true) < 0.4f * fold_speed(52.f, true));
}
TEST_CASE("door wings: a closing folding door holds only when it passes the place of the hold") {
// from 30 degrees: below the hold, so no hold
fold_run const below{30.f, false, {}, 2.f};
CHECK(below.held() == 0);
CHECK(below.positions.back() == 0.f);
// from 60 degrees: it holds at 40 on the way
fold_run const above{60.f, false, {}, 5.f};
CHECK(above.held() > 190);
CHECK(above.positions.back() == 0.f);
}
TEST_CASE("door wings: an opening folding door is fastest at the closed door and slows down towards the open one") {
CHECK(fold_speed(1.f, true) > fold_speed(20.f, true));
CHECK(fold_speed(60.f, true) > fold_speed(72.f, true));
CHECK(fold_speed(79.f, true) < 0.2f * fold_speed(1.f, true));
}
TEST_CASE("door wings: a closing folding door swings and shuts at once, each speeding up and slowing down to its end") {
// it moves from the start: no creeping at the open door
CHECK(fold_speed(fold_range, false) > 0.2f * fold_speed(60.f, false));
// the swing fastest in its middle, slower at its start and before the hold
CHECK(fold_speed(60.f, false) > fold_speed(78.f, false));
CHECK(fold_speed(60.f, false) > fold_speed(42.f, false));
// the shutting fastest in its middle too
CHECK(fold_speed(20.f, false) > fold_speed(38.f, false));
CHECK(fold_speed(20.f, false) > fold_speed(2.f, false));
// the swing over in 0.8 s, then the hold and the shutting
fold_run const closing{fold_range, false, {}, 1.f};
CHECK(std::abs(closing.positions[49] - fold_hold) < 1e-4f);
CHECK(closing.positions[43] > fold_hold);
}
TEST_CASE("door wings: a folding door stops at the closed and at the open position, and no further") {
door_wings::fold_state state;
door_wings::fold_begin(state, 40.f, fold_range, true, fold_coach, {});
CHECK(door_wings::fold_advance(state, 40.f, fold_range, 10.f) == fold_range);
door_wings::fold_begin(state, 20.f, fold_range, false, fold_coach, {});
CHECK(door_wings::fold_advance(state, 20.f, fold_range, 10.f) == 0.f);
// no door range: it stays where it is
door_wings::fold_begin(state, 1.f, 0.f, false, fold_coach, {});
CHECK(door_wings::fold_advance(state, 1.f, 0.f, 0.25f) == 1.f);
}
TEST_CASE("door wings: a folding door takes the times of the .fiz, and holds only with DoorFoldHold") {
// the times of the speeds of the .fiz
auto const coach{door_wings::fold_times_of(80.f, 32.f, 80.f / 1.5f, 3.3f)};
CHECK(coach.open == doctest::Approx(2.5f));
CHECK(coach.close == doctest::Approx(1.5f));
CHECK(coach.hold == doctest::Approx(3.3f));
// a bus without DoorFoldHold (88 degrees, OpenSpeed=30 CloseSpeed=38): its own times, and no hold
auto const bus{door_wings::fold_times_of(88.f, 30.f, 38.f, 0.f)};
door_wings::fold_state state;
door_wings::fold_begin(state, 88.f, 88.f, false, bus, {2.f, 0.4f, 0.f});
CHECK(state.hold_done);
auto position{88.f};
auto time{0.f};
while (position > 0.f && time < 10.f) {
position = door_wings::fold_advance(state, position, 88.f, 1.f / 60.f);
CHECK(state.rock == 0.f);
time += 1.f / 60.f;
}
CHECK(time == doctest::Approx(88.f / 38.f).epsilon(0.02));
// no speed in the .fiz: the door stays where it is, as it did at the constant speed of zero
door_wings::fold_begin(state, 0.f, 80.f, true, door_wings::fold_times_of(80.f, 0.f, 0.f, 0.f), {});
CHECK(door_wings::fold_advance(state, 0.f, 80.f, 1.f) == 0.f);
}