X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrainrouter.cpp;h=65a4a24500206cb0286a1f5cb3da4efc22358c7c;hb=1124a98ce5e1edee1904eeec089976d446601afd;hp=8cfedee8f28623013cfa719f436704ec2963af5f;hpb=ca9d278f9472206ad9a01190dcef9f0eb1bcc274;p=r2c2.git diff --git a/source/libr2c2/trainrouter.cpp b/source/libr2c2/trainrouter.cpp index 8cfedee..65a4a24 100644 --- a/source/libr2c2/trainrouter.cpp +++ b/source/libr2c2/trainrouter.cpp @@ -14,7 +14,7 @@ namespace R2C2 { TrainRouter::TrainRouter(Train &t): TrainAI(t), priority(0), - arriving(false), + arriving(0), dest_zone(0), dest_block(0), update_pending(false) @@ -30,14 +30,14 @@ void TrainRouter::set_priority(int 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()) { const BlockAllocator &allocator = train.get_block_allocator(); TrackIter first = allocator.first().track_iter(); - TrackIter next = allocator.last().next().track_iter(); + TrackIter next = fncb.track_iter(); if(!r->has_track(*next)) { lead = Route::find(next, *r); @@ -55,9 +55,17 @@ bool TrainRouter::set_route(const Route *r) if(r) routes.push_back(r); train.stop_at(0); - arriving = false; + arriving = 0; - train.reserve_more(); + /* TODO destination should also be cleared when manually setting a different + route, but not when the planner calls this. */ + if(!r) + { + dest_zone = 0; + dest_block = 0; + } + + train.refresh_blocks_from(*fncb); const Route *route = get_route(); signal_route_changed.emit(route); @@ -110,6 +118,12 @@ bool TrainRouter::is_destination(Track &track) const return false; } +void TrainRouter::set_departure_delay(const Time::TimeDelta &d) +{ + delay = d; + update_pending = true; +} + void TrainRouter::message(const Message &msg) { if(msg.type=="set-route") @@ -135,20 +149,30 @@ void TrainRouter::message(const Message &msg) else set_destination(*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(delay) + { + delay -= dt; + if(delay<=Time::zero) + delay = Time::zero; + } + if(update_pending) create_plans(train.get_layout()); - if(arriving && !train.get_speed()) + if(arriving==1 && !train.get_speed()) { - train.set_active(false); signal_arrived.emit(); signal_event.emit(Message("arrived")); - set_route(0); + arriving = 2; } + else if(arriving==2 && !train.get_block_allocator().is_active()) + set_route(0); } void TrainRouter::save(list &st) const @@ -176,15 +200,15 @@ void TrainRouter::block_reserved(Block &block, Train *t) return; } - BlockIter b_iter(&block, t->get_block_allocator().get_entry_to_block(block)); + 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); } @@ -206,13 +230,13 @@ void TrainRouter::block_reserved(Block &block, Train *t) 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_block_allocator().get_entry_to_block(block); - Track &track = *block.get_endpoint(entry).track; const Route &route = **++routes.begin(); - if(route.has_track(track)) + if(route.has_track(*b_iter.endpoint().track)) { routes.pop_front(); // XXX Exceptions? @@ -222,10 +246,9 @@ void TrainRouter::train_advanced(Block &block) if(!routes.empty()) { - BlockIter iter(&block, train.get_block_allocator().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; } }