]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/trainrouteplanner.h
New routing system for trains
[r2c2.git] / source / libr2c2 / trainrouteplanner.h
1 #ifndef LIBR2C2_TRAINROUTEPLANNER_H_
2 #define LIBR2C2_TRAINROUTEPLANNER_H_
3
4 #include <list>
5 #include <vector>
6 #include <msp/time/timedelta.h>
7 #include "trackiter.h"
8
9 namespace R2C2 {
10
11 class Layout;
12 class Route;
13 class Track;
14 class Train;
15 class TrainRouter;
16
17 class TrainRoutePlanner
18 {
19 private:
20         struct TrainRoutingState;
21
22         struct TrainRoutingInfo
23         {
24                 Train *train;
25                 TrainRouter *router;
26                 Route *route;
27                 std::list<TrainRoutingState *> waits;
28
29                 TrainRoutingInfo(Train &);
30         };
31
32         struct OccupiedTrack
33         {
34                 Track *track;
35                 float path_length;
36                 OccupiedTrack *next;
37                 unsigned n_tracks;
38                 unsigned refcount;
39
40                 OccupiedTrack(Track &, unsigned, OccupiedTrack *);
41                 OccupiedTrack(const OccupiedTrack &);
42                 ~OccupiedTrack();
43         };
44
45         enum TrainState
46         {
47                 MOVING,
48                 WAITING,
49                 BLOCKED,
50                 ARRIVED
51         };
52
53         struct TrainRoutingState
54         {
55                 TrainRoutingInfo *info;
56                 TrackIter track;
57                 unsigned path;
58                 OccupiedTrack *occupied_tracks;
59                 float offset;
60                 float back_offset;
61                 TrainState state;
62
63                 TrainRoutingState(TrainRoutingInfo &);
64                 TrainRoutingState(const TrainRoutingState &);
65                 ~TrainRoutingState();
66
67                 Msp::Time::TimeDelta get_time_to_next_track() const;
68                 bool is_occupied(Track &) const;
69                 void advance(float);
70                 void advance_track(unsigned);
71         };
72
73         struct RoutingStep
74         {
75                 Msp::Time::TimeDelta time;
76                 std::vector<TrainRoutingState> trains;
77                 RoutingStep *prev;
78
79                 RoutingStep();
80                 RoutingStep(RoutingStep *);
81
82                 void advance(const Msp::Time::TimeDelta &);
83                 bool is_goal() const;
84
85                 bool operator<(const RoutingStep &) const;
86         };
87
88         std::vector<TrainRoutingInfo> routed_trains;
89         std::list<RoutingStep> steps;
90
91 public:
92         TrainRoutePlanner(Layout &);
93
94         void plan();
95 private:
96         bool update_states(RoutingStep &);
97         int find_next_train(RoutingStep &);
98         void add_steps(RoutingStep &, unsigned);
99         void add_waiting_step(RoutingStep &, unsigned);
100         void add_steps(RoutingStep &, TrainRoutingState &train);
101         void create_routes(RoutingStep &);
102 };
103
104 } // namespace R2C2
105
106 #endif