]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/trainrouter.h
Sync timetable to clock on first tick
[r2c2.git] / source / libr2c2 / trainrouter.h
1 #ifndef LIBR2C2_TRAINROUTER_H_
2 #define LIBR2C2_TRAINROUTER_H_
3
4 #include <list>
5 #include <msp/datafile/objectloader.h>
6 #include "trainai.h"
7
8 namespace R2C2 {
9
10 class Block;
11 class Layout;
12 class Track;
13 class TrackChain;
14 class TrainRouteMetric;
15 class TrainRoutePlanner;
16
17 class TrainRouter: public TrainAI
18 {
19 public:
20         class Loader: public Msp::DataFile::ObjectLoader<TrainRouter>
21         {
22         public:
23                 Loader(TrainRouter &);
24         private:
25                 void route(const std::string &);
26         };
27
28         struct SequencePoint
29         {
30                 Block *block;
31                 Train *preceding_train;
32                 unsigned sequence_in;
33                 unsigned sequence_out;
34
35                 SequencePoint(Block &, unsigned);
36
37                 bool is_cleared() const;
38         };
39
40 private:
41         typedef std::list<const Route *> RouteList;
42
43         enum ArrivalState
44         {
45                 ON_THE_WAY,
46                 RESERVED_TO_END,
47                 ARRIVED
48         };
49
50 public:
51         sigc::signal<void, const Route *> signal_route_changed;
52         sigc::signal<void, const TrackChain *> signal_arrived;
53         sigc::signal<void, const TrackChain *> signal_waypoint_reached;
54
55 private:
56         int priority;
57         RouteList routes;
58         RouteList::iterator reserving_route;
59         ArrivalState arrival;
60         const TrackChain *destination;
61         std::vector<const TrackChain *> waypoints;
62         bool destination_changed;
63         std::vector<TrainRouteMetric *> metrics;
64         bool metrics_stale;
65         std::list<SequencePoint> sequence_points;
66         unsigned current_sequence;
67         bool sequence_check_pending;
68         Msp::Time::TimeDelta delay;
69         Msp::Time::TimeDelta duration;
70         Msp::RefPtr<TrainRoutePlanner> planner;
71
72 public:
73         TrainRouter(Train &);
74         ~TrainRouter();
75
76         void set_priority(int);
77         int get_priority() const { return priority; }
78
79         bool set_route(const Route *);
80         const Route *get_route() const;
81         unsigned get_current_sequence() const { return current_sequence; }
82 private:
83         void route_changed();
84
85 public:
86         void set_destination(const TrackChain &);
87         const TrackChain *get_destination() const { return destination; }
88         void add_waypoint(const TrackChain &);
89         unsigned get_n_waypoints() const { return waypoints.size(); }
90         const TrackChain &get_waypoint(unsigned) const;
91         const TrainRouteMetric &get_metric(int = -1) const;
92         void set_departure_delay(const Msp::Time::TimeDelta &);
93         const Msp::Time::TimeDelta &get_departure_delay() const { return delay; }
94         void set_trip_duration(const Msp::Time::TimeDelta &);
95         const Msp::Time::TimeDelta &get_trip_duration() const { return duration; }
96
97         virtual void message(const Message &);
98         virtual void tick(const Msp::Time::TimeDelta &);
99
100         void save(std::list<Msp::DataFile::Statement> &) const;
101
102 private:
103         void block_reserved(Block &, Train *);
104         void train_advanced(Block &);
105         void train_rear_advanced(Block &);
106
107         void create_metrics();
108         bool create_lead_route();
109         bool is_valid_for_track(const Route &, const TrackIter &) const;
110         bool advance_to_track(RouteList::iterator &, const TrackIter &);
111
112         static void start_planning(Layout &);
113 };
114
115 } // namespace R2C2
116
117 #endif