+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2006-2010 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"
+#include "controller.h"
namespace Marklin {
-class Locomotive;
-class TrafficManager;
+class Route;
+class Timetable;
+class Vehicle;
+class VehicleType;
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;
+ bool blocks_valid;
+
+ public:
+ Loader(Train &);
+ private:
+ virtual void finish();
+ void block(unsigned);
+ void block_hint(unsigned);
+ void name(const std::string &);
+ void real_speed(unsigned, float, float);
+ void route(const std::string &);
+ void timetable();
+ void vehicle(unsigned);
+ };
+
+ sigc::signal<void, const std::string &> signal_name_changed;
+ sigc::signal<void, const std::string &, float> signal_control_changed;
+ sigc::signal<void, unsigned, bool> signal_function_changed;
+ sigc::signal<void, const Route *> signal_route_changed;
+ sigc::signal<void> signal_arrived;
+ sigc::signal<void, const std::string &> signal_status_changed;
+
private:
- struct SectionRef
+ struct BlockRef
+ {
+ Block *block;
+ unsigned entry;
+
+ BlockRef(Block *, unsigned);
+ BlockRef next() const;
+ };
+
+ 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;
+ Layout &layout;
+ const VehicleType &loco_type;
+ unsigned address;
std::string name;
- Locomotive &loco;
- SectRefSeq cur_sections;
- SectRefSeq rsv_sections;
- unsigned target_speed;
+ int priority;
+ const Train *yielding_to;
+ std::vector<Vehicle *> vehicles;
+ std::list<BlockRef> cur_blocks;
+ std::list<BlockRef> rsv_blocks;
+ Block *pending_block;
+ bool reserving;
+ bool advancing;
+ Controller *controller;
+ Timetable *timetable;
+ bool active;
+ unsigned current_speed;
+ bool speed_changing;
+ bool reverse;
+ Msp::Time::TimeStamp stop_timeout;
+ unsigned functions;
+ const Route *route;
+ const Route *next_route;
+ bool end_of_route;
+ std::string status;
+ Msp::Time::TimeStamp last_entry_time;
+ float travel_dist;
+ bool pure_speed;
+ std::vector<RealSpeed> real_speed;
+ bool accurate_position;
+ float overshoot_dist;
+
+public:
+ Train(Layout &, const VehicleType &, unsigned);
+ ~Train();
+
+ Layout &get_layout() const { return layout; }
+ const VehicleType &get_locomotive_type() const { return loco_type; }
+ unsigned get_address() const { return address; }
+ void set_name(const std::string &);
+ const std::string &get_name() const { return name; }
+ void set_priority(int);
+ void yield_to(const Train &);
+ int get_priority() const { return priority; }
+ Controller &get_controller() const { return *controller; }
+
+ void add_vehicle(const VehicleType &);
+ void remove_vehicle(unsigned);
+ unsigned get_n_vehicles() const;
+ Vehicle &get_vehicle(unsigned);
+ const Vehicle &get_vehicle(unsigned) const;
+
+ void set_control(const std::string &, float);
+ void set_active(bool);
+ void set_function(unsigned, bool);
+ float get_control(const std::string &) const;
+ float get_speed() const;
+ bool is_active() const { return active; }
+ bool get_function(unsigned) const;
+ unsigned get_functions() const { return functions; }
+
+ void set_timetable(Timetable *);
+ Timetable *get_timetable() { return timetable; }
+
+ void set_route(const Route *);
+ void go_to(const Track &);
+ const Route *get_route() const { return route; }
+ void place(Block &, unsigned);
+ bool is_placed() const { return !cur_blocks.empty(); }
+ bool free_block(Block &);
+ int get_entry_to_block(Block &) const;
+ float get_reserved_distance() const;
+
+ const std::string &get_status() const { return status; }
+
+ void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
+
+ void save(std::list<Msp::DataFile::Statement> &) const;
+private:
+ void control_changed(const Controller::Control &);
+ void loco_speed_event(unsigned, unsigned, bool);
+ void loco_func_event(unsigned, unsigned, bool);
void sensor_event(unsigned, bool);
- bool reserve_more();
+ void turnout_event(unsigned, bool);
+ void halt_event(bool);
+ void block_reserved(const Block &, const Train *);
+ unsigned reserve_more();
+ float get_reserved_distance_until(const Block *, bool) const;
+ float get_real_speed(unsigned) const;
+ unsigned find_speed(float) const;
+ float get_travel_speed() const;
+ void set_status(const std::string &);
+ void release_blocks(std::list<BlockRef> &);
+ void release_blocks(std::list<BlockRef> &, std::list<BlockRef>::iterator, std::list<BlockRef>::iterator);
+ void reverse_blocks(std::list<BlockRef> &) const;
};
-typedef std::list<Train *> TrainSeq;
} // namespace Marklin