1 #ifndef LIBR2C2_TRAIN_H_
2 #define LIBR2C2_TRAIN_H_
4 #include <sigc++/signal.h>
5 #include <sigc++/trackable.h>
6 #include <msp/datafile/objectloader.h>
7 #include <msp/time/timestamp.h>
10 #include "controller.h"
22 class Train: public sigc::trackable
25 class Loader: public Msp::DataFile::ObjectLoader<Train>
34 virtual void finish();
36 void block_hint(unsigned);
37 void name(const std::string &);
38 void quantized_speed();
39 void route(const std::string &);
41 void vehicle(ArticleNumber);
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, TrainAI &, const TrainAI::Message &> signal_ai_event;
48 sigc::signal<void, const Route *> signal_route_changed;
49 sigc::signal<void, Block &> signal_advanced;
50 sigc::signal<void> signal_arrived;
51 sigc::signal<void, const std::string &> signal_status_changed;
59 RouteRef(const Route *, unsigned = 0);
62 typedef std::list<BlockIter> BlockList;
65 const VehicleType &loco_type;
70 const Train *yielding_to;
71 const Train *preceding_train;
72 std::vector<Vehicle *> vehicles;
74 BlockList::iterator cur_blocks_end;
75 BlockList::iterator clear_blocks_end;
79 Controller *controller;
80 std::list<TrainAI *> ais;
82 unsigned current_speed_step;
85 Msp::Time::TimeStamp stop_timeout;
87 std::list<RouteRef> routes;
90 Msp::Time::TimeStamp last_entry_time;
93 SpeedQuantizer *speed_quantizer;
94 bool accurate_position;
98 Train(Layout &, const VehicleType &, unsigned, const std::string &);
101 Layout &get_layout() const { return layout; }
102 const VehicleType &get_locomotive_type() const { return loco_type; }
103 unsigned get_address() const { return address; }
104 const std::string &get_protocol() const { return protocol; }
105 void set_name(const std::string &);
106 const std::string &get_name() const { return name; }
107 void set_priority(int);
108 void yield_to(const Train &);
109 int get_priority() const { return priority; }
110 const Train *get_preceding_train() const { return preceding_train; }
111 Controller &get_controller() const { return *controller; }
113 void add_vehicle(const VehicleType &);
114 void remove_vehicle(unsigned);
115 unsigned get_n_vehicles() const;
116 Vehicle &get_vehicle(unsigned);
117 const Vehicle &get_vehicle(unsigned) const;
119 void set_control(const std::string &, float);
120 void set_active(bool);
121 void set_function(unsigned, bool);
122 float get_control(const std::string &) const;
123 float get_speed() const;
124 float get_quantized_speed() const;
125 unsigned get_speed_step() const { return current_speed_step; }
126 bool is_active() const { return active; }
127 bool get_function(unsigned) const;
128 unsigned get_functions() const { return functions; }
130 void add_ai(TrainAI &);
131 void remove_ai(TrainAI &);
132 TrainAI *get_tagged_ai(const std::string &) const;
133 void ai_message(const TrainAI::Message &);
135 bool set_route(const Route *);
137 bool go_to(const Zone &);
138 bool divert(Track &);
139 const Route *get_route() const;
140 void place(Block &, unsigned);
142 bool is_placed() const { return !blocks.empty(); }
143 bool free_block(Block &);
144 void free_noncritical_blocks();
145 int get_entry_to_block(Block &) const;
146 float get_reserved_distance() const;
148 void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
150 void save(std::list<Msp::DataFile::Statement> &) const;
152 void control_changed(const Controller::Control &);
153 void loco_speed_event(unsigned, unsigned, bool);
154 void loco_func_event(unsigned, unsigned, bool);
155 void block_state_changed(Block &, Block::State);
156 void turnout_path_changed(Track &);
157 void halt_event(bool);
158 void block_reserved(const Block &, const Train *);
160 void check_turnout_paths(bool);
161 float get_reserved_distance_until(const Block *, bool) const;
162 void release_blocks();
163 void release_blocks(BlockList::iterator, BlockList::iterator);
164 void reverse_blocks(BlockList &) const;
165 bool advance_route(std::list<RouteRef>::iterator &, Track &);
166 Route *create_lead_route(Route *, const Route *);
167 bool is_valid_diversion(const Route &, const TrackIter &);