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