forked from eu07/maszyna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Event.h
753 lines (631 loc) · 21.8 KB
/
Event.h
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
/*
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 "Classes.h"
#include "scene.h"
#include "Names.h"
#include "EvLaunch.h"
#include "Logs.h"
#include "command.h"
#include "comparison.h"
#ifdef WITH_LUA
#include "lua.h"
#endif
// common event interface
class basic_event {
public:
// types
enum flags {
// shared values
text = 1 << 0,
value1 = 1 << 1,
value2 = 1 << 2,
// update values
mode_add = 1 << 3,
// whois
mode_alt = 1 << 3,
whois_load = 1 << 4,
whois_name = 1 << 5,
// condition values
track_busy = 1 << 3,
track_free = 1 << 4,
probability = 1 << 5
};
// constructor
basic_event() = default;
// destructor
virtual ~basic_event();
// methods
// restores event data from provided stream
virtual
void
deserialize( cParser &Input, scene::scratch_data &Scratchpad );
// prepares event for use
virtual
void
init() = 0;
// executes event
virtual
void
run();
// sends basic content of the class in legacy (text) format to provided stream
virtual
void
export_as_text( std::ostream &Output ) const;
// adds a sibling event executed together
void
append( basic_event *Event );
// returns: true if the event should be executed immediately
virtual
bool
is_instant() const;
// sends content of associated data cell to specified vehicle controller
virtual
void
send_command( TController &Controller );
// returns: true if associated data cell contains a command for vehicle controller
virtual
bool
is_command() const;
// input data access
virtual std::string input_text() const;
virtual TCommandType input_command() const;
virtual double input_value( int Index ) const;
virtual glm::dvec3 input_location() const;
void group( scene::group_handle Group );
scene::group_handle group() const;
std::string const &name() const { return m_name; }
// members
basic_event *m_next { nullptr }; // następny w kolejce // TODO: replace with event list in the manager
basic_event *m_sibling { nullptr }; // kolejny event z tą samą nazwą - od wersji 378
std::string m_name;
bool m_ignored { false }; // replacement for tp_ignored
bool m_passive { false }; // false gdy ma nie być dodawany do kolejki (skanowanie sygnałów)
int m_inqueue { 0 }; // ile razy dodany do kolejki
TDynamicObject const *m_activator { nullptr };
double m_launchtime { 0.0 };
double m_delay { 0.0 };
double m_delayrandom { 0.0 }; // zakres dodatkowego opóźnienia // standardowo nie będzie dodatkowego losowego opóźnienia
double m_delaydeparture { std::numeric_limits<double>::quiet_NaN() }; // departure-based event delay
protected:
// types
using basic_node = std::tuple<std::string, scene::basic_node *>;
using node_sequence = std::vector<basic_node>;
struct event_conditions {
unsigned int flags { 0 };
float probability { 0.0 }; // used by conditional_probability
double memcompare_value1 { 0.0 }; // used by conditional_memcompare
double memcompare_value2 { 0.0 }; // used by conditional_memcompare
std::string memcompare_text; // used by conditional_memcompare
comparison_operator memcompare_value1_operator { comparison_operator::equal }; // used by conditional_memcompare
comparison_operator memcompare_value2_operator { comparison_operator::equal }; // used by conditional_memcompare
comparison_operator memcompare_text_operator { comparison_operator::equal }; // used by conditional_memcompare
comparison_pass memcompare_pass { comparison_pass::all }; // used by conditional_memcompare
basic_event::node_sequence *memcompare_cells; // used by conditional_memcompare
std::vector<TTrack *> tracks; // used by conditional_track
bool has_else { false };
void deserialize( cParser &Input );
void bind( basic_event::node_sequence *Nodes );
void init();
// verifies whether event meets execution condition(s)
bool test() const;
// sends basic content of the class in legacy (text) format to provided stream
void export_as_text( std::ostream &Output ) const;
};
// methods
template <class TableType_>
void init_targets( TableType_ &Repository, std::string const &Targettype, bool const Logerrors = true );
// returns true if provided token is a an event desription keyword, false otherwise
static bool is_keyword( std::string const &Token );
// members
node_sequence m_targets; // targets of operation performed when this event is executed
private:
// methods
// event type string
virtual std::string type() const = 0;
// deserialization helper, converts provided string to a list of target nodes
virtual void deserialize_targets( std::string const &Input );
// deserialize() subclass details
virtual void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) = 0;
// run() subclass details
virtual void run_() = 0;
// export_as_text() subclass details
virtual void export_as_text_( std::ostream &Output ) const = 0;
// members
scene::group_handle m_group { null_handle }; // group this event belongs to, if any
};
// specialized event, sends received input to its target(s)
// TBD: replace the generic module with specialized mixins
class input_event : public basic_event {
friend basic_event * make_event( cParser &Input, scene::scratch_data &Scratchpad );
protected:
// types
struct input_data {
unsigned int flags { 0 };
std::string data_text;
double data_value_1 { 0.0 };
double data_value_2 { 0.0 };
basic_node data_source { "", nullptr };
glm::dvec3 location { 0.0 };
TCommandType command_type { TCommandType::cm_Unknown };
TMemCell const * data_cell() const;
TMemCell * data_cell();
};
// members
input_data m_input;
};
class updatevalues_event : public input_event {
public:
// methods
// prepares event for use
void init() override;
// returns: true if the event should be executed immediately
bool is_instant() const override;
private:
// methods
// event type string
std::string type() const override;
// deserialize() subclass details
void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) override;
// run() subclass details
void run_() override;
// export_as_text() subclass details
void export_as_text_( std::ostream &Output ) const override;
// members
event_conditions m_conditions;
};
class copyvalues_event : public input_event {
public:
// methods
// prepares event for use
void init() override;
private:
// methods
// event type string
std::string type() const override;
// deserialize() subclass details
void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) override;
// run() subclass details
void run_() override;
// export_as_text() subclass details
void export_as_text_( std::ostream &Output ) const override;
};
class getvalues_event : public input_event {
public:
// methods
// prepares event for use
void init() override;
// sends content of associated data cell to specified vehicle controller
void send_command( TController &Controller ) override;
// returns: true if associated data cell contains a command for vehicle controller
bool is_command() const override;
// input data access
std::string input_text() const override;
TCommandType input_command() const override;
double input_value( int Index ) const override;
glm::dvec3 input_location() const override;
private:
// methods
// event type string
std::string type() const override;
// deserialize() subclass details
void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) override;
// run() subclass details
void run_() override;
// export_as_text() subclass details
void export_as_text_( std::ostream &Output ) const override;
};
class putvalues_event : public input_event {
public:
// methods
// prepares event for use
void init() override;
// input data access
std::string input_text() const override;
TCommandType input_command() const override;
double input_value( int Index ) const override;
glm::dvec3 input_location() const override;
private:
// methods
// event type string
std::string type() const override;
// deserialize() subclass details
void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) override;
// run() subclass details
void run_() override;
// export_as_text() subclass details
void export_as_text_( std::ostream &Output ) const override;
//determines whether provided input should be passed to consist owner
bool is_command_for_owner( input_data const &Input ) const;
};
class whois_event : public input_event {
public:
// methods
// prepares event for use
void init() override;
private:
// methods
// event type string
std::string type() const override;
// deserialize() subclass details
void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) override;
// run() subclass details
void run_() override;
// export_as_text() subclass details
void export_as_text_( std::ostream &Output ) const override;
};
class logvalues_event : public basic_event {
public:
// methods
// prepares event for use
void init() override;
private:
// methods
// event type string
std::string type() const override;
// deserialize() subclass details
void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) override;
// run() subclass details
void run_() override;
// export_as_text() subclass details
void export_as_text_( std::ostream &Output ) const override;
};
class multi_event : public basic_event {
public:
// methods
// prepares event for use
void init() override;
std::vector<std::string> dump_children_names() const;
private:
// types
// wrapper for binding between editor-supplied name, event, and execution conditional flag
using conditional_event = std::tuple<std::string, basic_event *, bool>;
// methods
// event type string
std::string type() const override;
// deserialize() subclass details
void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) override;
// run() subclass details
void run_() override;
// export_as_text() subclass details
void export_as_text_( std::ostream &Output ) const override;
// members
std::vector<conditional_event> m_children; // events which are placed in the query when this event is executed
event_conditions m_conditions;
};
class sound_event : public basic_event {
public:
// methods
// prepares event for use
void init() override;
private:
// types
// wrapper for binding between editor-supplied name and sound object
using basic_sound = std::tuple<std::string, sound_source *>;
// methods
// event type string
std::string type() const override;
// deserialization helper, converts provided string to a list of target nodes
void deserialize_targets( std::string const &Input ) override;
// deserialize() subclass details
void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) override;
// run() subclass details
void run_() override;
// export_as_text() subclass details
void export_as_text_( std::ostream &Output ) const override;
// members
std::vector<basic_sound> m_sounds;
int m_soundmode{ 0 };
int m_soundradiochannel{ 0 };
};
// assigns a texture as specified replacable skin to a list of specified scene model nodes
// skin filename is built dynamically using specified expression and list of parameters from optional specified memory cell
class texture_event : public basic_event {
public:
// methods
// prepares event for use
void init() override;
private:
// types
struct input_data {
basic_node data_source { "", nullptr };
TMemCell const * data_cell() const;
TMemCell * data_cell();
};
// methods
// event type string
std::string type() const override;
// deserialize() subclass details
void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) override;
// run() subclass details
void run_() override;
// export_as_text() subclass details
void export_as_text_( std::ostream &Output ) const override;
// members
int m_skinindex { 0 }; // index of target replacable skin
std::string m_skin; // expression defining skin filename
input_data m_input; // optional source of expression parameters
};
class animation_event : public basic_event {
public:
// destuctor
~animation_event() override;
// methods
// prepares event for use
void init() override;
private:
// methods
// event type string
std::string type() const override;
// deserialize() subclass details
void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) override;
// run() subclass details
void run_() override;
// export_as_text() subclass details
void export_as_text_( std::ostream &Output ) const override;
// members
int m_animationtype{ 0 };
std::array<double, 4> m_animationparams{ 0.0 };
std::string m_animationsubmodel;
std::list<std::weak_ptr<TAnimContainer>> m_animationcontainers;
std::string m_animationfilename;
std::size_t m_animationfilesize{ 0 };
char *m_animationfiledata{ nullptr };
};
class lights_event : public basic_event {
public:
// methods
// prepares event for use
void init() override;
private:
// methods
// event type string
std::string type() const override;
// deserialize() subclass details
void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) override;
// run() subclass details
void run_() override;
// export_as_text() subclass details
void export_as_text_( std::ostream &Output ) const override;
// members
std::vector<float> m_lights;
};
class switch_event : public basic_event {
public:
// methods
// prepares event for use
void init() override;
private:
// methods
// event type string
std::string type() const override;
// deserialize() subclass details
void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) override;
// run() subclass details
void run_() override;
// export_as_text() subclass details
void export_as_text_( std::ostream &Output ) const override;
// members
int m_switchstate{ 0 };
float m_switchmoverate{ -1.f };
float m_switchmovedelay{ -1.f };
};
class track_event : public basic_event {
public:
// methods
// prepares event for use
void init() override;
private:
// methods
// event type string
std::string type() const override;
// deserialize() subclass details
void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) override;
// run() subclass details
void run_() override;
// export_as_text() subclass details
void export_as_text_( std::ostream &Output ) const override;
// members
float m_velocity{ 0.f };
};
class voltage_event : public basic_event {
public:
// methods
// prepares event for use
void init() override;
private:
// methods
// event type string
std::string type() const override;
// deserialize() subclass details
void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) override;
// run() subclass details
void run_() override;
// export_as_text() subclass details
void export_as_text_( std::ostream &Output ) const override;
// members
float m_voltage{ -1.f };
};
class visible_event : public basic_event {
public:
// methods
// prepares event for use
void init() override;
private:
// methods
// event type string
std::string type() const override;
// deserialize() subclass details
void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) override;
// run() subclass details
void run_() override;
// export_as_text() subclass details
void export_as_text_( std::ostream &Output ) const override;
// members
bool m_visible{ true };
};
class friction_event : public basic_event {
public:
// methods
// prepares event for use
void init() override;
private:
// methods
// event type string
std::string type() const override;
// deserialize() subclass details
void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) override;
// run() subclass details
void run_() override;
// export_as_text() subclass details
void export_as_text_( std::ostream &Output ) const override;
// members
float m_friction{ -1.f };
};
#ifdef WITH_LUA
class lua_event : public basic_event {
public:
lua_event(lua::eventhandler_t func);
void init() override;
private:
std::string type() const override;
void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) override;
void run_() override;
void export_as_text_( std::ostream &Output ) const override;
bool is_instant() const override;
lua::eventhandler_t lua_func = nullptr;
};
#endif
class message_event : public basic_event {
public:
// methods
// prepares event for use
void init() override;
private:
// methods
// event type string
std::string type() const override;
// deserialize() subclass details
void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) override;
// run() subclass details
void run_() override;
// export_as_text() subclass details
void export_as_text_( std::ostream &Output ) const override;
// members
std::string m_message;
};
basic_event *
make_event( cParser &Input, scene::scratch_data &Scratchpad );
class event_manager {
public:
// constructors
event_manager() = default;
// destructor
~event_manager();
// methods
// adds specified event launcher to the list of global launchers
void
queue( TEventLauncher *Launcher );
// inserts in the event query events assigned to event launchers capable of receiving specified radio message sent from specified location
void
queue_receivers( radio_message const Message, glm::dvec3 const &Location );
// legacy method, updates event queues
void
update();
// adds provided event to the collection. returns: true on success
// TBD, TODO: return handle to the event
bool
insert( basic_event *Event );
inline
bool
insert( TEventLauncher *Launcher ) {
return (
Launcher->IsRadioActivated() ?
m_radiodrivenlaunchers.insert( Launcher ) :
m_inputdrivenlaunchers.insert( Launcher ) ); }
inline void purge (TEventLauncher *Launcher) {
m_radiodrivenlaunchers.purge(Launcher);
m_inputdrivenlaunchers.purge(Launcher); }
// returns first event in the queue
inline
basic_event *
begin() {
return QueryRootEvent; }
basic_event*
FindEventById(uint32_t id);
uint32_t GetEventId(const basic_event *ev);
uint32_t GetEventId(std::string const &Name);
// legacy method, returns pointer to specified event, or null
basic_event *
FindEvent( std::string const &Name );
inline TEventLauncher* FindEventlauncher(std::string const &Name) {
auto ptr = m_inputdrivenlaunchers.find(Name);
return ptr ? ptr : m_radiodrivenlaunchers.find(Name);
}
// legacy method, inserts specified event in the event query
bool
AddToQuery( basic_event *Event, TDynamicObject const *Owner, double delay = 0.0 );
// legacy method, executes queued events
bool
CheckQuery();
// legacy method, initializes events after deserialization from scenario file
void
InitEvents();
// legacy method, initializes event launchers after deserialization from scenario file
void
InitLaunchers();
// sends basic content of the class in legacy (text) format to provided stream
void
export_as_text( std::ostream &Output ) const;
// returns all eventlaunchers in radius ignoring height
std::vector<TEventLauncher *>
find_eventlaunchers(glm::vec2 center, float radius) const;
private:
// types
using event_sequence = std::deque<basic_event *>;
using event_map = std::unordered_map<std::string, std::size_t>;
using eventlauncher_sequence = std::vector<TEventLauncher *>;
// members
event_sequence m_events;
/*
// NOTE: disabled until event class refactoring
event_sequence m_eventqueue;
*/
// legacy version of the above
basic_event *QueryRootEvent { nullptr };
basic_event *m_workevent { nullptr };
event_map m_eventmap;
basic_table<TEventLauncher> m_inputdrivenlaunchers;
basic_table<TEventLauncher> m_radiodrivenlaunchers;
eventlauncher_sequence m_launcherqueue;
command_relay m_relay;
};
inline
void
basic_event::group( scene::group_handle Group ) {
m_group = Group;
}
inline
scene::group_handle
basic_event::group() const {
return m_group;
}
template <class TableType_>
void
basic_event::init_targets( TableType_ &Repository, std::string const &Targettype, bool const Logerrors ) {
for( auto &target : m_targets ) {
std::get<scene::basic_node *>( target ) = Repository.find( std::get<std::string>( target ) );
if( std::get<scene::basic_node *>( target ) == nullptr ) {
m_ignored = true; // deaktywacja
if( Logerrors )
ErrorLog( "Bad event: \"" + m_name + "\" (type: " + type() + ") can't find " + Targettype +" \"" + std::get<std::string>( target ) + "\"" );
}
}
}
//---------------------------------------------------------------------------