X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrainrouter.cpp;h=9a86263b9bec5922fa58d8201529731e79c01944;hb=c435f10d8de8a4058c43bcfc6c7073c8a8798463;hp=6e588e7a7ac4302c97db28f968fa1c8d04a01b27;hpb=dec294d40194a640e7b4bccf20dd1baa4a87038c;p=r2c2.git diff --git a/source/libr2c2/trainrouter.cpp b/source/libr2c2/trainrouter.cpp index 6e588e7..9a86263 100644 --- a/source/libr2c2/trainrouter.cpp +++ b/source/libr2c2/trainrouter.cpp @@ -2,8 +2,10 @@ #include "route.h" #include "trackiter.h" #include "train.h" +#include "trackchain.h" +#include "trainroutemetric.h" +#include "trainrouteplanner.h" #include "trainrouter.h" -#include "zone.h" using namespace std; using namespace Msp; @@ -13,32 +15,35 @@ namespace R2C2 { TrainRouter::TrainRouter(Train &t): TrainAI(t), priority(0), - arriving(false), - yielding_to(0) + arriving(0), + destination(0), + update_pending(false) { train.get_layout().signal_block_reserved.connect(sigc::mem_fun(this, &TrainRouter::block_reserved)); train.signal_advanced.connect(sigc::mem_fun(this, &TrainRouter::train_advanced)); } -void TrainRouter::set_priority(int p) +TrainRouter::~TrainRouter() { - priority = p; + for(vector::iterator i=metrics.begin(); i!=metrics.end(); ++i) + delete *i; } -void TrainRouter::yield_to(const Train &t) +void TrainRouter::set_priority(int p) { - yielding_to = &t; + priority = p; } bool TrainRouter::set_route(const Route *r) { - train.free_noncritical_blocks(); + BlockIter fncb = train.get_first_noncritical_block(); Route *lead = 0; if(r && train.is_placed()) { - TrackIter first = train.get_tail_block().track_iter(); - TrackIter next = train.get_head_block().next().track_iter(); + const BlockAllocator &allocator = train.get_block_allocator(); + TrackIter first = allocator.first().track_iter(); + TrackIter next = fncb.track_iter(); if(!r->has_track(*next)) { lead = Route::find(next, *r); @@ -56,9 +61,17 @@ bool TrainRouter::set_route(const Route *r) if(r) routes.push_back(r); train.stop_at(0); - arriving = false; + arriving = 0; + + /* TODO destination should also be cleared when manually setting a different + route, but not when the planner calls this. */ + if(!r) + { + destination = 0; + waypoints.clear(); + } - train.reserve_more(); + train.refresh_blocks_from(*fncb); const Route *route = get_route(); signal_route_changed.emit(route); @@ -67,65 +80,79 @@ bool TrainRouter::set_route(const Route *r) return true; } -bool TrainRouter::go_to(Track &to) +bool TrainRouter::add_route(const Route &r) { - if(!train.get_speed()) - { - for(BlockIter i=train.get_tail_block(); (i && i->get_train()==&train); i=i.next()) - if(i->has_track(to)) - { - signal_arrived.emit(); - signal_event.emit(Message("arrived")); - return set_route(0); - } - } + if(routes.empty()) + return set_route(&r); - train.free_noncritical_blocks(); + // TODO Check that it can be reached from previous routes + routes.push_back(&r); - TrackIter next = train.get_head_block().next().track_iter(); + return true; +} - Route *route = Route::find(next, to); - if(!route) - return false; - create_lead_route(route, route); - return set_route(route); +void TrainRouter::add_wait(Block &block, Train *tr) +{ + Wait wait; + wait.block = █ + wait.train = tr; + waits.push_back(wait); } -bool TrainRouter::go_to(const Zone &to) +const Route *TrainRouter::get_route() const { - set tracks; - for(BlockIter i=train.get_tail_block(); (i && i->get_train()==&train); i=i.next()) - tracks.insert(i->get_tracks().begin(), i->get_tracks().end()); + if(routes.empty()) + return 0; + return routes.front(); +} - const Zone::TrackSet &ztracks = to.get_tracks(); - unsigned union_size = 0; - for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i) - union_size += tracks.count(*i); +void TrainRouter::set_destination(const TrackChain &d) +{ + destination = &d; + update_pending = true; +} - if(union_size==tracks.size() || union_size==ztracks.size()) - { - signal_arrived.emit(); - signal_event.emit(Message("arrived")); - return set_route(0); - } +bool TrainRouter::is_destination(Track &track) const +{ + if(destination) + return destination->has_track(track); + else + return false; +} - train.free_noncritical_blocks(); +void TrainRouter::add_waypoint(const TrackChain &wp) +{ + waypoints.push_back(&wp); + update_pending = true; +} - TrackIter next = train.get_head_block().next().track_iter(); +bool TrainRouter::is_waypoint(unsigned index, Track &track) const +{ + if(index>=waypoints.size()) + throw out_of_range("TrainRouter::is_waypoint"); - Route *route = Route::find(next, to); - if(!route) - return false; - create_lead_route(route, route); - route->add_tracks(ztracks); - return set_route(route); + return waypoints[index]->has_track(track); } -const Route *TrainRouter::get_route() const +const TrainRouteMetric &TrainRouter::get_metric(int index) const { - if(routes.empty()) - return 0; - return routes.front(); + if(!destination) + throw logic_error("no metrics"); + else if(update_pending) + throw logic_error("metrics are stale"); + + if(index<0) + return *metrics.front(); + else if(static_cast(index)>=waypoints.size()) + throw out_of_range("TrainRouter::get_metric"); + else + return *metrics[index+1]; +} + +void TrainRouter::set_departure_delay(const Time::TimeDelta &d) +{ + delay = d; + update_pending = true; } void TrainRouter::message(const Message &msg) @@ -139,26 +166,44 @@ void TrainRouter::message(const Message &msg) } else if(msg.type=="clear-route") set_route(0); - else if(msg.type=="go-to-track") - go_to(*msg.value.value()); - else if(msg.type=="go-to-zone") + else if(msg.type=="set-destination") { - if(msg.value.check_type()) - go_to(*msg.value.value()); + if(msg.value.check_type()) + set_destination(*msg.value.value()); else - go_to(*msg.value.value()); + set_destination(*msg.value.value()); } + else if(msg.type=="add-waypoint") + { + if(msg.value.check_type()) + add_waypoint(*msg.value.value()); + else + add_waypoint(*msg.value.value()); + } + else if(msg.type=="set-departure-delay") + set_departure_delay(msg.value.value()); } -void TrainRouter::tick(const Time::TimeStamp &, const Time::TimeDelta &) +void TrainRouter::tick(const Time::TimeDelta &dt) { - if(arriving && !train.get_speed()) + if(delay) { - train.set_active(false); - signal_arrived.emit(); - signal_event.emit(Message("arrived")); - set_route(0); + delay -= dt; + if(delay<=Time::zero) + delay = Time::zero; } + + if(update_pending) + create_plans(train.get_layout()); + + if(arriving==1 && !train.get_speed()) + { + signal_arrived.emit(destination); + signal_event.emit(Message("arrived", destination)); + arriving = 2; + } + else if(arriving==2 && !train.get_block_allocator().is_active()) + set_route(0); } void TrainRouter::save(list &st) const @@ -177,26 +222,30 @@ void TrainRouter::save(list &st) const void TrainRouter::block_reserved(Block &block, Train *t) { if(t!=&train) + { + if(!waits.empty() && waits.front().block==&block) + { + train.stop_at(0); + waits.pop_front(); + } return; + } - yielding_to = 0; - - BlockIter b_iter(&block, t->get_entry_to_block(block)); - BlockIter b_iter_next; + BlockIter b_iter = t->get_block_allocator().iter_for(block); RouteList::iterator route = routes.begin(); if(advance_route(route, block)) { // Check if the block is a turnout and set it to proper path - if(unsigned tid = block.get_turnout_id()) + if(unsigned taddr = block.get_turnout_address()) { - int path = (*route)->get_turnout(tid); + int path = (*route)->get_turnout(taddr); if(path>=0) b_iter.track_iter()->set_active_path(path); } // Check if the next block is still part of the designated route - b_iter_next = b_iter.next(*route); + BlockIter b_iter_next = b_iter.next(*route); RouteList::iterator next_route = route; if(!advance_route(next_route, *b_iter_next)) @@ -205,86 +254,62 @@ void TrainRouter::block_reserved(Block &block, Train *t) return; } - } - else - b_iter_next = b_iter.next(); - - // Check if there's another train and ask it to free the block if appropriate - if(b_iter_next) - { - if(Train *other_train = b_iter_next->get_train()) - { - /* There's another train ahead of us. If it wants to exit the block - from the same endpoint we're trying to enter from or the other way - around, treat it as coming towards us. Otherwise treat it as going - in the same direction. */ - int other_entry = other_train->get_entry_to_block(*b_iter_next); - if(other_entry<0) - throw logic_error("block reservation inconsistency"); - - unsigned exit = b_iter_next.reverse().entry(); - unsigned other_exit = BlockIter(b_iter_next.block(), other_entry).reverse().entry(); - bool entry_conflict = (b_iter_next.entry()==other_exit); - bool exit_conflict = (exit==static_cast(other_entry)); - // TODO: speed matching with preceding train - - TrainRouter *other_router = other_train->get_ai_of_type(); - int other_prio = (other_router ? other_router->get_priority() : 0); - - if(!entry_conflict && !exit_conflict && other_priofree_block(*b_iter_next); - } - else if(other_train!=yielding_to && (other_prioget_train()==other_train);) - { - if(!advance_route(j, *i)) - break; - last_contested = i; - i = i.next(*j); - } - - if(last_contested) - { - if(other_train->free_block(*last_contested)) - other_router->yield_to(train); - else - yield_to(*other_train); - } - } - } + if(!waits.empty() && waits.front().block==b_iter_next.block()) + train.stop_at(&block); } } void TrainRouter::train_advanced(Block &block) { + BlockIter b_iter = train.get_block_allocator().iter_for(block); + // Check if we've reached the next route if(routes.size()>1) { - unsigned entry = train.get_entry_to_block(block); - Track &track = *block.get_endpoint(entry).track; - const Route &route = **++routes.begin(); - if(route.has_track(track)) + Track &track = *b_iter.endpoint().track; + const Route *route = get_route(); + bool change_route = false; + if(route->is_loop()) + change_route = (*++routes.begin())->has_track(track); + else + change_route = !route->has_track(track); + + if(change_route) { routes.pop_front(); + route = get_route(); // XXX Exceptions? - signal_event.emit(Message("route-changed", get_route())); + signal_route_changed.emit(route); + signal_event.emit(Message("route-changed", route)); + } + } + + if(!waypoints.empty()) + { + const TrackChain &wp = *waypoints.front(); + TrackIter t_iter = b_iter.track_iter(); + if(wp.has_track(*t_iter)) + { + for(; t_iter; t_iter=t_iter.next()) + { + if(!wp.has_track(*t_iter)) + { + waypoints.erase(waypoints.begin()); + signal_waypoint_reached.emit(&wp); + signal_event.emit(Message("waypoint-reached", &wp)); + break; + } + else if(!block.has_track(*t_iter)) + break; + } } } if(!routes.empty()) { - BlockIter iter(&block, train.get_entry_to_block(block)); - iter = iter.next(); - if(iter && !is_on_route(*iter)) - arriving = true; + b_iter = b_iter.next(); + if(b_iter && !is_on_route(*b_iter)) + arriving = 1; } } @@ -299,6 +324,23 @@ const Route *TrainRouter::get_route_for_block(const Block &block) const return 0; } +void TrainRouter::create_metrics() +{ + for(vector::iterator i=metrics.begin(); i!=metrics.end(); ++i) + delete *i; + metrics.clear(); + + if(!destination) + return; + + metrics.push_back(new TrainRouteMetric(*destination)); + for(vector::const_iterator i=waypoints.begin(); i!=waypoints.end(); ++i) + metrics.push_back(new TrainRouteMetric(**i)); + + for(unsigned i=metrics.size(); --i>0; ) + metrics[i]->chain_to(*metrics[(i+1)%metrics.size()]); +} + Route *TrainRouter::create_lead_route(Route *lead, const Route *target) { if(!lead) @@ -308,27 +350,36 @@ Route *TrainRouter::create_lead_route(Route *lead, const Route *target) lead->set_temporary(true); } - set tracks; - for(BlockIter i=train.get_tail_block(); (i && i->get_train()==&train); i=i.next()) + bool target_reached = false; + for(TrackIter i=train.get_block_allocator().first().track_iter(); i; i=i.next()) { - const set &btracks = i->get_tracks(); - for(set::const_iterator j=btracks.begin(); j!=btracks.end(); ++j) - if(!target || !target->has_track(**j)) - tracks.insert(*j); + if(i->get_block().get_train()!=&train) + break; + if(target) + { + if(target->has_track(*i)) + target_reached = true; + else if(target_reached) + break; + } + lead->add_track(*i); } - lead->add_tracks(tracks); - return lead; } bool TrainRouter::advance_route(RouteList::iterator &iter, const Block &block) { const set &tracks = block.get_tracks(); + unsigned turnout_addr = block.get_turnout_address(); for(; iter!=routes.end(); ++iter) + { + if(turnout_addr && (*iter)->get_turnout(turnout_addr)<0) + continue; for(set::const_iterator j=tracks.begin(); j!=tracks.end(); ++j) if((*iter)->has_track(**j)) return true; + } return false; } @@ -339,6 +390,27 @@ bool TrainRouter::is_on_route(const Block &block) return advance_route(iter, block); } +void TrainRouter::create_plans(Layout &layout) +{ + const map &trains = layout.get_trains(); + for(map::const_iterator i=trains.begin(); i!=trains.end(); ++i) + if(TrainRouter *router = i->second->get_ai_of_type()) + { + if(router->update_pending) + router->create_metrics(); + router->update_pending = false; + } + + TrainRoutePlanner planner(layout); + planner.plan(); +} + + +TrainRouter::Wait::Wait(): + block(0), + train(0) +{ } + TrainRouter::Loader::Loader(TrainRouter &r): DataFile::ObjectLoader(r)