forked from tmj-fstate/maszyna
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Event.cpp
2256 lines (1937 loc) · 71.2 KB
/
Event.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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/.
*/
/*
MaSzyna EU07 locomotive simulator
Copyright (C) 2001-2004 Marcin Wozniak and others
*/
#include "stdafx.h"
#include "Event.h"
#include "simulation.h"
#include "messaging.h"
#include "Globals.h"
#include "MemCell.h"
#include "Track.h"
#include "Traction.h"
#include "TractionPower.h"
#include "sound.h"
#include "AnimModel.h"
#include "DynObj.h"
#include "Driver.h"
#include "Timer.h"
#include "Logs.h"
void
basic_event::event_conditions::bind( basic_event::node_sequence *Nodes ) {
cells = Nodes;
}
void
basic_event::event_conditions::init() {
tracks.clear();
if( flags & ( flags::track_busy | flags::track_free ) ) {
for( auto &target : *cells ) {
tracks.emplace_back( simulation::Paths.find( std::get<std::string>( target ) ) );
if( tracks.back() == nullptr ) {
// legacy compatibility behaviour, instead of disabling the event we disable the memory cell comparison test
// m_ignored = true; // deaktywacja
// ErrorLog( "Bad event: track \"" + std::get<std::string>( target ) + "\" referenced in event \"" + asName + "\" doesn't exist" );
flags &= ~( flags::track_busy | flags::track_free ); // zerowanie flag
}
}
}
}
bool
basic_event::event_conditions::test() const {
if( flags == 0 ) {
return true; // bezwarunkowo
}
// if there's conditions, check them
if( flags & flags::probability ) {
auto const randomroll { static_cast<float>( Random() ) };
WriteLog( "Test: Random integer - [" + std::to_string( randomroll ) + "] / [" + std::to_string( probability ) + "]" );
if( randomroll > probability ) {
return false;
}
}
if( flags & flags::track_busy ) {
for( auto *track : tracks ) {
if( true == track->IsEmpty() ) {
return false;
}
}
}
if( flags & flags::track_free ) {
for( auto *track : tracks ) {
if( false == track->IsEmpty() ) {
return false;
}
}
}
if( flags & ( flags::text | flags::value_1 | flags::value_2 ) ) {
// porównanie wartości
for( auto &cellwrapper : *cells ) {
auto *cell { static_cast<TMemCell *>( std::get<scene::basic_node *>( cellwrapper ) ) };
if( cell == nullptr ) {
// ErrorLog( "Event " + asName + " trying conditional_memcompare with nonexistent memcell" );
continue; // though this is technically error, we treat it as a success to maintain backward compatibility
}
auto const comparisonresult =
cell->Compare(
match_text,
match_value_1,
match_value_2,
flags );
std::string comparisonlog = "Test: MemCompare - " + cell->name() + " - ";
comparisonlog +=
"[" + cell->Text() + "]"
+ " [" + to_string( cell->Value1(), 2 ) + "]"
+ " [" + to_string( cell->Value2(), 2 ) + "]";
comparisonlog += (
true == comparisonresult ?
" == " :
" != " );
comparisonlog += (
TestFlag( flags, flags::text ) ?
"[" + std::string( match_text ) + "]" :
"[*]" );
comparisonlog += (
TestFlag( flags, flags::value_1 ) ?
" [" + to_string( match_value_1, 2 ) + "]" :
" [*]" );
comparisonlog += (
TestFlag( flags, flags::value_2 ) ?
" [" + to_string( match_value_2, 2 ) + "]" :
" [*]" );
WriteLog( comparisonlog );
if( false == comparisonresult ) {
return false;
}
}
}
// if we made it here it means we passed all checks
return true;
}
void
basic_event::event_conditions::deserialize( cParser &Input ) { // 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" ) {
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 >> match_text;
flags |= flags::text;
}
Input.getTokens();
if( Input.peek() != "*" ) //"*" - nie brac val1 pod uwage
{
Input >> match_value_1;
flags |= flags::value_1;
}
Input.getTokens();
if( Input.peek() != "*" ) //"*" - nie brac val2 pod uwage
{
Input >> match_value_2;
flags |= flags::value_2;
}
}
}
}
// 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
<< "propability "
<< probability << ' ';
}
if( ( flags & ( flags::text | flags::value_1 | flags::value_2 ) ) != 0 ) {
Output
<< "memcompare "
<< ( ( flags & flags::text ) == 0 ? "*" : match_text ) << ' '
<< ( ( flags & flags::value_1 ) == 0 ? "*" : to_string( match_value_1 ) ) << ' '
<< ( ( flags & flags::value_2 ) == 0 ? "*" : to_string( match_value_2 ) ) << ' ';
}
}
}
basic_event::~basic_event() {
m_sibling = nullptr; // nie usuwać podczepionych tutaj
}
void
basic_event::deserialize( cParser &Input, scene::scratch_data &Scratchpad ) {
std::string token;
Input.getTokens();
Input >> m_delay;
Input.getTokens();
Input >> token;
deserialize_targets( token );
if (m_name.substr(0, 5) == "none_")
m_ignored = true; // Ra: takie są ignorowane
deserialize_( Input, Scratchpad );
// subclass method is expected to leave next token past its own data preloaded on its exit
while( ( false == ( token = Input.peek() ).empty() )
&& ( token != "endevent" ) ) {
if( token == "randomdelay" ) { // losowe opóźnienie
Input.getTokens();
Input >> m_delayrandom; // Ra 2014-03-11
}
Input.getTokens();
}
}
void
basic_event::deserialize_targets( std::string const &Input ) {
cParser targetparser { Input };
std::string target;
while( false == ( target = targetparser.getToken<std::string>( true, "|," ) ).empty() ) {
// actual bindings to targets of proper type are created during scenario initialization
if( target != "none" ) {
m_targets.emplace_back( target, nullptr );
}
}
}
void
basic_event::run() {
WriteLog( "EVENT LAUNCHED" + ( m_activator ? ( " by " + m_activator->asName ) : "" ) + ": " + m_name );
run_();
}
// sends basic content of the class in legacy (text) format to provided stream
void
basic_event::export_as_text( std::ostream &Output ) const {
// header
Output << "event ";
// name
Output << m_name << ' ';
// type
Output << type() << ' ';
// delay
Output << m_delay << ' ';
// target node(s)
if( m_targets.empty() ) {
Output << "none";
}
else {
auto targetidx { 0 };
for( auto &target : m_targets ) {
auto *targetnode { std::get<scene::basic_node *>( target ) };
Output
<< ( targetnode != nullptr ? targetnode->name() : std::get<std::string>( target ) )
<< ( ++targetidx < m_targets.size() ? '|' : ' ' );
}
}
// type-specific attributes
export_as_text_( Output );
if( m_delayrandom != 0.0 ) {
Output
<< "randomdelay "
<< m_delayrandom << ' ';
}
// footer
Output
<< "endevent"
<< "\n";
}
void
basic_event::append( basic_event *Event ) {
// doczepienie kolejnych z tą samą nazwą
// TODO: remove recursion
if( m_sibling )
m_sibling->append( Event ); // rekurencja! - góra kilkanaście eventów będzie potrzebne
else {
m_sibling = Event;
Event->m_passive = false; // ten doczepiony może być tylko kolejkowany
}
};
// returns: true if the event should be executed immediately
bool
basic_event::is_instant() const {
return false;
}
// sends content of associated data cell to specified vehicle controller
void
basic_event::send_command( TController &Controller ) {
return;
}
// returns: true if associated data cell contains a command for vehicle controller
bool
basic_event::is_command() const {
return false;
};
std::string
basic_event::input_text() const {
// odczytanie komendy z eventu
return "";
};
TCommandType
basic_event::input_command() const {
// odczytanie komendy z eventu
return TCommandType::cm_Unknown;
};
double
basic_event::input_value( int Index ) const {
// odczytanie komendy z eventu
return 0.0;
};
glm::dvec3
basic_event::input_location() const {
// pobranie współrzędnych eventu
return glm::dvec3( 0, 0, 0 );
};
bool
basic_event::is_keyword( std::string const &Token ) {
return ( Token == "endevent" )
|| ( Token == "randomdelay" );
}
TMemCell const *
input_event::input_data::data_cell() const {
return static_cast<TMemCell const *>( std::get<scene::basic_node *>( data_source ) );
}
TMemCell *
input_event::input_data::data_cell() {
return static_cast<TMemCell *>( std::get<scene::basic_node *>( data_source ) );
}
// prepares event for use
void
updatevalues_event::init() {
// sumowanie wartości // zmiana wartości
init_targets( simulation::Memory, "memory cell" );
// conditional data
m_conditions.bind( &m_targets );
m_conditions.init();
}
// event type string
std::string
updatevalues_event::type() const {
return (
( m_input.flags & flags::mode_add ) == 0 ?
"updatevalues" :
"addvalues" );
}
// deserialize() subclass details
void
updatevalues_event::deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) {
Input.getTokens( 1, false ); // case sensitive
Input >> m_input.data_text;
if( m_input.data_text != "*" ) { //"*" - nie brac command pod uwage
m_input.flags |= flags::text;
}
Input.getTokens();
if( Input.peek() != "*" ) { //"*" - nie brac val1 pod uwage
Input >> m_input.data_value_1;
m_input.flags |= flags::value_1;
}
Input.getTokens();
if( Input.peek() != "*" ) { //"*" - nie brac val2 pod uwage
Input >> m_input.data_value_2;
m_input.flags |= flags::value_2;
}
Input.getTokens();
// optional blocks
std::string token;
while( ( false == ( token = Input.peek() ).empty() )
&& ( false == is_keyword( token ) ) ) {
if( token == "condition" ) {
m_conditions.deserialize( Input );
// NOTE: condition deserialization leaves preloaded next token
}
}
}
// run() subclass details
// TODO: update and copy values run_ methods are largely identical, refactor to a single helper
void
updatevalues_event::run_() {
if( false == m_conditions.test() ) { return; }
WriteLog( "Type: " + std::string( ( m_input.flags & flags::mode_add ) ? "AddValues" : "UpdateValues" ) + " & Track command - ["
+ ( ( m_input.flags & flags::text ) ? m_input.data_text : "X" ) + "] ["
+ ( ( m_input.flags & flags::value_1 ) ? to_string( m_input.data_value_1, 2 ) : "X" ) + "] ["
+ ( ( m_input.flags & flags::value_2 ) ? to_string( m_input.data_value_2, 2 ) : "X" ) + "]" );
// TODO: dump status of target cells after the operation
for( auto &target : m_targets ) {
auto *targetcell { static_cast<TMemCell *>( std::get<scene::basic_node *>( target ) ) };
if( targetcell == nullptr ) { continue; }
targetcell->UpdateValues(
m_input.data_text,
m_input.data_value_1,
m_input.data_value_2,
m_input.flags );
if( targetcell->Track == nullptr ) { continue; }
// McZapkie-100302 - updatevalues oprocz zmiany wartosci robi putcommand dla wszystkich 'dynamic' na danym torze
auto const location { targetcell->location() };
for( auto dynamic : targetcell->Track->Dynamics ) {
targetcell->PutCommand(
dynamic->Mechanik,
&location );
}
}
}
// export_as_text() subclass details
void
updatevalues_event::export_as_text_( std::ostream &Output ) const {
Output
<< ( ( m_input.flags & flags::text ) == 0 ? "*" : m_input.data_text ) << ' '
<< ( ( m_input.flags & flags::value_1 ) == 0 ? "*" : to_string( m_input.data_value_1 ) ) << ' '
<< ( ( m_input.flags & flags::value_2 ) == 0 ? "*" : to_string( m_input.data_value_2 ) ) << ' ';
m_conditions.export_as_text( Output );
}
// returns: true if the event should be executed immediately
bool
updatevalues_event::is_instant() const {
return ( ( ( m_input.flags & flags::mode_add ) != 0 ) && ( m_delay == 0.0 ) );
}
// prepares event for use
void
getvalues_event::init() {
init_targets( simulation::Memory, "memory cell" );
// custom target initialization code
for( auto &target : m_targets ) {
auto *targetcell { static_cast<TMemCell *>( std::get<scene::basic_node *>( target ) ) };
if( targetcell == nullptr ) { continue; }
if( targetcell->IsVelocity() ) {
// jeśli odczyt komórki a komórka zawiera komendę SetVelocity albo ShuntVelocity
// to event nie będzie dodawany do kolejki
m_passive = true;
}
}
// NOTE: GetValues retrieves data only from first specified memory cell
// TBD, TODO: allow retrieval from more than one cell?
m_input.data_source = m_targets.front();
}
// event type string
std::string
getvalues_event::type() const {
return "getvalues";
}
// deserialize() subclass details
void
getvalues_event::deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) {
// nothing to do here, just preload next token
Input.getTokens();
}
// run() subclass details
void
getvalues_event::run_() {
auto const *cell { m_input.data_cell() };
WriteLog( "Type: GetValues - "
+ cell->name() + " - ["
+ cell->Text() + "] ["
+ to_string( cell->Value1(), 2 ) + "] ["
+ to_string( cell->Value2(), 2 ) + "]" );
if( m_activator == nullptr ) { return; }
cell->PutCommand(
m_activator->Mechanik,
&( cell->location() ) );
// potwierdzenie wykonania dla serwera (odczyt semafora już tak nie działa)
if( Global.iMultiplayer ) {
multiplayer::WyslijEvent( m_name, m_activator->name() );
}
}
// export_as_text() subclass details
void
getvalues_event::export_as_text_( std::ostream &Output ) const {
// nothing to do here
}
// sends content of associated data cell to specified vehicle controller
void
getvalues_event::send_command( TController &Controller ) {
Controller.PutCommand( input_text(), input_value( 1 ), input_value( 2 ), nullptr );
m_input.data_cell()->StopCommandSent(); // komenda z komórki została wysłana
}
// returns: true if associated data cell contains a command for vehicle controller
bool
getvalues_event::is_command() const {
// info o komendzie z komórki
return m_input.data_cell()->StopCommand();
}
// input data access
std::string
getvalues_event::input_text() const {
return m_input.data_cell()->Text();
}
TCommandType
getvalues_event::input_command() const {
return m_input.data_cell()->Command();
}
double
getvalues_event::input_value( int Index ) const {
Index &= 1; // tylko 1 albo 2 jest prawidłowy
return ( Index == 1 ? m_input.data_cell()->Value1() : m_input.data_cell()->Value2() );
}
glm::dvec3
getvalues_event::input_location() const {
return m_input.data_cell()->location(); // współrzędne podłączonej komórki pamięci
}
// prepares event for use
void
putvalues_event::init() {
// nothing to do here
}
// event type string
std::string
putvalues_event::type() const {
return "putvalues";
}
// deserialize() subclass details
void
putvalues_event::deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) {
Input.getTokens( 3 );
// location, previously held in param 3, 4, 5
Input
>> m_input.location.x
>> m_input.location.y
>> m_input.location.z;
// przesunięcie
// tmp->pCenter.RotateY(aRotate.y/180.0*M_PI); //Ra 2014-11: uwzględnienie rotacji
// TBD, TODO: take into account rotation as well?
if( false == Scratchpad.location.offset.empty() ) {
m_input.location += Scratchpad.location.offset.top();
}
std::string token;
Input.getTokens( 1, false ); // komendy 'case sensitive'
Input >> token;
// command type, previously held in param 6
if( token.substr( 0, 19 ) == "PassengerStopPoint:" ) {
if( token.find( '#' ) != std::string::npos )
token.erase( token.find( '#' ) ); // obcięcie unikatowości
win1250_to_ascii( token ); // get rid of non-ascii chars
m_input.command_type = TCommandType::cm_PassengerStopPoint;
// nie do kolejki (dla SetVelocity też, ale jak jest do toru dowiązany)
m_passive = true;
}
else if( token == "SetVelocity" ) {
m_input.command_type = TCommandType::cm_SetVelocity;
m_passive = true;
}
else if( token == "RoadVelocity" ) {
m_input.command_type = TCommandType::cm_RoadVelocity;
m_passive = true;
}
else if( token == "SectionVelocity" ) {
m_input.command_type = TCommandType::cm_SectionVelocity;
m_passive = true;
}
else if( token == "ShuntVelocity" ) {
m_input.command_type = TCommandType::cm_ShuntVelocity;
m_passive = true;
}
else if( token == "OutsideStation" ) {
m_input.command_type = TCommandType::cm_OutsideStation;
m_passive = true; // ma być skanowny, aby AI nie przekraczało W5
}
else {
m_input.command_type = TCommandType::cm_Unknown;
}
// update data, previously stored in params 0, 1, 2
m_input.data_text = token;
Input.getTokens();
if( Input.peek() != "none" ) {
Input >> m_input.data_value_1;
}
Input.getTokens();
if( Input.peek() != "none" ) {
Input >> m_input.data_value_2;
}
// preload next token
Input.getTokens();
}
// run() subclass details
void
putvalues_event::run_() {
WriteLog(
"Type: PutValues - ["
+ m_input.data_text + "] ["
+ to_string( m_input.data_value_1, 2 ) + "] ["
+ to_string( m_input.data_value_2, 2 ) + "]" );
if( m_activator == nullptr ) { return; }
// zamiana, bo fizyka ma inaczej niż sceneria
// NOTE: y & z swap, negative x
TLocation const loc {
-m_input.location.x,
m_input.location.z,
m_input.location.y };
if( m_activator->Mechanik ) {
// przekazanie rozkazu do AI
m_activator->Mechanik->PutCommand(
m_input.data_text,
m_input.data_value_1,
m_input.data_value_2,
loc );
}
else {
// przekazanie do pojazdu
m_activator->MoverParameters->PutCommand(
m_input.data_text,
m_input.data_value_1,
m_input.data_value_2,
loc );
}
}
// export_as_text() subclass details
void
putvalues_event::export_as_text_( std::ostream &Output ) const {
Output
// location
<< m_input.location.x << ' '
<< m_input.location.y << ' '
<< m_input.location.z << ' '
// command
<< m_input.data_text << ' '
<< m_input.data_value_1 << ' '
<< m_input.data_value_2 << ' ';
}
// input data access
std::string
putvalues_event::input_text() const {
return m_input.data_text;
}
TCommandType
putvalues_event::input_command() const {
return m_input.command_type; // komenda zakodowana binarnie
}
double
putvalues_event::input_value( int Index ) const {
Index &= 1; // tylko 1 albo 2 jest prawidłowy
return ( Index == 1 ? m_input.data_value_1 : m_input.data_value_2 );
}
glm::dvec3
putvalues_event::input_location() const {
return m_input.location;
}
// prepares event for use
void
copyvalues_event::init() {
// skopiowanie komórki do innej
init_targets( simulation::Memory, "memory cell" );
// source cell
std::get<scene::basic_node *>( m_input.data_source ) = simulation::Memory.find( std::get<std::string>( m_input.data_source ) );
if( std::get<scene::basic_node *>( m_input.data_source ) == nullptr ) {
m_ignored = true; // deaktywacja
ErrorLog( "Bad event: \"" + m_name + "\" (type: " + type() + ") can't find memory cell \"" + std::get<std::string>( m_input.data_source ) + "\"" );
}
}
// event type string
std::string
copyvalues_event::type() const {
return "copyvalues";
}
// deserialize() subclass details
void
copyvalues_event::deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) {
m_input.flags = ( flags::text | flags::value_1 | flags::value_2 ); // normalnie trzy
std::string token;
int paramidx { 0 };
while( ( true == Input.getTokens() )
&& ( false == ( token = Input.peek() ).empty() )
&& ( false == is_keyword( token ) ) ) {
Input >> token;
switch( ++paramidx ) {
case 1: { // nazwa drugiej komórki (źródłowej) // previously stored in param 9
std::get<std::string>( m_input.data_source ) = token;
break;
}
case 2: { // maska wartości
m_input.flags = stol_def( token, ( flags::text | flags::value_1 | flags::value_2 ) );
break;
}
default: {
break;
}
}
}
}
// run() subclass details
// TODO: update and copy values run_ methods are largely identical, refactor to a single helper
void
copyvalues_event::run_() {
// skopiowanie wartości z innej komórki
auto const *datasource { static_cast<TMemCell *>( std::get<scene::basic_node *>( m_input.data_source ) ) };
m_input.data_text = datasource->Text();
m_input.data_value_1 = datasource->Value1();
m_input.data_value_2 = datasource->Value2();
WriteLog( "Type: CopyValues - ["
+ ( ( m_input.flags & flags::text ) ? m_input.data_text : "X" ) + "] ["
+ ( ( m_input.flags & flags::value_1 ) ? to_string( m_input.data_value_1, 2 ) : "X" ) + "] ["
+ ( ( m_input.flags & flags::value_2 ) ? to_string( m_input.data_value_2, 2 ) : "X" ) + "]" );
// TODO: dump status of target cells after the operation
for( auto &target : m_targets ) {
auto *targetcell { static_cast<TMemCell *>( std::get<scene::basic_node *>( target ) ) };
if( targetcell == nullptr ) { continue; }
targetcell->UpdateValues(
m_input.data_text,
m_input.data_value_1,
m_input.data_value_2,
m_input.flags );
if( targetcell->Track == nullptr ) { continue; }
// McZapkie-100302 - updatevalues oprocz zmiany wartosci robi putcommand dla wszystkich 'dynamic' na danym torze
auto const location { targetcell->location() };
for( auto dynamic : targetcell->Track->Dynamics ) {
targetcell->PutCommand(
dynamic->Mechanik,
&location );
}
}
}
// export_as_text() subclass details
void
copyvalues_event::export_as_text_( std::ostream &Output ) const {
auto const *datasource { static_cast<TMemCell *>( std::get<scene::basic_node *>( m_input.data_source ) ) };
Output
<< ( datasource != nullptr ?
datasource->name() :
std::get<std::string>( m_input.data_source ) )
<< ' ' << ( m_input.flags & ( flags::text | flags::value_1 | flags::value_2 ) ) << ' ';
}
// prepares event for use
void
whois_event::init() {
init_targets( simulation::Memory, "memory cell" );
}
// event type string
std::string
whois_event::type() const {
return "whois";
}
// deserialize() subclass details
void
whois_event::deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) {
m_input.flags = ( flags::text | flags::value_1 | flags::value_2 ); // normalnie trzy
std::string token;
int paramidx { 0 };
while( ( true == Input.getTokens() )
&& ( false == ( token = Input.peek() ).empty() )
&& ( false == is_keyword( token ) ) ) {
Input >> token;
switch( ++paramidx ) {
case 1: { // maska wartości
m_input.flags = stol_def( token, ( flags::text | flags::value_1 | flags::value_2 ) );
break;
}
default: {
break;
}
}
}
}
// run() subclass details
void
whois_event::run_() {
for( auto &target : m_targets ) {
auto *targetcell { static_cast<TMemCell *>( std::get<scene::basic_node *>( target ) ) };
if( targetcell == nullptr ) { continue; }
// event effect code
// +24: vehicle type, consist brake level, obstacle distance
// +16: load type, load amount, max load amount
// +8: destination, direction, engine power
// +0: train name, station count, stop on next station
if( m_input.flags & flags::load ) {
// +16 or +24
// jeśli pytanie o ładunek
if( m_input.flags & flags::mode_alt ) {
// jeśli typ pojazdu
// TODO: define and recognize individual request types
auto const owner { (
( ( m_activator->Mechanik != nullptr ) && ( m_activator->Mechanik->primary() ) ) ?
m_activator->Mechanik :
m_activator->ctOwner ) };
auto const consistbrakelevel { (
owner != nullptr ?
owner->fReady :
-1.0 ) };
auto const collisiondistance { (
owner != nullptr ?
owner->TrackBlock() :
-1.0 ) };
targetcell->UpdateValues(
m_activator->MoverParameters->TypeName, // typ pojazdu
consistbrakelevel,
collisiondistance,
m_input.flags & ( flags::text | flags::value_1 | flags::value_2 ) );
WriteLog(
"Type: WhoIs (" + to_string( m_input.flags ) + ") - "
+ "[type: " + m_activator->MoverParameters->TypeName + "], "
+ "[consist brake level: " + to_string( consistbrakelevel, 2 ) + "], "
+ "[obstacle distance: " + to_string( collisiondistance, 2 ) + " m]" );
}
else {
// jeśli parametry ładunku
targetcell->UpdateValues(
m_activator->MoverParameters->LoadType.name, // nazwa ładunku
m_activator->MoverParameters->LoadAmount, // aktualna ilość
m_activator->MoverParameters->MaxLoad, // maksymalna ilość
m_input.flags & ( flags::text | flags::value_1 | flags::value_2 ) );
WriteLog(
"Type: WhoIs (" + to_string( m_input.flags ) + ") - "
+ "[load type: " + m_activator->MoverParameters->LoadType.name + "], "
+ "[current load: " + to_string( m_activator->MoverParameters->LoadAmount, 2 ) + "], "
+ "[max load: " + to_string( m_activator->MoverParameters->MaxLoad, 2 ) + "]" );
}
}
// +8
else if( m_input.flags & flags::mode_alt ) { // jeśli miejsce docelowe pojazdu
targetcell->UpdateValues(
m_activator->asDestination, // adres docelowy
m_activator->DirectionGet(), // kierunek pojazdu względem czoła składu (1=zgodny,-1=przeciwny)
m_activator->MoverParameters->Power, // moc pojazdu silnikowego: 0 dla wagonu
m_input.flags & ( flags::text | flags::value_1 | flags::value_2 ) );
WriteLog(
"Type: WhoIs (" + to_string( m_input.flags ) + ") - "
+ "[destination: " + m_activator->asDestination + "], "
+ "[direction: " + to_string( m_activator->DirectionGet() ) + "], "
+ "[engine power: " + to_string( m_activator->MoverParameters->Power, 2 ) + "]" );
}
// +0
else if( m_activator->Mechanik ) {
if( m_activator->Mechanik->primary() ) { // tylko jeśli ktoś tam siedzi - nie powinno dotyczyć pasażera!
targetcell->UpdateValues(
m_activator->Mechanik->TrainName(),
m_activator->Mechanik->StationCount() - m_activator->Mechanik->StationIndex(), // ile przystanków do końca
m_activator->Mechanik->IsStop() ?
1 :
0, // 1, gdy ma tu zatrzymanie
m_input.flags );
WriteLog(
"Type: WhoIs (" + to_string( m_input.flags ) + ") - "
+ "[train: " + m_activator->Mechanik->TrainName() + "], "
+ "[stations left: " + to_string( m_activator->Mechanik->StationCount() - m_activator->Mechanik->StationIndex() ) + "], "
+ "[stop at next: " + ( m_activator->Mechanik->IsStop() ? "yes" : "no") + "]" );
}
}
}
}
// export_as_text() subclass details
void
whois_event::export_as_text_( std::ostream &Output ) const {
Output << ( m_input.flags & ( flags::text | flags::value_1 | flags::value_2 ) ) << ' ';
}
// prepares event for use
void
logvalues_event::init() {
init_targets( simulation::Memory, "memory cell" );
}
// event type string
std::string
logvalues_event::type() const {
return "logvalues";
}
// deserialize() subclass details
void
logvalues_event::deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) {
// nothing to do here, just preload next token
Input.getTokens();