]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/timetable.h
TrainAI framework
[r2c2.git] / source / libr2c2 / timetable.h
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2010-2011  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 "trainai.h"
16
17 namespace R2C2 {
18
19 class Block;
20 class Track;
21 class Train;
22 class Zone;
23
24 class Timetable: public TrainAI, 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_str(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 tag(const std::string &);
40                 void travel_to(unsigned);
41                 void travel_past(unsigned);
42                 void wait(unsigned);
43                 void wait_train(unsigned, unsigned);
44                 void wait_until(unsigned, unsigned);
45         };
46
47         enum RowType
48         {
49                 GOTO_SENSOR,
50                 GOTO_ZONE,
51                 TRAVEL_TO,
52                 TRAVEL_PAST,
53                 WAIT_TIME,
54                 WAIT_UNTIL,
55                 WAIT_TRAIN,
56                 ARRIVE,
57                 SPEED,
58                 REVERSE,
59                 ROUTE
60         };
61
62         struct Row
63         {
64                 RowType type;
65                 std::vector<Msp::Variant> params;
66
67                 Row(RowType);
68
69                 template<typename T>
70                 Row(RowType, const T &);
71
72                 template<typename T>
73                 const T &get_param(unsigned) const;
74
75                 std::string str() const;
76
77                 Msp::DataFile::Statement save() const;
78
79                 static Row parse(const std::string &);
80         };
81
82 private:
83         bool enabled;
84         std::vector<Row> rows;
85         unsigned current_row;
86         bool executing;
87         Block *pending_block;
88         Train *pending_train;
89         Msp::Time::TimeStamp wait_timeout;
90         bool arrived;
91
92 public:
93         Timetable(Train &);
94
95         void set_enabled(bool);
96         bool is_enabled() const { return enabled; }
97         void reset();
98
99         void clear();
100         void append(const Row &);
101         void insert(unsigned, const Row &);
102         unsigned get_n_rows() const { return rows.size(); }
103         const Row &get_row(unsigned) const;
104
105         void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
106         void save(std::list<Msp::DataFile::Statement> &) const;
107 private:
108         Track &get_sensor(unsigned);
109         Track &get_turnout(unsigned);
110         Zone &get_zone(const std::string &);
111         void sensor_event(unsigned, bool);
112         void block_reserved(Block &, Train *);
113         void train_advanced(Block &);
114         void train_arrived();
115 };
116
117 } // namespace R2C2
118
119 #endif