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