X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Flibr2c2%2Fblockallocator.cpp;h=cb6b912de53481552990bb5f76936bf5ee801778;hb=7b4155fa0581d7c5fe198e4fb1dea61fa0846eec;hp=8e8726955c42cfddee688a7a0a1c775355857748;hpb=42a5c6606e82b1a27cb6d4d7afa630c75c869f97;p=r2c2.git diff --git a/source/libr2c2/blockallocator.cpp b/source/libr2c2/blockallocator.cpp index 8e87269..cb6b912 100644 --- a/source/libr2c2/blockallocator.cpp +++ b/source/libr2c2/blockallocator.cpp @@ -34,11 +34,13 @@ BlockAllocator::BlockAllocator(Train &t): pending_block(0), stop_at_block(0), reserving(false), - advancing(false) + advancing(false), + reserve_pending(false) { Layout &layout = train.get_layout(); layout.signal_block_reserved.connect(sigc::mem_fun(this, &BlockAllocator::block_reserved)); layout.signal_sensor_state_changed.connect(sigc::mem_fun(this, &BlockAllocator::sensor_state_changed)); + layout.get_driver().signal_halt.connect(sigc::mem_fun(this, &BlockAllocator::halt_event)); const set &tracks = layout.get_all(); for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) @@ -58,6 +60,7 @@ void BlockAllocator::set_active(bool a) { release_blocks_end(cur_blocks_end); pending_block = 0; + reserve_pending = false; } } @@ -185,11 +188,23 @@ bool BlockAllocator::is_block_current(const Block &block) const return find_if(blocks.begin(), end, BlockMatch(block))!=cur_blocks_end; } +void BlockAllocator::tick() +{ + if(reserve_pending) + { + reserve_pending = false; + reserve_more(); + } +} + void BlockAllocator::reserve_more() { if(blocks.empty()) throw logic_error("no blocks"); + if(train.get_layout().get_driver().is_halted()) + return; + BlockIter start = blocks.back(); if(&*start==stop_at_block) return; @@ -354,6 +369,8 @@ void BlockAllocator::advance_back() const Vehicle &veh = train.get_vehicle(rev ? 0 : train.get_n_vehicles()-1); const Block &veh_block = veh.get_placement().get_position(rev ? VehiclePlacement::FRONT_AXLE : VehiclePlacement::BACK_AXLE)->get_block(); + bool pending_cleared = false; + /* Sensors aren't guaranteed to be detriggered in order. Go through the block list and locate the first sensor that's still active. */ BlockList::iterator end = blocks.end(); @@ -387,6 +404,9 @@ void BlockAllocator::advance_back() end = i; } + if(block==pending_block) + pending_cleared = true; + // Stop if we encounter an active sensor or the train's last vehicle if(block==&veh_block || active_sensor) { @@ -397,7 +417,12 @@ void BlockAllocator::advance_back() if(i!=end) ++end; release_blocks_begin(end); + signal_rear_advanced.emit(*last()); } + + if(pending_cleared) + reserve_more(); + return; } } @@ -469,13 +494,13 @@ void BlockAllocator::turnout_path_changing(Track &track) void BlockAllocator::turnout_path_changed(Track &track) { if(&track.get_block()==pending_block && !reserving) - reserve_more(); + reserve_pending = true; } void BlockAllocator::block_reserved(Block &block, const Train *tr) { if(&block==pending_block && !tr && !reserving) - reserve_more(); + reserve_pending = true; } void BlockAllocator::sensor_state_changed(Sensor &sensor, Sensor::State state) @@ -494,7 +519,7 @@ void BlockAllocator::sensor_state_changed(Sensor &sensor, Sensor::State state) advance_front(next_sensor); if(active) - reserve_more(); + reserve_pending = true; } else if(!is_block_current(*block)) train.get_layout().emergency(block, "Sensor for "+train.get_name()+" triggered out of order"); @@ -551,6 +576,12 @@ void BlockAllocator::update_next_sensor(Sensor *after) next_sensor = 0; } +void BlockAllocator::halt_event(bool halted) +{ + if(active && !halted) + reserve_more(); +} + void BlockAllocator::save(list &st) const { if(!blocks.empty() && cur_blocks_end!=blocks.begin()) @@ -567,7 +598,7 @@ void BlockAllocator::save(list &st) const st.push_back((DataFile::Statement("hint"), prev->get_id())); - for(BlockList::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i) + for(BlockList::const_iterator i=cur_blocks.begin(); (i!=cur_blocks.end() && i->block()!=pending_block); ++i) st.push_back((DataFile::Statement("block"), (*i)->get_id())); } }