From: Mikko Rasa Date: Sun, 17 Feb 2013 13:21:52 +0000 (+0200) Subject: Set turnout paths synchronously when allocating blocks X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=5b1b2196908c159d4a1339bce2682afa22700c35;p=r2c2.git Set turnout paths synchronously when allocating blocks --- diff --git a/source/libr2c2/train.cpp b/source/libr2c2/train.cpp index 93bd427..a45675c 100644 --- a/source/libr2c2/train.cpp +++ b/source/libr2c2/train.cpp @@ -45,7 +45,6 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a, const string &p): protocol(p), preceding_train(0), cur_blocks_end(blocks.end()), - clear_blocks_end(blocks.end()), pending_block(0), reserving(false), advancing(false), @@ -384,7 +383,7 @@ float Train::get_reserved_distance() const if(next && next->get_type().is_turnout()) margin = 15*layout.get_catalogue().get_scale(); - return max(get_reserved_distance_until(0, false)-margin, 0.0f); + return max(get_reserved_distance_until(pending_block, false)-margin, 0.0f); } void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) @@ -648,13 +647,8 @@ void Train::block_state_changed(Block &block, Block::State state) 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) - { - if(&**i==pending_block) - reserve_more(); - else - check_turnout_paths(false); - } + if((*i)->get_turnout_id()==track.get_turnout_id() && !reserving && &**i==pending_block) + reserve_more(); } void Train::halt_event(bool h) @@ -677,6 +671,12 @@ void Train::reserve_more() BlockIter start = blocks.back(); if(&*start==stop_at_block) return; + else if(&*start==pending_block) + { + TrackIter track = start.track_iter(); + if(!(track.endpoint().paths&(1<get_active_path()))) + return; + } pending_block = 0; preceding_train = 0; @@ -725,8 +725,6 @@ void Train::reserve_more() if(cur_blocks_end==blocks.end()) --cur_blocks_end; - if(clear_blocks_end==blocks.end()) - --clear_blocks_end; TrackIter track = block.track_iter(); if(track->is_path_changing()) @@ -734,6 +732,29 @@ void Train::reserve_more() pending_block = &*block; break; } + else + { + const TrackType::Endpoint &entry_ep = track.endpoint(); + unsigned path = track->get_active_path(); + if(!(entry_ep.paths&(1<1; ++path, mask>>=1) ; + + track->set_active_path(path); + if(track->is_path_changing()) + { + pending_block = &*block; + break; + } + } + else + // XXX Do something here + break; + } + } if(&*block==stop_at_block) break; @@ -744,56 +765,11 @@ void Train::reserve_more() dist += block->get_path_length(block.entry()); } - check_turnout_paths(true); - // Make any sensorless blocks at the beginning immediately current - while(cur_blocks_end!=clear_blocks_end && !(*cur_blocks_end)->get_sensor_id()) + while(cur_blocks_end!=blocks.end() && !(*cur_blocks_end)->get_sensor_id()) ++cur_blocks_end; } -void Train::check_turnout_paths(bool set) -{ - if(clear_blocks_end==blocks.end()) - return; - - for(list::iterator i=clear_blocks_end; i!=blocks.end(); ++i) - { - if((*i)->get_turnout_id()) - { - TrackIter track = i->track_iter(); - const TrackType::Endpoint &track_ep = track.endpoint(); - - unsigned path = 0; - list::iterator j = i; - if(++j!=blocks.end()) - { - TrackIter rev = j->track_iter().flip(); - unsigned mask = rev.endpoint().paths&track_ep.paths; - for(path=0; mask>1; mask>>=1, ++path) ; - } - else - return; - - if(path!=track->get_active_path()) - { - if(set && !track->is_path_changing()) - { - track->set_active_path(path); - if(track->is_path_changing()) - continue; - } - else - continue; - } - } - - if(i==clear_blocks_end) - ++clear_blocks_end; - if(i==cur_blocks_end && !(*i)->get_sensor_id()) - ++cur_blocks_end; - } -} - float Train::get_reserved_distance_until(const Block *until_block, bool back) const { if(blocks.empty()) @@ -807,9 +783,9 @@ float Train::get_reserved_distance_until(const Block *until_block, bool back) co return 0; BlockList::const_iterator block = blocks.begin(); - while(block!=clear_blocks_end && !(*block)->has_track(*track)) + while(block!=blocks.end() && !(*block)->has_track(*track)) ++block; - if(block==clear_blocks_end || &**block==until_block) + if(block==blocks.end() || &**block==until_block) return 0; float result = veh.get_offset(); @@ -836,7 +812,7 @@ float Train::get_reserved_distance_until(const Block *until_block, bool back) co else { ++block; - if(block==clear_blocks_end) + if(block==blocks.end()) break; } @@ -861,8 +837,6 @@ void Train::release_blocks(BlockList::iterator begin, BlockList::iterator end) { if(begin==cur_blocks_end) cur_blocks_end = end; - if(begin==clear_blocks_end) - clear_blocks_end = end; Block &block = **begin; blocks.erase(begin++); diff --git a/source/libr2c2/train.h b/source/libr2c2/train.h index 7c388ad..fa638f8 100644 --- a/source/libr2c2/train.h +++ b/source/libr2c2/train.h @@ -57,7 +57,6 @@ private: std::vector vehicles; BlockList blocks; BlockList::iterator cur_blocks_end; - BlockList::iterator clear_blocks_end; Block *pending_block; Block *stop_at_block; bool reserving; @@ -138,7 +137,6 @@ private: public: void reserve_more(); private: - void check_turnout_paths(bool); float get_reserved_distance_until(const Block *, bool) const; void release_blocks(); void release_blocks(BlockList::iterator, BlockList::iterator);