18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00
Files
maszyna/world/timetablereader.cpp
jerrrrycho 022e59fde3 feat(timetable): station names keep their polish letters, a W4 finds its station by one key
MaSzyna-EU07/maszyna#94. The timetable reader and the W4 event (putvalues PassengerStopPoint:<station>) threw
the polish letters of a name away as they read it. Now:

- the reader keeps the name as the file spells it in TMTableEntry::StationLabel and
  timetable_data::RelationLabel1/2 (TTrainParameters::RelationLabel1/2), for showing; StationName and
  Relation1/2 hold the ascii form as before, byte for byte, so the announcement files, the destination
  boards, the memory cells written by WhoIs events, the python dictionary, the log and the black box get
  what they always got
- the W4 event keeps its text as the scenery spells it (a scenery saved by the editor no longer loses the
  letters of its W4 events); the black box map still gets the ascii form
- world/stationnames.h has the one place which tells whether a W4 is for a station:
  timetable::stop_point_is_for(), used by TController::TableUpdateStopPoint() and
  TTrainParameters::RewindTimeTable(). It compares keys (ascii form, lower case, spaces as underscores), so
  the timetable and the W4 may each spell the station with or without the letters
- TTrainParameters::relation_caption() / station_caption() and TController::NextStop() give the names for
  the player: with the letters, in utf-8

The folding stays in the matching because the game data needs it: in 36 scenario x station pairs of the
Steam install (6 of the SVN) the timetable and the W4 spell the station differently (BALTYK_GLOWNY in
capitals against Baltyk_Glowny, Rzepedz with one of its two marks, Lupkow without them), and no
announcement file has a letter outside of ascii in its name.

Every W4 which matched a station still does. New matches, none of them in the game data: a memory cell
text spelled with polish letters, a space where the timetable has an underscore, a name longer than the 32
characters (sizeof(std::string)) the old comparison took. A memory cell text with a # part is still
compared whole.

win1250_to_ascii() builds its map once: the comparison runs it for every stop point a driver scans.
2026-11-18 23:50:07 +01:00

330 lines
10 KiB
C++

