]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/train.h
Add networking library and a remote control program
[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 Sensor;
20 class TrafficManager;
21
22 class Train: public sigc::trackable
23 {
24 public:
25         class Loader: public Msp::DataFile::BasicLoader<Train>
26         {
27         public:
28                 Loader(Train &);
29         };
30
31         sigc::signal<void, const std::string &> signal_name_changed;
32         sigc::signal<void, unsigned> signal_target_speed_changed;
33         sigc::signal<void, const std::string &> signal_status_changed;
34
35 private:
36         struct BlockRef
37         {
38                 Block *block;
39                 unsigned entry;
40
41                 BlockRef(Block *s, unsigned e): block(s), entry(e) { }
42         };
43
44         TrafficManager &trfc_mgr;
45         std::string name;
46         Locomotive &loco;
47         std::list<BlockRef> cur_blocks;
48         std::list<BlockRef> rsv_blocks;
49         unsigned target_speed;
50         Msp::Time::TimeStamp try_reserve;
51         std::string status;
52
53         Msp::Time::TimeStamp last_entry_time;
54         float travel_dist;
55         unsigned travel_speed;
56         bool pure_speed;
57         float speed_scale;
58         float speed_scale_weight;
59
60         Track *cur_track;
61         unsigned cur_track_ep;
62         float offset;
63         Point pos;
64
65 public:
66         Train(TrafficManager &, Locomotive &);
67
68         void set_name(const std::string &);
69         void set_speed(unsigned);
70         void set_reverse(bool);
71         const std::string &get_name() const { return name; }
72         Locomotive &get_locomotive() const { return loco; }
73         unsigned get_target_speed() const { return target_speed; }
74         const std::string &get_status() const { return status; }
75         const Point &get_position() const { return pos; }
76         void place(Block *, unsigned);
77         bool free_block(Block *);
78         void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
79         void save(std::list<Msp::DataFile::Statement> &) const;
80 private:
81         void locomotive_reverse_changed(bool);
82         void sensor_event(bool, Sensor *);
83         void turnout_route_changing(unsigned, Turnout *);
84         void turnout_route_changed(unsigned, Turnout *);
85         unsigned reserve_more();
86         void update_speed();
87         void set_status(const std::string &);
88         void set_position(const Block::Endpoint &);
89 };
90
91 } // namespace Marklin
92
93 #endif