{
update_next_sensor(0);
// Immediately advance to just before the next sensor
- advance_to(next_sensor ? next_sensor->get_block() : 0);
+ advance_front(next_sensor);
}
}
}
}
-void BlockAllocator::advance_to(const Block *block)
+void BlockAllocator::advance_front(const Block *block)
{
BlockList::iterator end;
if(block)
train.signal_advanced.emit(**i);
}
+void BlockAllocator::advance_front(const Sensor *sensor)
+{
+ advance_front(sensor ? sensor->get_block() : 0);
+}
+
+void BlockAllocator::advance_back()
+{
+ const Vehicle &veh = train.get_vehicle(train.get_controller().get_reverse() ? 0 : train.get_n_vehicles()-1);
+ const Block &veh_block = veh.get_track()->get_block();
+
+ /* Sensors aren't guaranteed to be detriggered in order. Go through the
+ block list and locate the first sensor that's still active. */
+ Sensor *last_inactive = 0;
+ for(BlockList::iterator i=blocks.begin(); (i!=cur_blocks_end && i->block()!=&veh_block); ++i)
+ if(Sensor *sensor = (*i)->get_sensor())
+ {
+ if(sensor->get_state())
+ {
+ if(last_inactive)
+ release_blocks_begin(i);
+ return;
+ }
+ else
+ last_inactive = sensor;
+ }
+}
+
void BlockAllocator::release_blocks_begin(const BlockList::iterator &end)
{
for(BlockList::iterator i=blocks.begin(); i!=end; )
if(&sensor==next_sensor)
{
update_next_sensor(next_sensor);
- advance_to(next_sensor ? next_sensor->get_block() : 0);
+ advance_front(next_sensor);
if(active)
reserve_more();
train.get_layout().emergency("Sensor for "+train.get_name()+" triggered out of order");
}
else if(state==Sensor::INACTIVE)
- {
- const Vehicle &veh = train.get_vehicle(train.get_controller().get_reverse() ? 0 : train.get_n_vehicles()-1);
- const Block &veh_block = veh.get_track()->get_block();
-
- /* 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();
- for(BlockList::iterator i=blocks.begin(); i!=cur_blocks_end; ++i)
- {
- // Avoid freeing blocks that still hold the train's vehicles
- if(&**i==&veh_block)
- break;
-
- if(Sensor *s = (*i)->get_sensor())
- {
- if(s->get_state()>=Sensor::MAYBE_INACTIVE)
- break;
- else
- end = i;
- }
- }
-
- if(end!=blocks.end())
- // Free blocks up to the last inactive sensor
- release_blocks_begin(++end);
- }
+ advance_back();
}
void BlockAllocator::update_next_sensor(Sensor *after)