using namespace std;
using namespace Msp;
+namespace {
+
+struct SetFlag
+{
+ bool &flag;
+
+ SetFlag(bool &f): flag(f) { flag = true; }
+ ~SetFlag() { flag = false; }
+};
+
+}
+
+
namespace Marklin {
Train::Train(Layout &l, const VehicleType &t, unsigned a):
next_route = 0;
end_of_route = false;
- if(route)
+ if(route && !cur_blocks.empty())
{
BlockRef &last = (rsv_blocks.empty() ? cur_blocks.back() : rsv_blocks.back());
BlockRef next = last.next();
float Train::get_reserved_distance() const
{
+ if(cur_blocks.empty())
+ return 0;
+
Vehicle &veh = *(reverse ? vehicles.back() : vehicles.front());
const VehicleType &vtype = veh.get_type();
float d = get_real_speed(current_speed)*(dt/Time::sec);
if(ok)
{
- advancing = true;
+ SetFlag setf(advancing);
vehicle.advance(reverse ? -d : d);
- advancing = false;
}
else if(accurate_position)
{
}
}
- if(end!=cur_blocks.begin())
+ if(end!=cur_blocks.begin() && end!=cur_blocks.end())
// Free blocks up to the last inactive sensor
release_blocks(cur_blocks, cur_blocks.begin(), end);
}
}
}
- reserving = true;
+ SetFlag setf(reserving);
bool got_more = false;
BlockRef *good = last;
last = &rsv_blocks.back();
}
- reserving = false;
// Make any sensorless blocks at the beginning immediately current
list<BlockRef>::iterator i;