]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/train.h
Get rid of the TrainAI tagging system
[r2c2.git] / source / libr2c2 / train.h
1 #ifndef LIBR2C2_TRAIN_H_
2 #define LIBR2C2_TRAIN_H_
3
4 #include <sigc++/signal.h>
5 #include <sigc++/trackable.h>
6 #include <msp/datafile/objectloader.h>
7 #include <msp/time/timestamp.h>
8 #include "block.h"
9 #include "blockiter.h"
10 #include "controller.h"
11 #include "trainai.h"
12
13 namespace R2C2 {
14
15 class ArticleNumber;
16 class SpeedQuantizer;
17 class Vehicle;
18 class VehicleType;
19
20 class Train: public sigc::trackable
21 {
22 public:
23         class Loader: public Msp::DataFile::ObjectLoader<Train>
24         {
25         private:
26                 Block *prev_block;
27                 bool blocks_valid;
28
29         public:
30                 Loader(Train &);
31         private:
32                 virtual void finish();
33                 void block(unsigned);
34                 void block_hint(unsigned);
35                 void name(const std::string &);
36                 void quantized_speed();
37                 void router();
38                 void timetable();
39                 void vehicle(ArticleNumber);
40         };
41
42         sigc::signal<void, const std::string &> signal_name_changed;
43         sigc::signal<void, const std::string &, float> signal_control_changed;
44         sigc::signal<void, unsigned, bool> signal_function_changed;
45         sigc::signal<void, TrainAI &, const TrainAI::Message &> signal_ai_event;
46         sigc::signal<void, Block &> signal_advanced;
47
48 private:
49         typedef std::list<BlockIter> BlockList;
50
51         Layout &layout;
52         const VehicleType &loco_type;
53         unsigned address;
54         std::string protocol;
55         std::string name;
56         const Train *preceding_train;
57         std::vector<Vehicle *> vehicles;
58         BlockList blocks;
59         BlockList::iterator cur_blocks_end;
60         Block *pending_block;
61         Block *stop_at_block;
62         bool reserving;
63         bool advancing;
64         Controller *controller;
65         std::list<TrainAI *> ais;
66         bool active;
67         unsigned current_speed_step;
68         bool speed_changing;
69         bool reverse;
70         Msp::Time::TimeStamp stop_timeout;
71         unsigned functions;
72
73         Msp::Time::TimeStamp last_entry_time;
74         float travel_dist;
75         bool pure_speed;
76         SpeedQuantizer *speed_quantizer;
77         bool accurate_position;
78         float overshoot_dist;
79
80 public:
81         Train(Layout &, const VehicleType &, unsigned, const std::string &);
82         ~Train();
83
84         Layout &get_layout() const { return layout; }
85         const VehicleType &get_locomotive_type() const { return loco_type; }
86         unsigned get_address() const { return address; }
87         const std::string &get_protocol() const { return protocol; }
88         void set_name(const std::string &);
89         const std::string &get_name() const { return name; }
90         const Train *get_preceding_train() const { return preceding_train; }
91         Controller &get_controller() const { return *controller; }
92
93         void add_vehicle(const VehicleType &);
94         void remove_vehicle(unsigned);
95         unsigned get_n_vehicles() const;
96         Vehicle &get_vehicle(unsigned);
97         const Vehicle &get_vehicle(unsigned) const;
98
99         void set_control(const std::string &, float);
100         void set_active(bool);
101         void set_function(unsigned, bool);
102         float get_control(const std::string &) const;
103         float get_speed() const;
104         float get_quantized_speed() const;
105         unsigned get_speed_step() const { return current_speed_step; }
106         bool is_active() const { return active; }
107         bool get_function(unsigned) const;
108         unsigned get_functions() const { return functions; }
109
110         void add_ai(TrainAI &);
111         void remove_ai(TrainAI &);
112         void ai_message(const TrainAI::Message &);
113
114         template<typename T>
115         T *get_ai_of_type() const
116         {
117                 for(std::list<TrainAI *>::const_iterator i=ais.begin(); i!=ais.end(); ++i)
118                         if(T *ai = dynamic_cast<T *>(*i))
119                                 return ai;
120                 return 0;
121         }
122
123         void place(Block &, unsigned);
124         void unplace();
125         bool is_placed() const { return !blocks.empty(); }
126         void stop_at(Block *);
127         bool free_block(Block &);
128         void free_noncritical_blocks();
129         const BlockIter &get_head_block() const;
130         const BlockIter &get_tail_block() const;
131         int get_entry_to_block(const Block &) const;
132         float get_reserved_distance() const;
133
134         void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
135
136         void save(std::list<Msp::DataFile::Statement> &) const;
137 private:
138         void control_changed(const Controller::Control &);
139         void loco_speed_event(unsigned, unsigned, bool);
140         void loco_func_event(unsigned, unsigned, bool);
141         void block_state_changed(Block &, Block::State);
142         void turnout_path_changed(Track &);
143         void halt_event(bool);
144         void block_reserved(const Block &, const Train *);
145 public:
146         void reserve_more();
147 private:
148         float get_reserved_distance_until(const Block *, bool) const;
149         void release_blocks();
150         void release_blocks(BlockList::iterator, BlockList::iterator);
151         void reverse_blocks(BlockList &) const;
152 };
153
154 } // namespace R2C2
155
156 #endif