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