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