+void Train::check_turnout_paths(bool set)
+{
+ if(clear_blocks_end==blocks.end())
+ return;
+
+ list<RouteRef>::iterator route = routes.begin();
+
+ for(list<BlockIter>::iterator i=clear_blocks_end; i!=blocks.end(); ++i)
+ {
+ advance_route(route, *i->track_iter());
+
+ if((*i)->get_turnout_id())
+ {
+ TrackIter track = i->track_iter();
+ const TrackType::Endpoint &track_ep = track->get_type().get_endpoints()[track.entry()];
+
+ int path = -1;
+ for(list<RouteRef>::iterator j=route; (path<0 && j!=routes.end()); ++j)
+ path = j->route->get_turnout((*i)->get_turnout_id());
+ if(path<0)
+ path = track->get_active_path();
+ if(!(track_ep.paths&(1<<path)))
+ {
+ list<BlockIter>::iterator j = i;
+ if(++j!=blocks.end())
+ {
+ TrackIter rev = j->track_iter().flip();
+ unsigned mask = rev->get_type().get_endpoints()[rev.entry()].paths&track_ep.paths;
+ for(path=0; mask>1; mask>>=1, ++path) ;
+ }
+ else
+ return;
+ }
+
+ if(path!=static_cast<int>(track->get_active_path()))
+ {
+ if(set)
+ track->set_active_path(path);
+
+ continue;
+ }
+ }
+
+ if(i==clear_blocks_end)
+ ++clear_blocks_end;
+ }
+}
+
+float Train::get_reserved_distance_until(const Block *until_block, bool back) const
+{
+ if(blocks.empty())
+ return 0;
+
+ Vehicle &veh = *(reverse!=back ? vehicles.back() : vehicles.front());
+ const VehicleType &vtype = veh.get_type();
+
+ TrackIter track(veh.get_track(), veh.get_entry());
+ if(!track) // XXX Probably unnecessary
+ return 0;
+
+ BlockList::const_iterator block = blocks.begin();
+ while(block!=clear_blocks_end && !(*block)->has_track(*track))
+ ++block;
+ if(block==clear_blocks_end || &**block==until_block)
+ return 0;
+
+ float result = veh.get_offset();
+ if(reverse!=back)
+ track = track.reverse();
+ else
+ result = track->get_type().get_path_length(track->get_active_path())-result;
+ result -= vtype.get_length()/2;
+
+ while(1)
+ {
+ track = track.next();
+ if(!track)
+ break;
+
+ if(!(*block)->has_track(*track))
+ {
+ if(back)
+ {
+ if(block==blocks.begin())
+ break;
+ --block;
+ }
+ else
+ {
+ ++block;
+ if(block==clear_blocks_end)
+ break;
+ }
+
+ if(&**block==until_block)
+ break;
+ }
+
+ result += track->get_type().get_path_length(track->get_active_path());
+ }
+
+ return result;
+}
+