diff --git a/CMakeLists.txt b/CMakeLists.txt index 70d06d5c9..616005596 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -226,6 +226,7 @@ set(SOURCES "editor/project/projectGenerated.cpp" "editor/project/projectMigration.cpp" "editor/project/sceneryText.cpp" +"editor/project/scenarioIndex.cpp" "editor/editorNormsReport.cpp" "editor/editorProfile.cpp" "editor/editorGauge.cpp" diff --git a/editor/project/scenarioIndex.cpp b/editor/project/scenarioIndex.cpp new file mode 100644 index 000000000..364c8a8d0 --- /dev/null +++ b/editor/project/scenarioIndex.cpp @@ -0,0 +1,331 @@ +/* +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 "editor/project/scenarioIndex.hpp" +#include "editor/project/sceneryText.hpp" + +#include +#include +#include +#include +#include +#include +#include + +namespace project +{ + +namespace +{ + +using scenery_text::statement; + +// the files the files include, by a quick look at their words, each file looked at once for all the scenarios: a scenario whose includes +// don't reach the file of the map doesn't use it, and isn't read as a whole +class include_graph +{ + public: + explicit include_graph(std::filesystem::path Folder) : m_folder{std::move(Folder)} {} + + // the includes of the root reach the map, or a file named by the parameters of a template, which only reading it all tells + bool may_reach(std::string const &Root, std::string const &Map, bool const SkipTerrain) + { + std::vector pending{Root}; + std::set seen; + while (false == pending.empty()) + { + auto const name{pending.back()}; + pending.pop_back(); + auto const key{scenery_text::lowered(name)}; + if (false == seen.insert(key).second) + continue; + if (scenery_text::same_file(name, Map) || name.find("(p") != std::string::npos) + return true; + if (SkipTerrain && name != Root && key.find("_ter.scm") != std::string::npos) + continue; + for (auto const &include : includes(key, name)) + pending.push_back(include); + } + return false; + } + + private: + std::vector const &includes(std::string const &Key, std::string const &Name) + { + auto found{m_includes.find(Key)}; + if (found != m_includes.end()) + return found->second; + std::ifstream file{m_folder / Name, std::ios::binary}; + std::string const text{std::istreambuf_iterator(file), std::istreambuf_iterator()}; + return m_includes.emplace(Key, scenery_text::includes_in(text)).first->second; + } + + std::filesystem::path m_folder; + std::map> m_includes; +}; + +// the events a track calls by its name and theirs, : (TTrack::AssignEvents(), Global.iHiddenEvents) +constexpr std::array named_sequences{"event0", "eventall0", "event1", "eventall1", "event2", "eventall2"}; + +// the words which end the parameters of an event (basic_event::is_keyword()) +bool event_keyword(std::string_view const Word) +{ + return Word == "endevent" || Word == "randomdelay" || Word == "departuredelay"; +} + +// the targets of an event, | or , between them, none for none (basic_event::deserialize_targets()) +std::vector targets_of(std::string const &Word) +{ + std::vector result; + std::size_t at{0}; + while (at <= Word.size()) + { + auto end{Word.find_first_of("|,", at)}; + end = (end == std::string::npos ? Word.size() : end); + auto target{Word.substr(at, end - at)}; + if (false == target.empty() && target != "none") + result.push_back(std::move(target)); + at = end + 1; + } + return result; +} + +// the statements which name or define what the index is about +bool kept(std::string_view const Keyword, std::string_view const Type) +{ + if (Keyword == "node") + return Type == "track" || Type == "memcell" || Type == "eventlauncher" || Type == "dynamic"; + return Keyword == "event" || Keyword == "trainset" || Keyword == "endtrainset" || Keyword == "isolated"; +} + +class collector +{ + public: + collector(scenery_text::scenery const &Scenery, std::vector &Output) : m_scenery{Scenery}, m_output{Output} {} + + void name(reference_kind const Kind, std::string const &Name, reference_use const Use, std::string const &From, statement const &Statement) + { + if (Name.empty() || Name == "none") + return; + m_output.push_back({Kind, Name, Use, From, m_scenery.files[Statement.file].name, Statement.line}); + } + + void event(statement const &Event) + { + auto const name{Event.word(0)}; + auto const type{Event.word(1)}; + auto const targets{targets_of(Event.word(3))}; + // the parameters, up to the end of the event + std::vector parameters; + for (std::size_t i = 4; i < Event.words.size(); ++i) + parameters.push_back(Event.word(i)); + if (type == "multiple") + { + bool tracks{false}; + bool cells{false}; + bool condition{false}; + for (auto const &word : parameters) + { + if (condition) + { + tracks = tracks || word == "trackoccupied" || word == "trackfree"; + cells = cells || word == "memcompare" || word == "memcompareex"; + continue; + } + if (event_keyword(word)) + break; + if (word == "condition") + condition = true; + else if (word != "else" && false == word.starts_with("none_")) + this->name(reference_kind::event, word, reference_use::event_child, name, Event); + } + for (auto const &target : targets) + { + if (tracks) + this->name(reference_kind::track, target, reference_use::condition, name, Event); + if (cells) + this->name(reference_kind::memcell, target, reference_use::condition, name, Event); + } + } + else if (type == "addvalues" || type == "updatevalues" || type == "getvalues" || type == "whois" || type == "logvalues" || type == "copyvalues") + { + for (auto const &target : targets) + this->name(reference_kind::memcell, target, reference_use::event_target, name, Event); + if (type == "copyvalues" && false == parameters.empty() && false == event_keyword(parameters.front())) + this->name(reference_kind::memcell, parameters.front(), reference_use::copy_source, name, Event); + } + else if (type == "switch" || type == "trackvel" || type == "trackbuckle") + { + for (auto const &target : targets) + this->name(reference_kind::track, target, reference_use::event_target, name, Event); + } + // the event a track calls by its name + if (auto const colon{name.rfind(':')}; colon != std::string::npos && colon > 0 && std::ranges::find(named_sequences, std::string_view{name}.substr(colon + 1)) != named_sequences.end()) + this->name(reference_kind::track, name.substr(0, colon), reference_use::named_event, name, Event); + } + + void track(statement const &Track) + { + auto const name{Track.word(2)}; + for (std::size_t i = 4; i + 1 < Track.words.size(); ++i) + { + auto const word{Track.word(i)}; + if (std::ranges::find(named_sequences, word) != named_sequences.end()) + this->name(reference_kind::event, Track.word(++i), reference_use::track_event, name, Track); + } + } + + void launcher(statement const &Launcher) + { + // eventlauncher [] [condition ] ... end + auto const name{Launcher.word(2)}; + this->name(reference_kind::event, Launcher.word(10), reference_use::launcher, name, Launcher); + std::size_t next{11}; + auto const second{Launcher.word(next)}; + if (second != "end" && second != "condition" && second != "traintriggered") + { + this->name(reference_kind::event, second, reference_use::launcher, name, Launcher); + ++next; + } + if (Launcher.word(next) == "condition") + this->name(reference_kind::memcell, Launcher.word(next + 1), reference_use::launcher_condition, name, Launcher); + } + + private: + scenery_text::scenery const &m_scenery; + std::vector &m_output; +}; + +} // namespace + +char const *kind_name(reference_kind const Kind) +{ + switch (Kind) + { + case reference_kind::event: return "event"; + case reference_kind::memcell: return "memcell"; + default: return "track"; + } +} + +char const *use_name(reference_use const Use) +{ + switch (Use) + { + case reference_use::trainset: return "trainset"; + case reference_use::vehicle: return "vehicle"; + case reference_use::event_target: return "event target"; + case reference_use::condition: return "condition"; + case reference_use::copy_source: return "copy source"; + case reference_use::event_child: return "multiple event"; + case reference_use::launcher: return "event launcher"; + case reference_use::launcher_condition: return "event launcher condition"; + case reference_use::track_event: return "track event"; + case reference_use::memcell_track: return "memory cell track"; + case reference_use::isolated: return "isolated"; + default: return "named event"; + } +} + +scenario_index index_scenarios(std::filesystem::path const &Folder, std::string const &Map) +{ + scenario_index result; + result.map = Map; + std::vector scenarios; + std::error_code error; + for (std::filesystem::directory_iterator file{Folder, std::filesystem::directory_options::skip_permission_denied, error}, end; file != end; file.increment(error)) + { + auto const name{file->path().filename().generic_string()}; + if (file->is_regular_file(error) && scenery_text::lowered(name).ends_with(".scn")) + scenarios.push_back(name); + } + std::ranges::sort(scenarios, [](std::string const &A, std::string const &B) { return scenery_text::lowered(A) < scenery_text::lowered(B); }); + + include_graph includes{Folder}; + for (auto const &scenario : scenarios) + { + scenery_text::read_options options; + options.map = Map; + options.keep = kept; + // the terrain of a scenario with its binary terrain file comes from that one, its text files aren't read (cParser) + auto binary{Folder / scenario}; + binary.replace_extension(".sbt"); + options.skip_terrain = std::filesystem::exists(binary, error); + if (false == includes.may_reach(scenario, Map, options.skip_terrain)) + continue; + auto const scenery{scenery_text::read(Folder, scenario, options)}; + if (false == scenery.uses_map()) + continue; + for (auto const &problem : scenery.problems) + result.problems.push_back(scenario + ": " + problem); + + scenario_use use; + use.scenario = scenario; + collector names{scenery, use.references}; + bool trainset{false}; + for (auto const &statement : scenery.statements) + { + auto const &keyword{statement.keyword}; + auto const type{keyword == "node" ? statement.word(3) : std::string{}}; + // what the map defines + if (statement.map) + { + if (type == "track") + result.tracks.insert(statement.word(2)); + else if (type == "memcell") + result.memcells.insert(statement.word(2)); + else if (keyword == "event") + result.events.insert(statement.word(0)); + } + // the trainsets wherever they are, a scenario which is its own map too + if (keyword == "trainset") + { + trainset = true; + names.name(reference_kind::track, statement.word(1), reference_use::trainset, statement.word(0), statement); + } + else if (keyword == "endtrainset") + trainset = false; + else if (type == "dynamic" && false == trainset) + names.name(reference_kind::track, statement.word(7), reference_use::vehicle, statement.word(2), statement); + if (statement.map) + continue; + // what the rest of the scenario names + if (keyword == "event") + names.event(statement); + else if (keyword == "isolated") + { + for (std::size_t i = 1; i + 1 < statement.words.size(); ++i) + names.name(reference_kind::track, statement.word(i), reference_use::isolated, statement.word(0), statement); + } + else if (type == "track") + names.track(statement); + else if (type == "memcell") + names.name(reference_kind::track, statement.word(10), reference_use::memcell_track, statement.word(2), statement); + else if (type == "eventlauncher") + names.launcher(statement); + } + result.scenarios.push_back(std::move(use)); + } + + // of what the scenarios name, what the map defines + for (auto &use : result.scenarios) + { + std::erase_if(use.references, [&](reference const &Reference) { + auto const &defined{Reference.kind == reference_kind::track ? result.tracks : Reference.kind == reference_kind::event ? result.events : result.memcells}; + return false == defined.contains(Reference.name); + }); + std::ranges::sort(use.references); + auto const repeated{std::ranges::unique(use.references)}; + use.references.erase(repeated.begin(), repeated.end()); + } + return result; +} + +} // namespace project diff --git a/editor/project/scenarioIndex.hpp b/editor/project/scenarioIndex.hpp new file mode 100644 index 000000000..3b2ab1d87 --- /dev/null +++ b/editor/project/scenarioIndex.hpp @@ -0,0 +1,89 @@ +/* +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 +#include +#include + +// The scenarios using a map and what they name in it (editor-tracks-domain.md §0 pkt 7, §3 "Indeks scenariuszy"): a change of the map +// mustn't take away a track, an event or a memory cell a scenario relies on. Read only, from the text of the files as the simulator reads +// it: the scenarios are the *.scn files of the scenery folder, a scenario uses the map where the file of the map is among its files (its +// includes followed, as the simulator follows them: relative to the folder, with the parameters of the templates), and what it names in +// the map is what its statements outside the files of the map name of the tracks, the events and the memory cells the files of the map +// define, the trainsets wherever they are. +// +// What isn't seen: the names made in the simulator (the tracks of the roads, the parts of the turntables), the names in the scripts +// (lua), in the timetables and in the commands the memory cells give the drivers, the random includes; the names compared as the parser +// makes them, the letters A to Z small. +namespace project +{ + +enum class reference_kind +{ + track, + event, + memcell +}; + +enum class reference_use +{ + trainset, // a trainset starts on the track + vehicle, // a vehicle standing on its own is put on the track + event_target, // an event acts on it: switch, trackvel, trackbuckle on a track; updatevalues, getvalues, copyvalues, whois, logvalues on a cell + condition, // the condition of an event checks it: trackoccupied, trackfree, memcompare + copy_source, // copyvalues takes from the cell + event_child, // a multiple event calls the event + launcher, // an event launcher calls the event + launcher_condition, // an event launcher checks the cell + track_event, // a track of the scenario calls the event (event0, event1, event2, eventall0...) + memcell_track, // a memory cell of the scenario names the track + isolated, // an isolated circuit of the scenario holds the track + named_event // an event of the scenario named after the track (:event1 ...), which the track calls +}; + +char const *kind_name(reference_kind const Kind); +char const *use_name(reference_use const Use); + +struct reference +{ + reference_kind kind{reference_kind::track}; + std::string name; // made small, as the simulator compares the names + reference_use use{reference_use::trainset}; + std::string from; // the event, the trainset, the node of the scenario which names it + std::string file; // where that is, relative to the scenery folder + std::size_t line{0}; + + auto operator<=>(reference const &) const = default; +}; + +struct scenario_use +{ + std::string scenario; // the file, relative to the scenery folder + std::vector references; // in the order of their kinds, names and uses +}; + +struct scenario_index +{ + std::string map; + // what the files of the map define, the names made small + std::set tracks; + std::set events; + std::set memcells; + std::vector scenarios; // the ones using the map, in the order of their files + std::vector problems; // includes which can't be read and the like, by scenario +}; + +// the scenarios of the scenery folder using the map (relative to the folder), what each one names in it +scenario_index index_scenarios(std::filesystem::path const &Folder, std::string const &Map); + +} // namespace project diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 2d3bee36d..d35b6f7da 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -64,11 +64,12 @@ add_executable(${UNIT_TESTS_NAME} "${CMAKE_SOURCE_DIR}/editor/layout/layoutCommands.cpp" "${CMAKE_SOURCE_DIR}/editor/layout/layoutTurnout.cpp" "${CMAKE_SOURCE_DIR}/editor/layout/layoutGenerator.cpp" - # the project file of the domain, the tracks written kept by their checksums, the //$ lines migrated into it (test_project_*.cpp) + # the project file of the domain, the tracks written kept by their checksums, the //$ lines migrated into it, the scenarios using a map (test_project_*.cpp, test_scenario_index.cpp) "${CMAKE_SOURCE_DIR}/editor/project/projectFile.cpp" "${CMAKE_SOURCE_DIR}/editor/project/projectGenerated.cpp" "${CMAKE_SOURCE_DIR}/editor/project/projectMigration.cpp" "${CMAKE_SOURCE_DIR}/editor/project/sceneryText.cpp" + "${CMAKE_SOURCE_DIR}/editor/project/scenarioIndex.cpp" ${UNIT_TEST_CASES}) # the core library brings the game's build settings along diff --git a/tests/unit/data/project/index/inna.scn b/tests/unit/data/project/index/inna.scn new file mode 100644 index 000000000..055417dd7 --- /dev/null +++ b/tests/unit/data/project/index/inna.scn @@ -0,0 +1,6 @@ +// a scenario of another map: tracks of the same names aren't the map's +node -1 0 tor_1 track normal 100.0 1.435 0.15 25 20 0 flat vis rail_screw_used1 4 tpbps-new2 0.2 0.5 1.1 0 0 0 0 0 0 0 0 0 0 0 100 0 0 velocity 60 endtrack +FirstInit +trainset obcy tor_1 0.0 0.0 +node -1 0 loco dynamic pkp ep07 ep07 0 headdriver 3 0 enddynamic +endtrainset diff --git a/tests/unit/data/project/index/mapa/mapa.scm b/tests/unit/data/project/index/mapa/mapa.scm new file mode 100644 index 000000000..5f7c14495 --- /dev/null +++ b/tests/unit/data/project/index/mapa/mapa.scm @@ -0,0 +1,7 @@ +// the map of the tests of the scenario index (test_scenario_index.cpp): its tracks, its events and its memory cells, some of them from a +// template included with parameters +include mapa/tory.scm end +include mapa/zdarzenia.scm end +origin 100 0 0 +include mapa/semafor.inc sem_a 10.0 5.0 tor_2 end +endorigin diff --git a/tests/unit/data/project/index/mapa/semafor.inc b/tests/unit/data/project/index/mapa/semafor.inc new file mode 100644 index 000000000..b34d21be6 --- /dev/null +++ b/tests/unit/data/project/index/mapa/semafor.inc @@ -0,0 +1,4 @@ +// a signal of the map, a template: its memory cell and its events named after the first parameter +node -1 0 (p1)_mem memcell (p2) 0 (p3) none 0 0 (p4) endmemcell +event (p1)_s1 updatevalues 0 (p1)_mem SetVelocity 100 100 endevent +event (p1)_sr2 updatevalues 0.5 (p1)_mem SetVelocity 0 0 endevent diff --git a/tests/unit/data/project/index/mapa/tory.scm b/tests/unit/data/project/index/mapa/tory.scm new file mode 100644 index 000000000..aba271392 --- /dev/null +++ b/tests/unit/data/project/index/mapa/tory.scm @@ -0,0 +1,8 @@ +// the tracks of the map +node -1 0 tor_1 track normal 100.0 1.435 0.15 25 20 0 flat vis rail_screw_used1 4 tpbps-new2 0.2 0.5 1.1 0 0 0 0 0 0 0 0 0 0 0 100 0 0 velocity 60 event1 tor_1_zajety endtrack +node -1 0 Tor_2 track normal 100.0 1.435 0.15 25 20 0 flat vis rail_screw_used1 4 tpbps-new2 0.2 0.5 1.1 0 0 100 0 0 0 0 0 0 0 0 200 0 0 velocity 60 endtrack +node -1 0 tor_3 track normal 100.0 1.435 0.15 25 20 0 flat vis rail_screw_used1 4 tpbps-new2 0.2 0.5 1.1 0 0 200 0 0 0 0 0 0 0 0 300 0 0 velocity 60 endtrack +node -1 0 zwr_1 track switch 33.23 1.435 0.15 25 20 0 flat vis rail_screw_used1 4 tpbps-new2 0.2 0.5 1.1 +0 0 300 0 0 0 0 0 0 0 0 333.23 0 0 +0 0 300 0 0 0 0 0 0 1.8 0 333.1 0 500 +velocity 40 endtrack diff --git a/tests/unit/data/project/index/mapa/zdarzenia.scm b/tests/unit/data/project/index/mapa/zdarzenia.scm new file mode 100644 index 000000000..66e01801d --- /dev/null +++ b/tests/unit/data/project/index/mapa/zdarzenia.scm @@ -0,0 +1,5 @@ +// the events and the memory cells of the map +node -1 0 stacja_mem memcell 0 0 0 none 0 0 tor_1 endmemcell +event tor_1_zajety multiple 0 none sem_a_s1 endevent +event zwr_1_plus switch 0 zwr_1 0 endevent +/* event w_komentarzu multiple 0 none tor_1_zajety endevent */ diff --git a/tests/unit/data/project/index/poranek.scn b/tests/unit/data/project/index/poranek.scn new file mode 100644 index 000000000..6446f503e --- /dev/null +++ b/tests/unit/data/project/index/poranek.scn @@ -0,0 +1,18 @@ +// a scenario of the map: its trainset on a track of the map, its own events calling the events of the map and acting on its tracks +// and memory cells, a track of its own calling an event of the map, an event launcher +time 6:00 5:00 20:00 endtime +include mapa/mapa.scm end +node -1 0 bocznica track normal 50.0 1.435 0.15 25 20 0 flat vis rail_screw_used1 4 tpbps-new2 0.2 0.5 1.1 0 0 -50 0 0 0 0 0 0 0 0 0 0 0 velocity 20 event2 sem_a_sr2 endtrack +event start_poranek multiple 0 none sem_a_s1 tor_1_zajety none_pominiete nieznane endevent +event zwrotnica switch 1.0 zwr_1 1 endevent +event komorka updatevalues 0 stacja_mem Jump_to_track 1 2 endevent +event Tor_2:event1 multiple 0 none start_poranek endevent +node -1 0 przycisk eventlauncher 0 0 0 5 h 0 sem_a_s1 condition sem_a_mem * 100 * end +// trainset w_komentarzu tor_3 0 0 +FirstInit +trainset rano tor_1 10.0 0.0 +node -1 0 loco dynamic pkp ep07 ep07 0 headdriver 3 0 enddynamic +endtrainset +trainset na_bocznicy bocznica 0.0 0.0 +node -1 0 wagon dynamic pkp 412z 412z 0 nobody 3 0 enddynamic +endtrainset diff --git a/tests/unit/data/project/index/wieczor.scn b/tests/unit/data/project/index/wieczor.scn new file mode 100644 index 000000000..4dac6aea4 --- /dev/null +++ b/tests/unit/data/project/index/wieczor.scn @@ -0,0 +1,13 @@ +// another scenario of the map, which includes it through a file of its own: a trainset, a vehicle standing on its own, an isolated +// circuit, the conditions of the events and the cells copied +include wieczor/wstep.scm end +node -1 0 moja_mem memcell 0 0 0 none 0 0 tor_3 endmemcell +isolated odc_1 tor_1 Tor_2 endisolated +event kopia copyvalues 0 moja_mem stacja_mem 7 endevent +event warunek multiple 0 sem_a_mem sem_a_s1 condition memcompare * 100 * endevent +event zajetosc multiple 0 tor_3 sem_a_sr2 condition trackoccupied endevent +FirstInit +node -1 0 stojak dynamic pkp 412z 412z tor_2 50.0 nobody 0 0 enddynamic +trainset wieczorny tor_3 0.0 0.0 +node -1 0 loco dynamic pkp ep07 ep07 0 headdriver 3 0 enddynamic +endtrainset diff --git a/tests/unit/data/project/index/wieczor/wstep.scm b/tests/unit/data/project/index/wieczor/wstep.scm new file mode 100644 index 000000000..144065cca --- /dev/null +++ b/tests/unit/data/project/index/wieczor/wstep.scm @@ -0,0 +1,2 @@ +// the map included by the scenario through a file of its own +include mapa/mapa.scm end diff --git a/tests/unit/test_scenario_index.cpp b/tests/unit/test_scenario_index.cpp new file mode 100644 index 000000000..604625043 --- /dev/null +++ b/tests/unit/test_scenario_index.cpp @@ -0,0 +1,130 @@ +// the scenarios using a map and what they name in it (editor/project/scenarioIndex.cpp, editor-tracks-domain.md §3 "Indeks +// scenariuszy"): the small scenery of data/project/index, a map of tracks, events and memory cells (some from a template included with +// parameters) and three scenarios, two of them using the map (one through a file of its own), one another map with tracks of the same +// names; the trainsets on the tracks of the map, the events of the map called, its memory cells used, the rest of the names left out + +#include "stdafx.h" +#include "doctest.h" +#include "editor/project/scenarioIndex.hpp" +#include "editor/project/sceneryText.hpp" + +#include +#include + +namespace { + +std::filesystem::path folder() { + return std::filesystem::path{EU07_UNIT_TESTS_DATA} / "project" / "index"; +} + +// a reference as a line: the kind, the name, the use, what names it, where +std::string described(project::reference const &Reference) { + return std::format("{} {} {} {} {}:{}", project::kind_name(Reference.kind), Reference.name, project::use_name(Reference.use), Reference.from, Reference.file, Reference.line); +} + +std::string described(std::vector const &References) { + std::string result; + for (auto const &reference : References) { + result += described(reference) + "\n"; + } + return result; +} + +} // namespace + +TEST_CASE("scenario index: the scenarios using the map, what the map defines and what each scenario names in it") { + auto const index{project::index_scenarios(folder(), "mapa/mapa.scm")}; + CHECK(index.problems.empty()); + CHECK(index.map == "mapa/mapa.scm"); + // the template of the signal gives the names of its parameters; a comment defines nothing + CHECK(index.tracks == std::set{"tor_1", "tor_2", "tor_3", "zwr_1"}); + CHECK(index.events == std::set{"sem_a_s1", "sem_a_sr2", "tor_1_zajety", "zwr_1_plus"}); + CHECK(index.memcells == std::set{"sem_a_mem", "stacja_mem"}); + // the scenario of another map isn't among them, the one including the map through a file of its own is + REQUIRE(index.scenarios.size() == 2); + CHECK(index.scenarios[0].scenario == "poranek.scn"); + CHECK(index.scenarios[1].scenario == "wieczor.scn"); + + // the trainset on a track of the map, the events of the map called by an event, by a track and by a launcher of the scenario, the + // switch and the cell acted on, the cell checked by the launcher, the track whose event the scenario names after it; not the trainset + // on a track of the scenario, the events of the scenario, the names in a comment, an event of none_, a name defined nowhere + CHECK(described(index.scenarios[0].references) == + "track tor_1 trainset rano poranek.scn:13\n" + "track tor_2 named event tor_2:event1 poranek.scn:9\n" + "track zwr_1 event target zwrotnica poranek.scn:7\n" + "event sem_a_s1 multiple event start_poranek poranek.scn:6\n" + "event sem_a_s1 event launcher przycisk poranek.scn:10\n" + "event sem_a_sr2 track event bocznica poranek.scn:5\n" + "event tor_1_zajety multiple event start_poranek poranek.scn:6\n" + "memcell sem_a_mem event launcher condition przycisk poranek.scn:10\n" + "memcell stacja_mem event target komorka poranek.scn:8\n"); + // the isolated circuit, the vehicle on its own, the trainset, the conditions of the events on a track and on a cell, the cell copied + // from, the track of a cell of the scenario; not the cell copied into, which is the scenario's + CHECK(described(index.scenarios[1].references) == + "track tor_1 isolated odc_1 wieczor.scn:5\n" + "track tor_2 vehicle stojak wieczor.scn:10\n" + "track tor_2 isolated odc_1 wieczor.scn:5\n" + "track tor_3 trainset wieczorny wieczor.scn:11\n" + "track tor_3 condition zajetosc wieczor.scn:8\n" + "track tor_3 memory cell track moja_mem wieczor.scn:4\n" + "event sem_a_s1 multiple event warunek wieczor.scn:7\n" + "event sem_a_sr2 multiple event zajetosc wieczor.scn:8\n" + "memcell sem_a_mem condition warunek wieczor.scn:7\n" + "memcell stacja_mem copy source kopia wieczor.scn:6\n"); + + // a file of the map is used by the same scenarios; a map nobody uses + auto const part{project::index_scenarios(folder(), "mapa/tory.scm")}; + CHECK(part.scenarios.size() == 2); + CHECK(part.events.empty()); + auto const none{project::index_scenarios(folder(), "nie/ma.scm")}; + CHECK(none.scenarios.empty()); + CHECK(none.tracks.empty()); +} + +TEST_CASE("scenario index: the scenery read as the simulator reads it, the includes followed with their parameters and placements") { + scenery_text::read_options options; + options.map = "mapa/mapa.scm"; + auto const scenery{scenery_text::read(folder(), "wieczor.scn", options)}; + CHECK(scenery.problems.empty()); + CHECK(scenery.uses_map()); + // the scenario, its file, the map and the files of the map + REQUIRE(scenery.files.size() == 6); + CHECK(scenery.files[0].name == "wieczor.scn"); + CHECK_FALSE(scenery.files[0].map); + CHECK(scenery.files[1].name == "wieczor/wstep.scm"); + CHECK_FALSE(scenery.files[1].map); + CHECK(scenery.files[2].name == "mapa/mapa.scm"); + CHECK(scenery.files[2].map); + CHECK(scenery.files[5].name == "mapa/semafor.inc"); + CHECK(scenery.files[5].map); + CHECK(scenery.files[5].parameters == std::vector{"sem_a", "10.0", "5.0", "tor_2"}); + CHECK(scenery.files[5].context.offset == glm::dvec3{100.0, 0.0, 0.0}); + CHECK(scenery.files[3].context.offset == glm::dvec3{0.0}); + // the memory cell of the template, its parameters in place + auto const cell{std::ranges::find_if(scenery.statements, [](scenery_text::statement const &Statement) { return Statement.keyword == "node" && Statement.word(2) == "sem_a_mem"; })}; + REQUIRE(cell != scenery.statements.end()); + CHECK(cell->map); + CHECK(cell->word(4) == "10.0"); + CHECK(cell->word(10) == "tor_2"); + CHECK(cell->words.back().text == "endmemcell"); + // a file which isn't there is told, the rest read + auto const missing{scenery_text::read(folder(), "brak.scn", options)}; + CHECK(missing.problems.size() == 1); + CHECK(missing.statements.empty()); + CHECK_FALSE(missing.uses_map()); +} + +TEST_CASE("scenario index: the words of the text as the parser splits them") { + auto const tokens{scenery_text::split("node -1 0 a;track // a comment\n\"two words\" /* a\ncomment */ endtrack//glued\nx")}; + REQUIRE(tokens.size() == 8); + CHECK(tokens[3].text == "a"); + CHECK(tokens[4].text == "track"); + CHECK(tokens[5].text == "\"two words\""); + CHECK(tokens[5].line == 2); + CHECK(tokens[6].text == "endtrack"); + CHECK(tokens[6].line == 3); + CHECK(tokens[7].text == "x"); + CHECK(tokens[7].line == 4); + CHECK(scenery_text::lowered("Tor_A:Event1 \xC5\xBB") == "tor_a:event1 \xC5\xBB"); + CHECK(scenery_text::same_file("Mapa\\Tory.SCM", "mapa/tory.scm")); +}