X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrainrouter.cpp;h=122b91a3bd195837f90455cc3da66289d37ea453;hb=68877070660f2806cc2a7f0ccb69153059ef6ce7;hp=d95bdff4bc85c0db6afa45ff55ef3ffe166e0ae0;hpb=195ba0e08c597190481ac205afa05243c68fb39b;p=r2c2.git diff --git a/source/libr2c2/trainrouter.cpp b/source/libr2c2/trainrouter.cpp index d95bdff..122b91a 100644 --- a/source/libr2c2/trainrouter.cpp +++ b/source/libr2c2/trainrouter.cpp @@ -1,3 +1,4 @@ +#include "driver.h" #include "layout.h" #include "route.h" #include "trackiter.h" @@ -67,6 +68,8 @@ void TrainRouter::use_planned_route() { if(!planner || planner->get_result()!=TrainRoutePlanner::COMPLETE) return; + if(waypoints.empty()) + return; const list &planned_routes = planner->get_routes_for(train); @@ -83,7 +86,7 @@ void TrainRouter::use_planned_route() void TrainRouter::route_changed() { - BlockIter fncb = train.get_first_noncritical_block(); + BlockIter fncb = train.get_last_critical_block().next(); arrival = ON_THE_WAY; reserving_route = routes.begin(); @@ -118,7 +121,7 @@ void TrainRouter::route_changed() if(!sequence_points.empty()) { const SequencePoint &sp = sequence_points.front(); - if(sp.block==fncb.block() && !sp.is_cleared()) + if(sp.block==fncb.block() && sp.preceding_train) { arrival = WAITING_FOR_SEQUENCE; sequence_check_pending = true; @@ -225,24 +228,28 @@ void TrainRouter::message(const Message &msg) void TrainRouter::tick(const Time::TimeDelta &dt) { - if(delay) - { - delay -= dt; - if(delaycheck()!=TrainRoutePlanner::PENDING) apply_plan(train.get_layout(), *planner); + Layout &layout = train.get_layout(); + if(!layout.get_driver().is_halted() && !layout.get_clock().is_stopped()) + { + if(delay) + { + delay -= dt; + if(delayget_block().get_train()==&train); i=i.next()) + for(TrackIter i=last_track_rev; (i && i->get_block().get_train()==&train); i=i.next()) { if(routes.front()->has_track(*i)) ++count; else if(count>0) { if(count==routes.front()->get_tracks().size()) - next_track = i.flip(); + last_track_rev = i; break; } } - if(!routes.front()->has_track(*next_track) && !routes.front()->has_track(*next_track.flip())) + TrackIter next_track = last_track_rev.flip(); + if(!routes.front()->has_track(*last_track_rev) && !routes.front()->has_track(*next_track)) { Route *pf = Route::find(next_track, *routes.front()); if(!pf) @@ -464,7 +472,7 @@ bool TrainRouter::create_lead_route() } Route *lead = 0; - for(TrackIter i=next_track.flip(); (i && i->get_block().get_train()==&train); i=i.next()) + for(TrackIter i=last_track_rev; (i && i->get_block().get_train()==&train); i=i.next()) { if(!lead && !routes.front()->has_track(*i)) { @@ -485,27 +493,25 @@ bool TrainRouter::create_lead_route() return true; } -bool TrainRouter::is_valid_for_track(const Route &route, const TrackIter &track) const -{ - if(!route.has_track(*track)) - return false; - if(track->get_type().is_turnout() && route.get_turnout(track->get_turnout_address())<0 && route.has_track(*track.flip())) - return false; - return true; -} - bool TrainRouter::advance_to_track(RouteList::iterator &route, const TrackIter &track) { - if(!is_valid_for_track(**route, track)) + Track &prev_track = *track.flip(); + unsigned taddr = (track->get_type().is_turnout() ? track->get_turnout_address() : 0); + for(unsigned i=0; route!=routes.end(); ++i) { - ++route; - if(route==routes.end()) - return false; - if(!is_valid_for_track(**route, track)) + bool in_route = (*route)->has_track(*track); + bool prev_in_route = (*route)->has_track(prev_track); + bool known_path = (!taddr || (*route)->get_turnout(taddr)>=0); + + if(in_route && (known_path || !prev_in_route)) + return true; + else if(i==0 || prev_in_route) + ++route; + else throw logic_error("internal error (routes are not continuous)"); } - return true; + return false; } void TrainRouter::get_routers(Layout &layout, vector &routers) @@ -538,6 +544,9 @@ void TrainRouter::start_planning(Layout &layout) void TrainRouter::apply_plan(Layout &layout, TrainRoutePlanner &planner) { + if(planner.get_result()==TrainRoutePlanner::FAILED) + layout.emergency(0, "Route planning failed"); + vector routers; get_routers(layout, routers);