X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrain.cpp;h=93ebebec3c16ef66502da7045fe6df24cf429a4d;hb=a72309e64614bb1e230f9842d94dc261be1301e6;hp=38765cee7fde73517df49f546c5205bdd95caefa;hpb=d3a7a9e9ad694d52ccca8b6038501772fdc1dfd5;p=r2c2.git diff --git a/source/libr2c2/train.cpp b/source/libr2c2/train.cpp index 38765ce..93ebebe 100644 --- a/source/libr2c2/train.cpp +++ b/source/libr2c2/train.cpp @@ -63,7 +63,6 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a, const string &p): reverse(false), functions(0), end_of_route(false), - status("Unplaced"), travel_dist(0), pure_speed(false), speed_quantizer(0), @@ -86,7 +85,7 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a, const string &p): layout.get_driver().signal_loco_function.connect(sigc::mem_fun(this, &Train::loco_func_event)); layout.signal_block_reserved.connect(sigc::mem_fun(this, &Train::block_reserved)); - layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Train::sensor_event)); + layout.signal_block_state_changed.connect(sigc::mem_fun(this, &Train::block_state_changed)); layout.get_driver().signal_halt.connect(sigc::mem_fun(this, &Train::halt_event)); @@ -180,10 +179,7 @@ void Train::set_active(bool a) reserve_more(); } else - { stop_timeout = Time::now()+2*Time::sec; - set_status("Stopped"); - } } void Train::set_function(unsigned func, bool state) @@ -203,6 +199,14 @@ float Train::get_speed() const return controller->get_speed(); } +float Train::get_quantized_speed() const +{ + if(speed_quantizer) + return speed_quantizer->quantize_speed(controller->get_speed()); + else + return controller->get_speed(); +} + bool Train::get_function(unsigned func) const { return (functions>>func)&1; @@ -221,9 +225,9 @@ void Train::remove_ai(TrainAI &ai) ais.erase(i); } -TrainAI *Train::get_tagged_ai(const string &tag) +TrainAI *Train::get_tagged_ai(const string &tag) const { - for(list::iterator i=ais.begin(); i!=ais.end(); ++i) + for(list::const_iterator i=ais.begin(); i!=ais.end(); ++i) if((*i)->get_tag()==tag) return *i; @@ -433,10 +437,7 @@ void Train::place(Block &block, unsigned entry) accurate_position = false; if(!block.reserve(this)) - { - set_status("Unplaced"); return; - } blocks.push_back(BlockIter(&block, entry)); if(reverse) @@ -463,8 +464,6 @@ void Train::unplace() for(vector::iterator i=vehicles.begin(); i!=vehicles.end(); ++i) (*i)->unplace(); - - set_status("Unplaced"); } bool Train::free_block(Block &block) @@ -584,6 +583,7 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) (*i)->tick(t, dt); controller->tick(dt); float speed = controller->get_speed(); + bool moving = speed>0; if(controller->get_reverse()!=reverse) { @@ -605,17 +605,12 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) driver.set_loco_speed(address, speed_step); pure_speed = false; - - if(speed_step) - set_status(format("Traveling %d kmh", get_travel_speed())); - else - set_status("Waiting"); } speed = speed_quantizer->get_speed(current_speed_step); } - if(speed) + if(moving) { if(!active) set_active(true); @@ -740,9 +735,9 @@ void Train::loco_func_event(unsigned addr, unsigned func, bool state) } } -void Train::sensor_event(unsigned addr, bool state) +void Train::block_state_changed(Block &block, Block::State state) { - if(state) + if(state==Block::MAYBE_ACTIVE) { // Find the first sensor block from our reserved blocks that isn't this sensor BlockList::iterator end; @@ -750,7 +745,7 @@ void Train::sensor_event(unsigned addr, bool state) for(end=cur_blocks_end; end!=blocks.end(); ++end) if((*end)->get_sensor_id()) { - if((*end)->get_sensor_id()!=addr) + if(&**end!=&block) { if(result==0) result = 2; @@ -768,19 +763,15 @@ void Train::sensor_event(unsigned addr, bool state) // Compute speed and update related state float travel_time_secs = (Time::now()-last_entry_time)/Time::sec; - if(pure_speed) - { - if(speed_quantizer && current_speed_step>0) - speed_quantizer->learn(current_speed_step, travel_dist/travel_time_secs, travel_time_secs); - set_status(format("Traveling %d kmh", get_travel_speed())); - } + if(pure_speed && speed_quantizer && current_speed_step>0) + speed_quantizer->learn(current_speed_step, travel_dist/travel_time_secs, travel_time_secs); travel_dist = 0; for(BlockList::iterator j=cur_blocks_end; j!=end; ++j) { travel_dist += (*j)->get_path_length(j->entry()); - if((*j)->get_sensor_id()==addr && !advancing) + if(&**j==&block && !advancing) { TrackIter track = j->track_iter(); if(reverse) @@ -823,7 +814,7 @@ void Train::sensor_event(unsigned addr, bool state) else if(result==3) layout.emergency("Sensor for "+name+" triggered out of order"); } - else + else if(state==Block::INACTIVE) { const Vehicle &veh = *(reverse ? vehicles.front() : vehicles.back()); @@ -1173,23 +1164,6 @@ float Train::get_reserved_distance_until(const Block *until_block, bool back) co return result; } -float Train::get_travel_speed() const -{ - float speed = 0; - if(speed_quantizer) - speed = speed_quantizer->get_speed(current_speed_step); - else - speed = controller->get_speed(); - float scale = layout.get_catalogue().get_scale(); - return static_cast(round(speed/scale*3.6/5))*5; -} - -void Train::set_status(const string &s) -{ - status = s; - signal_status_changed.emit(s); -} - void Train::release_blocks() { release_blocks(blocks.begin(), blocks.end()); @@ -1336,8 +1310,6 @@ void Train::Loader::finish() TrackIter track = obj.blocks.front().track_iter(); float offset = 2*obj.layout.get_catalogue().get_scale(); obj.vehicles.back()->place(*track, track.entry(), offset, Vehicle::BACK_BUFFER); - - obj.set_status("Stopped"); } }