]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/train.h
TrainAI framework
[r2c2.git] / source / libr2c2 / train.h
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2006-2011  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #ifndef LIBR2C2_TRAIN_H_
9 #define LIBR2C2_TRAIN_H_
10
11 #include <sigc++/signal.h>
12 #include <sigc++/trackable.h>
13 #include <msp/time/timestamp.h>
14 #include "block.h"
15 #include "blockiter.h"
16 #include "controller.h"
17 #include "trainai.h"
18
19 namespace R2C2 {
20
21 class ArticleNumber;
22 class Route;
23 class SpeedQuantizer;
24 class Vehicle;
25 class VehicleType;
26 class Zone;
27
28 class Train: public sigc::trackable
29 {
30 public:
31         class Loader: public Msp::DataFile::BasicLoader<Train>
32         {
33         private:
34                 Block *prev_block;
35                 bool blocks_valid;
36
37         public:
38                 Loader(Train &);
39         private:
40                 virtual void finish();
41                 void block(unsigned);
42                 void block_hint(unsigned);
43                 void name(const std::string &);
44                 void quantized_speed();
45                 void route(const std::string &);
46                 void timetable();
47                 void vehicle(ArticleNumber);
48         };
49
50         sigc::signal<void, const std::string &> signal_name_changed;
51         sigc::signal<void, const std::string &, float> signal_control_changed;
52         sigc::signal<void, unsigned, bool> signal_function_changed;
53         sigc::signal<void, TrainAI &, const TrainAI::Message &> signal_ai_event;
54         sigc::signal<void, const Route *> signal_route_changed;
55         sigc::signal<void, Block &> signal_advanced;
56         sigc::signal<void> signal_arrived;
57         sigc::signal<void, const std::string &> signal_status_changed;
58
59 private:
60         struct RouteRef
61         {
62                 const Route *route;
63                 unsigned diversion;
64
65                 RouteRef(const Route *, unsigned = 0);
66         };
67
68         typedef std::list<BlockIter> BlockList;
69
70         Layout &layout;
71         const VehicleType &loco_type;
72         unsigned address;
73         std::string protocol;
74         std::string name;
75         int priority;
76         const Train *yielding_to;
77         const Train *preceding_train;
78         std::vector<Vehicle *> vehicles;
79         BlockList blocks;
80         BlockList::iterator cur_blocks_end;
81         BlockList::iterator clear_blocks_end;
82         Block *pending_block;
83         bool reserving;
84         bool advancing;
85         Controller *controller;
86         std::list<TrainAI *> ais;
87         bool active;
88         unsigned current_speed_step;
89         bool speed_changing;
90         bool reverse;
91         Msp::Time::TimeStamp stop_timeout;
92         unsigned functions;
93         std::list<RouteRef> routes;
94         bool end_of_route;
95         std::string status;
96
97         Msp::Time::TimeStamp last_entry_time;
98         float travel_dist;
99         bool pure_speed;
100         SpeedQuantizer *speed_quantizer;
101         bool accurate_position;
102         float overshoot_dist;
103
104 public:
105         Train(Layout &, const VehicleType &, unsigned, const std::string &);
106         ~Train();
107
108         Layout &get_layout() const { return layout; }
109         const VehicleType &get_locomotive_type() const { return loco_type; }
110         unsigned get_address() const { return address; }
111         const std::string &get_protocol() const { return protocol; }
112         void set_name(const std::string &);
113         const std::string &get_name() const { return name; }
114         void set_priority(int);
115         void yield_to(const Train &);
116         int get_priority() const { return priority; }
117         const Train *get_preceding_train() const { return preceding_train; }
118         Controller &get_controller() const { return *controller; }
119
120         void add_vehicle(const VehicleType &);
121         void remove_vehicle(unsigned);
122         unsigned get_n_vehicles() const;
123         Vehicle &get_vehicle(unsigned);
124         const Vehicle &get_vehicle(unsigned) const;
125
126         void set_control(const std::string &, float);
127         void set_active(bool);
128         void set_function(unsigned, bool);
129         float get_control(const std::string &) const;
130         float get_speed() const;
131         bool is_active() const { return active; }
132         bool get_function(unsigned) const;
133         unsigned get_functions() const { return functions; }
134
135         void add_ai(TrainAI &);
136         void remove_ai(TrainAI &);
137         TrainAI *get_tagged_ai(const std::string &);
138         void ai_message(const TrainAI::Message &);
139
140         bool set_route(const Route *);
141         bool go_to(Track &);
142         bool go_to(const Zone &);
143         bool divert(Track &);
144         const Route *get_route() const;
145         void place(Block &, unsigned);
146         void unplace();
147         bool is_placed() const { return !blocks.empty(); }
148         bool free_block(Block &);
149         void free_noncritical_blocks();
150         int get_entry_to_block(Block &) const;
151         float get_reserved_distance() const;
152
153         const std::string &get_status() const { return status; }
154
155         void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
156
157         void save(std::list<Msp::DataFile::Statement> &) const;
158 private:
159         void control_changed(const Controller::Control &);
160         void loco_speed_event(unsigned, unsigned, bool);
161         void loco_func_event(unsigned, unsigned, bool);
162         void sensor_event(unsigned, bool);
163         void turnout_path_changed(Track &);
164         void halt_event(bool);
165         void block_reserved(const Block &, const Train *);
166         void reserve_more();
167         void check_turnout_paths(bool);
168         float get_reserved_distance_until(const Block *, bool) const;
169         float get_real_speed(unsigned) const;
170         unsigned find_speed_step(float) const;
171         float get_travel_speed() const;
172         void set_status(const std::string &);
173         void release_blocks();
174         void release_blocks(BlockList::iterator, BlockList::iterator);
175         void reverse_blocks(BlockList &) const;
176         bool advance_route(std::list<RouteRef>::iterator &, Track &);
177         Route *create_lead_route(Route *, const Route *);
178         bool is_valid_diversion(const Route &, const TrackIter &);
179 };
180
181 } // namespace R2C2
182
183 #endif