From 45048d1eadafda8d956a074a730216cb74bffc51 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 30 Dec 2009 18:32:26 +0000 Subject: [PATCH 1/1] Improve the block allocation algorithm --- source/libmarklin/train.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index 5cc4705..c127086 100644 --- a/source/libmarklin/train.cpp +++ b/source/libmarklin/train.cpp @@ -359,11 +359,15 @@ unsigned Train::reserve_more() break; int entry = link->get_endpoint_by_link(*last->block); + if(entry<0) + throw LogicError("Block links are inconsistent!"); if(!link->reserve(this)) { - // If we found another train going in the same direction as us, we can keep the blocks we got + // If we found another train and it's not headed straight for us, we can keep the blocks we got int other_entry = link->get_train()->get_entry_to_block(*link); - if(other_entry==entry || link->traverse(entry)==link->traverse(other_entry)) + if(other_entry<0) + throw LogicError("Block reservation inconsistency"); + if(static_cast(entry)!=link->traverse(other_entry)) { good = last; good_sens = nsens; @@ -377,12 +381,9 @@ unsigned Train::reserve_more() const Block::Endpoint &ep = link->get_endpoints()[entry]; const Endpoint &track_ep = ep.track->get_type().get_endpoints()[ep.track_ep]; - if(track_ep.paths&(track_ep.paths-1)) - { - // We're facing the points - keep the blocks reserved so far - good = last; - good_sens = nsens; - } + // Keep the blocks reserved so far, as either us or the other train can diverge + good = last; + good_sens = nsens; Turnout &turnout = trfc_mgr.get_control().get_turnout(link->get_turnout_id()); -- 2.43.0