5 #include "trainroutemetric.h"
6 #include "trainrouteplanner.h"
7 #include "trainrouter.h"
15 TrainRoutePlanner::TrainRoutePlanner(Layout &layout)
17 const map<unsigned, Train *> &trains = layout.get_trains();
18 for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
20 TrainRoutingInfo info(*i->second);
21 if(info.router && info.router->get_destination())
22 routed_trains.push_back(info);
25 queue.push_back(RoutingStep());
26 RoutingStep &start = queue.back();
27 for(vector<TrainRoutingInfo>::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i)
28 start.trains.push_back(TrainRoutingState(*i));
29 start.update_estimate();
32 void TrainRoutePlanner::plan()
34 const RoutingStep *goal = 0;
37 const RoutingStep &step = get_step();
51 const TrainRoutePlanner::RoutingStep &TrainRoutePlanner::get_step()
53 steps.splice(steps.end(), queue, queue.begin());
57 void TrainRoutePlanner::add_steps(const RoutingStep &step)
59 list<RoutingStep> new_steps;
60 step.create_successors(new_steps);
62 queue.merge(new_steps);
65 void TrainRoutePlanner::create_routes(const RoutingStep &goal)
67 for(vector<TrainRoutingInfo>::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i)
69 Route *route = new Route(i->train->get_layout());
70 route->set_name("Router");
71 route->set_temporary(true);
72 i->routes.push_front(route);
74 for(unsigned j=0; j<3; ++j)
75 i->track_history[j] = 0;
78 for(const RoutingStep *i=&goal; i; i=i->prev)
80 for(vector<TrainRoutingState>::const_iterator j=i->trains.begin(); j!=i->trains.end(); ++j)
82 Track **history = j->info->track_history;
83 if(j->track.track()==history[0])
86 if(j->state==WAITING || j->state==BLOCKED)
87 j->info->waits.push_front(&*j);
89 Route *route = j->info->routes.front();
90 if(route->has_track(*j->track))
92 route = new Route(j->info->train->get_layout());
93 route->set_name("Router");
94 route->set_temporary(true);
95 for(unsigned k=2; k>0; --k)
96 route->add_track(*history[k]);
97 j->info->routes.push_front(route);
101 route->add_track(*history[0]);
102 for(unsigned k=2; k>0; --k)
103 history[k] = history[k-1];
104 history[0] = j->track.track();
108 for(vector<TrainRoutingInfo>::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i)
110 for(list<Route *>::iterator j=i->routes.begin(); j!=i->routes.end(); ++j)
111 i->router->add_route(**j);
112 const TrainRoutingState *current_wait = 0;
113 for(list<const TrainRoutingState *>::const_iterator j=i->waits.begin(); j!=i->waits.end(); ++j)
114 if(!current_wait || (*j)->track.track()!=current_wait->track.track())
116 Block &block = (*j)->track.next()->get_block();
117 i->router->add_wait(block, 0);
124 TrainRoutePlanner::TrainRoutingInfo::TrainRoutingInfo(Train &t):
126 speed(train->get_maximum_speed()),
127 router(train->get_ai_of_type<TrainRouter>())
129 // If no maximum speed is specified, use a sensible default
131 speed = 20*train->get_layout().get_catalogue().get_scale();
135 TrainRoutePlanner::OccupiedTrack::OccupiedTrack(Track &t, unsigned p, OccupiedTrack *n):
137 path_length(track->get_type().get_path_length(p)),
139 n_tracks(next ? next->n_tracks+1 : 1),
146 TrainRoutePlanner::OccupiedTrack::OccupiedTrack(const OccupiedTrack &other):
148 path_length(other.path_length),
150 n_tracks(other.n_tracks),
157 TrainRoutePlanner::OccupiedTrack::~OccupiedTrack()
159 if(next && !--next->refcount)
164 TrainRoutePlanner::TrainRoutingState::TrainRoutingState(TrainRoutingInfo &inf):
168 delay(info->router->get_departure_delay()),
169 waypoint(info->router->get_n_waypoints() ? 0 : -1),
172 const Vehicle *veh = &info->train->get_vehicle(0);
174 TrackOffsetIter track_and_offs = veh->get_placement().get_position(VehiclePlacement::FRONT_BUFFER);
175 track = track_and_offs.track_iter();
176 offset = track_and_offs.offset();
177 path = track->get_active_path();
179 while(Vehicle *next = veh->get_link(1))
181 track_and_offs = veh->get_placement().get_position(VehiclePlacement::BACK_BUFFER);
182 back_offset = track_and_offs.offset();
184 TrackIter iter = track_and_offs.track_iter();
187 occupied_tracks = new OccupiedTrack(*iter, iter->get_active_path(), occupied_tracks);
188 if(iter.track()==track.track())
196 TrainRoutePlanner::TrainRoutingState::TrainRoutingState(const TrainRoutingState &other):
200 occupied_tracks(other.occupied_tracks),
201 offset(other.offset),
202 back_offset(other.back_offset),
205 waypoint(other.waypoint),
206 remaining_estimate(other.remaining_estimate),
207 blocked_by(other.blocked_by)
209 ++occupied_tracks->refcount;
212 TrainRoutePlanner::TrainRoutingState::~TrainRoutingState()
214 if(occupied_tracks && !--occupied_tracks->refcount)
215 delete occupied_tracks;
218 Time::TimeDelta TrainRoutePlanner::TrainRoutingState::get_time_to_next_track() const
220 return ((track->get_type().get_path_length(path)-offset)/info->speed)*Time::sec+delay;
223 bool TrainRoutePlanner::TrainRoutingState::is_occupying(Track &trk) const
225 OccupiedTrack *occ = occupied_tracks;
226 for(unsigned n=occ->n_tracks; n>0; --n, occ=occ->next)
232 bool TrainRoutePlanner::TrainRoutingState::check_arrival()
234 TrainRouter &router = *info->router;
235 TrackIter next_track = track.next(path);
237 if(waypoint<0 && router.is_destination(*track) && !router.is_destination(*next_track))
242 else if(waypoint>=0 && router.is_waypoint(waypoint, *track) && !router.is_waypoint(waypoint, *next_track))
245 if(waypoint>=static_cast<int>(router.get_n_waypoints()))
252 void TrainRoutePlanner::TrainRoutingState::advance(float distance)
255 back_offset += distance;
257 OccupiedTrack *last_occ = occupied_tracks;
258 for(unsigned n=occupied_tracks->n_tracks; n>1; --n)
259 last_occ = last_occ->next;
261 // XXX What if there's multiple tracks to remove?
262 if(back_offset>last_occ->path_length)
264 back_offset -= last_occ->path_length;
265 if(occupied_tracks->refcount>1)
267 --occupied_tracks->refcount;
268 occupied_tracks = new OccupiedTrack(*occupied_tracks);
270 --occupied_tracks->n_tracks;
273 remaining_estimate -= distance;
276 void TrainRoutePlanner::TrainRoutingState::advance(const Time::TimeDelta &dt)
284 float secs = dt/Time::sec;
287 secs -= delay/Time::sec;
292 advance(info->speed*secs);
295 void TrainRoutePlanner::TrainRoutingState::advance_track(unsigned next_path)
297 float distance = occupied_tracks->path_length-offset;
298 track = track.next(path);
300 occupied_tracks = new OccupiedTrack(*track, path, occupied_tracks);
305 void TrainRoutePlanner::TrainRoutingState::update_estimate()
307 TrackIter iter = track.reverse(path);
308 float distance = info->router->get_metric(waypoint).get_distance_from(*iter.track(), iter.entry());
309 distance += track->get_type().get_path_length(path)-offset;
310 remaining_estimate = distance;
314 TrainRoutePlanner::RoutingStep::RoutingStep():
318 TrainRoutePlanner::RoutingStep::RoutingStep(const RoutingStep *p):
320 total_estimate(p->total_estimate),
325 void TrainRoutePlanner::RoutingStep::create_successors(list<RoutingStep> &new_steps) const
327 RoutingStep next(this);
328 if(next.update_states())
330 if(next.check_deadlocks())
333 new_steps.push_back(next);
337 int train_index = find_next_train();
341 TrainRoutingState &train = next.trains[train_index];
343 Time::TimeDelta dt = train.get_time_to_next_track();
346 if(train.check_arrival())
348 new_steps.push_back(next);
352 TrackIter next_track = train.track.next(train.path);
353 train.advance_track(0);
355 const TrackType::Endpoint &next_entry_ep = next_track.endpoint();
356 for(unsigned i=0; next_entry_ep.paths>>i; ++i)
357 if(next_entry_ep.has_path(i))
360 train.update_estimate();
361 next.update_estimate();
363 new_steps.push_back(next);
366 if(next_entry_ep.paths!=next_track->get_type().get_paths())
368 RoutingStep wait(this);
370 wait.trains[train_index].state = WAITING;
372 new_steps.push_back(wait);
376 bool TrainRoutePlanner::RoutingStep::update_states()
378 bool changes = false;
379 for(vector<TrainRoutingState>::iterator i=trains.begin(); i!=trains.end(); ++i)
381 if(i->state==ARRIVED)
384 TrainState old_state = i->state;
386 TrackIter next_track = i->track.next(i->path);
389 i->blocked_by = get_occupant(*next_track);
392 else if(i->state==BLOCKED)
398 if(i->state!=old_state)
405 bool TrainRoutePlanner::RoutingStep::check_deadlocks() const
407 for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
409 if(i->state!=BLOCKED)
415 int slow = i->blocked_by;
416 int fast = trains[slow].blocked_by;
417 while(fast>=0 && trains[fast].blocked_by>=0)
422 slow = trains[slow].blocked_by;
423 fast = trains[trains[fast].blocked_by].blocked_by;
430 int TrainRoutePlanner::RoutingStep::get_occupant(Track &track) const
432 for(unsigned i=0; i<trains.size(); ++i)
433 if(trains[i].is_occupying(track))
439 int TrainRoutePlanner::RoutingStep::find_next_train() const
441 Time::TimeDelta min_dt;
443 for(unsigned i=0; i<trains.size(); ++i)
444 if(trains[i].state==MOVING)
446 Time::TimeDelta dt = trains[i].get_time_to_next_track();
447 if(dt<min_dt || next_train<0)
457 void TrainRoutePlanner::RoutingStep::advance(const Time::TimeDelta &dt)
460 for(vector<TrainRoutingState>::iterator i=trains.begin(); i!=trains.end(); ++i)
464 void TrainRoutePlanner::RoutingStep::update_estimate()
466 for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
468 Time::TimeDelta t = time+(i->remaining_estimate/i->info->speed)*Time::sec+i->delay;
469 if(i==trains.begin() || t>total_estimate)
474 bool TrainRoutePlanner::RoutingStep::is_viable() const
476 if(total_estimate<Time::zero)
479 for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
480 if(i->remaining_estimate<0)
483 for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
490 bool TrainRoutePlanner::RoutingStep::is_goal() const
492 for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
493 if(i->state!=ARRIVED)
498 bool TrainRoutePlanner::RoutingStep::operator<(const RoutingStep &other) const
500 return total_estimate<other.total_estimate;