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