]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/train.h
Full vehicle unification
[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
16 namespace Marklin {
17
18 class ControlModel;
19 class Route;
20 class Timetable;
21 class Vehicle;
22 class VehicleType;
23
24 class Train: public sigc::trackable
25 {
26 public:
27         class Loader: public Msp::DataFile::BasicLoader<Train>
28         {
29         private:
30                 Block *prev_block;
31                 bool blocks_valid;
32
33         public:
34                 Loader(Train &);
35         private:
36                 void block(unsigned);
37                 void block_hint(unsigned);
38                 void name(const std::string &);
39                 void real_speed(unsigned, float, float);
40                 void route(const std::string &);
41                 void timetable();
42                 void vehicle(unsigned);
43         };
44
45         sigc::signal<void, const std::string &> signal_name_changed;
46         sigc::signal<void, const std::string &, float> signal_control_changed;
47         sigc::signal<void, unsigned, bool> signal_function_changed;
48         sigc::signal<void, const Route *> signal_route_changed;
49         sigc::signal<void> signal_arrived;
50         sigc::signal<void, const std::string &> signal_status_changed;
51
52 private:
53         struct BlockRef
54         {
55                 Block *block;
56                 unsigned entry;
57
58                 BlockRef(Block *, unsigned);
59                 BlockRef next() const;
60         };
61
62         struct RealSpeed
63         {
64                 float speed;
65                 float weight;
66
67                 RealSpeed();
68                 void add(float, float);
69         };
70
71         Layout &layout;
72         const VehicleType &loco_type;
73         unsigned address;
74         std::string name;
75         int priority;
76         std::vector<Vehicle *> vehicles;
77         std::list<BlockRef> cur_blocks;
78         std::list<BlockRef> rsv_blocks;
79         Block *pending_block;
80         ControlModel *control;
81         Timetable *timetable;
82         bool active;
83         unsigned current_speed;
84         bool speed_changing;
85         bool reverse;
86         Msp::Time::TimeStamp stop_timeout;
87         unsigned functions;
88         const Route *route;
89         const Route *next_route;
90         bool end_of_route;
91         std::string status;
92
93         Msp::Time::TimeStamp last_entry_time;
94         float travel_dist;
95         bool pure_speed;
96         std::vector<RealSpeed> real_speed;
97         bool accurate_position;
98         float overshoot_dist;
99
100 public:
101         Train(Layout &, const VehicleType &, unsigned);
102         ~Train();
103
104         Layout &get_layout() const { return layout; }
105         const VehicleType &get_locomotive_type() const { return loco_type; }
106         unsigned get_address() const { return address; }
107         void set_name(const std::string &);
108         const std::string &get_name() const { return name; }
109         void set_priority(int);
110         int get_priority() const { return priority; }
111         ControlModel &get_control() const { return *control; }
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         bool is_active() const { return active; }
124         bool get_function(unsigned) const;
125         unsigned get_functions() const { return functions; }
126
127         void set_timetable(Timetable *);
128         Timetable *get_timetable() { return timetable; }
129
130         void set_route(const Route *);
131         void go_to(const Track &);
132         const Route *get_route() const { return route; }
133         void place(Block &, unsigned);
134         bool is_placed() const { return !cur_blocks.empty(); }
135         bool free_block(Block &);
136         int get_entry_to_block(Block &) const;
137         float get_reserved_distance() const;
138
139         const std::string &get_status() const { return status; }
140
141         void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
142
143         void save(std::list<Msp::DataFile::Statement> &) const;
144 private:
145         void loco_speed_event(unsigned, unsigned, bool);
146         void loco_func_event(unsigned, unsigned, bool);
147         void sensor_event(unsigned, bool);
148         void turnout_event(unsigned, bool);
149         void halt_event(bool);
150         void block_reserved(const Block &, const Train *);
151         unsigned reserve_more();
152         float get_real_speed(unsigned) const;
153         unsigned find_speed(float) const;
154         float get_travel_speed() const;
155         void set_status(const std::string &);
156         void release_blocks(std::list<BlockRef> &);
157         void release_blocks(std::list<BlockRef> &, std::list<BlockRef>::iterator, std::list<BlockRef>::iterator);
158         void reverse_blocks(std::list<BlockRef> &) const;
159 };
160
161 } // namespace Marklin
162
163 #endif