X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Ftrain.cpp;h=f745313233d57ce7e2b513ee2628c815c69dade2;hb=9b68c21ebf8ce26d92246ecd4a392d2908b511f7;hp=63a14e991c9e814a525ce43139ccc9366d42ea64;hpb=651698847d5293cfb15b6fb23a394701388c0151;p=r2c2.git diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index 63a14e9..f745313 100644 --- a/source/libmarklin/train.cpp +++ b/source/libmarklin/train.cpp @@ -29,7 +29,10 @@ Train::Train(Layout &l, const LocoType &t, unsigned a): target_speed(0), current_speed(0), reverse(false), + functions(0), route(0), + next_route(0), + end_of_route(false), status("Unplaced"), travel_dist(0), travel_speed(0), @@ -124,10 +127,64 @@ bool Train::get_function(unsigned func) const 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; + } + } + route = r; + next_route = 0; + end_of_route = false; + + if(route) + { + 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)) + { + next_route = route; + route = Route::find(*ep.track, ep.track_ep, *next_route); + } + } + + if(target_speed && reserve_more()<2) + update_speed(); + signal_route_changed.emit(route); } +void Train::go_to(const Track &to) +{ + for(list::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i) + if(i->block->get_tracks().count(const_cast(&to))) + { + set_speed(0); + set_route(0); + return; + } + + BlockRef *last = 0; + if(rsv_blocks.empty()) + last = &cur_blocks.back(); + else + { + for(list::iterator i=rsv_blocks.begin(); (i!=rsv_blocks.end() && !last); ++i) + if(i->block->get_sensor_id()) + last = &*i; + } + + BlockRef next = last->next(); + const Block::Endpoint &ep = next.block->get_endpoints()[next.entry]; + + set_route(Route::find(*ep.track, ep.track_ep, to)); +} + void Train::place(Block &block, unsigned entry) { if(target_speed) @@ -184,6 +241,7 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) if(stop_timeout && t>=stop_timeout) { release_blocks(rsv_blocks); + end_of_route = false; stop_timeout = Time::TimeStamp(); } @@ -214,7 +272,7 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) } if(cur_track) - pos = cur_track->get_point(cur_track_ep, path, offset); + pos = cur_track->get_point(cur_track_ep, path, offset).pos; } } @@ -224,8 +282,6 @@ void Train::save(list &st) const for(unsigned i=0; i<=14; ++i) if(real_speed[i].weight) st.push_back((DataFile::Statement("real_speed"), i, real_speed[i].speed, real_speed[i].weight)); - if(route) - st.push_back((DataFile::Statement("route"), route->get_name())); if(!cur_blocks.empty()) { @@ -239,6 +295,9 @@ void Train::save(list &st) const for(list::const_iterator i=blocks.begin(); i!=blocks.end(); ++i) st.push_back((DataFile::Statement("block"), i->block->get_id())); } + + if(route) + st.push_back((DataFile::Statement("route"), route->get_name())); } void Train::loco_speed_event(unsigned addr, unsigned speed, bool rev) @@ -303,12 +362,36 @@ void Train::sensor_event(unsigned addr, bool state) last_entry_time = Time::now(); pure_speed = true; + // Check if we've reached the next route + if(next_route) + { + const set &rtracks = next_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; + // XXX Exceptions? + signal_route_changed.emit(route); + break; + } + } + // Move blocks up to the next sensor to our current blocks cur_blocks.splice(cur_blocks.end(), rsv_blocks, rsv_blocks.begin(), i); // Try to get more blocks if we're moving - if(target_speed && reserve_more()<2) - update_speed(); + if(target_speed) + { + unsigned nsens = reserve_more(); + if(!nsens && end_of_route) + { + set_speed(0); + set_route(0); + } + else if(nsens<2) + update_speed(); + } } } else @@ -335,8 +418,13 @@ void Train::sensor_event(unsigned addr, bool state) void Train::turnout_event(unsigned addr, bool) { - if(pending_block && addr==pending_block->get_turnout_id()) - reserve_more(); + if(pending_block) + { + unsigned pending_addr = pending_block->get_turnout_id(); + bool double_addr = (*pending_block->get_tracks().begin())->get_type().is_double_address(); + if(addr==pending_addr || (double_addr && addr==pending_addr+1)) + reserve_more(); + } } void Train::block_reserved(const Block &block, const Train *train) @@ -357,11 +445,22 @@ unsigned Train::reserve_more() pending_block = 0; - // See how many blocks we already have + // See how many sensor blocks we already have unsigned nsens = 0; for(list::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i) if(i->block->get_sensor_id()) ++nsens; + + const Route *cur_route = 0; + if(route) + { + unsigned exit = last->block->traverse(last->entry); + Track *track = last->block->get_endpoints()[exit].track; + if(route->get_tracks().count(track)) + cur_route = route; + else if(next_route && next_route->get_tracks().count(track)) + cur_route = next_route; + } bool got_more = false; BlockRef *good = last; @@ -377,6 +476,25 @@ unsigned Train::reserve_more() int entry = link->get_endpoint_by_link(*last->block); if(entry<0) throw LogicError("Block links are inconsistent!"); + + const Block::Endpoint &entry_ep = link->get_endpoints()[entry]; + + if(cur_route) + { + 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)) + { + // Keep the blocks if we arrived at the end of the route + good = last; + good_sens = nsens; + end_of_route = true; + break; + } + } + else if(route && route->get_tracks().count(entry_ep.track)) + cur_route = route; + if(!link->reserve(this)) { // If we found another train and it's not headed straight for us, we can keep the blocks we got @@ -394,8 +512,7 @@ unsigned Train::reserve_more() if(link->get_turnout_id()) { - const Block::Endpoint &ep = link->get_endpoints()[entry]; - const Endpoint &track_ep = ep.track->get_type().get_endpoints()[ep.track_ep]; + const Endpoint &track_ep = entry_ep.track->get_type().get_endpoints()[entry_ep.track_ep]; // Keep the blocks reserved so far, as either us or the other train can diverge good = last; @@ -403,10 +520,10 @@ unsigned Train::reserve_more() // Figure out what path we'd like to take on the turnout int path = -1; - if(route) - path = route->get_turnout(link->get_turnout_id()); + if(cur_route) + path = cur_route->get_turnout(link->get_turnout_id()); if(path<0) - path = ep.track->get_active_path(); + path = entry_ep.track->get_active_path(); if(!((track_ep.paths>>path)&1)) { for(unsigned i=0; track_ep.paths>>i; ++i) @@ -414,12 +531,12 @@ unsigned Train::reserve_more() path = i; } - if(path!=static_cast(ep.track->get_active_path())) + if(path!=static_cast(entry_ep.track->get_active_path())) { // The turnout is set to wrong path - switch and wait for it link->reserve(0); pending_block = link; - ep.track->set_active_path(path); + entry_ep.track->set_active_path(path); break; } } @@ -445,7 +562,7 @@ unsigned Train::reserve_more() if(got_more) update_speed(); - return nsens; + return good_sens; } void Train::update_speed() @@ -581,6 +698,25 @@ void Train::reverse_blocks(list &blocks) const } +Train::BlockRef::BlockRef(Block *b, unsigned e): + block(b), + entry(e) +{ } + +Train::BlockRef Train::BlockRef::next() const +{ + Block *blk = block->get_endpoints()[block->traverse(entry)].link; + if(!blk) + throw InvalidState("At end of line"); + + int ep = blk->get_endpoint_by_link(*block); + if(ep<0) + throw LogicError("Block links are inconsistent"); + + return BlockRef(blk, ep); +} + + Train::RealSpeed::RealSpeed(): speed(0), weight(0)