overshoot_dist(false)
{
if(!loco_type.is_locomotive())
- throw InvalidParameterValue("Initial vehicle must be a locomotive");
+ throw invalid_argument("Train::Train");
unsigned speed_steps = layout.get_driver().get_protocol_speed_steps(protocol);
if(speed_steps)
void Train::remove_vehicle(unsigned i)
{
if(i>=vehicles.size())
- throw InvalidParameterValue("Vehicle index out of range");
+ throw out_of_range("Train::remove_vehicle");
if(i==0)
- throw InvalidParameterValue("Can't remove the locomotive");
+ throw logic_error("can't remove locomotive");
delete vehicles[i];
vehicles.erase(vehicles.begin()+i);
if(i<vehicles.size())
Vehicle &Train::get_vehicle(unsigned i)
{
if(i>=vehicles.size())
- throw InvalidParameterValue("Vehicle index out of range");
+ throw out_of_range("Train::get_vehicle");
return *vehicles[i];
}
const Vehicle &Train::get_vehicle(unsigned i) const
{
if(i>=vehicles.size())
- throw InvalidParameterValue("Vehicle index out of range");
+ throw out_of_range("Train::get_vehicle");
return *vehicles[i];
}
if(a==active)
return;
if(!a && controller->get_speed())
- throw InvalidState("Can't deactivate while moving");
+ throw logic_error("moving");
active = a;
if(active)
void Train::set_function(unsigned func, bool state)
{
if(!loco_type.get_functions().count(func))
- throw InvalidParameterValue("Invalid function");
+ throw invalid_argument("Train::set_function");
layout.get_driver().set_loco_function(address, func, state);
}
bool Train::divert(Track &from)
{
if(!from.get_turnout_id())
- throw InvalidParameterValue("Can't divert from a non-turnout");
+ throw invalid_argument("Train::divert");
if(routes.empty())
return false;
if(end!=routes.end())
break;
else if(!diversion->has_track(*track))
- throw LogicError("Pathfinder returned a bad route");
+ throw logic_error("bad diversion");
track = track.next(diversion->get_path(*track));
}
void Train::place(Block &block, unsigned entry)
{
if(controller->get_speed())
- throw InvalidState("Must be stopped before placing");
+ throw logic_error("moving");
release_blocks();
set_active(false);
accurate_position = false;
+ blocks.push_back(BlockIter(&block, entry));
if(!block.reserve(this))
+ {
+ blocks.pop_back();
return;
+ }
- blocks.push_back(BlockIter(&block, entry));
if(reverse)
{
TrackIter track = BlockIter(&block, entry).reverse().track_iter();
void Train::unplace()
{
if(controller->get_speed())
- throw InvalidState("Must be stopped before unplacing");
+ throw logic_error("moving");
release_blocks();
}
}
-int Train::get_entry_to_block(Block &block) const
+int Train::get_entry_to_block(const Block &block) const
{
for(BlockList::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
if(i->block()==&block)
if(dist>10*layout.get_catalogue().get_scale())
{
- blocks.front()->reserve(0);
+ Block &block = *blocks.front();
blocks.pop_front();
+ block.reserve(0);
}
}
}
}
}
+ blocks.push_back(block);
bool reserved = block->reserve(this);
if(!reserved)
{
+ blocks.pop_back();
/* We've found another train. If it wants to exit the block from the
same endpoint we're trying to enter from or the other way around,
treat it as coming towards us. Otherwise treat it as going in the
Train *other_train = block->get_train();
int other_entry = other_train->get_entry_to_block(*block);
if(other_entry<0)
- throw LogicError("Block reservation inconsistency");
+ throw logic_error("block reservation inconsistency");
unsigned exit = block.reverse().entry();
unsigned other_exit = BlockIter(block.block(), other_entry).reverse().entry();
/* Ask a lesser priority train going to the same direction to free
the block for us */
if(other_train->free_block(*block))
- reserved = block->reserve(this);
+ {
+ blocks.push_back(block);
+ if(!(reserved = block->reserve(this)))
+ blocks.pop_back();
+ }
}
else if(other_train!=yielding_to && (other_prio<priority || (other_prio==priority && entry_conflict)))
{
if(!contested_blocks.empty() && contested_blocks.front()==block)
contested_blocks.pop_front();
- blocks.push_back(block);
-
if(cur_blocks_end==blocks.end())
--cur_blocks_end;
if(clear_blocks_end==blocks.end())
if(i==clear_blocks_end)
++clear_blocks_end;
+ if(i==cur_blocks_end && !(*i)->get_sensor_id())
+ ++cur_blocks_end;
}
}
Train::Loader::Loader(Train &t):
- DataFile::BasicLoader<Train>(t),
+ DataFile::ObjectLoader<Train>(t),
prev_block(0),
blocks_valid(true)
{
if(entry<0)
entry = 0;
- blk->reserve(&obj);
obj.blocks.push_back(BlockIter(blk, entry));
+ blk->reserve(&obj);
if(blk->get_sensor_id())
obj.layout.get_driver().set_sensor(blk->get_sensor_id(), true);