]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/train.h
Change the control system to separate speed and reverse
[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 struct TrainControl;
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 BlockRef
56         {
57                 Block *block;
58                 unsigned entry;
59
60                 BlockRef(Block *, unsigned);
61                 BlockRef next() const;
62         };
63
64         struct RealSpeed
65         {
66                 float speed;
67                 float weight;
68
69                 RealSpeed();
70                 void add(float, float);
71         };
72
73         Layout &layout;
74         const VehicleType &loco_type;
75         unsigned address;
76         std::string name;
77         int priority;
78         std::vector<Vehicle *> vehicles;
79         std::list<BlockRef> cur_blocks;
80         std::list<BlockRef> rsv_blocks;
81         Block *pending_block;
82         ControlModel *control;
83         Timetable *timetable;
84         bool active;
85         unsigned current_speed;
86         bool speed_changing;
87         bool reverse;
88         Msp::Time::TimeStamp stop_timeout;
89         unsigned functions;
90         const Route *route;
91         const Route *next_route;
92         bool end_of_route;
93         std::string status;
94
95         Msp::Time::TimeStamp last_entry_time;
96         float travel_dist;
97         bool pure_speed;
98         std::vector<RealSpeed> real_speed;
99         bool accurate_position;
100         float overshoot_dist;
101
102 public:
103         Train(Layout &, const VehicleType &, unsigned);
104         ~Train();
105
106         Layout &get_layout() const { return layout; }
107         const VehicleType &get_locomotive_type() const { return loco_type; }
108         unsigned get_address() const { return address; }
109         void set_name(const std::string &);
110         const std::string &get_name() const { return name; }
111         void set_priority(int);
112         int get_priority() const { return priority; }
113         ControlModel &get_control_model() const { return *control; }
114
115         void add_vehicle(const VehicleType &);
116         void remove_vehicle(unsigned);
117         unsigned get_n_vehicles() const;
118         Vehicle &get_vehicle(unsigned);
119         const Vehicle &get_vehicle(unsigned) const;
120
121         void set_control(const std::string &, float);
122         void set_active(bool);
123         void set_function(unsigned, bool);
124         float get_control(const std::string &) const;
125         float get_speed() const;
126         bool is_active() const { return active; }
127         bool get_function(unsigned) const;
128         unsigned get_functions() const { return functions; }
129
130         void set_timetable(Timetable *);
131         Timetable *get_timetable() { return timetable; }
132
133         void set_route(const Route *);
134         void go_to(const Track &);
135         const Route *get_route() const { return route; }
136         void place(Block &, unsigned);
137         bool is_placed() const { return !cur_blocks.empty(); }
138         bool free_block(Block &);
139         int get_entry_to_block(Block &) const;
140         float get_reserved_distance() const;
141
142         const std::string &get_status() const { return status; }
143
144         void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
145
146         void save(std::list<Msp::DataFile::Statement> &) const;
147 private:
148         void control_changed(const TrainControl &);
149         void loco_speed_event(unsigned, unsigned, bool);
150         void loco_func_event(unsigned, unsigned, bool);
151         void sensor_event(unsigned, bool);
152         void turnout_event(unsigned, bool);
153         void halt_event(bool);
154         void block_reserved(const Block &, const Train *);
155         unsigned reserve_more();
156         float get_real_speed(unsigned) const;
157         unsigned find_speed(float) const;
158         float get_travel_speed() const;
159         void set_status(const std::string &);
160         void release_blocks(std::list<BlockRef> &);
161         void release_blocks(std::list<BlockRef> &, std::list<BlockRef>::iterator, std::list<BlockRef>::iterator);
162         void reverse_blocks(std::list<BlockRef> &) const;
163 };
164
165 } // namespace Marklin
166
167 #endif