]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/blockallocator.cpp
Redesign the train activation system
[r2c2.git] / source / libr2c2 / blockallocator.cpp
index 429e19f31db95a3e4f827cc049079ad162ca6803..b38b62d6d1f1cd37763fcb2c3979dff8d4e97439 100644 (file)
@@ -39,7 +39,22 @@ BlockAllocator::BlockAllocator(Train &t):
        const set<Track *> &tracks = layout.get_all<Track>();
        for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
                if((*i)->get_turnout_id())
+               {
+                       (*i)->signal_path_changing.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changing), sigc::ref(**i))));
                        (*i)->signal_path_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changed), sigc::ref(**i))));
+               }
+}
+
+void BlockAllocator::set_active(bool a)
+{
+       active = a;
+       if(active)
+               reserve_more();
+       else
+       {
+               release_blocks_end(cur_blocks_end);
+               pending_block = 0;
+       }
 }
 
 void BlockAllocator::start_from(const BlockIter &block)
@@ -51,8 +66,22 @@ void BlockAllocator::start_from(const BlockIter &block)
        reserve_block(block);
 }
 
+void BlockAllocator::rewind_to(const Block &block)
+{
+       if(!active)
+               return;
+
+       BlockList::iterator i = find_if(cur_blocks_end, blocks.end(), BlockMatch(block));
+       if(i!=blocks.end())
+       {
+               release_blocks_end(i);
+               reserve_more();
+       }
+}
+
 void BlockAllocator::clear()
 {
+       active = false;
        release_blocks_begin(blocks.end());
        pending_block = 0;
        stop_at_block = 0;
@@ -61,6 +90,8 @@ void BlockAllocator::clear()
 void BlockAllocator::stop_at(const Block *block)
 {
        stop_at_block = block;
+       if(active && !block)
+               reserve_more();
 }
 
 const BlockIter &BlockAllocator::first() const
@@ -92,7 +123,7 @@ const BlockIter &BlockAllocator::last_current() const
 
 const BlockIter &BlockAllocator::iter_for(const Block &block) const
 {
-       BlockList::const_iterator i = find_if(blocks.begin(), blocks.end(), IterBlockMatch(block));
+       BlockList::const_iterator i = find_if(blocks.begin(), blocks.end(), BlockMatch(block));
        if(i==blocks.end())
                throw key_error(&block);
        return *i;
@@ -100,12 +131,13 @@ const BlockIter &BlockAllocator::iter_for(const Block &block) const
 
 bool BlockAllocator::has_block(const Block &block) const
 {
-       return find_if(blocks.begin(), blocks.end(), IterBlockMatch(block))!=blocks.end();
+       return find_if(blocks.begin(), blocks.end(), BlockMatch(block))!=blocks.end();
 }
 
 bool BlockAllocator::is_block_current(const Block &block) const
 {
-       return find_if(blocks.begin(), cur_blocks_end, IterBlockMatch(block))!=cur_blocks_end;
+       BlockList::const_iterator end = cur_blocks_end;
+       return find_if(blocks.begin(), end, BlockMatch(block))!=cur_blocks_end;
 }
 
 void BlockAllocator::reserve_more()
@@ -232,29 +264,6 @@ bool BlockAllocator::reserve_block(const BlockIter &block)
        }
 }
 
-bool BlockAllocator::release_from(const Block &block)
-{
-       bool have_sensor = false;
-       for(BlockList::iterator i=cur_blocks_end; i!=blocks.end(); ++i)
-       {
-               if(i->block()==&block)
-               {
-                       if(have_sensor)
-                               release_blocks_end(i);
-                       return have_sensor;
-               }
-               else if((*i)->get_sensor_id())
-                       have_sensor = true;
-       }
-
-       return false;
-}
-
-void BlockAllocator::release_noncurrent()
-{
-       release_blocks_end(cur_blocks_end);
-}
-
 void BlockAllocator::release_blocks_begin(const BlockList::iterator &end)
 {
        for(BlockList::iterator i=blocks.begin(); i!=end; )
@@ -294,10 +303,24 @@ void BlockAllocator::release_block(const BlockList::iterator &i)
 
 void BlockAllocator::reverse()
 {
-       release_noncurrent();
+       release_blocks_end(cur_blocks_end);
        blocks.reverse();
        for(BlockList::iterator i=blocks.begin(); i!=blocks.end(); ++i)
                *i = i->reverse();
+
+       if(active)
+               reserve_more();
+}
+
+void BlockAllocator::turnout_path_changing(Track &track)
+{
+       BlockList::iterator i = find_if(blocks.begin(), blocks.end(), BlockMatch(track.get_block()));
+       if(i!=blocks.end())
+       {
+               ++i;
+               release_blocks_end(i);
+               pending_block = &track.get_block();
+       }
 }
 
 void BlockAllocator::turnout_path_changed(Track &track)
@@ -350,6 +373,9 @@ void BlockAllocator::sensor_state_changed(Sensor &sensor, Sensor::State state)
                        for(BlockList::iterator j=cur_blocks_end; j!=end; ++j)
                                train.signal_advanced.emit(**j);
                        cur_blocks_end = end;
+
+                       if(active)
+                               reserve_more();
                }
                else if(result==3)
                        train.get_layout().emergency("Sensor for "+train.get_name()+" triggered out of order");