]> git.tdb.fi Git - r2c2.git/blob - trainrouter.h
457120944627cb29b059e4622b787f2389927578
[r2c2.git] / 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         std::vector<const TrackChain *> waypoints;
61         bool waypoints_changed;
62         std::vector<TrainRouteMetric *> metrics;
63         bool metrics_stale;
64         std::list<SequencePoint> sequence_points;
65         unsigned current_sequence;
66         bool sequence_check_pending;
67         Msp::Time::TimeDelta delay;
68         Msp::Time::TimeDelta duration;
69         Msp::RefPtr<TrainRoutePlanner> planner;
70
71 public:
72         TrainRouter(Train &);
73         ~TrainRouter();
74
75         void set_priority(int);
76         int get_priority() const { return priority; }
77
78         bool set_route(const Route *);
79         const Route *get_route() const;
80         unsigned get_current_sequence() const { return current_sequence; }
81 private:
82         void use_planned_route();
83         void route_changed();
84
85 public:
86         void set_destination(const TrackChain &);
87         const TrackChain *get_destination() const { return waypoints.empty() ? 0 : waypoints.back(); }
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 get_routers(Layout &, std::vector<TrainRouter *> &);
113         static void start_planning(Layout &);
114         static void apply_plan(Layout &, TrainRoutePlanner &);
115 };
116
117 } // namespace R2C2
118
119 #endif