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