5 #include "trainrouteplanner.h"
6 #include "trainrouter.h"
14 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 void TrainRouter::set_priority(int p)
31 bool TrainRouter::set_route(const Route *r)
33 BlockIter fncb = train.get_first_noncritical_block();
36 if(r && train.is_placed())
38 const BlockAllocator &allocator = train.get_block_allocator();
39 TrackIter first = allocator.first().track_iter();
40 TrackIter next = fncb.track_iter();
41 if(!r->has_track(*next))
43 lead = Route::find(next, *r);
46 create_lead_route(lead, lead);
48 else if(!r->has_track(*first))
49 lead = create_lead_route(0, r);
54 routes.push_back(lead);
60 /* TODO destination should also be cleared when manually setting a different
61 route, but not when the planner calls this. */
68 train.refresh_blocks_from(*fncb);
70 const Route *route = get_route();
71 signal_route_changed.emit(route);
72 signal_event.emit(Message("route-changed", route));
77 void TrainRouter::add_wait(Block &block, Train *tr)
82 waits.push_back(wait);
85 const Route *TrainRouter::get_route() const
89 return routes.front();
92 void TrainRouter::set_destination(const Zone &zone)
96 update_pending = true;
99 void TrainRouter::set_destination(const Block &block)
103 update_pending = true;
106 bool TrainRouter::has_destination() const
108 return dest_zone || dest_block;
111 bool TrainRouter::is_destination(Track &track) const
114 return dest_zone->has_track(track);
116 return dest_block->has_track(track);
121 void TrainRouter::set_departure_delay(const Time::TimeDelta &d)
124 update_pending = true;
127 void TrainRouter::message(const Message &msg)
129 if(msg.type=="set-route")
131 if(msg.value.check_type<Route *>())
132 set_route(msg.value.value<Route *>());
134 set_route(msg.value.value<const Route *>());
136 else if(msg.type=="clear-route")
138 else if(msg.type=="set-destination-block")
140 if(msg.value.check_type<Block *>())
141 set_destination(*msg.value.value<Block *>());
143 set_destination(*msg.value.value<const Block *>());
145 else if(msg.type=="set-destination-zone")
147 if(msg.value.check_type<Zone *>())
148 set_destination(*msg.value.value<Zone *>());
150 set_destination(*msg.value.value<const Zone *>());
152 else if(msg.type=="set-departure-delay")
153 set_departure_delay(msg.value.value<Time::TimeDelta>());
156 void TrainRouter::tick(const Time::TimeDelta &dt)
161 if(delay<=Time::zero)
166 create_plans(train.get_layout());
168 if(arriving==1 && !train.get_speed())
170 signal_arrived.emit();
171 signal_event.emit(Message("arrived"));
174 else if(arriving==2 && !train.get_block_allocator().is_active())
178 void TrainRouter::save(list<DataFile::Statement> &st) const
180 st.push_back((DataFile::Statement("priority"), priority));
184 RouteList::const_iterator i = routes.begin();
185 for(; (i!=routes.end() && (*i)->is_temporary()); ++i) ;
187 st.push_back((DataFile::Statement("route"), (*i)->get_name()));
191 void TrainRouter::block_reserved(Block &block, Train *t)
195 if(!waits.empty() && waits.front().block==&block)
203 BlockIter b_iter = t->get_block_allocator().iter_for(block);
205 RouteList::iterator route = routes.begin();
206 if(advance_route(route, block))
208 // Check if the block is a turnout and set it to proper path
209 if(unsigned taddr = block.get_turnout_address())
211 int path = (*route)->get_turnout(taddr);
213 b_iter.track_iter()->set_active_path(path);
216 // Check if the next block is still part of the designated route
217 BlockIter b_iter_next = b_iter.next(*route);
219 RouteList::iterator next_route = route;
220 if(!advance_route(next_route, *b_iter_next))
222 train.stop_at(&block);
226 if(!waits.empty() && waits.front().block==b_iter_next.block())
227 train.stop_at(&block);
231 void TrainRouter::train_advanced(Block &block)
233 BlockIter b_iter = train.get_block_allocator().iter_for(block);
235 // Check if we've reached the next route
238 const Route &route = **++routes.begin();
239 if(route.has_track(*b_iter.endpoint().track))
243 signal_event.emit(Message("route-changed", get_route()));
249 b_iter = b_iter.next();
250 if(b_iter && !is_on_route(*b_iter))
255 const Route *TrainRouter::get_route_for_block(const Block &block) const
257 const set<Track *> &tracks = block.get_tracks();
258 for(RouteList::const_iterator i=routes.begin(); i!=routes.end(); ++i)
259 for(set<Track *>::const_iterator j=tracks.begin(); j!=tracks.end(); ++j)
260 if((*i)->has_track(**j))
266 Route *TrainRouter::create_lead_route(Route *lead, const Route *target)
270 lead = new Route(train.get_layout());
271 lead->set_name("Lead");
272 lead->set_temporary(true);
276 for(BlockIter i=train.get_block_allocator().first(); (i && i->get_train()==&train); i=i.next())
278 const set<Track *> &btracks = i->get_tracks();
279 for(set<Track *>::const_iterator j=btracks.begin(); j!=btracks.end(); ++j)
280 if(!target || !target->has_track(**j))
284 lead->add_tracks(tracks);
289 bool TrainRouter::advance_route(RouteList::iterator &iter, const Block &block)
291 const set<Track *> &tracks = block.get_tracks();
292 for(; iter!=routes.end(); ++iter)
293 for(set<Track *>::const_iterator j=tracks.begin(); j!=tracks.end(); ++j)
294 if((*iter)->has_track(**j))
300 bool TrainRouter::is_on_route(const Block &block)
302 RouteList::iterator iter = routes.begin();
303 return advance_route(iter, block);
306 void TrainRouter::create_plans(Layout &layout)
308 TrainRoutePlanner planner(layout);
311 const map<unsigned, Train *> &trains = layout.get_trains();
312 for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
313 if(TrainRouter *router = i->second->get_ai_of_type<TrainRouter>())
314 router->update_pending = false;
318 TrainRouter::Wait::Wait():
324 TrainRouter::Loader::Loader(TrainRouter &r):
325 DataFile::ObjectLoader<TrainRouter>(r)
327 add("priority", &TrainRouter::priority);
328 add("route", &Loader::route);
331 void TrainRouter::Loader::route(const string &n)
333 obj.set_route(&obj.train.get_layout().get_route(n));