/*
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 "world/timetablereader.h"
#include "utilities/Logs.h"
#include "utilities/utilities.h"
#include "world/stationnames.h"
namespace Mtable {
namespace {
char const *const hrsd = ".";
// the rest of the row "Flagi" of the header after its label: flag entries up to the closing bracket. returns true when
// a row with no closing bracket ran into the line under the box
bool read_header_flags(std::istream &fin, timetable_data &Data) {
std::string s;
do {
fin >> s;
} while (!(s == "|" || fin.fail()));
while (fin >> s) {
if (s.starts_with('[')) {
// the next row, or the line under the box
ErrorLog("Bad timetable: row \"Flagi\" of " + Data.TrainName + " without the closing bracket");
return contains(s, "_______|");
}
auto const closing{s.ends_with(']')};
for (auto const &entry : Split(closing ? std::string_view{s}.substr(0, s.size() - 1) : std::string_view{s}, ',')) {
if (false == entry.empty() && false == timetable::read_flag(entry, Data.Flags)) {
ErrorLog("Bad timetable: unknown flag \"" + entry + "\" in row \"Flagi\" of " + Data.TrainName);
}
}
if (closing) {
break;
}
}
return false;
}
} // namespace
// the reader as TTrainParameters::LoadTTfile had it, but for: the row "Flagi" of the header and the flags of the
// station ware; the loops end with the end of the file, where they used to spin forever (on a truncated file, or one
// with a number where the reader expects it which isn't one). a file the old reader read gets read the same
int read_timetable(std::istream &fin, timetable_data &Data) {
std::string lines;
std::string s;
bool EndTable{false};
double vActual{-1};
int conversionerror{0};
auto &TimeTable{Data.TimeTable};
auto &StationCount{Data.StationCount};
// the table is read from its start, whatever Data held before
StationCount = 0;
TimeTable.fill(TMTableEntry{});
// zapisywanie prędkości maksymalnej do wcześniejszych odcinków
auto const UpdateVelocity{[&TimeTable, &StationCount](double const Velocity) {
int i = StationCount;
while (i >= 0 && TimeTable[i].vmax == -1) {
TimeTable[i].vmax = static_cast<float>(Velocity); // prędkość dojazdu do przystanku i
--i; // ewentualnie do poprzedniego też
}
}};
while (fin.good() && !(conversionerror != 0 || EndTable)) {
std::getline(fin, lines); /*wczytanie linii*/
if (contains(lines, "___________________")) /*linia pozioma górna*/
{
fin >> s;
if (s == "[") /*lewy pion*/
{
fin >> s;
if (s == "Rodzaj") /*"Rodzaj i numer pociagu"*/
do {
fin >> s;
} while (!(s == "|" || fin.fail())); /*środkowy pion*/
}
}
else if (lines == "") {
break;
}
fin >> s; /*nazwa pociągu*/
Data.TrainName = s; // nadanie nazwy z pliku TXT (bez ścieżki do pliku)
while (fin >> s) {
if (contains(s, "_______|")) {
break;
}
if (s == "Kategoria") {
do {
fin >> s;
} while (!(s == "|" || fin.fail()));
fin >> Data.TrainCategory;
continue;
}
if (s == "Nazwa") {
do {
fin >> s;
} while (!(s == "|" || fin.fail()));
fin >> Data.TrainLabel;
continue;
}
if (s == "Flagi") {
// service flags of the whole train (eu07/maszyna#95)
if (read_header_flags(fin, Data)) {
break;
}
continue;
}
}
{ /*czytaj naglowek*/
while (fin >> s) {
if (s == "[")
break;
} /*pierwsza linia z relacją*/
while (fin >> s) {
if (s != "|")
break;
}
if (s != "|") {
// the name is kept as written, for showing; the ascii form is what the rest of the simulator goes by
Data.RelationLabel1 = s;
Data.Relation1 = timetable::station_ascii(s);
}
else
conversionerror = -5;
while (fin >> s) {
if (s == "Relacja")
break;
} /*druga linia z relacją*/
while (fin >> s) {
if (s == "|")
break;
}
if (fin >> Data.RelationLabel2) {
Data.Relation2 = timetable::station_ascii(Data.RelationLabel2);
}
while (fin >> s) {
if (s == "Wymagany")
break;
}
while (fin >> s) {
if (s == "|" || s == "\n")
break;
}
fin >> s;
s = s.substr(0, s.find("%"));
Data.BrakeRatio = atof(s.c_str());
while (fin >> s) {
if (s == "Seria")
break;
}
do {
fin >> s;
} while (!(s == "|" || fin.fail()));
fin >> Data.LocSeries;
fin >> Data.LocLoad; // = s2rE(ReadWord(fin));
do {
fin >> s;
} while (!(contains(s, "[______________") || fin.fail()));
// the header's flags hold from the start, entry 0 keeps them for the stations to inherit
TimeTable[0].flags = Data.Flags;
auto activeradiochannel{-1};
while (!fin.fail() && !EndTable) {
// both bounds, so TimeTable[StationCount] and TimeTable[StationCount - 1] below stay in the table
if (StationCount < 0 || StationCount >= MaxTTableSize) {
conversionerror = timetable_too_long;
break;
}
++StationCount;
do {
fin >> s;
} while (!(s == "[" || fin.fail()));
TMTableEntry *record = &TimeTable[StationCount];
{
if (s == "[")
fin >> s;
else
conversionerror = -4;
if (false == contains(s, "|")) {
record->km = static_cast<float>(atof(s.c_str()));
fin >> s;
}
if (contains(s, "|_____|")) /*zmiana predkosci szlakowej*/
UpdateVelocity(vActual);
else {
fin >> s;
if (false == contains(s, "|"))
vActual = atof(s.c_str());
}
while (false == contains(s, "|") && false == fin.fail())
fin >> s;
// the name is kept as written, for showing; the W4 stop points, the announcement files, the scenery
// events and the scripts go by its ascii form, as they always did
if (fin >> record->StationLabel) {
record->StationName = timetable::station_ascii(record->StationLabel);
}
do {
fin >> s;
} while (!(s == "1" || s == "2" || fin.fail()));
record->TrackNo = atoi(s.c_str());
fin >> s;
if (s != "|") {
if (contains(s, hrsd)) {
record->Ah = atoi(s.substr(0, s.find(hrsd)).c_str()); // godzina przyjazdu
record->Am = static_cast<float>(atof(s.substr(s.find(hrsd) + 1, s.length()).c_str())); // minuta przyjazdu
}
else {
record->Ah = TimeTable[StationCount - 1].Ah; // godzina z poprzedniej pozycji
record->Am = static_cast<float>(atof(s.c_str())); // bo tylko minuty podane
}
}
do {
fin >> s;
} while (!(s != "|" || fin.fail()));
if (s != "]")
record->tm = static_cast<float>(atof(s.c_str()));
do {
fin >> s;
} while (!(s == "[" || fin.fail()));
fin >> s;
if (false == contains(s, "|")) {
/*tu s moze byc miejscem zmiany predkosci szlakowej*/
fin >> s;
}
if (s.find("|_____|") != std::string::npos) /*zmiana predkosci szlakowej*/
UpdateVelocity(vActual);
else {
fin >> s;
if (false == contains(s, "|"))
vActual = atof(s.c_str());
}
while (false == contains(s, "|") && false == fin.fail())
fin >> s;
// stationware. added fix for empty entry
fin >> s;
while (false == (s == "1" || s == "2" || fin.fail())) {
record->StationWare += s;
fin >> s;
}
// cache relevant station data
record->is_maintenance = contains(s, "pt");
// service flags (eu07/maszyna#95): the ones of the station before, changed by the ones given here
record->flags = TimeTable[StationCount - 1].flags;
record->StationWare = timetable::extract_flags(record->StationWare, record->flags);
{
auto const stationware{Split(record->StationWare, ',')};
for (auto const &entry : stationware) {
if (entry.front() != 'R') {
continue;
}
auto const [entrytype, entrynumber]{split_string_and_number(entry)};
if (entrytype == "R" && entrynumber <= 10) {
auto const radiochannel{entrynumber};
if (record->radio_channel == -1 || radiochannel != activeradiochannel) {
// if the station has more than one radiochannel listed,
// it generally means we should switch to the one we weren't using so far
// TODO: reverse this behaviour (keep the channel used so far) once W28 signs are included in the system
record->radio_channel = radiochannel;
}
}
}
if (record->radio_channel != -1) {
activeradiochannel = record->radio_channel;
}
}
record->TrackNo = atoi(s.c_str());
fin >> s;
if (s != "|") {
if (contains(s, hrsd)) {
record->Dh = atoi(s.substr(0, s.find(hrsd)).c_str()); // godzina odjazdu
record->Dm = static_cast<float>(atof(s.substr(s.find(hrsd) + 1, s.length()).c_str())); // minuta odjazdu
}
else {
record->Dh = TimeTable[StationCount - 1].Dh; // godzina z poprzedniej pozycji
record->Dm = static_cast<float>(atof(s.c_str())); // bo tylko minuty podane
}
}
else {
record->Dh = record->Ah; // odjazd o tej samej, co przyjazd (dla ostatniego też)
record->Dm = record->Am; // bo są używane do wyliczenia opóźnienia po dojechaniu
}
do {
fin >> s;
} while (!(s != "|" || fin.fail()));
if (s != "]")
record->tm = static_cast<float>(atof(s.c_str()));
do {
fin >> s;
} while (!(contains(s, "[") || fin.fail()));
if (false == contains(s, "_|_"))
fin >> s;
if (false == contains(s, "|")) {
/*tu s moze byc miejscem zmiany predkosci szlakowej*/
fin >> s;
}
if (contains(s, "|_____|")) /*zmiana predkosci szlakowej*/
UpdateVelocity(vActual);
else {
fin >> s;
if (false == contains(s, "|"))
vActual = atof(s.c_str());
}
while (false == contains(s, "|") && false == fin.fail())
fin >> s;
while (false == contains(s, "]") && false == fin.fail())
fin >> s;
if (contains(s, "_|_"))
EndTable = true;
} /*timetableline*/
}
if (false == EndTable && conversionerror == 0) {
// the old reader went on reading past the end of the file here, forever
conversionerror = timetable_truncated;
}
}
} /*while eof*/
return conversionerror;
}
} // namespace Mtable