X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrain.cpp;h=b2a49c722e7732938cfce5ed3cdefa95b5838d9b;hb=f730acd5fa2b4f0d0185770ff7950ab814aec47c;hp=74bc50328ce8b6bb60f7f917c149b97989d5e7b7;hpb=0c2a3e6c435fd4cf05cc5275f750d341842aa543;p=r2c2.git diff --git a/source/libr2c2/train.cpp b/source/libr2c2/train.cpp index 74bc503..b2a49c7 100644 --- a/source/libr2c2/train.cpp +++ b/source/libr2c2/train.cpp @@ -89,6 +89,7 @@ void Train::add_vehicle(const VehicleType &vt) vehicles.back()->attach_back(*veh); vehicles.push_back(veh); veh->set_train(this); + signal_vehicle_added.emit(vehicles.size()-1, *veh); } void Train::remove_vehicle(unsigned i) @@ -97,10 +98,17 @@ void Train::remove_vehicle(unsigned i) throw out_of_range("Train::remove_vehicle"); if(i==0) throw logic_error("can't remove locomotive"); - delete vehicles[i]; + + Vehicle *veh = vehicles[i]; vehicles.erase(vehicles.begin()+i); + veh->detach_front(); if(idetach_back(); vehicles[i-1]->attach_back(*vehicles[i]); + } + signal_vehicle_removed.emit(i, *veh); + delete veh; } unsigned Train::get_n_vehicles() const @@ -340,6 +348,8 @@ void Train::tick(const Time::TimeDelta &dt) } } } + else if(intent_to_move && !allocator.is_active()) + allocator.set_active(true); else if(allocator.is_active() && !intent_to_move && !stop_timeout) stop_timeout = 2*Time::sec; } @@ -412,13 +422,9 @@ void Train::loco_func_event(unsigned addr, unsigned func, bool state) void Train::sensor_state_changed(Sensor &sensor, Sensor::State state) { - Block *block = 0; - if(TrackCircuit *tc = dynamic_cast(&sensor)) - block = &tc->get_block(); - else - return; + Block *block = sensor.get_block(); - if(block->get_train()==this && state==Sensor::MAYBE_ACTIVE) + if(block && block->get_train()==this && state==Sensor::MAYBE_ACTIVE) { if(last_entry_block) { @@ -480,8 +486,7 @@ float Train::get_reserved_distance_until(const Block *until_block) const if(!track) // XXX Probably unnecessary return 0; - BlockIter block = track.block_iter(); - if(&*block==until_block) + if(&track->get_block()==until_block) return 0; // Account for the vehicle's offset on its current track @@ -492,6 +497,8 @@ float Train::get_reserved_distance_until(const Block *until_block) const result = track->get_type().get_path_length(track->get_active_path())-result; result -= veh.get_type().get_length()/2; + BlockIter block = track.block_iter(); + // Count remaining distance in the vehicle's current block for(track=track.next(); &track->get_block()==&*block; track=track.next()) result += track->get_type().get_path_length(track->get_active_path());