]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/train.h
Change a few functions in Train to take Block reference instead of pointer
[r2c2.git] / source / libmarklin / train.h
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009 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 Locomotive;
19 class Route;
20 class Sensor;
21 class TrafficManager;
22 class Turnout;
23
24 class Train: public sigc::trackable
25 {
26 public:
27         class Loader: public Msp::DataFile::BasicLoader<Train>
28         {
29         public:
30                 Loader(Train &);
31         private:
32                 void real_speed(unsigned, float, float);
33         };
34
35         sigc::signal<void, const std::string &> signal_name_changed;
36         sigc::signal<void, unsigned> signal_target_speed_changed;
37         sigc::signal<void, const Route *> signal_route_changed;
38         sigc::signal<void, const std::string &> signal_status_changed;
39
40 private:
41         struct BlockRef
42         {
43                 Block *block;
44                 unsigned entry;
45
46                 BlockRef(Block *s, unsigned e): block(s), entry(e) { }
47         };
48
49         struct RealSpeed
50         {
51                 float speed;
52                 float weight;
53
54                 RealSpeed();
55                 void add(float, float);
56         };
57
58         TrafficManager &trfc_mgr;
59         std::string name;
60         Locomotive &loco;
61         std::list<BlockRef> cur_blocks;
62         std::list<BlockRef> rsv_blocks;
63         Block *pending_block;
64         unsigned target_speed;
65         const Route *route;
66         Msp::Time::TimeStamp try_reserve;
67         std::string status;
68
69         Msp::Time::TimeStamp last_entry_time;
70         float travel_dist;
71         unsigned travel_speed;
72         bool pure_speed;
73         std::vector<RealSpeed> real_speed;
74
75         Track *cur_track;
76         unsigned cur_track_ep;
77         float offset;
78         Point pos;
79
80 public:
81         Train(TrafficManager &, Locomotive &);
82
83         void set_name(const std::string &);
84         void set_speed(unsigned);
85         void set_reverse(bool);
86         void set_route(const Route *);
87         const std::string &get_name() const { return name; }
88         Locomotive &get_locomotive() const { return loco; }
89         unsigned get_target_speed() const { return target_speed; }
90         const Route *get_route() const { return route; }
91         const std::string &get_status() const { return status; }
92         const Point &get_position() const { return pos; }
93         void place(Block &, unsigned);
94         bool free_block(Block &);
95         int get_entry_to_block(Block &) const;
96         void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
97         void save(std::list<Msp::DataFile::Statement> &) const;
98 private:
99         void locomotive_reverse_changed(bool);
100         void sensor_event(bool, Sensor *);
101         void turnout_path_changing(unsigned, Turnout *);
102         void turnout_path_changed(unsigned, Turnout *);
103         unsigned reserve_more();
104         void update_speed();
105         float get_real_speed(unsigned) const;
106         unsigned find_speed(float) const;
107         void set_status(const std::string &);
108         void set_position(const Block::Endpoint &);
109         void release_blocks(std::list<BlockRef> &);
110 };
111
112 } // namespace Marklin
113
114 #endif