]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/timetable.h
Get rid of the TrainAI tagging system
[r2c2.git] / source / libr2c2 / timetable.h
1 #ifndef LIBR2C2_TIMETABLE_H_
2 #define LIBR2C2_TIMETABLE_H_
3
4 #include <string>
5 #include <vector>
6 #include <sigc++/trackable.h>
7 #include <msp/datafile/objectloader.h>
8 #include "block.h"
9 #include "trainai.h"
10
11 namespace R2C2 {
12
13 class Track;
14 class Train;
15 class Zone;
16
17 class Timetable: public TrainAI, public sigc::trackable
18 {
19 public:
20         class Loader: public Msp::DataFile::ObjectLoader<Timetable>
21         {
22         public:
23                 Loader(Timetable &);
24         private:
25                 void arrive();
26                 void goto_sensor(unsigned);
27                 void goto_sensor_str(const std::string &);
28                 void goto_zone(const std::string &);
29                 void route(const std::string &);
30                 void reverse();
31                 void speed(unsigned);
32                 void travel_to(unsigned);
33                 void travel_past(unsigned);
34                 void wait(unsigned);
35                 void wait_train(unsigned, unsigned);
36                 void wait_until(unsigned, unsigned);
37         };
38
39         enum RowType
40         {
41                 GOTO_SENSOR,
42                 GOTO_ZONE,
43                 TRAVEL_TO,
44                 TRAVEL_PAST,
45                 WAIT_TIME,
46                 WAIT_UNTIL,
47                 WAIT_TRAIN,
48                 ARRIVE,
49                 SPEED,
50                 REVERSE,
51                 ROUTE
52         };
53
54         struct Row
55         {
56                 RowType type;
57                 std::vector<Msp::Variant> params;
58
59                 Row(RowType);
60
61                 template<typename T>
62                 Row(RowType, const T &);
63
64                 template<typename T>
65                 const T &get_param(unsigned) const;
66
67                 std::string str() const;
68
69                 Msp::DataFile::Statement save() const;
70
71                 static Row parse(const std::string &);
72         };
73
74 private:
75         bool enabled;
76         std::vector<Row> rows;
77         unsigned current_row;
78         bool executing;
79         Block *pending_block;
80         Train *pending_train;
81         Msp::Time::TimeStamp wait_timeout;
82         bool arrived;
83
84 public:
85         Timetable(Train &);
86
87         void set_enabled(bool);
88         bool is_enabled() const { return enabled; }
89         void reset();
90
91         void clear();
92         void append(const Row &);
93         void insert(unsigned, const Row &);
94         unsigned get_n_rows() const { return rows.size(); }
95         const Row &get_row(unsigned) const;
96
97         void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
98         void save(std::list<Msp::DataFile::Statement> &) const;
99 private:
100         Track &get_sensor(unsigned);
101         Track &get_turnout(unsigned);
102         Zone &get_zone(const std::string &);
103         void block_state_changed(Block &, Block::State);
104         void block_reserved(Block &, Train *);
105         void train_advanced(Block &);
106         void event(TrainAI &, const Message &);
107 };
108
109 } // namespace R2C2
110
111 #endif