5 #include "trackchain.h"
6 #include "trainroutemetric.h"
7 #include "trainrouteplanner.h"
8 #include "trainrouter.h"
15 TrainRouter::TrainRouter(Train &t):
22 train.get_layout().signal_block_reserved.connect(sigc::mem_fun(this, &TrainRouter::block_reserved));
23 train.signal_advanced.connect(sigc::mem_fun(this, &TrainRouter::train_advanced));
26 TrainRouter::~TrainRouter()
28 for(vector<TrainRouteMetric *>::iterator i=metrics.begin(); i!=metrics.end(); ++i)
32 void TrainRouter::set_priority(int p)
37 bool TrainRouter::set_route(const Route *r)
39 BlockIter fncb = train.get_first_noncritical_block();
42 if(r && train.is_placed())
44 const BlockAllocator &allocator = train.get_block_allocator();
45 TrackIter first = allocator.first().track_iter();
46 TrackIter next = fncb.track_iter();
47 if(!r->has_track(*next))
49 lead = Route::find(next, *r);
52 create_lead_route(lead, lead);
54 else if(!r->has_track(*first))
55 lead = create_lead_route(0, r);
60 routes.push_back(lead);
66 /* TODO destination should also be cleared when manually setting a different
67 route, but not when the planner calls this. */
74 train.refresh_blocks_from(*fncb);
76 const Route *route = get_route();
77 signal_route_changed.emit(route);
78 signal_event.emit(Message("route-changed", route));
83 bool TrainRouter::add_route(const Route &r)
88 // TODO Check that it can be reached from previous routes
94 void TrainRouter::add_wait(Block &block, Train *tr)
99 waits.push_back(wait);
102 const Route *TrainRouter::get_route() const
106 return routes.front();
109 void TrainRouter::set_destination(const TrackChain &d)
112 update_pending = true;
115 bool TrainRouter::is_destination(Track &track) const
118 return destination->has_track(track);
123 void TrainRouter::add_waypoint(const TrackChain &wp)
125 waypoints.push_back(&wp);
126 update_pending = true;
129 bool TrainRouter::is_waypoint(unsigned index, Track &track) const
131 if(index>=waypoints.size())
132 throw out_of_range("TrainRouter::is_waypoint");
134 return waypoints[index]->has_track(track);
137 const TrainRouteMetric &TrainRouter::get_metric(int index) const
140 throw logic_error("no metrics");
141 else if(update_pending)
142 throw logic_error("metrics are stale");
145 return *metrics.front();
146 else if(static_cast<unsigned>(index)>=waypoints.size())
147 throw out_of_range("TrainRouter::get_metric");
149 return *metrics[index+1];
152 void TrainRouter::set_departure_delay(const Time::TimeDelta &d)
155 update_pending = true;
158 void TrainRouter::message(const Message &msg)
160 if(msg.type=="set-route")
162 if(msg.value.check_type<Route *>())
163 set_route(msg.value.value<Route *>());
165 set_route(msg.value.value<const Route *>());
167 else if(msg.type=="clear-route")
169 else if(msg.type=="set-destination")
171 if(msg.value.check_type<TrackChain *>())
172 set_destination(*msg.value.value<TrackChain *>());
174 set_destination(*msg.value.value<const TrackChain *>());
176 else if(msg.type=="add-waypoint")
178 if(msg.value.check_type<TrackChain *>())
179 add_waypoint(*msg.value.value<TrackChain *>());
181 add_waypoint(*msg.value.value<const TrackChain *>());
183 else if(msg.type=="set-departure-delay")
184 set_departure_delay(msg.value.value<Time::TimeDelta>());
187 void TrainRouter::tick(const Time::TimeDelta &dt)
192 if(delay<=Time::zero)
197 create_plans(train.get_layout());
199 if(arriving==1 && !train.get_speed())
201 signal_arrived.emit(destination);
202 signal_event.emit(Message("arrived", destination));
205 else if(arriving==2 && !train.get_block_allocator().is_active())
209 void TrainRouter::save(list<DataFile::Statement> &st) const
211 st.push_back((DataFile::Statement("priority"), priority));
215 RouteList::const_iterator i = routes.begin();
216 for(; (i!=routes.end() && (*i)->is_temporary()); ++i) ;
218 st.push_back((DataFile::Statement("route"), (*i)->get_name()));
222 void TrainRouter::block_reserved(Block &block, Train *t)
226 if(!waits.empty() && waits.front().block==&block)
234 BlockIter b_iter = t->get_block_allocator().iter_for(block);
236 RouteList::iterator route = routes.begin();
237 if(advance_route(route, block))
239 // Check if the block is a turnout and set it to proper path
240 if(unsigned taddr = block.get_turnout_address())
242 int path = (*route)->get_turnout(taddr);
244 b_iter.track_iter()->set_active_path(path);
247 // Check if the next block is still part of the designated route
248 BlockIter b_iter_next = b_iter.next(*route);
250 RouteList::iterator next_route = route;
251 if(!advance_route(next_route, *b_iter_next))
253 train.stop_at(&block);
257 if(!waits.empty() && waits.front().block==b_iter_next.block())
258 train.stop_at(&block);
262 void TrainRouter::train_advanced(Block &block)
264 BlockIter b_iter = train.get_block_allocator().iter_for(block);
266 // Check if we've reached the next route
269 Track &track = *b_iter.endpoint().track;
270 const Route *route = get_route();
271 bool change_route = false;
273 change_route = (*++routes.begin())->has_track(track);
275 change_route = !route->has_track(track);
282 signal_route_changed.emit(route);
283 signal_event.emit(Message("route-changed", route));
287 if(!waypoints.empty())
289 const TrackChain &wp = *waypoints.front();
290 TrackIter t_iter = b_iter.track_iter();
291 if(wp.has_track(*t_iter))
293 for(; t_iter; t_iter=t_iter.next())
295 if(!wp.has_track(*t_iter))
297 waypoints.erase(waypoints.begin());
298 signal_waypoint_reached.emit(&wp);
299 signal_event.emit(Message("waypoint-reached", &wp));
302 else if(!block.has_track(*t_iter))
310 b_iter = b_iter.next();
311 if(b_iter && !is_on_route(*b_iter))
316 const Route *TrainRouter::get_route_for_block(const Block &block) const
318 const set<Track *> &tracks = block.get_tracks();
319 for(RouteList::const_iterator i=routes.begin(); i!=routes.end(); ++i)
320 for(set<Track *>::const_iterator j=tracks.begin(); j!=tracks.end(); ++j)
321 if((*i)->has_track(**j))
327 void TrainRouter::create_metrics()
329 for(vector<TrainRouteMetric *>::iterator i=metrics.begin(); i!=metrics.end(); ++i)
336 metrics.push_back(new TrainRouteMetric(*destination));
337 for(vector<const TrackChain *>::const_iterator i=waypoints.begin(); i!=waypoints.end(); ++i)
338 metrics.push_back(new TrainRouteMetric(**i));
340 for(unsigned i=metrics.size(); --i>0; )
341 metrics[i]->chain_to(*metrics[(i+1)%metrics.size()]);
344 Route *TrainRouter::create_lead_route(Route *lead, const Route *target)
348 lead = new Route(train.get_layout());
349 lead->set_name("Lead");
350 lead->set_temporary(true);
353 bool target_reached = false;
354 for(TrackIter i=train.get_block_allocator().first().track_iter(); i; i=i.next())
356 if(i->get_block().get_train()!=&train)
360 if(target->has_track(*i))
361 target_reached = true;
362 else if(target_reached)
371 bool TrainRouter::advance_route(RouteList::iterator &iter, const Block &block)
373 const set<Track *> &tracks = block.get_tracks();
374 unsigned turnout_addr = block.get_turnout_address();
375 for(; iter!=routes.end(); ++iter)
377 if(turnout_addr && (*iter)->get_turnout(turnout_addr)<0)
379 for(set<Track *>::const_iterator j=tracks.begin(); j!=tracks.end(); ++j)
380 if((*iter)->has_track(**j))
387 bool TrainRouter::is_on_route(const Block &block)
389 RouteList::iterator iter = routes.begin();
390 return advance_route(iter, block);
393 void TrainRouter::create_plans(Layout &layout)
395 const map<unsigned, Train *> &trains = layout.get_trains();
396 for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
397 if(TrainRouter *router = i->second->get_ai_of_type<TrainRouter>())
399 if(router->update_pending)
400 router->create_metrics();
401 router->update_pending = false;
404 TrainRoutePlanner planner(layout);
409 TrainRouter::Wait::Wait():
415 TrainRouter::Loader::Loader(TrainRouter &r):
416 DataFile::ObjectLoader<TrainRouter>(r)
418 add("priority", &TrainRouter::priority);
419 add("route", &Loader::route);
422 void TrainRouter::Loader::route(const string &n)
424 obj.set_route(&obj.train.get_layout().get_route(n));