diff --git a/CMakeLists.txt b/CMakeLists.txt index 90b42cf1f..560f9bfb6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -866,6 +866,7 @@ set(CORE_SOURCES "vehicle/linebreaker.cpp" "vehicle/smoke.cpp" "vehicle/odometer.cpp" +"vehicle/cabdefinitions.cpp" "world/timetablestart.cpp" "vehicle/timetableservice.cpp" "world/timetableflags.cpp" diff --git a/tests/unit/test_cab_definitions.cpp b/tests/unit/test_cab_definitions.cpp new file mode 100644 index 000000000..1f686cbe8 --- /dev/null +++ b/tests/unit/test_cab_definitions.cpp @@ -0,0 +1,142 @@ +#include "stdafx.h" +#include "doctest.h" +#include "scratch.h" + +#include "vehicle/cabdefinitions.h" +#include "utilities/Globals.h" +#include "utilities/parser.h" + +#include +#include +#include +#include + +// the cabs a vehicle's .mmd defines, and where the player goes when it doesn't define the cab the vehicle is occupied from +// or the cab a cab change leads to (TTrain::CabChange(), TTrain::Init(), TTrain::UpdateCab()): a single-desk locomotive +// may define its one cab only, and the player stays in it instead of ending up in a cab with no model and no controls + +using cab_definitions::ChangeStep; +using cab_definitions::Definitions; +using cab_definitions::Fallback; + +namespace { + +Definitions cabs(bool const Middle, bool const Front, bool const Rear) { + Definitions definitions; + definitions.defined = {Middle, Front, Rear}; + return definitions; +} + +Definitions const all{cabs(true, true, true)}; +Definitions const frontOnly{cabs(false, true, false)}; +Definitions const noMiddle{cabs(false, true, true)}; +Definitions const none{cabs(false, false, false)}; + +} // namespace + +TEST_CASE("cab definitions: the cab numbers of the physics name the entries of the .mmd") { + CHECK(cab_definitions::Index(1) == 1); + CHECK(cab_definitions::Index(-1) == 2); + CHECK(cab_definitions::Index(0) == 0); + CHECK(frontOnly.Has(1)); + CHECK_FALSE(frontOnly.Has(-1)); + CHECK_FALSE(frontOnly.Has(0)); + CHECK(frontOnly.Any()); + CHECK_FALSE(none.Any()); +} + +TEST_CASE("cab definitions: the .mmd is read for its cab entries, in its includes as well") { + test::scratch_folder folder{"cab_definitions"}; + folder.write("single.mmd", "internaldata:\nsounds: none\ncab1definition:\ncab1model: kabina.t3d\ncomponents:\nend\n"); + folder.write("double.mmd", "cab1definition:\ncab1model: a.t3d\ncab2definition:\ncab2model: b.t3d\ncab0definition:\ncab0model: none\n"); + folder.write("nocab.mmd", "internaldata:\nsounds: none\n"); + folder.write("included.mmd", "include cab.inc end\n"); + folder.write("cab.inc", "cab2definition:\ncab2model: b.t3d\n"); + + auto const single{cab_definitions::Find("single.mmd", folder.path())}; + CHECK(single.Has(1)); + CHECK_FALSE(single.Has(-1)); + CHECK_FALSE(single.Has(0)); + + auto const both{cab_definitions::Find("double.mmd", folder.path())}; + CHECK(both.Has(1)); + CHECK(both.Has(-1)); + CHECK(both.Has(0)); + + CHECK_FALSE(cab_definitions::Find("nocab.mmd", folder.path()).Any()); + + auto const included{cab_definitions::Find("included.mmd", folder.path())}; + CHECK(included.Has(-1)); + CHECK_FALSE(included.Has(1)); +} + +TEST_CASE("cab definitions: looking for the cabs leaves the shared random engine as it was") { + // a random set of includes draws from the engine kept in sync across the network; the look ahead before the cab is + // loaded mustn't make the cab loaded afterwards, or anything later in the run, draw differently + test::scratch_folder folder{"cab_definitions_random"}; + folder.write("random.mmd", "include [ a.inc b.inc c.inc ] end\n"); + folder.write("a.inc", "cab1definition:\n"); + folder.write("b.inc", "cab1definition:\n"); + folder.write("c.inc", "cab1definition:\n"); + + auto const before{Global.random_engine}; + CHECK(cab_definitions::Find("random.mmd", folder.path()).Has(1)); + CHECK(Global.random_engine == before); + + // the same file read the way the cab is loaded does draw from it, so the check above means something + { + cParser parser("random.mmd", cParser::buffer_FILE, folder.path(), true, std::vector(), true); + std::string token; + parser.getTokens(); + parser >> token; + CHECK(token == "cab1definition:"); + } + CHECK_FALSE(Global.random_engine == before); + Global.random_engine = before; +} + +TEST_CASE("cab definitions: with every cab defined the cab change goes one cab at a time, as it always did") { + CHECK(ChangeStep(1, -1, all) == -1); + CHECK(ChangeStep(0, -1, all) == -1); + CHECK(ChangeStep(-1, 1, all) == 1); + CHECK(ChangeStep(0, 1, all) == 1); + // past the end of the vehicle the step is still made, and fails as it did, so the player can go on to the next one + CHECK(ChangeStep(-1, -1, all) == -1); + CHECK(ChangeStep(1, 1, all) == 1); +} + +TEST_CASE("cab definitions: a single-desk vehicle keeps the player in its one cab") { + // the 401Da with cab1definition: only: the middle and the rear cab aren't there, the player stays put + CHECK(ChangeStep(1, -1, frontOnly) == 0); + // past the end, as with every vehicle + CHECK(ChangeStep(1, 1, frontOnly) == 1); + // occupied from the rear, the way the AI or the scenery may leave it: the change forward takes the player to the cab + // there is, past the middle one that isn't + CHECK(ChangeStep(-1, 1, frontOnly) == 2); + CHECK(ChangeStep(0, 1, frontOnly) == 1); + CHECK(ChangeStep(0, -1, frontOnly) == 0); +} + +TEST_CASE("cab definitions: a vehicle without the middle cab changes from one end straight to the other") { + CHECK(ChangeStep(1, -1, noMiddle) == -2); + CHECK(ChangeStep(-1, 1, noMiddle) == 2); +} + +TEST_CASE("cab definitions: a vehicle without any cab leaves the player where they are") { + // a car the player walked into: the change fails and the player goes on through the gangway, if there is one + CHECK(ChangeStep(1, -1, none) == 0); + CHECK(ChangeStep(0, 1, none) == 0); + CHECK(ChangeStep(-1, -1, none) == -1); +} + +TEST_CASE("cab definitions: the cab loaded in place of one the .mmd doesn't define") { + CHECK(Fallback(1, all) == std::optional{1}); + CHECK(Fallback(-1, all) == std::optional{-1}); + CHECK(Fallback(0, all) == std::optional{0}); + CHECK(Fallback(-1, frontOnly) == std::optional{1}); + CHECK(Fallback(0, frontOnly) == std::optional{1}); + CHECK(Fallback(0, noMiddle) == std::optional{1}); + CHECK(Fallback(1, cabs(false, false, true)) == std::optional{-1}); + CHECK(Fallback(1, cabs(true, false, false)) == std::optional{0}); + CHECK_FALSE(Fallback(1, none).has_value()); +} diff --git a/vehicle/Train.h b/vehicle/Train.h index e77e8ffc4..4211e6b26 100644 --- a/vehicle/Train.h +++ b/vehicle/Train.h @@ -964,6 +964,11 @@ public: // reszta może by?publiczna glm::vec2 pMechViewAngle { 0.0, 0.0 }; // camera pitch and yaw values, preserved while in external view private: + // drops the cab loaded so far: its model, its controls and its screens + void clearCab(); + // loads the cab of the given number, or another cab the .mmd defines where it has no such cab + bool initializeCabOrFallback(int CabNo, std::string const &FileName); + int missingCabIndex{-1}; // the occupied cab the .mmd has no definition for, while another cab is loaded in its place double fBlinkTimer{0.0}; float fHaslerTimer{0.0f}; float fConverterTimer; // hunter-261211: dla przekaznika diff --git a/vehicle/cab/setup.cpp b/vehicle/cab/setup.cpp index 2796e640c..354989a33 100644 --- a/vehicle/cab/setup.cpp +++ b/vehicle/cab/setup.cpp @@ -21,6 +21,7 @@ http://mozilla.org/MPL/2.0/. #include "vehicle/holdcoillever.h" #include "vehicle/compartmentlightsswitch.h" #include "vehicle/odometer.h" +#include "vehicle/cabdefinitions.h" #include "vehicle/mainctrlact.h" #include "utilities/Globals.h" #include "simulation/simulation.h" @@ -177,7 +178,7 @@ bool TTrain::LoadMMediaFile(std::string const &asFileName) return true; } -bool TTrain::InitializeCab(int NewCabNo, std::string const &asFileName) +void TTrain::clearCab() { m_controlmapper.clear(); // clear python screens @@ -208,27 +209,16 @@ bool TTrain::InitializeCab(int NewCabNo, std::string const &asFileName) } // reset view angles pMechViewAngle = {0.0, 0.0}; + DynamicObject->mdKabina = nullptr; // likwidacja wskaźnika na dotychczasową kabinę +} +bool TTrain::InitializeCab(int NewCabNo, std::string const &asFileName) +{ is_cab_initialized = true; // the attempt may fail, but it's the attempt that counts bool parse = false; - int cabindex = 0; - DynamicObject->mdKabina = nullptr; // likwidacja wskaźnika na dotychczasową kabinę - switch (NewCabNo) - { // ustalenie numeru kabiny do wczytania - case -1: - cabindex = 2; - break; - case 1: - cabindex = 1; - break; - case 0: - cabindex = 0; - break; - default: - break; - } - iCabn = cabindex; + // ustalenie numeru kabiny do wczytania + int const cabindex{cab_definitions::Index(NewCabNo)}; std::string cabstr("cab" + std::to_string(cabindex) + "definition:"); @@ -244,7 +234,15 @@ bool TTrain::InitializeCab(int NewCabNo, std::string const &asFileName) parser >> token; } while (token != "" && token != cabstr); - if (token == cabstr) + if (token != cabstr) + { + // the .mmd has no such cab: the cab loaded so far stays as it is, the caller decides what to do without the new one + return false; + } + clearCab(); + iCabn = cabindex; + auto const nullvector{glm::vec3()}; + { // jeśli znaleziony wpis kabiny Cabine[cabindex].Load(parser); @@ -474,10 +472,6 @@ bool TTrain::InitializeCab(int NewCabNo, std::string const &asFileName) } } } - else - { - return false; - } /* if (DynamicObject->mdKabina) { diff --git a/vehicle/cab/train.cpp b/vehicle/cab/train.cpp index 5173a40b5..9d5814a1a 100644 --- a/vehicle/cab/train.cpp +++ b/vehicle/cab/train.cpp @@ -21,6 +21,7 @@ http://mozilla.org/MPL/2.0/. #include "vehicle/holdcoillever.h" #include "vehicle/compartmentlightsswitch.h" #include "vehicle/odometer.h" +#include "vehicle/cabdefinitions.h" #include "vehicle/mainctrlact.h" #include "utilities/Globals.h" #include "simulation/simulation.h" @@ -122,7 +123,7 @@ bool TTrain::Init(TDynamicObject *NewDynamicObject, bool e3d) auto const filename{occupied->TypeName + ".mmd"}; LoadMMediaFile(filename); - InitializeCab(occupied->CabOccupied(), filename); + initializeCabOrFallback(occupied->CabOccupied(), filename); Global.CurrentMaxTextureSize = Global.iMaxTextureSize; @@ -547,16 +548,44 @@ TDynamicObject *TTrain::find_nearest_consist_vehicle(bool freefly, glm::vec3 pos // cab movement update, fixed step part void TTrain::UpdateCab() { - + auto const cabindex{occupied->CabOccupied() == -1 ? 2 : occupied->CabOccupied()}; // numer kabiny (-1: kabina B) + if (cabindex == missingCabIndex) + { + // the .mmd has no such cab: the cab loaded in its place stays + return; + } // Ra: przesiadka, jeśli AI zmieniło kabinę (a człon?)... if (DynamicObject->Mechanik // może nie być? - && DynamicObject->Mechanik->AIControllFlag() && iCabn != ( // numer kabiny (-1: kabina B) - occupied->CabOccupied() == -1 ? 2 : occupied->CabOccupied())) + && DynamicObject->Mechanik->AIControllFlag() && iCabn != cabindex && false == InitializeCab(occupied->CabOccupied(), occupied->TypeName + ".mmd")) { - - InitializeCab(occupied->CabOccupied(), occupied->TypeName + ".mmd"); + ErrorLog(std::format("Bad config: \"{}{}.mmd\" has no \"cab{}definition:\" entry, cab{}definition: stays loaded", DynamicObject->asBaseDir, occupied->TypeName, cabindex, iCabn)); + missingCabIndex = cabindex; + return; } - iCabn = occupied->CabOccupied() == -1 ? 2 : occupied->CabOccupied(); + iCabn = cabindex; +} + +bool TTrain::initializeCabOrFallback(int const CabNo, std::string const &FileName) +{ + missingCabIndex = -1; + if (InitializeCab(CabNo, FileName)) + { + return true; + } + auto const cabindex{cab_definitions::Index(CabNo)}; + if (auto const cab{cab_definitions::Fallback(CabNo, cab_definitions::Find(FileName, DynamicObject->asBaseDir))}; cab.has_value() && cab_definitions::Index(*cab) != cabindex) + { + ErrorLog(std::format("Bad config: \"{}{}\" has no \"cab{}definition:\" entry, cab{}definition: is used instead", DynamicObject->asBaseDir, FileName, cabindex, cab_definitions::Index(*cab))); + if (InitializeCab(*cab, FileName)) + { + missingCabIndex = cabindex; + return true; + } + } + // no cab in the .mmd at all (a car, say): no cab model and no controls, as it always was + clearCab(); + iCabn = cabindex; + return false; } void TTrain::add_distance(double const Distance) @@ -586,8 +615,15 @@ bool TTrain::CabChange(int iDirection) } else { // jeśli pojazd prowadzony ręcznie albo wcale (wagon) + auto const filename{occupied->TypeName + ".mmd"}; + auto const step{cab_definitions::ChangeStep(occupied->CabOccupied(), iDirection, cab_definitions::Find(filename, DynamicObject->asBaseDir))}; + if (step == 0) + { + // the .mmd has no cab that way: the player stays in the cab they are in + return false; + } occupied->CabDeactivisationAuto(); - if (occupied->ChangeCab(iDirection) && InitializeCab(occupied->CabOccupied(), occupied->TypeName + ".mmd")) + if (occupied->ChangeCab(step) && InitializeCab(occupied->CabOccupied(), filename)) { // zmiana kabiny w ramach tego samego pojazdu occupied->CabActivisationAuto(); // załączenie rozrządu (wirtualne kabiny) @@ -762,7 +798,7 @@ void TTrain::MoveToVehicle(TDynamicObject *target) { auto const filename{Occupied()->TypeName + ".mmd"}; LoadMMediaFile(filename); - InitializeCab(Occupied()->CabActive, filename); + initializeCabOrFallback(Occupied()->CabActive, filename); } Dynamic()->ABuSetModelShake({}); // zerowanie przesunięcia przed powrotem? diff --git a/vehicle/cabdefinitions.cpp b/vehicle/cabdefinitions.cpp new file mode 100644 index 000000000..621888e1e --- /dev/null +++ b/vehicle/cabdefinitions.cpp @@ -0,0 +1,81 @@ +/* +This Source Code Form is subject to the +terms of the Mozilla Public License, v. +2.0. If a copy of the MPL was not +distributed with this file, You can +obtain one at +http://mozilla.org/MPL/2.0/. +*/ + +#include "stdafx.h" +#include "vehicle/cabdefinitions.h" +#include "utilities/parser.h" +#include "utilities/Globals.h" + +#include +#include + +namespace cab_definitions { + +int Index(int const CabNo) { + switch (CabNo) { + case -1: + return 2; + case 1: + return 1; + default: + return 0; + } +} + +Definitions Find(std::string const &FileName, std::string const &Path) { + Definitions cabs; + // a random set of includes draws from the shared random engine, kept in sync across the network: the look ahead + // leaves it as it was, so the cab loaded afterwards draws the same and the rest of the run doesn't change + auto const randomengine{Global.random_engine}; + // the same parser TTrain::InitializeCab() looks for the cab with, so both see the same entries + cParser parser(FileName, cParser::buffer_FILE, Path, true, std::vector(), true); + std::string token; + do { + token = ""; + parser.getTokens(); + parser >> token; + for (auto index{0}; index < 3; ++index) { + if (token == "cab" + std::to_string(index) + "definition:") { + cabs.defined[index] = true; + } + } + } while (false == token.empty() && false == (cabs.defined[0] && cabs.defined[1] && cabs.defined[2])); + Global.random_engine = randomengine; + return cabs; +} + +int ChangeStep(int const CabNo, int const Direction, Definitions const &Cabs) { + auto target{CabNo + Direction}; + if (std::abs(target) > 1) { + // past the end of the vehicle: the change fails, as it always did + return Direction; + } + if (target == 0 && false == Cabs.Has(0)) { + // no middle cab: on to the cab beyond it + target += Direction; + } + if (std::abs(target) > 1 || false == Cabs.Has(target)) { + return 0; + } + return target - CabNo; +} + +std::optional Fallback(int const CabNo, Definitions const &Cabs) { + if (Cabs.Has(CabNo)) { + return CabNo; + } + for (auto const cab : {1, -1, 0}) { + if (Cabs.Has(cab)) { + return cab; + } + } + return std::nullopt; +} + +} // namespace cab_definitions diff --git a/vehicle/cabdefinitions.h b/vehicle/cabdefinitions.h new file mode 100644 index 000000000..7ed21835f --- /dev/null +++ b/vehicle/cabdefinitions.h @@ -0,0 +1,49 @@ +/* +This Source Code Form is subject to the +terms of the Mozilla Public License, v. +2.0. If a copy of the MPL was not +distributed with this file, You can +obtain one at +http://mozilla.org/MPL/2.0/. +*/ + +#pragma once + +#include +#include +#include + +// the cabs a vehicle's .mmd defines (cab0definition:, cab1definition:, cab2definition:). The cab the vehicle is occupied +// from is a matter of its physics (-1, 0, 1), and the AI changes it on any vehicle, but the player can only sit in a cab +// the .mmd defines. A single-desk locomotive may define its one cab only: the player then stays in it whichever way the +// vehicle is driven, instead of ending up in a cab with no model and no controls. +namespace cab_definitions { + +// the index of the cab of the given number in the .mmd and in TTrain::Cabine: 0 the middle one (the engine room), +// 1 the front one, 2 the rear one +int Index(int CabNo); + +struct Definitions { + std::array defined{}; // by the index of the cab + + bool Has(int const CabNo) const { + return defined[Index(CabNo)]; + } + bool Any() const { + return defined[0] || defined[1] || defined[2]; + } +}; + +// the cab entries of the .mmd, read with the parser the cab is loaded with +Definitions Find(std::string const &FileName, std::string const &Path); + +// the step the cab change in the given direction makes from the occupied cab: the direction itself where the cab there +// is defined, or where there's no cab of the vehicle there at all (the change then fails as it always did); twice the +// direction past a middle cab the .mmd doesn't define; 0 where the .mmd has no cab to go to, so the player stays put +int ChangeStep(int CabNo, int Direction, Definitions const &Cabs); + +// the cab to load for the occupied one: that one where the .mmd defines it, otherwise the front, the rear or the middle +// one, in this order, whichever it defines first; none if it defines no cab at all +std::optional Fallback(int CabNo, Definitions const &Cabs); + +} // namespace cab_definitions