X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Ftrain.cpp;h=5bc50d85ff23ee5011535e34e5e6142f6b08fccc;hb=2029c5e4220e0809a39744a28ca9e2ff22e8ad28;hp=d6ed044954286ba549c0da0c0e75d363e300861e;hpb=1f9af43b6ab300693c044b431e95b25422b36507;p=r2c2.git diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index d6ed044..5bc50d8 100644 --- a/source/libmarklin/train.cpp +++ b/source/libmarklin/train.cpp @@ -55,8 +55,6 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a): speed_changing(false), reverse(false), functions(0), - route(0), - next_route(0), end_of_route(false), status("Unplaced"), travel_dist(0), @@ -207,61 +205,179 @@ void Train::set_timetable(Timetable *tt) void Train::set_route(const Route *r) { - if(!rsv_blocks.empty()) - { - for(list::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; - } - } + free_noncritical_blocks(); - route = r; - next_route = 0; + routes.clear(); + if(r) + routes.push_back(r); end_of_route = false; - if(route && !cur_blocks.empty()) + if(r && !cur_blocks.empty()) { + BlockRef &first = cur_blocks.front(); 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)) + const Block::Endpoint &first_ep = first.block->get_endpoints()[first.entry]; + const Block::Endpoint &next_ep = next.block->get_endpoints()[next.entry]; + if(!r->has_track(*next_ep.track)) { - next_route = route; - route = Route::find(*ep.track, ep.track_ep, *next_route); + Route *lead = Route::find(*next_ep.track, next_ep.track_ep, *r); + create_lead_route(lead, lead); + routes.push_front(lead); } + else if(!r->has_track(*first_ep.track)) + routes.push_front(create_lead_route(0, r)); } reserve_more(); - signal_route_changed.emit(route); + signal_route_changed.emit(get_route()); } -void Train::go_to(const Track &to) +void Train::go_to(Track &to) { for(list::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i) - if(i->block->get_tracks().count(const_cast(&to))) + if(i->block->has_track(to)) { signal_arrived.emit(); set_route(0); return; } - BlockRef *last = 0; - if(rsv_blocks.empty()) - last = &cur_blocks.back(); + free_noncritical_blocks(); + + 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]; + + Route *route = Route::find(*ep.track, ep.track_ep, to); + create_lead_route(route, route); + set_route(route); +} + +bool Train::divert(Track &from) +{ + if(!from.get_turnout_id()) + throw InvalidParameterValue("Can't divert from a non-turnout"); + if(routes.empty()) + return false; + + int path = -1; + unsigned from_ep = 0; + list::iterator route = routes.begin(); + Block *block = cur_blocks.back().block; + unsigned entry = cur_blocks.back().entry; + set visited; + + // Follow our routes to find out where we're entering the turnout + while(1) + { + Block *link = block->get_link(block->traverse(entry, route->route)); + entry = link->get_endpoint_by_link(*block); + block = link; + + const Block::Endpoint &entry_ep = block->get_endpoints()[entry]; + + if(visited.count(entry_ep.track)) + return false; + visited.insert(entry_ep.track); + + if(!advance_route(route, *entry_ep.track)) + return false; + + if(entry_ep.track==&from) + { + if(block->get_train()==this && !free_block(*block)) + return false; + + from_ep = entry_ep.track_ep; + path = route->route->get_turnout(from.get_turnout_id()); + break; + } + } + + // Check that more than one path is available + unsigned ep_paths = from.get_type().get_endpoints()[from_ep].paths; + if(!(ep_paths&(ep_paths-1))) + return false; + + // Choose some other path + for(int i=0; ep_paths>>i; ++i) + if((ep_paths&(1<get_endpoint_by_link(from); + + set tracks; + for(list::iterator i=routes.begin(); i!=routes.end(); ++i) + tracks.insert(i->route->get_tracks().begin(), i->route->get_tracks().end()); + Route *diversion = 0; + try + { + diversion = Route::find(*track, ep, tracks); + } + catch(const Msp::Exception &) + { + return false; + } + + diversion->set_name("Diversion"); + diversion->add_track(from); + diversion->set_turnout(from.get_turnout_id(), path); + + if(!is_valid_diversion(*diversion, from, from_ep)) + { + delete diversion; + return false; + } + + // Follow the diversion route until we get back to the original route + list::iterator end = routes.end(); + while(1) + { + path = 0; + if(track->get_turnout_id()) + path = diversion->get_turnout(track->get_turnout_id()); + Track *next = track->get_link(track->traverse(ep, path)); + + for(list::iterator i=route; (end==routes.end() && i!=routes.end()); ++i) + if(i->route->has_track(*next)) + end = i; + + if(end!=routes.end()) + break; + else if(!diversion->has_track(*next)) + throw Exception("Pathfinder returned a bad route"); + + ep = next->get_endpoint_by_link(*track); + track = next; + } + + if(route==end) + // We are rejoining the same route we diverted from, duplicate it + routes.insert(end, *route); else { - for(list::iterator i=rsv_blocks.begin(); (i!=rsv_blocks.end() && !last); ++i) - if(i->block->get_sensor_id()) - last = &*i; + ++route; + routes.erase(route, end); } + routes.insert(end, RouteRef(diversion, from.get_turnout_id())); - BlockRef next = last->next(); - const Block::Endpoint &ep = next.block->get_endpoints()[next.entry]; + return true; +} - set_route(Route::find(*ep.track, ep.track_ep, to)); +const Route *Train::get_route() const +{ + if(routes.empty()) + return 0; + return routes.front().route; } void Train::place(Block &block, unsigned entry) @@ -337,6 +453,75 @@ bool Train::free_block(Block &block) return false; } +void Train::free_noncritical_blocks() +{ + if(cur_blocks.empty() || rsv_blocks.empty()) + return; + + if(controller->get_speed()==0) + { + release_blocks(rsv_blocks); + return; + } + + float margin = 10*layout.get_catalogue().get_scale(); + float min_dist = controller->get_braking_distance()*1.3+margin; + + Vehicle &veh = *(reverse ? vehicles.back() : vehicles.front()); + + Track *track = veh.get_track(); + list::iterator block = cur_blocks.begin(); + bool in_rsv = false; + while(block!=rsv_blocks.end() && !block->block->has_track(*track)) + { + ++block; + if(block==cur_blocks.end()) + { + block = rsv_blocks.begin(); + in_rsv = true; + } + } + + unsigned entry = veh.get_entry(); + float dist = veh.get_offset(); + if(reverse) + entry = track->traverse(entry); + else + dist = track->get_type().get_path_length(track->get_active_path())-dist; + dist -= veh.get_type().get_length()/2; + + bool nsens = 0; + while(1) + { + Track *next = track->get_link(track->traverse(entry)); + entry = next->get_endpoint_by_link(*track); + track = next; + + if(!block->block->has_track(*track)) + { + ++block; + if(block==cur_blocks.end()) + { + block = rsv_blocks.begin(); + in_rsv = true; + } + if(block==rsv_blocks.end()) + return; + + if(dist>min_dist && nsens>0) + { + release_blocks(rsv_blocks, block, rsv_blocks.end()); + return; + } + + if(in_rsv && block->block->get_sensor_id()) + ++nsens; + } + + dist += track->get_type().get_path_length(track->get_active_path()); + } +} + int Train::get_entry_to_block(Block &block) const { for(list::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i) @@ -403,7 +588,7 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) bool ok = false; for(list::const_iterator i=cur_blocks.begin(); (!ok && i!=cur_blocks.end()); ++i) - ok = i->block->get_tracks().count(track); + ok = i->block->has_track(*track); float d = get_real_speed(current_speed)*(dt/Time::sec); if(ok) @@ -423,6 +608,7 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) } else if(end_of_route && rsv_blocks.empty()) { + set_active(false); signal_arrived.emit(); set_route(0); } @@ -434,7 +620,7 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) if(dist>10*layout.get_catalogue().get_scale()) { cur_blocks.front().block->reserve(0); - cur_blocks.erase(cur_blocks.begin()); + cur_blocks.pop_front(); } } } @@ -466,12 +652,12 @@ void Train::save(list &st) const st.push_back((DataFile::Statement("block"), i->block->get_id())); } - if(route) + if(!routes.empty()) { - if(!route->is_temporary()) - st.push_back((DataFile::Statement("route"), route->get_name())); - else if(next_route && !next_route->is_temporary()) - st.push_back((DataFile::Statement("route"), next_route->get_name())); + list::const_iterator i = routes.begin(); + for(; (i!=routes.end() && i->route->is_temporary()); ++i) ; + if(i!=routes.end()) + st.push_back((DataFile::Statement("route"), i->route->get_name())); } if(timetable) @@ -576,16 +762,15 @@ void Train::sensor_event(unsigned addr, bool state) overshoot_dist = 0; // Check if we've reached the next route - if(next_route) + if(routes.size()>1) { - const set &rtracks = next_route->get_tracks(); + const set &rtracks = (++routes.begin())->route->get_tracks(); for(list::iterator j=rsv_blocks.begin(); j!=i; ++j) if(rtracks.count(j->block->get_endpoints()[j->entry].track)) { - route = next_route; - next_route = 0; + routes.pop_front(); // XXX Exceptions? - signal_route_changed.emit(route); + signal_route_changed.emit(routes.front().route); break; } } @@ -608,7 +793,7 @@ void Train::sensor_event(unsigned addr, bool state) list::iterator end = cur_blocks.begin(); for(list::iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i) { - if(i->block->get_tracks().count(veh.get_track())) + if(i->block->has_track(*veh.get_track())) break; if(i->block->get_sensor_id()) { @@ -630,7 +815,7 @@ void Train::sensor_event(unsigned addr, bool state) void Train::turnout_event(unsigned addr, bool) { - if(pending_block) + if(pending_block && (!pending_block->get_train() || pending_block->get_train()==this)) { unsigned pending_addr = pending_block->get_turnout_id(); bool double_addr = (*pending_block->get_tracks().begin())->get_type().is_double_address(); @@ -652,7 +837,7 @@ void Train::halt_event(bool h) void Train::block_reserved(const Block &block, const Train *train) { - if(&block==pending_block && !train) + if(&block==pending_block && !train && !reserving) reserve_more(); } @@ -689,24 +874,16 @@ unsigned Train::reserve_more() if(end_of_route) return nsens; - const Route *cur_route = 0; - if(route) - { - const set &tracks = start->block->get_tracks(); - for(set::const_iterator i=tracks.begin(); (cur_route!=route && i!=tracks.end()); ++i) - { - if(route->get_tracks().count(*i)) - cur_route = route; - else if(next_route && next_route->get_tracks().count(*i)) - cur_route = next_route; - } - } + list::iterator cur_route = routes.begin(); + advance_route(cur_route, *start->block->get_endpoints()[start->entry].track); float approach_margin = 50*layout.get_catalogue().get_scale(); float min_dist = controller->get_braking_distance()*1.3+approach_margin*2; BlockRef *last = start; BlockRef *good = start; + Track *divert_track = 0; + bool try_divert = false; unsigned good_sens = nsens; float good_dist = dist; Train *blocking_train = 0; @@ -718,8 +895,12 @@ unsigned Train::reserve_more() { // Traverse to the next block float length = 0; - unsigned exit = last->block->traverse(last->entry, cur_route, &length); - Block *link = last->block->get_link(exit); + Block *link = 0; + { + const Route *route = (cur_route!=routes.end() ? cur_route->route : 0); + unsigned exit = last->block->traverse(last->entry, route, &length); + link = last->block->get_link(exit); + } if(!link) break; @@ -729,11 +910,9 @@ unsigned Train::reserve_more() const Block::Endpoint &entry_ep = link->get_endpoints()[entry]; - if(cur_route) + if(cur_route!=routes.end()) { - if(cur_route!=next_route && next_route && next_route->get_tracks().count(entry_ep.track)) - cur_route = next_route; - else if(!cur_route->get_tracks().count(entry_ep.track)) + if(!advance_route(cur_route, *entry_ep.track)) { // Keep the blocks if we arrived at the end of the route if(!blocking_train) @@ -746,8 +925,8 @@ unsigned Train::reserve_more() break; } } - else if(route && route->get_tracks().count(entry_ep.track)) - cur_route = route; + else if(!routes.empty() && routes.front().route->has_track(*entry_ep.track)) + cur_route = routes.begin(); if(link->get_endpoints().size()<2) { @@ -764,11 +943,12 @@ unsigned Train::reserve_more() { if(link->get_train()!=blocking_train) { - // XXX is it possible that this won't free all the blocks we want? if(blocking_train->free_block(*contested_blocks.back().block)) { // Roll back and start actually reserving the blocks last = &rsv_blocks.back(); + cur_route = routes.begin(); + advance_route(cur_route, *last->block->get_endpoints()[last->entry].track); if(blocking_train->get_priority()==priority) blocking_train->yield_to(*this); blocking_train = 0; @@ -776,7 +956,9 @@ unsigned Train::reserve_more() } else { + yield_to(*blocking_train); pending_block = contested_blocks.front().block; + try_divert = divert_track; break; } } @@ -800,24 +982,27 @@ unsigned Train::reserve_more() if(other_entry<0) throw LogicError("Block reservation inconsistency"); - int other_prio = other_train->get_priority(); - bool entry_conflict = (static_cast(entry)==link->traverse(other_entry)); bool exit_conflict = (link->traverse(entry)==static_cast(other_entry)); - if(!entry_conflict && !exit_conflict) + if(!entry_conflict && !last->block->get_turnout_id()) { - /* Same direction, keep the blocks we got so far and wait for the - other train to pass */ + /* The other train is not coming to the blocks we're holding, so we + can keep them. */ good = last; good_sens = nsens; good_dist = dist; + } + + int other_prio = other_train->get_priority(); - // Ask a lesser priority train to free the block for us - if(other_train->get_priority()free_block(*link)) - reserved = link->reserve(this); + if(!entry_conflict && !exit_conflict && other_priofree_block(*link)) + reserved = link->reserve(this); } - else if(other_prioget_turnout_id()) { const Endpoint &track_ep = entry_ep.track->get_type().get_endpoints()[entry_ep.track_ep]; + bool multiple_paths = (track_ep.paths&(track_ep.paths-1)); - // Keep the blocks reserved so far, as either us or the other train can diverge - good = last; - good_sens = nsens; - good_dist = dist; + if(multiple_paths || !last->block->get_turnout_id()) + { + /* We can keep the blocks reserved so far if we are facing the + points or if there was no turnout immediately before this one. + With multiple successive turnouts (as is common in crossovers) it's + best to hold at one we can divert from. */ + good = last; + good_sens = nsens; + good_dist = dist; + } // Figure out what path we'd like to take on the turnout int path = -1; - if(cur_route) - path = cur_route->get_turnout(link->get_turnout_id()); + for(list::iterator i=cur_route; (path<0 && i!=routes.end()); ++i) + path = i->route->get_turnout(link->get_turnout_id()); if(path<0) path = entry_ep.track->get_active_path(); - if(!((track_ep.paths>>path)&1)) + if(!(track_ep.paths&(1<>i; ++i) - if((track_ep.paths>>i)&1) + if(track_ep.paths&(1<diversion!=link->get_turnout_id()) + /* There's multiple paths to be taken and we are on a route - take + note of the diversion possibility */ + divert_track = entry_ep.track; } if(!contested_blocks.empty() && contested_blocks.front().block==link) @@ -885,7 +1085,7 @@ unsigned Train::reserve_more() while(!rsv_blocks.empty() && &rsv_blocks.back()!=good) { rsv_blocks.back().block->reserve(0); - rsv_blocks.erase(--rsv_blocks.end()); + rsv_blocks.pop_back(); } if(!rsv_blocks.empty() && &rsv_blocks.back()!=start) @@ -898,6 +1098,9 @@ unsigned Train::reserve_more() if(i!=rsv_blocks.begin()) cur_blocks.splice(cur_blocks.end(), rsv_blocks, rsv_blocks.begin(), i); + if(try_divert && divert(*divert_track)) + return reserve_more(); + return good_sens; } @@ -914,7 +1117,7 @@ float Train::get_reserved_distance_until(const Block *until_block, bool back) co return 0; list::const_iterator block = cur_blocks.begin(); - while(block!=rsv_blocks.end() && !block->block->get_tracks().count(track)) + while(block!=rsv_blocks.end() && !block->block->has_track(*track)) { ++block; if(block==cur_blocks.end()) @@ -943,7 +1146,7 @@ float Train::get_reserved_distance_until(const Block *until_block, bool back) co Track *next = track->get_link(track->traverse(entry)); - if(!block->block->get_tracks().count(next)) + if(!block->block->has_track(*next)) { if(back) { @@ -1071,6 +1274,100 @@ void Train::reverse_blocks(list &blocks) const i->entry = i->block->traverse(i->entry); } +bool Train::advance_route(list::iterator &iter, Track &track) +{ + while(iter!=routes.end() && !iter->route->has_track(track)) + ++iter; + if(iter==routes.end()) + return false; + + list::iterator next = iter; + ++next; + if(next!=routes.end() && next->diversion && next->route->has_track(track)) + iter = next; + + 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(list::iterator i=cur_blocks.begin(); i!=rsv_blocks.end(); ) + { + const set &btracks = i->block->get_tracks(); + for(set::const_iterator j=btracks.begin(); j!=btracks.end(); ++j) + if(!target || !target->has_track(**j)) + tracks.insert(*j); + + if(++i==cur_blocks.end()) + i = rsv_blocks.begin(); + } + + lead->add_tracks(tracks); + + return lead; +} + +bool Train::is_valid_diversion(const Route &diversion, Track &from, unsigned from_ep) +{ + float diversion_len = 0; + Track *track = &from; + unsigned ep = from_ep; + while(diversion.has_track(*track)) + { + unsigned path = 0; + if(track->get_turnout_id()) + path = diversion.get_turnout(track->get_turnout_id()); + diversion_len += track->get_type().get_path_length(path); + + Track *next = track->get_link(track->traverse(ep, path)); + ep = next->get_endpoint_by_link(*track); + track = next; + + if(track==&from) + return false; + } + + list::iterator route = routes.begin(); + if(!advance_route(route, from)) + return false; + + set visited; + float route_len = 0; + track = &from; + ep = from_ep; + while(1) + { + unsigned path = 0; + if(track->get_turnout_id()) + path = route->route->get_turnout(track->get_turnout_id()); + route_len += track->get_type().get_path_length(path); + + if(track!=&from && diversion.has_track(*track)) + break; + + if(visited.count(track)) + return false; + visited.insert(track); + + Track *next = track->get_link(track->traverse(ep, path)); + ep = next->get_endpoint_by_link(*track); + track = next; + + if(!advance_route(route, *track)) + return false; + } + + return diversion_lenget_endpoints()[blkref.entry]; - obj.vehicles.back()->place(*bep.track, bep.track_ep, 0, Vehicle::BACK_BUFFER); + float offset = 2*obj.layout.get_catalogue().get_scale(); + obj.vehicles.back()->place(*bep.track, bep.track_ep, offset, Vehicle::BACK_BUFFER); obj.set_status("Stopped"); }