]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/train.h
b579d7f37deba40ba47d607baabf0e42817e7de7
[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 LocoType;
19 class Route;
20
21 class Train: public sigc::trackable
22 {
23 public:
24         class Loader: public Msp::DataFile::BasicLoader<Train>
25         {
26         private:
27                 Block *prev_block;
28
29         public:
30                 Loader(Train &);
31         private:
32                 void block(unsigned);
33                 void block_hint(unsigned);
34                 void name(const std::string &);
35                 void real_speed(unsigned, float, float);
36                 void route(const std::string &);
37         };
38
39         sigc::signal<void, const std::string &> signal_name_changed;
40         sigc::signal<void, unsigned> signal_target_speed_changed;
41         sigc::signal<void, unsigned> signal_speed_changed;
42         sigc::signal<void, bool> signal_reverse_changed;
43         sigc::signal<void, unsigned, bool> signal_function_changed;
44         sigc::signal<void, const Route *> signal_route_changed;
45         sigc::signal<void, const std::string &> signal_status_changed;
46
47 private:
48         struct BlockRef
49         {
50                 Block *block;
51                 unsigned entry;
52
53                 BlockRef(Block *, unsigned);
54                 BlockRef next() const;
55         };
56
57         struct RealSpeed
58         {
59                 float speed;
60                 float weight;
61
62                 RealSpeed();
63                 void add(float, float);
64         };
65
66         Layout &layout;
67         const LocoType &loco_type;
68         unsigned address;
69         std::string name;
70         std::list<BlockRef> cur_blocks;
71         std::list<BlockRef> rsv_blocks;
72         Block *pending_block;
73         unsigned target_speed;
74         unsigned current_speed;
75         bool reverse;
76         Msp::Time::TimeStamp stop_timeout;
77         unsigned functions;
78         const Route *route;
79         const Route *next_route;
80         bool end_of_route;
81         std::string status;
82
83         Msp::Time::TimeStamp last_entry_time;
84         float travel_dist;
85         unsigned travel_speed;
86         bool pure_speed;
87         std::vector<RealSpeed> real_speed;
88
89         Track *cur_track;
90         unsigned cur_track_ep;
91         float offset;
92         Point pos;
93
94 public:
95         Train(Layout &, const LocoType &, unsigned);
96         ~Train();
97
98         const LocoType &get_locomotive_type() const { return loco_type; }
99         unsigned get_address() const { return address; }
100         void set_name(const std::string &);
101         const std::string &get_name() const { return name; }
102
103         void set_speed(unsigned);
104         void set_reverse(bool);
105         void set_function(unsigned, bool);
106         unsigned get_target_speed() const { return target_speed; }
107         unsigned get_speed() const { return current_speed; }
108         bool get_reverse() const { return reverse; }
109         bool get_function(unsigned) const;
110         unsigned get_functions() const { return functions; }
111
112         void set_route(const Route *);
113         void go_to(const Track &);
114         const Route *get_route() const { return route; }
115         void place(Block &, unsigned);
116         bool is_placed() const { return !cur_blocks.empty(); }
117         bool free_block(Block &);
118         int get_entry_to_block(Block &) const;
119
120         const std::string &get_status() const { return status; }
121         const Point &get_position() const { return pos; }
122
123         void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
124
125         void save(std::list<Msp::DataFile::Statement> &) const;
126 private:
127         void loco_speed_event(unsigned, unsigned, bool);
128         void loco_func_event(unsigned, unsigned, bool);
129         void sensor_event(unsigned, bool);
130         void turnout_event(unsigned, bool);
131         void block_reserved(const Block &, const Train *);
132         unsigned reserve_more();
133         void update_speed();
134         float get_real_speed(unsigned) const;
135         unsigned find_speed(float) const;
136         void set_status(const std::string &);
137         void set_position(const Block::Endpoint &);
138         void release_blocks(std::list<BlockRef> &);
139         void release_blocks(std::list<BlockRef> &, std::list<BlockRef>::iterator, std::list<BlockRef>::iterator);
140         void reverse_blocks(std::list<BlockRef> &) const;
141 };
142
143 } // namespace Marklin
144
145 #endif