unsigned good_sens = nsens;
while(good_sens<3)
{
- if(last->block->get_endpoints().size()<2)
- {
- good = last;
- good_sens = nsens;
- break;
- }
-
// Traverse to the next block
unsigned exit = last->block->traverse(last->entry);
Block *link = last->block->get_link(exit);
else if(route && route->get_tracks().count(entry_ep.track))
cur_route = route;
+ if(link->get_endpoints().size()<2)
+ {
+ good = last;
+ good_sens = nsens;
+ break;
+ }
+
bool reserved = link->reserve(this);
if(!reserved)
{
Train::Loader::Loader(Train &t):
DataFile::BasicLoader<Train>(t),
- prev_block(0)
+ prev_block(0),
+ blocks_valid(true)
{
add("block", &Loader::block);
add("block_hint", &Loader::block_hint);
add("vehicle", &Loader::vehicle);
}
+void Train::Loader::finish()
+{
+ if(!obj.cur_blocks.empty())
+ {
+ const BlockRef &blkref = obj.cur_blocks.front();
+ const Block::Endpoint &bep = blkref.block->get_endpoints()[blkref.entry];
+ obj.vehicles.back()->place(bep.track, bep.track_ep, 0, Vehicle::BACK_BUFFER);
+
+ obj.set_status("Stopped");
+ }
+}
+
void Train::Loader::block(unsigned id)
{
- Block &blk = obj.layout.get_block(id);
+ if(!blocks_valid)
+ return;
+
+ Block *blk;
+ try
+ {
+ blk = &obj.layout.get_block(id);
+ }
+ catch(const KeyError &)
+ {
+ blocks_valid = false;
+ return;
+ }
+
int entry = -1;
if(prev_block)
- entry = blk.get_endpoint_by_link(*prev_block);
+ entry = blk->get_endpoint_by_link(*prev_block);
if(entry<0)
entry = 0;
- blk.reserve(&obj);
- obj.cur_blocks.push_back(BlockRef(&blk, entry));
- obj.set_status("Stopped");
- const Block::Endpoint &bep = blk.get_endpoints()[entry];
- obj.vehicles.front()->place(bep.track, bep.track_ep, 0, Vehicle::BACK_BUFFER);
+ blk->reserve(&obj);
+ obj.cur_blocks.push_back(BlockRef(blk, entry));
- if(blk.get_sensor_id())
- obj.layout.get_driver().set_sensor(blk.get_sensor_id(), true);
+ if(blk->get_sensor_id())
+ obj.layout.get_driver().set_sensor(blk->get_sensor_id(), true);
- prev_block = &blk;
+ prev_block = blk;
}
void Train::Loader::block_hint(unsigned id)
{
- prev_block = &obj.layout.get_block(id);
+ try
+ {
+ prev_block = &obj.layout.get_block(id);
+ }
+ catch(const KeyError &)
+ {
+ blocks_valid = false;
+ }
}
void Train::Loader::name(const string &n)