]> git.tdb.fi Git - r2c2.git/commitdiff
Avoid various problems associated with dead end blocks
authorMikko Rasa <tdb@tdb.fi>
Sun, 26 Sep 2010 11:26:23 +0000 (11:26 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sun, 26 Sep 2010 11:26:23 +0000 (11:26 +0000)
Deal with nonexistent blocks while loading trains

source/libmarklin/block.cpp
source/libmarklin/train.cpp
source/libmarklin/train.h

index 51f8ddb0dfe45dbd954f994ba75b1c82f759e290..aa16fffc6ba5b6e3f04879f0e3d5fdb206b809c3 100644 (file)
@@ -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;
+       }
 }
 
 
index eadc37de3d460a136ed8e94f0ebd108e9d258a53..c53f30f66e6de0f05cca61da9b24f20486579e13 100644 (file)
@@ -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<Train>(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)
index e8785f5e5994deb4ae275e670a0187676dd03534..ee66e1d1ca29e0dc38df83e4fc86dc0a3f0b548a 100644 (file)
@@ -33,6 +33,7 @@ public:
        public:
                Loader(Train &);
        private:
+               virtual void finish();
                void block(unsigned);
                void block_hint(unsigned);
                void name(const std::string &);