5 #include "trackchain.h"
6 #include "trainroutemetric.h"
7 #include "trainrouteplanner.h"
8 #include "trainrouter.h"
15 TrainRouter::TrainRouter(Train &t):
20 destination_changed(false),
23 sequence_check_pending(false)
25 train.get_layout().signal_block_reserved.connect(sigc::mem_fun(this, &TrainRouter::block_reserved));
26 train.signal_advanced.connect(sigc::mem_fun(this, &TrainRouter::train_advanced));
29 TrainRouter::~TrainRouter()
31 for(vector<TrainRouteMetric *>::iterator i=metrics.begin(); i!=metrics.end(); ++i)
35 void TrainRouter::set_priority(int p)
40 bool TrainRouter::set_route(const Route *r)
42 BlockIter fncb = train.get_first_noncritical_block();
45 if(r && train.is_placed())
47 const BlockAllocator &allocator = train.get_block_allocator();
48 TrackIter first = allocator.first().track_iter();
49 TrackIter next = fncb.track_iter();
50 if(!r->has_track(*next))
52 lead = Route::find(next, *r);
55 create_lead_route(lead, lead);
57 else if(!r->has_track(*first))
58 lead = create_lead_route(0, r);
63 routes.push_back(lead);
69 sequence_points.clear();
71 sequence_check_pending = false;
78 const Route *TrainRouter::get_route() const
82 return routes.front();
85 void TrainRouter::route_changed()
87 BlockIter fncb = train.get_first_noncritical_block();
89 reserving_route = routes.begin();
90 bool already_at_end = false;
93 TrackIter track = train.get_block_allocator().first().track_iter();
94 for(; track; track=track.next())
96 if(!advance_to_track(reserving_route, *track))
98 already_at_end = true;
101 if(&track->get_block()==fncb.block())
109 train.refresh_blocks_from(*fncb);
112 const Route *route = get_route();
113 signal_route_changed.emit(route);
114 signal_event.emit(Message("route-changed", route));
117 void TrainRouter::set_destination(const TrackChain &d)
120 destination_changed = true;
121 metrics_stale = true;
124 bool TrainRouter::is_destination(Track &track) const
127 return destination->has_track(track);
132 void TrainRouter::add_waypoint(const TrackChain &wp)
134 waypoints.push_back(&wp);
135 destination_changed = true;
136 metrics_stale = true;
139 bool TrainRouter::is_waypoint(unsigned index, Track &track) const
141 if(index>=waypoints.size())
142 throw out_of_range("TrainRouter::is_waypoint");
144 return waypoints[index]->has_track(track);
147 const TrainRouteMetric &TrainRouter::get_metric(int index) const
150 throw logic_error("no metrics");
151 else if(metrics_stale)
152 throw logic_error("metrics are stale");
155 return *metrics.front();
156 else if(static_cast<unsigned>(index)>=waypoints.size())
157 throw out_of_range("TrainRouter::get_metric");
159 return *metrics[index+1];
162 void TrainRouter::set_departure_delay(const Time::TimeDelta &d)
165 destination_changed = true;
168 void TrainRouter::message(const Message &msg)
170 if(msg.type=="set-route")
172 if(msg.value.check_type<Route *>())
173 set_route(msg.value.value<Route *>());
175 set_route(msg.value.value<const Route *>());
177 else if(msg.type=="clear-route")
179 else if(msg.type=="set-destination")
181 if(msg.value.check_type<TrackChain *>())
182 set_destination(*msg.value.value<TrackChain *>());
184 set_destination(*msg.value.value<const TrackChain *>());
186 else if(msg.type=="add-waypoint")
188 if(msg.value.check_type<TrackChain *>())
189 add_waypoint(*msg.value.value<TrackChain *>());
191 add_waypoint(*msg.value.value<const TrackChain *>());
193 else if(msg.type=="set-departure-delay")
194 set_departure_delay(msg.value.value<Time::TimeDelta>());
197 void TrainRouter::tick(const Time::TimeDelta &dt)
202 if(delay<=Time::zero)
206 if(destination_changed)
209 start_planning(train.get_layout());
210 else if(planner->get_result()!=TrainRoutePlanner::PENDING)
212 destination_changed = false;
213 if(planner->get_result()==TrainRoutePlanner::COMPLETE)
215 const list<Route *> &planned_routes = planner->get_routes_for(train);
217 routes.push_back(create_lead_route(0, planned_routes.front()));
218 routes.insert(routes.end(), planned_routes.begin(), planned_routes.end());
219 sequence_points = planner->get_sequence_for(train);
220 current_sequence = 0;
221 sequence_check_pending = false;
229 if(sequence_check_pending)
231 if(sequence_points.front().is_cleared())
233 sequence_check_pending = false;
236 if(arriving==1 && !train.get_speed())
238 signal_arrived.emit(destination);
239 signal_event.emit(Message("arrived", destination));
242 else if(arriving==2 && !train.get_block_allocator().is_active())
246 void TrainRouter::save(list<DataFile::Statement> &st) const
248 st.push_back((DataFile::Statement("priority"), priority));
252 RouteList::const_iterator i = routes.begin();
253 for(; (i!=routes.end() && (*i)->is_temporary()); ++i) ;
255 st.push_back((DataFile::Statement("route"), (*i)->get_name()));
259 void TrainRouter::block_reserved(Block &block, Train *t)
269 SequencePoint &sp = sequence_points.front();
270 if(sp.preceding_train==t && sp.block==&block)
275 sequence_check_pending = true;
281 if(!sequence_points.empty())
283 SequencePoint &sp = sequence_points.front();
286 current_sequence = sp.sequence_out;
287 sequence_points.pop_front();
291 TrackIter track = train.get_block_allocator().iter_for(block).track_iter();
293 // Is the block a turnout? If so, set it to the proper path.
294 if(unsigned taddr = block.get_turnout_address())
296 int path = (*reserving_route)->get_turnout(taddr);
298 track->set_active_path(path);
301 if(reserving_route==routes.end() || !(*reserving_route)->has_track(*track))
303 reserving_route = routes.begin();
304 track = t->get_block_allocator().first().track_iter();
305 for(; track; track=track.next())
307 if(!advance_to_track(reserving_route, *track))
308 throw logic_error("internal error (reservation outside of route)");
309 else if(&track->get_block()==&block)
314 // Do we need to move to the next route?
315 for(; track; track=track.next((*reserving_route)->get_path(*track)))
317 if(!advance_to_track(reserving_route, *track))
319 train.stop_at(&block);
322 if(&track->get_block()!=&block)
326 if(!sequence_points.empty())
328 SequencePoint &sp = sequence_points.front();
329 if(sp.block==&track->get_block() && !sp.is_cleared())
330 train.stop_at(&block);
334 void TrainRouter::train_advanced(Block &block)
336 BlockIter b_iter = train.get_block_allocator().iter_for(block);
338 // Check if we've reached the next route
341 Track &track = *b_iter.endpoint().track;
342 const Route *route = get_route();
343 bool change_route = false;
345 change_route = (*++routes.begin())->has_track(track);
347 change_route = !route->has_track(track);
354 signal_route_changed.emit(route);
355 signal_event.emit(Message("route-changed", route));
359 if(!waypoints.empty())
361 const TrackChain &wp = *waypoints.front();
362 TrackIter t_iter = b_iter.track_iter();
363 if(wp.has_track(*t_iter))
365 for(; t_iter; t_iter=t_iter.next())
367 if(!wp.has_track(*t_iter))
369 waypoints.erase(waypoints.begin());
370 signal_waypoint_reached.emit(&wp);
371 signal_event.emit(Message("waypoint-reached", &wp));
374 else if(!block.has_track(*t_iter))
382 b_iter = b_iter.next();
383 if(b_iter && !is_on_route(*b_iter))
388 void TrainRouter::create_metrics()
390 for(vector<TrainRouteMetric *>::iterator i=metrics.begin(); i!=metrics.end(); ++i)
397 metrics.push_back(new TrainRouteMetric(*destination));
398 for(vector<const TrackChain *>::const_iterator i=waypoints.begin(); i!=waypoints.end(); ++i)
399 metrics.push_back(new TrainRouteMetric(**i));
401 for(unsigned i=metrics.size(); --i>0; )
402 metrics[i]->chain_to(*metrics[(i+1)%metrics.size()]);
405 Route *TrainRouter::create_lead_route(Route *lead, const Route *target)
409 lead = new Route(train.get_layout());
410 lead->set_name("Lead");
411 lead->set_temporary(true);
414 bool target_reached = false;
415 for(TrackIter i=train.get_block_allocator().first().track_iter(); i; i=i.next())
417 if(i->get_block().get_train()!=&train)
421 if(target->has_track(*i))
422 target_reached = true;
423 else if(target_reached)
432 bool TrainRouter::is_valid_for_track(const Route &route, Track &track) const
434 if(!route.has_track(track))
436 if(track.get_type().is_turnout() && route.get_turnout(track.get_turnout_address())<0)
441 bool TrainRouter::advance_to_track(RouteList::iterator &route, Track &track)
443 if(!is_valid_for_track(**route, track))
446 if(route==routes.end())
448 if(!is_valid_for_track(**route, track))
449 throw logic_error("internal error (routes are not continuous)");
455 void TrainRouter::start_planning(Layout &layout)
457 RefPtr<TrainRoutePlanner> planner = new TrainRoutePlanner(layout);
459 const map<unsigned, Train *> &trains = layout.get_trains();
460 for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
461 if(TrainRouter *router = i->second->get_ai_of_type<TrainRouter>())
463 if(router->metrics_stale)
465 router->create_metrics();
466 router->metrics_stale = false;
468 router->planner = planner;
475 TrainRouter::SequencePoint::SequencePoint(Block &b, unsigned o):
482 bool TrainRouter::SequencePoint::is_cleared() const
487 TrainRouter *router = preceding_train->get_ai_of_type<TrainRouter>();
488 return router->get_current_sequence()>=sequence_in;
492 TrainRouter::Loader::Loader(TrainRouter &r):
493 DataFile::ObjectLoader<TrainRouter>(r)
495 add("priority", &TrainRouter::priority);
496 add("route", &Loader::route);
499 void TrainRouter::Loader::route(const string &n)
501 obj.set_route(&obj.train.get_layout().get_route(n));