X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Fblockallocator.cpp;h=2e816775f7dfa7c86683a559e3f1f638e436b33f;hb=b1936d09d6f37f941e594493e8a7e18fca88cc55;hp=547415aa965173ad5eeb68fb0966eceb6aff000c;hpb=18c529dfdd34e07f4f6021151ddce29fa85437bc;p=r2c2.git diff --git a/source/libr2c2/blockallocator.cpp b/source/libr2c2/blockallocator.cpp index 547415a..2e81677 100644 --- a/source/libr2c2/blockallocator.cpp +++ b/source/libr2c2/blockallocator.cpp @@ -60,13 +60,53 @@ void BlockAllocator::set_active(bool a) } } -void BlockAllocator::start_from(const BlockIter &block) +bool BlockAllocator::start_from(const BlockIter &block) { if(!block) throw invalid_argument("BlockAllocator::start_from"); + float remaining_length = 0; + unsigned n_vehs = train.get_n_vehicles(); + for(unsigned i=0; iget_train()) + break; + blocks_to_reserve.push_back(b); + for(TrackIter t=b.track_iter(); (t && &t->get_block()==&*b); t=t.next()) + remaining_length -= t->get_type().get_path_length(t->get_active_path()); + if(remaining_length<=0) + break; + } + + if(remaining_length>0) + return false; + + for(BlockList::iterator i=blocks_to_reserve.begin(); i!=blocks_to_reserve.end(); ++i) + { + blocks.push_back(*i); + try + { + (*i)->reserve(&train); + } + catch(...) + { + blocks.pop_back(); + while(i!=blocks_to_reserve.begin()) + { + blocks.pop_back(); + (*--i)->reserve(0); + } + throw; + } + } + + return true; } void BlockAllocator::rewind_to(const Block &block)