X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Ftrain.cpp;h=0084f38fab8d4764c79c15873134b9f4a31d3049;hb=975ea87cc7be179618b06291cb2506a2523cad1f;hp=d9bade7dca338c5f20a53ca2624f53f2d4420298;hpb=9def58c4bbec8788430740a1e9afce64a496231f;p=r2c2.git diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index d9bade7..0084f38 100644 --- a/source/libmarklin/train.cpp +++ b/source/libmarklin/train.cpp @@ -40,10 +40,11 @@ struct SetFlag namespace Marklin { -Train::Train(Layout &l, const VehicleType &t, unsigned a): +Train::Train(Layout &l, const VehicleType &t, unsigned a, const string &p): layout(l), loco_type(t), address(a), + protocol(p), priority(0), yielding_to(0), cur_blocks_end(blocks.end()), @@ -54,7 +55,7 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a): controller(new AIControl(*this, new SimpleController)), timetable(0), active(false), - current_speed(0), + current_speed_step(0), speed_changing(false), reverse(false), functions(0), @@ -62,7 +63,7 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a): status("Unplaced"), travel_dist(0), pure_speed(false), - real_speed(15), + real_speed(layout.get_driver().get_protocol_speed_steps(protocol)+1), accurate_position(false), overshoot_dist(false) { @@ -73,7 +74,7 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a): layout.add_train(*this); - layout.get_driver().add_loco(address); + layout.get_driver().add_loco(address, protocol); layout.get_driver().signal_loco_speed.connect(sigc::mem_fun(this, &Train::loco_speed_event)); layout.get_driver().signal_loco_function.connect(sigc::mem_fun(this, &Train::loco_func_event)); @@ -522,7 +523,7 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) timetable->tick(t); controller->tick(dt); float speed = controller->get_speed(); - unsigned speed_notch = find_speed(speed); + unsigned speed_step = find_speed_step(speed); if(controller->get_reverse()!=reverse) { @@ -534,14 +535,14 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) reserve_more(); } - if(speed_notch!=current_speed && !speed_changing && !driver.is_halted() && driver.get_power()) + if(speed_step!=current_speed_step && !speed_changing && !driver.is_halted() && driver.get_power()) { speed_changing = true; - driver.set_loco_speed(address, speed_notch); + driver.set_loco_speed(address, speed_step); pure_speed = false; - if(speed_notch) + if(speed_step) set_status(format("Traveling %d kmh", get_travel_speed())); else set_status("Waiting"); @@ -559,7 +560,11 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) for(BlockList::const_iterator i=blocks.begin(); (!ok && i!=cur_blocks_end); ++i) ok = (*i)->has_track(*track); - float d = get_real_speed(current_speed)*(dt/Time::sec); + float d; + if(real_speed.size()>1) + d = get_real_speed(current_speed_step)*(dt/Time::sec); + else + d = speed*(dt/Time::sec); if(ok) { SetFlag setf(advancing); @@ -604,7 +609,7 @@ void Train::save(list &st) const if(i!=vehicles.begin()) st.push_back((DataFile::Statement("vehicle"), (*i)->get_type().get_article_number())); - for(unsigned i=0; i<=14; ++i) + for(unsigned i=0; i0) { - RealSpeed &rs = real_speed[current_speed]; + RealSpeed &rs = real_speed[current_speed_step]; rs.add(travel_dist/travel_time_secs, travel_time_secs); } set_status(format("Traveling %d kmh", get_travel_speed())); @@ -1094,6 +1099,8 @@ float Train::get_reserved_distance_until(const Block *until_block, bool back) co float Train::get_real_speed(unsigned i) const { + if(i==0) + return 0; if(real_speed[i].weight) return real_speed[i].speed; @@ -1102,7 +1109,7 @@ float Train::get_real_speed(unsigned i) const for(low=i; low>0; --low) if(real_speed[low].weight) break; - for(high=i; high<14; ++high) + for(high=i; high(low*real/real_speed[low].speed), 14U), last+3); + return min(min(static_cast(low*real/real_speed[low].speed), real_speed.size()-1), last+limit); } float f = (real-real_speed[low].speed)/(real_speed[high].speed-real_speed[low].speed); @@ -1157,7 +1167,7 @@ unsigned Train::find_speed(float real) const float Train::get_travel_speed() const { - float speed = get_real_speed(current_speed); + float speed = get_real_speed(current_speed_step); float scale = layout.get_catalogue().get_scale(); return static_cast(round(speed/scale*3.6/5))*5; } @@ -1379,6 +1389,8 @@ void Train::Loader::name(const string &n) void Train::Loader::real_speed(unsigned i, float speed, float weight) { + if(i>=obj.real_speed.size()) + return; obj.real_speed[i].speed = speed; obj.real_speed[i].weight = weight; }