18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 07:07:05 +01:00
Files
maszyna/world/eventconditions.cpp
T
pawelw 1a12c7ef4f fix(events): say in the log when a condition passes because it can't test, and when a call is dropped (#448)
* fix(events): say in the log when a condition passes because it can't test, and when a call is dropped

A condition which can't do its test passes, and nothing told the scenery
author. Now the log says so when the scenery loads:

- trackfree / trackoccupied with no target, or with a target that isn't a
  track (an isolated section, a memory cell): the track test passes every
  time;
- a word in a condition the reader doesn't know, such as a name written
  after trackfree: it's skipped;
- a multiple comparing a memory cell that doesn't exist: besides the cell
  (already logged), that its whole memcompare condition is off and it
  always runs the events before its else.

And log.txt says when an event called while it's still in the queue isn't
added again, with the vehicle of the lost call and of the queued one.

Nothing changes in how events run.

* fix(events): log a lost call once per stay in the queue, set the activator of a joint event

A standing vehicle queues its event0 / eventall0 every frame, so an event
with a delay logged "EVENT ALREADY IN QUEUE" once a frame until it ran
(65 thousand lines in 70 s of a train standing on such a track). Now a lost
call is logged only when it comes from another vehicle than the one which
queued the event, and once per stay in the queue.

An instant event handing over to its joint (same-name) event queued that
event with the vehicle of its previous run. It gets the vehicle of the
call now, which it runs with and which the log names.

The message about a missing memory cell says that only the memcompare test
is off, and that the event always runs the events before its else only when
no track or probability test is left.

* fix(events): don't log a lost call from another vehicle of the same train

A standing train calls its eventall0 from every vehicle, so the second
vehicle's call was logged once per stay in the queue, and as the line is
written once per stay, it could hide a lost call of another train standing
on another track with the same event. A call from a vehicle coupled to the
one which queued the event isn't logged now.

* test(events): the unknown word in a condition is logged with the event and skipped

A name written after trackfree is read as a word the condition reader
doesn't know: one line in the log names the event and the word, and only
the track test is set. The known words of a condition log nothing.

* test(events): turn the log capture off when a case stops at a failed REQUIRE
2026-11-27 23:51:42 +01:00

137 lines
5.6 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/.
*/
// the text of the event conditions: read from the scenery and written back by the editor. kept apart from
// Event.cpp so the unit tests can check that what is written reads back the same
#include "stdafx.h"
#include "world/Event.h"
#include "utilities/Logs.h"
#include <format>
bool
basic_event::is_keyword( std::string const &Token ) {
// TODO: convert to array lookup if keyword list gets longer
return Token == "endevent"
|| Token == "randomdelay"
|| Token == "departuredelay";
}
void
basic_event::event_conditions::deserialize( cParser &Input, std::string_view const Eventname ) {
// przetwarzanie warunków, wspólne dla Multiple i UpdateValues
std::string token;
while( true == Input.getTokens()
&& false == (token = Input.peek()).empty()
&& false == basic_event::is_keyword(token) ) {
if( token == "trackoccupied" ) {
flags |= flags::track_busy;
}
else if( token == "trackfree" ) {
flags |= flags::track_free;
}
else if( token == "propability" || token == "probability") { //remove propability in few years after changing old scenery scripts 01.2021
flags |= flags::probability;
Input.getTokens();
Input >> probability;
}
else if( token == "memcompare" ) {
Input.getTokens( 1, false ); // case sensitive
if( Input.peek() != "*" ) //"*" - nie brac command pod uwage
{ // zapamiętanie łańcucha do porównania
Input >> memcompare_text;
flags |= flags::text;
}
Input.getTokens();
if( Input.peek() != "*" ) //"*" - nie brac val1 pod uwage
{
Input >> memcompare_value1;
flags |= flags::value1;
}
Input.getTokens();
if( Input.peek() != "*" ) //"*" - nie brac val2 pod uwage
{
Input >> memcompare_value2;
flags |= flags::value2;
}
}
else if( token == "memcompareex" ) {
memcompare_pass = comparison_pass_from_string( Input.getToken<std::string>() );
Input.getTokens();
if( Input.peek() != "*" ) //"*" - nie brac pod uwage
{ // two tokens, operator followed by comparison value
std::string operatorstring;
Input >> operatorstring;
memcompare_text_operator = comparison_operator_from_string( operatorstring );
Input.getTokens( 1, false ); // case sensitive
Input >> memcompare_text;
flags |= flags::text;
}
Input.getTokens();
if( Input.peek() != "*" ) //"*" - nie brac pod uwage
{ // two tokens, operator followed by comparison value
std::string operatorstring;
Input >> operatorstring;
memcompare_value1_operator = comparison_operator_from_string( operatorstring );
Input.getTokens();
Input >> memcompare_value1;
flags |= flags::value1;
}
Input.getTokens();
if( Input.peek() != "*" ) //"*" - nie brac pod uwage
{ // two tokens, operator followed by comparison value
std::string operatorstring;
Input >> operatorstring;
memcompare_value2_operator = comparison_operator_from_string( operatorstring );
Input.getTokens();
Input >> memcompare_value2;
flags |= flags::value2;
}
}
else {
// e.g. a name written after trackfree: the track test takes the targets of the event, never a name of its own
ErrorLog( std::format( "Bad event: \"{}\" has an unknown word \"{}\" in its condition, it's skipped", Eventname, token ) );
}
}
}
// sends basic content of the class in legacy (text) format to provided stream
void
basic_event::event_conditions::export_as_text( std::ostream &Output ) const {
if( flags != 0 ) {
Output << "condition ";
if( ( flags & flags::track_busy ) != 0 ) {
Output << "trackoccupied ";
}
if( ( flags & flags::track_free ) != 0 ) {
Output << "trackfree ";
}
if( ( flags & flags::probability ) != 0 ) {
Output
<< "probability "
<< probability << ' ';
}
if( ( flags & ( flags::text | flags::value1 | flags::value2 ) ) != 0 ) {
// NOTE: export doesn't preserve original memcompare condition, these are all upgraded to memcompareex format for simplicity
// memcompareex <pass> then for the text and both values either * or the operator followed by the compared value,
// the order deserialize() reads them in. a legacy memcompare is the same as memcompareex all with all operators ==
Output
<< "memcompareex "
<< to_string( memcompare_pass ) << ' '
<< ( ( flags & flags::text ) == 0 ? "*" : to_string( memcompare_text_operator ) + ' ' + memcompare_text ) << ' '
<< ( ( flags & flags::value1 ) == 0 ? "*" : to_string( memcompare_value1_operator ) + ' ' + std::to_string( memcompare_value1 ) ) << ' '
<< ( ( flags & flags::value2 ) == 0 ? "*" : to_string( memcompare_value2_operator ) + ' ' + std::to_string( memcompare_value2 ) ) << ' ';
}
}
}