+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
#ifndef LIBMARKLIN_TRAIN_H_
#define LIBMARKLIN_TRAIN_H_
+#include <sigc++/signal.h>
#include <sigc++/trackable.h>
-#include "section.h"
+#include <msp/time/timestamp.h>
+#include "block.h"
namespace Marklin {
class Locomotive;
+class Route;
+class Sensor;
class TrafficManager;
+class Turnout;
class Train: public sigc::trackable
{
public:
- Train(TrafficManager &, Locomotive &);
- const std::string &get_name() const { return name; }
- void set_speed(unsigned);
- void place(Section *, const Section::Endpoint *);
- bool free_section(Section *);
- void tick();
+ class Loader: public Msp::DataFile::BasicLoader<Train>
+ {
+ private:
+ Block *prev_block;
+
+ public:
+ Loader(Train &);
+ private:
+ void block(unsigned);
+ void block_hint(unsigned);
+ void real_speed(unsigned, float, float);
+ void route(const std::string &);
+ };
+
+ sigc::signal<void, const std::string &> signal_name_changed;
+ sigc::signal<void, unsigned> signal_target_speed_changed;
+ sigc::signal<void, const Route *> signal_route_changed;
+ sigc::signal<void, const std::string &> signal_status_changed;
+
private:
- struct SectionRef
+ struct BlockRef
+ {
+ Block *block;
+ unsigned entry;
+
+ BlockRef(Block *s, unsigned e): block(s), entry(e) { }
+ };
+
+ struct RealSpeed
{
- Section *section;
- const Section::Endpoint *entry;
+ float speed;
+ float weight;
- SectionRef(Section *s, const Section::Endpoint *e): section(s), entry(e) { }
+ RealSpeed();
+ void add(float, float);
};
- typedef std::list<SectionRef> SectRefSeq;
TrafficManager &trfc_mgr;
std::string name;
Locomotive &loco;
- SectRefSeq cur_sections;
- SectRefSeq rsv_sections;
+ std::list<BlockRef> cur_blocks;
+ std::list<BlockRef> rsv_blocks;
+ Block *pending_block;
unsigned target_speed;
+ const Route *route;
+ Msp::Time::TimeStamp try_reserve;
+ std::string status;
- void sensor_event(unsigned, bool);
- bool reserve_more();
+ Msp::Time::TimeStamp last_entry_time;
+ float travel_dist;
+ unsigned travel_speed;
+ bool pure_speed;
+ std::vector<RealSpeed> real_speed;
+
+ Track *cur_track;
+ unsigned cur_track_ep;
+ float offset;
+ Point pos;
+
+public:
+ Train(TrafficManager &, Locomotive &);
+
+ void set_name(const std::string &);
+ void set_speed(unsigned);
+ void set_reverse(bool);
+ void set_route(const Route *);
+ const std::string &get_name() const { return name; }
+ Locomotive &get_locomotive() const { return loco; }
+ unsigned get_target_speed() const { return target_speed; }
+ const Route *get_route() const { return route; }
+ const std::string &get_status() const { return status; }
+ const Point &get_position() const { return pos; }
+ void place(Block &, unsigned);
+ bool is_placed() const { return !cur_blocks.empty(); }
+ bool free_block(Block &);
+ int get_entry_to_block(Block &) const;
+ void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
+ void save(std::list<Msp::DataFile::Statement> &) const;
+private:
+ void locomotive_reverse_changed(bool);
+ void sensor_event(bool, Sensor *);
+ void turnout_path_changing(unsigned, Turnout *);
+ void turnout_path_changed(unsigned, Turnout *);
+ unsigned reserve_more();
+ void update_speed();
+ float get_real_speed(unsigned) const;
+ unsigned find_speed(float) const;
+ void set_status(const std::string &);
+ void set_position(const Block::Endpoint &);
+ void release_blocks(std::list<BlockRef> &);
+ void reverse_blocks(std::list<BlockRef> &) const;
};
-typedef std::list<Train *> TrainSeq;
} // namespace Marklin