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