From 39d3db437ae419bf8195d33422d5ee38962175aa Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 26 Sep 2010 11:26:23 +0000 Subject: [PATCH] Avoid various problems associated with dead end blocks Deal with nonexistent blocks while loading trains --- source/libmarklin/block.cpp | 5 +++ source/libmarklin/train.cpp | 68 ++++++++++++++++++++++++++----------- source/libmarklin/train.h | 1 + 3 files changed, 55 insertions(+), 19 deletions(-) diff --git a/source/libmarklin/block.cpp b/source/libmarklin/block.cpp index 51f8ddb..aa16fff 100644 --- a/source/libmarklin/block.cpp +++ b/source/libmarklin/block.cpp @@ -199,6 +199,11 @@ void Block::determine_id() swap(id1, id2); id = (id1<<16)|id2; } + else if(endpoints.size()==1) + { + unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1; + id = 0x10000 | id1; + } } diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index eadc37d..c53f30f 100644 --- a/source/libmarklin/train.cpp +++ b/source/libmarklin/train.cpp @@ -705,13 +705,6 @@ unsigned Train::reserve_more() unsigned good_sens = nsens; while(good_sens<3) { - if(last->block->get_endpoints().size()<2) - { - good = last; - good_sens = nsens; - break; - } - // Traverse to the next block unsigned exit = last->block->traverse(last->entry); Block *link = last->block->get_link(exit); @@ -740,6 +733,13 @@ unsigned Train::reserve_more() else if(route && route->get_tracks().count(entry_ep.track)) cur_route = route; + if(link->get_endpoints().size()<2) + { + good = last; + good_sens = nsens; + break; + } + bool reserved = link->reserve(this); if(!reserved) { @@ -952,7 +952,8 @@ void Train::RealSpeed::add(float s, float w) Train::Loader::Loader(Train &t): DataFile::BasicLoader(t), - prev_block(0) + prev_block(0), + blocks_valid(true) { add("block", &Loader::block); add("block_hint", &Loader::block_hint); @@ -964,30 +965,59 @@ Train::Loader::Loader(Train &t): add("vehicle", &Loader::vehicle); } +void Train::Loader::finish() +{ + if(!obj.cur_blocks.empty()) + { + const BlockRef &blkref = obj.cur_blocks.front(); + const Block::Endpoint &bep = blkref.block->get_endpoints()[blkref.entry]; + obj.vehicles.back()->place(bep.track, bep.track_ep, 0, Vehicle::BACK_BUFFER); + + obj.set_status("Stopped"); + } +} + void Train::Loader::block(unsigned id) { - Block &blk = obj.layout.get_block(id); + if(!blocks_valid) + return; + + Block *blk; + try + { + blk = &obj.layout.get_block(id); + } + catch(const KeyError &) + { + blocks_valid = false; + return; + } + int entry = -1; if(prev_block) - entry = blk.get_endpoint_by_link(*prev_block); + entry = blk->get_endpoint_by_link(*prev_block); if(entry<0) entry = 0; - blk.reserve(&obj); - obj.cur_blocks.push_back(BlockRef(&blk, entry)); - obj.set_status("Stopped"); - const Block::Endpoint &bep = blk.get_endpoints()[entry]; - obj.vehicles.front()->place(bep.track, bep.track_ep, 0, Vehicle::BACK_BUFFER); + blk->reserve(&obj); + obj.cur_blocks.push_back(BlockRef(blk, entry)); - if(blk.get_sensor_id()) - obj.layout.get_driver().set_sensor(blk.get_sensor_id(), true); + if(blk->get_sensor_id()) + obj.layout.get_driver().set_sensor(blk->get_sensor_id(), true); - prev_block = &blk; + prev_block = blk; } void Train::Loader::block_hint(unsigned id) { - prev_block = &obj.layout.get_block(id); + try + { + prev_block = &obj.layout.get_block(id); + } + catch(const KeyError &) + { + blocks_valid = false; + } } void Train::Loader::name(const string &n) diff --git a/source/libmarklin/train.h b/source/libmarklin/train.h index e8785f5..ee66e1d 100644 --- a/source/libmarklin/train.h +++ b/source/libmarklin/train.h @@ -33,6 +33,7 @@ public: public: Loader(Train &); private: + virtual void finish(); void block(unsigned); void block_hint(unsigned); void name(const std::string &); -- 2.45.2