]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/train.cpp
Make LCD output selectable at runtime through an extra I/O pin
[r2c2.git] / source / libmarklin / train.cpp
index 7030f3814f69d53e6d62faf2bc9333aef0ea2c34..859d3daea7988ea7544eb111cb72964613e7a8cd 100644 (file)
@@ -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));
 
@@ -291,7 +292,7 @@ bool Train::divert(Track &from)
                        int route_path = route->route->get_turnout(from.get_turnout_id());
 
                        // Check that more than one path is available
-                       unsigned ep_paths = track->get_type().get_endpoints()[track.entry()].paths;
+                       unsigned ep_paths = track.endpoint().paths;
                        if(!(ep_paths&(ep_paths-1)))
                                return false;
 
@@ -391,7 +392,7 @@ void Train::place(Block &block, unsigned entry)
        }
        else
        {
-               const Block::Endpoint &bep = block.get_endpoints()[entry];
+               const Block::Endpoint &bep = block.get_endpoint(entry);
                vehicles.back()->place(*bep.track, bep.track_ep, 0, Vehicle::BACK_BUFFER);
        }
 }
@@ -513,7 +514,6 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
        if(!active && stop_timeout && t>=stop_timeout)
        {
                release_blocks(cur_blocks_end, blocks.end());
-               end_of_route = false;
                stop_timeout = Time::TimeStamp();
        }
 
@@ -523,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)
        {
@@ -535,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");
@@ -560,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);
@@ -605,7 +609,7 @@ void Train::save(list<DataFile::Statement> &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; i<real_speed.size(); ++i)
                if(real_speed[i].weight)
                        st.push_back((DataFile::Statement("real_speed"), i, real_speed[i].speed, real_speed[i].weight));
 
