+bool Train::get_function(unsigned func) const
+{
+ return (functions>>func)&1;
+}
+
+void Train::set_route(const Route *r)
+{
+ if(!rsv_blocks.empty())
+ {
+ for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
+ if(i->block->get_sensor_id())
+ {
+ release_blocks(rsv_blocks, ++i, rsv_blocks.end());
+ break;
+ }
+ }
+
+ route = r;
+ next_route = 0;
+ end_of_route = false;
+
+ if(route)
+ {
+ BlockRef &last = (rsv_blocks.empty() ? cur_blocks.back() : rsv_blocks.back());
+ BlockRef next = last.next();
+ const Block::Endpoint &ep = next.block->get_endpoints()[next.entry];
+ if(!route->get_tracks().count(ep.track))
+ {
+ next_route = route;
+ route = Route::find(*ep.track, ep.track_ep, *next_route);
+ }
+ }
+
+ if(target_speed && reserve_more()<2)
+ update_speed();
+
+ signal_route_changed.emit(route);
+}
+
+void Train::go_to(const Track &to)
+{
+ for(list<BlockRef>::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
+ if(i->block->get_tracks().count(const_cast<Track *>(&to)))
+ {
+ set_speed(0);
+ set_route(0);
+ return;
+ }
+
+ BlockRef *last = 0;
+ if(rsv_blocks.empty())
+ last = &cur_blocks.back();
+ else
+ {
+ for(list<BlockRef>::iterator i=rsv_blocks.begin(); (i!=rsv_blocks.end() && !last); ++i)
+ if(i->block->get_sensor_id())
+ last = &*i;
+ }
+
+ BlockRef next = last->next();
+ const Block::Endpoint &ep = next.block->get_endpoints()[next.entry];
+
+ set_route(Route::find(*ep.track, ep.track_ep, to));
+}
+
+void Train::place(Block &block, unsigned entry)
+{
+ if(target_speed)
+ set_speed(0);
+
+ release_blocks(rsv_blocks);
+ release_blocks(cur_blocks);
+
+ if(!block.reserve(this))