X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrain.cpp;h=93bd427ba56193248a0a420b028bc622123032d2;hb=d0af7846e05691d65d8021e46c8f81e8ca05199a;hp=eb5724b762a9777494ec71ec23acccf8bf81a787;hpb=66c2c7ca5a4bd369293959bc211b040834343381;p=r2c2.git diff --git a/source/libr2c2/train.cpp b/source/libr2c2/train.cpp index eb5724b..93bd427 100644 --- a/source/libr2c2/train.cpp +++ b/source/libr2c2/train.cpp @@ -15,6 +15,7 @@ #include "trackiter.h" #include "tracktype.h" #include "train.h" +#include "trainrouter.h" #include "vehicle.h" #include "vehicletype.h" #include "zone.h" @@ -42,8 +43,6 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a, const string &p): loco_type(t), address(a), protocol(p), - priority(0), - yielding_to(0), preceding_train(0), cur_blocks_end(blocks.end()), clear_blocks_end(blocks.end()), @@ -56,7 +55,6 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a, const string &p): speed_changing(false), reverse(false), functions(0), - end_of_route(false), travel_dist(0), pure_speed(false), speed_quantizer(0), @@ -106,16 +104,6 @@ void Train::set_name(const string &n) signal_name_changed.emit(name); } -void Train::set_priority(int p) -{ - priority = p; -} - -void Train::yield_to(const Train &t) -{ - yielding_to = &t; -} - void Train::add_vehicle(const VehicleType &vt) { Vehicle *veh = new Vehicle(layout, vt); @@ -234,97 +222,6 @@ void Train::ai_message(const TrainAI::Message &msg) (*i)->message(msg); } -bool Train::set_route(const Route *r) -{ - free_noncritical_blocks(); - - Route *lead = 0; - if(r && !blocks.empty()) - { - TrackIter first = blocks.front().track_iter(); - TrackIter next = blocks.back().next().track_iter(); - if(!r->has_track(*next)) - { - lead = Route::find(next, *r); - if(!lead) - return false; - create_lead_route(lead, lead); - routes.push_front(lead); - } - else if(!r->has_track(*first)) - lead = create_lead_route(0, r); - } - - routes.clear(); - if(lead) - routes.push_back(lead); - if(r) - routes.push_back(r); - end_of_route = false; - - reserve_more(); - - signal_route_changed.emit(get_route()); - - return true; -} - -bool Train::go_to(Track &to) -{ - for(BlockList::const_iterator i=blocks.begin(); i!=cur_blocks_end; ++i) - if((*i)->has_track(to)) - { - signal_arrived.emit(); - return set_route(0); - } - - free_noncritical_blocks(); - - TrackIter next = blocks.back().next().track_iter(); - - Route *route = Route::find(next, to); - if(!route) - return false; - create_lead_route(route, route); - return set_route(route); -} - -bool Train::go_to(const Zone &to) -{ - set tracks; - for(BlockList::const_iterator i=blocks.begin(); i!=blocks.end(); ++i) - tracks.insert((*i)->get_tracks().begin(), (*i)->get_tracks().end()); - - 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); - - if(union_size==tracks.size() || union_size==ztracks.size()) - { - signal_arrived.emit(); - return set_route(0); - } - - free_noncritical_blocks(); - - TrackIter next = blocks.back().next().track_iter(); - - Route *route = Route::find(next, to); - if(!route) - return false; - create_lead_route(route, route); - route->add_tracks(ztracks); - return set_route(route); -} - -const Route *Train::get_route() const -{ - if(routes.empty()) - return 0; - return routes.front(); -} - void Train::place(Block &block, unsigned entry) { if(controller->get_speed()) @@ -368,6 +265,11 @@ void Train::unplace() (*i)->unplace(); } +void Train::stop_at(Block *block) +{ + stop_at_block = block; +} + bool Train::free_block(Block &block) { if(get_reserved_distance_until(&block, false)get_braking_distance()*1.3) @@ -450,6 +352,20 @@ void Train::free_noncritical_blocks() } } +const BlockIter &Train::get_head_block() const +{ + if(blocks.empty()) + throw logic_error("no blocks"); + return blocks.back(); +} + +const BlockIter &Train::get_tail_block() const +{ + if(blocks.empty()) + throw logic_error("no blocks"); + return blocks.front(); +} + int Train::get_entry_to_block(const Block &block) const { for(BlockList::const_iterator i=blocks.begin(); i!=blocks.end(); ++i) @@ -543,12 +459,6 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) } } } - else if(end_of_route && cur_blocks_end==blocks.end()) - { - set_active(false); - signal_arrived.emit(); - set_route(0); - } if(!blocks.empty() && !blocks.front()->get_sensor_id()) { @@ -567,8 +477,6 @@ void Train::save(list &st) const { st.push_back((DataFile::Statement("name"), name)); - st.push_back((DataFile::Statement("priority"), priority)); - for(vector::const_iterator i=vehicles.begin(); i!=vehicles.end(); ++i) if(i!=vehicles.begin()) st.push_back((DataFile::Statement("vehicle"), (*i)->get_type().get_article_number())); @@ -593,22 +501,22 @@ void Train::save(list &st) const st.push_back((DataFile::Statement("block"), (*i)->get_id())); } - if(!routes.empty()) - { - list::const_iterator i = routes.begin(); - for(; (i!=routes.end() && (*i)->is_temporary()); ++i) ; - if(i!=routes.end()) - st.push_back((DataFile::Statement("route"), (*i)->get_name())); - } - // XXX Need more generic way of saving AI state for(list::const_iterator i=ais.begin(); i!=ais.end(); ++i) - if(Timetable *timetable = dynamic_cast(*i)) + { + if(TrainRouter *router = dynamic_cast(*i)) + { + DataFile::Statement ss("router"); + router->save(ss.sub); + st.push_back(ss); + } + else if(Timetable *timetable = dynamic_cast(*i)) { DataFile::Statement ss("timetable"); timetable->save(ss.sub); st.push_back(ss); } + } } void Train::control_changed(const Controller::Control &ctrl) @@ -697,20 +605,6 @@ void Train::block_state_changed(Block &block, Block::State state) accurate_position = true; overshoot_dist = 0; - // Check if we've reached the next route - if(routes.size()>1) - { - const Route &route = **(++routes.begin()); - for(BlockList::iterator j=cur_blocks_end; j!=end; ++j) - if(route.has_track(*j->track_iter())) - { - routes.pop_front(); - // XXX Exceptions? - signal_route_changed.emit(routes.front()); - break; - } - } - // Move blocks up to the next sensor to our current blocks for(BlockList::iterator j=cur_blocks_end; j!=end; ++j) signal_advanced.emit(**j); @@ -755,7 +649,12 @@ void Train::turnout_path_changed(Track &track) { for(list::iterator i=blocks.begin(); i!=blocks.end(); ++i) if((*i)->get_turnout_id()==track.get_turnout_id() && !reserving) - check_turnout_paths(false); + { + if(&**i==pending_block) + reserve_more(); + else + check_turnout_paths(false); + } } void Train::halt_event(bool h) @@ -772,10 +671,12 @@ void Train::block_reserved(const Block &block, const Train *train) void Train::reserve_more() { - if(!active || blocks.empty() || end_of_route) + if(!active || blocks.empty()) return; BlockIter start = blocks.back(); + if(&*start==stop_at_block) + return; pending_block = 0; preceding_train = 0; @@ -791,155 +692,51 @@ void Train::reserve_more() dist += (*i)->get_path_length(i->entry()); } - list::iterator cur_route = routes.begin(); - advance_route(cur_route, *start.track_iter()); - float approach_margin = 50*layout.get_catalogue().get_scale(); float min_dist = controller->get_braking_distance()*1.3+approach_margin*2; BlockIter block = start; - list::iterator good_end = blocks.end(); - Train *blocking_train = 0; - BlockList contested_blocks; SetFlag setf(reserving); while(1) { BlockIter last = block; - block = block.next(cur_route!=routes.end() ? *cur_route : 0); + block = block.next(); if(!block || block->get_endpoints().size()<2) - { - if(!blocking_train) - { - good_end = blocks.end(); - end_of_route = true; - } + // The track ends here break; - } - - TrackIter track = block.track_iter(); - - if(cur_route!=routes.end()) - { - if(!advance_route(cur_route, *track)) - { - // Keep the blocks if we arrived at the end of the route - if(!blocking_train) - { - good_end = blocks.end(); - end_of_route = true; - } - break; - } - } if(block->get_turnout_id() && !last->get_turnout_id()) { - /* We can keep the blocks if we arrive at a turnout from a non-turnout - block. Having a turnout block as our last reserved block is not good - as it would limit our diversion possibilities for little benefit. */ - good_end = blocks.end(); + /* We are arriving at a turnout. See if we have enough blocks and + distance reserved. */ if(nsens>=3 && dist>=min_dist) break; } - if(blocking_train) - { - if(block->get_train()!=blocking_train) - { - if(blocking_train->free_block(*contested_blocks.back())) - { - // Roll back and start actually reserving the blocks - block = blocks.back(); - cur_route = routes.begin(); - advance_route(cur_route, *block.track_iter().track()); - if(blocking_train->get_priority()==priority) - blocking_train->yield_to(*this); - blocking_train = 0; - continue; - } - else - { - yield_to(*blocking_train); - pending_block = contested_blocks.front().block(); - break; - } - } - else - { - contested_blocks.push_back(block); - continue; - } - } - blocks.push_back(block); - bool reserved = block->reserve(this); - if(!reserved) + if(!block->reserve(this)) { blocks.pop_back(); - /* We've found another train. 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. */ - Train *other_train = block->get_train(); - int other_entry = other_train->get_entry_to_block(*block); - if(other_entry<0) - throw logic_error("block reservation inconsistency"); - - unsigned exit = block.reverse().entry(); - unsigned other_exit = BlockIter(block.block(), other_entry).reverse().entry(); - bool entry_conflict = (block.entry()==other_exit); - bool exit_conflict = (exit==static_cast(other_entry)); - if(!entry_conflict && !last->get_turnout_id()) - { - /* The other train is not coming to the blocks we're holding, so we - can keep them. */ - good_end = blocks.end(); - - if(static_cast(other_entry)==block.entry()) - preceding_train = other_train; - } - - int other_prio = other_train->get_priority(); - - if(!entry_conflict && !exit_conflict && other_priofree_block(*block)) - { - blocks.push_back(block); - if(!(reserved = block->reserve(this))) - blocks.pop_back(); - } - } - else if(other_train!=yielding_to && (other_priois_path_changing()) + { + pending_block = &*block; + break; + } + + if(&*block==stop_at_block) + break; if(block->get_sensor_id()) ++nsens; @@ -947,13 +744,6 @@ void Train::reserve_more() dist += block->get_path_length(block.entry()); } - // Unreserve blocks that were not good - release_blocks(good_end, blocks.end()); - - if(blocks.back()!=start) - // We got some new blocks, so no longer need to yield - yielding_to = 0; - check_turnout_paths(true); // Make any sensorless blocks at the beginning immediately current @@ -986,12 +776,13 @@ void Train::check_turnout_paths(bool set) if(path!=track->get_active_path()) { - if(set) + if(set && !track->is_path_changing()) + { track->set_active_path(path); - - /* Check again, in case the driver was able to service the request - instantly */ - if(!set || path!=track->get_active_path()) + if(track->is_path_changing()) + continue; + } + else continue; } } @@ -1076,9 +867,6 @@ void Train::release_blocks(BlockList::iterator begin, BlockList::iterator end) Block &block = **begin; blocks.erase(begin++); block.reserve(0); - - if(begin==blocks.end()) - end_of_route = false; } } @@ -1089,39 +877,6 @@ void Train::reverse_blocks(BlockList &blks) const *i = i->reverse(); } -bool Train::advance_route(list::iterator &iter, Track &track) -{ - while(iter!=routes.end() && !(*iter)->has_track(track)) - ++iter; - if(iter==routes.end()) - return false; - - return true; -} - -Route *Train::create_lead_route(Route *lead, const Route *target) -{ - if(!lead) - { - lead = new Route(layout); - lead->set_name("Lead"); - lead->set_temporary(true); - } - - set tracks; - for(BlockList::iterator i=blocks.begin(); i!=blocks.end(); ++i) - { - 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); - } - - lead->add_tracks(tracks); - - return lead; -} - Train::Loader::Loader(Train &t): DataFile::ObjectLoader(t), @@ -1131,9 +886,8 @@ Train::Loader::Loader(Train &t): add("block", &Loader::block); add("block_hint", &Loader::block_hint); add("name", &Loader::name); - add("priority", &Train::priority); add("quantized_speed", &Loader::quantized_speed); - add("route", &Loader::route); + add("router", &Loader::router); add("timetable", &Loader::timetable); add("vehicle", &Loader::vehicle); } @@ -1202,9 +956,10 @@ void Train::Loader::quantized_speed() load_sub(*obj.speed_quantizer); } -void Train::Loader::route(const string &n) +void Train::Loader::router() { - obj.set_route(&obj.layout.get_route(n)); + TrainRouter *rtr = new TrainRouter(obj); + load_sub(*rtr); } void Train::Loader::timetable()