+ 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;
+ }