X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrain.cpp;h=285a15a27fb83794bcffa9406c144b416e1d0079;hb=d41f66805bc9fe0b33e3d46b47f52e67b5782028;hp=d1fb4ed8a81b9608bb82827ccbf5071a3b395251;hpb=d2dfed1a38c5e8487532e9055fad464cf54efd83;p=r2c2.git diff --git a/source/libr2c2/train.cpp b/source/libr2c2/train.cpp index d1fb4ed..285a15a 100644 --- a/source/libr2c2/train.cpp +++ b/source/libr2c2/train.cpp @@ -6,6 +6,7 @@ #include #include #include "aicontrol.h" +#include "beamgate.h" #include "block.h" #include "catalogue.h" #include "driver.h" @@ -36,7 +37,6 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a, const string &p): allocator(*this), advancing(false), controller(new SimpleController), - active(false), current_speed_step(0), speed_changing(false), reverse(false), @@ -54,6 +54,7 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a, const string &p): speed_quantizer = new SpeedQuantizer(speed_steps); vehicles.push_back(new Vehicle(layout, loco_type)); + vehicles.back()->set_train(this); layout.add_train(*this); @@ -88,6 +89,8 @@ void Train::add_vehicle(const VehicleType &vt) Vehicle *veh = new Vehicle(layout, 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) @@ -96,10 +99,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 @@ -126,23 +136,6 @@ void Train::set_control(const string &n, float v) controller->set_control(n, v); } -void Train::set_active(bool a) -{ - if(a==active) - return; - if(!a && controller->get_speed()) - throw logic_error("moving"); - - active = a; - if(active) - { - stop_timeout = Time::TimeStamp(); - allocator.reserve_more(); - } - else - stop_timeout = Time::now()+2*Time::sec; -} - void Train::set_function(unsigned func, bool state) { if(!loco_type.get_functions().count(func)) @@ -199,10 +192,9 @@ void Train::place(const BlockIter &block) if(controller->get_speed()) throw logic_error("moving"); - set_active(false); - accurate_position = false; - allocator.start_from(block); + accurate_position = false; + last_entry_block = BlockIter(); if(reverse) vehicles.front()->place(block.reverse().track_iter(), 0, Vehicle::FRONT_BUFFER); @@ -216,9 +208,8 @@ void Train::unplace() throw logic_error("moving"); allocator.clear(); - - set_active(false); accurate_position = false; + last_entry_block = BlockIter(); for(vector::iterator i=vehicles.begin(); i!=vehicles.end(); ++i) (*i)->unplace(); @@ -227,48 +218,48 @@ void Train::unplace() void Train::stop_at(Block *block) { allocator.stop_at(block); - if(active && !block) - allocator.reserve_more(); } -bool Train::free_block(Block &block) +bool Train::is_block_critical(const Block &block) const { - if(get_reserved_distance_until(&block)get_braking_distance()*1.3) - return false; - - return allocator.release_from(block); + return get_reserved_distance_until(&block)<=controller->get_braking_distance()*1.3; } -void Train::free_noncritical_blocks() +BlockIter Train::get_first_noncritical_block() const { if(allocator.empty()) - return; + return BlockIter(); + + BlockIter i = allocator.last_current().next(); if(controller->get_speed()==0) - { - allocator.release_noncurrent(); - return; - } + return i; float margin = 10*layout.get_catalogue().get_scale(); float min_dist = controller->get_braking_distance()*1.3+margin; - BlockIter i = allocator.last_current().next(); float dist = 0; bool sensor_seen = false; for(; i->get_train()==this; i=i.next()) { if(dist>min_dist && sensor_seen) - { - allocator.release_from(*i); - return; - } + return i; dist += i->get_path_length(i.entry()); if(i->get_sensor_id()) sensor_seen = true; } + + return i; +} + +void Train::refresh_blocks_from(Block &block) +{ + if(is_block_critical(block)) + allocator.rewind_to(*get_first_noncritical_block()); + else + allocator.rewind_to(block); } float Train::get_reserved_distance() const @@ -284,23 +275,30 @@ float Train::get_reserved_distance() const return max(get_reserved_distance_until(0)-margin, 0.0f); } -void Train::reserve_more() +void Train::tick(const Time::TimeDelta &dt) { - allocator.reserve_more(); -} - -void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) -{ - if(!active && stop_timeout && t>=stop_timeout) + if(stop_timeout) { - allocator.release_noncurrent(); - stop_timeout = Time::TimeStamp(); + stop_timeout -= dt; + if(stop_timeout<=Time::zero) + { + allocator.set_active(false); + stop_timeout = Time::TimeDelta(); + } } + travel_time += dt; + Driver &driver = layout.get_driver(); + bool intent_to_move = false; for(list::iterator i=ais.begin(); i!=ais.end(); ++i) - (*i)->tick(t, dt); + { + (*i)->tick(dt); + if((*i)->has_intent_to_move()) + intent_to_move = true; + } + controller->tick(dt); float speed = controller->get_speed(); bool moving = speed>0; @@ -314,8 +312,7 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) driver.set_loco_reverse(address, r); allocator.reverse(); - if(active) - allocator.reserve_more(); + last_entry_block = BlockIter(); } if(speed_quantizer) @@ -334,8 +331,8 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) if(moving) { - if(!active) - set_active(true); + if(!allocator.is_active()) + allocator.set_active(true); Vehicle &vehicle = *(reverse ? vehicles.back() : vehicles.front()); @@ -355,6 +352,10 @@ void Train::tick(const Time::TimeStamp &t, 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; } void Train::save(list &st) const @@ -425,31 +426,32 @@ 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 + if(!current_speed_step || state!=Sensor::MAYBE_ACTIVE) + return; + + Block *block = sensor.get_block(); + if(!block || block->get_train()!=this) return; - if(block->get_train()==this && state==Sensor::MAYBE_ACTIVE) + if(last_entry_block && &*last_entry_block!=block) + { + for(BlockIter i=last_entry_block.next(); (i && &*i!=block); i=i.next()) + if(i->get_train()!=this || i->get_sensor_id()) + return; + } + + if(dynamic_cast(&sensor)) { - if(last_entry_block) + if(last_entry_block && pure_speed && speed_quantizer) { - float travel_distance = -1; - if(pure_speed && speed_quantizer && current_speed_step>0) - travel_distance = 0; + float travel_distance = 0; for(BlockIter i=last_entry_block; &*i!=block; i=i.next()) - { - if(i->get_sensor_id()) - return; - if(travel_distance>=0) - travel_distance += i->get_path_length(i.entry()); - } + travel_distance += i->get_path_length(i.entry()); if(travel_distance>0) { - float travel_time_secs = (Time::now()-last_entry_time)/Time::sec; + float travel_time_secs = travel_time/Time::sec; if(travel_time_secs>=2) speed_quantizer->learn(current_speed_step, travel_distance/travel_time_secs, travel_time_secs); @@ -457,7 +459,7 @@ void Train::sensor_state_changed(Sensor &sensor, Sensor::State state) } last_entry_block = allocator.iter_for(*block); - last_entry_time = Time::now(); + travel_time = Time::zero; pure_speed = true; accurate_position = true; overshoot_dist = 0; @@ -474,6 +476,25 @@ void Train::sensor_state_changed(Sensor &sensor, Sensor::State state) vehicles.front()->place(track, 0, Vehicle::FRONT_AXLE); } } + else if(BeamGate *gate = dynamic_cast(&sensor)) + { + if(!advancing && vehicles.front()->get_track()) + { + TrackIter track = allocator.iter_for(*block).track_iter(); + for(; (track && &track->get_block()==block); track=track.next()) + if(track.track()==gate->get_track()) + { + if(reverse) + track = track.reverse(); + float offset = gate->get_offset_from_endpoint(track.entry()); + if(reverse) + vehicles.back()->place(track, offset, Vehicle::BACK_BUFFER); + else + vehicles.front()->place(track, offset, Vehicle::FRONT_BUFFER); + break; + } + } + } } void Train::halt_event(bool h) @@ -489,12 +510,11 @@ float Train::get_reserved_distance_until(const Block *until_block) const Vehicle &veh = *(reverse ? vehicles.back() : vehicles.front()); - TrackIter track = veh.get_track_iter(); + TrackIter track = veh.get_track_iter().track_iter(); 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 @@ -505,6 +525,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()); @@ -576,6 +598,7 @@ void Train::Loader::vehicle(ArticleNumber art_nr) Vehicle *veh = new Vehicle(obj.layout, vtype); obj.vehicles.back()->attach_back(*veh); obj.vehicles.push_back(veh); + veh->set_train(&obj); } } // namespace R2C2