X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrain.cpp;h=599f2dcdf28b27d92a1401b9fe025d701c4949d1;hb=4086edbfae9a04da491cdb349a3bef6af5c132d9;hp=415d49146d290e1865c65043800d7ff4e33d21d6;hpb=2d44923e135a119fde31a7a50e5fcd3e400e4f41;p=r2c2.git diff --git a/source/libr2c2/train.cpp b/source/libr2c2/train.cpp index 415d491..599f2dc 100644 --- a/source/libr2c2/train.cpp +++ b/source/libr2c2/train.cpp @@ -1,13 +1,7 @@ -/* $Id$ - -This file is part of R²C² -Copyright © 2006-2011 Mikkosoft Productions, Mikko Rasa -Distributed under the GPL -*/ - #include #include -#include +#include +#include #include #include #include "aicontrol.h" @@ -70,7 +64,7 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a, const string &p): 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) @@ -132,9 +126,9 @@ void Train::add_vehicle(const VehicleType &vt) 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()) - 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]; } @@ -170,7 +164,7 @@ void Train::set_active(bool a) if(a==active) return; if(!a && controller->get_speed()) - throw InvalidState("Can't deactivate while moving"); + throw logic_error("moving"); active = a; if(active) @@ -185,7 +179,7 @@ void Train::set_active(bool a) 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); } @@ -327,7 +321,7 @@ bool Train::go_to(const Zone &to) 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; @@ -401,7 +395,7 @@ bool Train::divert(Track &from) 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)); } @@ -429,17 +423,20 @@ const Route *Train::get_route() const 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(); @@ -455,7 +452,7 @@ void Train::place(Block &block, unsigned entry) void Train::unplace() { if(controller->get_speed()) - throw InvalidState("Must be stopped before unplacing"); + throw logic_error("moving"); release_blocks(); @@ -548,7 +545,7 @@ void Train::free_noncritical_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) @@ -654,8 +651,9 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) if(dist>10*layout.get_catalogue().get_scale()) { - blocks.front()->reserve(0); + Block &block = *blocks.front(); blocks.pop_front(); + block.reserve(0); } } } @@ -973,9 +971,11 @@ void Train::reserve_more() } } + 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 @@ -983,7 +983,7 @@ void Train::reserve_more() 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(); @@ -1006,7 +1006,11 @@ void Train::reserve_more() /* 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_prioget_sensor_id()) + ++cur_blocks_end; } } @@ -1295,7 +1299,7 @@ Train::RouteRef::RouteRef(const Route *r, unsigned d): Train::Loader::Loader(Train &t): - DataFile::BasicLoader(t), + DataFile::ObjectLoader(t), prev_block(0), blocks_valid(true) { @@ -1329,7 +1333,7 @@ void Train::Loader::block(unsigned id) { blk = &obj.layout.get_block(id); } - catch(const KeyError &) + catch(const key_error &) { blocks_valid = false; return; @@ -1341,8 +1345,8 @@ void Train::Loader::block(unsigned id) 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); @@ -1356,7 +1360,7 @@ void Train::Loader::block_hint(unsigned id) { prev_block = &obj.layout.get_block(id); } - catch(const KeyError &) + catch(const key_error &) { blocks_valid = false; }