@@ -647,7 +651,7 @@ void Train::loco_speed_event(unsigned addr, unsigned speed, bool)
 {
        if(addr==address)
        {
-               current_speed = speed;
+               current_speed_step = speed;
                speed_changing = false;
                pure_speed = false;
        }
@@ -698,9 +702,9 @@ void Train::sensor_event(unsigned addr, bool state)
 
                        if(pure_speed)
                        {
-                               if(current_speed)
+                               if(current_speed_step>0)
                                {
-                                       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()));
@@ -731,9 +735,9 @@ void Train::sensor_event(unsigned addr, bool state)
                        // Check if we've reached the next route
                        if(routes.size()>1)
                        {
-                               const set<Track *> &rtracks = (++routes.begin())->route->get_tracks();
+                               const Route &route = *(++routes.begin())->route;
                                for(BlockList::iterator j=cur_blocks_end; j!=end; ++j)
-                                       if(rtracks.count((*j)->get_endpoints()[j->entry()].track))
+                                       if(route.has_track(*j->track_iter()))
                                        {
                                                routes.pop_front();
                                                // XXX Exceptions?
@@ -783,13 +787,8 @@ void Train::sensor_event(unsigned addr, bool state)
 void Train::turnout_path_changed(Track &track)
 {
        for(list<BlockIter>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
-               if((*i)->get_turnout_id()==track.get_turnout_id())
-               {
-                       if(!reserving)
-                               check_turnout_paths(false);
-                       else if(i==clear_blocks_end)
-                               ++clear_blocks_end;
-               }
+               if((*i)->get_turnout_id()==track.get_turnout_id() && !reserving)
+                       check_turnout_paths(false);
 }
 
 void Train::halt_event(bool h)
@@ -804,10 +803,10 @@ void Train::block_reserved(const Block &block, const Train *train)
                reserve_more();
 }
 
-unsigned Train::reserve_more()
+void Train::reserve_more()
 {
-       if(!active || blocks.empty())
-               return 0;
+       if(!active || blocks.empty() || end_of_route)
+               return;
 
        BlockIter start = blocks.back();
 
@@ -823,9 +822,6 @@ unsigned Train::reserve_more()
                if(nsens>0)
                        dist += (*i)->get_path_length(i->entry());
        }
-       
-       if(end_of_route)
-               return nsens;
 
        list<RouteRef>::iterator cur_route = routes.begin();
        advance_route(cur_route, *start.track_iter());
@@ -837,19 +833,21 @@ unsigned Train::reserve_more()
        list<BlockIter>::iterator good_end = blocks.end();
        Track *divert_track = 0;
        bool try_divert = false;
-       unsigned good_sens = nsens;
-       float good_dist = dist;
        Train *blocking_train = 0;
        BlockList contested_blocks;
 
        SetFlag setf(reserving);
 
-       while(good_sens<3 || good_dist<min_dist || !contested_blocks.empty())
+       while(1)
        {
                BlockIter last = block;
                block = block.next(cur_route!=routes.end() ? cur_route->route : 0);
-               if(!block)
+               if(!block || block->get_endpoints().size()<2)
+               {
+                       if(!blocking_train)
+                               good_end = blocks.end();
                        break;
+               }
 
                TrackIter track = block.track_iter();
 
@@ -861,25 +859,20 @@ unsigned Train::reserve_more()
                                if(!blocking_train)
                                {
                                        good_end = blocks.end();
-                                       good_sens = nsens;
-                                       good_dist = dist;
                                        end_of_route = true;
                                }
                                break;
                        }
                }
-               else if(!routes.empty() && routes.front().route->has_track(*track))
-                       cur_route = routes.begin();
 
-               if(block->get_endpoints().size()<2)
+               if(block->get_turnout_id() && !last->get_turnout_id())
                {
-                       if(!blocking_train)
-                       {
-                               good_end = blocks.end();
-                               good_sens = nsens;
-                               good_dist = dist;
-                       }
-                       break;
+                       /* We can keep the blocks if we arrive at a turnout from a non-turnout
+                       block.  Having a turnout block as our last reserved block is not good
+                       as it would limit our diversion possibilities for little benefit. */
+                       good_end = blocks.end();
+                       if(nsens>=3 && dist>=min_dist)
+                               break;
                }
 
                if(blocking_train)
@@ -929,13 +922,9 @@ unsigned Train::reserve_more()
                        bool entry_conflict = (block.entry()==other_exit);
                        bool exit_conflict = (exit==static_cast<unsigned>(other_entry));
                        if(!entry_conflict && !last->get_turnout_id())
-                       {
                                /* The other train is not coming to the blocks we're holding, so we
                                can keep them. */
                                good_end = blocks.end();
-                               good_sens = nsens;
-                               good_dist = dist;
-                       }
 
                        int other_prio = other_train->get_priority();
 
@@ -968,20 +957,9 @@ unsigned Train::reserve_more()
 
                if(block->get_turnout_id())
                {
-                       const TrackType::Endpoint &track_ep = track->get_type().get_endpoints()[track.entry()];
+                       const TrackType::Endpoint &track_ep = track.endpoint();
                        bool multiple_paths = (track_ep.paths&(track_ep.paths-1));
 
-                       if(multiple_paths || !last->get_turnout_id())
-                       {
-                               /* We can keep the blocks reserved so far if we are facing the
-                               points or if there was no turnout immediately before this one.
-                               With multiple successive turnouts (as is common in crossovers) it's
-                               best to hold at one we can divert from. */
-                               good_end = blocks.end();
-                               good_sens = nsens;
-                               good_dist = dist;
-                       }
-
                        if(multiple_paths && cur_route!=routes.end() && cur_route->diversion!=block->get_turnout_id())
                                /* There's multiple paths to be taken and we are on a route - take
                                note of the diversion possibility */
@@ -1016,13 +994,11 @@ unsigned Train::reserve_more()
        check_turnout_paths(true);
 
        // Make any sensorless blocks at the beginning immediately current
-       while(cur_blocks_end!=blocks.end() && !(*cur_blocks_end)->get_sensor_id())
+       while(cur_blocks_end!=clear_blocks_end && !(*cur_blocks_end)->get_sensor_id())
                ++cur_blocks_end;
 
        if(try_divert && divert(*divert_track))
-               return reserve_more();
-
-       return good_sens;
+               reserve_more();
 }
 
 void Train::check_turnout_paths(bool set)
@@ -1030,41 +1006,33 @@ void Train::check_turnout_paths(bool set)
        if(clear_blocks_end==blocks.end())
                return;
 
-       list<RouteRef>::iterator route = routes.begin();
-
        for(list<BlockIter>::iterator i=clear_blocks_end; i!=blocks.end(); ++i)
        {
-               advance_route(route, *i->track_iter());
-
                if((*i)->get_turnout_id())
                {
                        TrackIter track = i->track_iter();
-                       const TrackType::Endpoint &track_ep = track->get_type().get_endpoints()[track.entry()];
-
-                       int path = -1;
-                       for(list<RouteRef>::iterator j=route; (path<0 && j!=routes.end()); ++j)
-                               path = j->route->get_turnout((*i)->get_turnout_id());
-                       if(path<0)
-                               path = track->get_active_path();
-                       if(!(track_ep.paths&(1<<path)))
+                       const TrackType::Endpoint &track_ep = track.endpoint();
+
+                       unsigned path = 0;
+                       list<BlockIter>::iterator j = i;
+                       if(++j!=blocks.end())
                        {
-                               list<BlockIter>::iterator j = i;
-                               if(++j!=blocks.end())
-                               {
-                                       TrackIter rev = j->track_iter().flip();
-                                       unsigned mask = rev->get_type().get_endpoints()[rev.entry()].paths&track_ep.paths;
-                                       for(path=0; mask>1; mask>>=1, ++path) ;
-                               }
-                               else
-                                       return;
+                               TrackIter rev = j->track_iter().flip();
+                               unsigned mask = rev.endpoint().paths&track_ep.paths;
+                               for(path=0; mask>1; mask>>=1, ++path) ;
                        }
+                       else
+                               return;
 
-                       if(path!=static_cast<int>(track->get_active_path()))
+                       if(path!=track->get_active_path())
                        {
                                if(set)
                                        track->set_active_path(path);
 
-                               continue;
+                               /* Check again, in case the driver was able to service the request
+                               instantly */
+                               if(!set || path!=track->get_active_path())
+                                       continue;
                        }
                }
 
@@ -1131,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;
 
@@ -1139,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+1<real_speed.size(); ++high)
                if(real_speed[high].weight)
                        break;
 
@@ -1159,15 +1129,17 @@ float Train::get_real_speed(unsigned i) const
                return 0;
 }
 
-unsigned Train::find_speed(float real) const
+unsigned Train::find_speed_step(float real) const
 {
-       if(real<=real_speed[0].speed)
+       if(real_speed.size()<=1)
+               return 0;
+       if(real<=real_speed[1].speed*0.5)
                return 0;
 
        unsigned low = 0;
        unsigned high = 0;
        unsigned last = 0;
-       for(unsigned i=0; (!high && i<=14); ++i)
+       for(unsigned i=0; (!high && i<real_speed.size()); ++i)
                if(real_speed[i].weight)
                {
                        last = i;
@@ -1178,14 +1150,15 @@ unsigned Train::find_speed(float real) const
                }
        if(!high)
        {
+               unsigned limit = real_speed.size()/5;
                if(!low)
                {
                        if(real)
-                               return 3;
+                               return limit;
                        else
                                return 0;
                }
-               return min(min(static_cast<unsigned>(low*real/real_speed[low].speed), 14U), last+3);
+               return min(min(static_cast<unsigned>(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);
@@ -1194,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<int>(round(speed/scale*3.6/5))*5;
 }
@@ -1222,6 +1195,9 @@ void Train::release_blocks(BlockList::iterator begin, BlockList::iterator end)
                Block &block = **begin;
                blocks.erase(begin++);
                block.reserve(0);
+
+               if(begin==blocks.end())
+                       end_of_route = false;
        }
 }
 
@@ -1413,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;
 }