X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Fblockallocator.cpp;h=674722bb9d21b6951bd0f8b0405fad8e4ecd4db3;hb=a5e1363ceac1ab552849640c9021fcda362c4416;hp=547415aa965173ad5eeb68fb0966eceb6aff000c;hpb=18c529dfdd34e07f4f6021151ddce29fa85437bc;p=r2c2.git diff --git a/source/libr2c2/blockallocator.cpp b/source/libr2c2/blockallocator.cpp index 547415a..674722b 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_path_length(); + 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)