]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/train.cpp
Reformat the block reservation loop
[r2c2.git] / source / libmarklin / train.cpp
index 9c56a5ddb5fc4f46147bad8645698c940683b22e..7af10280228fa751970c4df739f1506400dc7271 100644 (file)
@@ -11,6 +11,7 @@ Distributed under the GPL
 #include <msp/time/utils.h>
 #include "control.h"
 #include "except.h"
+#include "route.h"
 #include "tracktype.h"
 #include "trafficmanager.h"
 #include "train.h"
@@ -42,8 +43,8 @@ Train::Train(TrafficManager &tm, Locomotive &l):
        const map<unsigned, Turnout *> &turnouts = trfc_mgr.get_control().get_turnouts();
        for(map<unsigned, Turnout *>::const_iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
        {
-               i->second->signal_route_changing.connect(sigc::bind(sigc::mem_fun(this, &Train::turnout_route_changing), i->second));
-               i->second->signal_route_changed.connect(sigc::bind(sigc::mem_fun(this, &Train::turnout_route_changed), i->second));
+               i->second->signal_path_changing.connect(sigc::bind(sigc::mem_fun(this, &Train::turnout_path_changing), i->second));
+               i->second->signal_path_changed.connect(sigc::bind(sigc::mem_fun(this, &Train::turnout_path_changed), i->second));
        }
 }
 
@@ -80,6 +81,12 @@ void Train::set_reverse(bool rev)
        loco.set_reverse(rev);
 }
 
+void Train::set_route(const Route *r)
+{
+       route = r;
+       signal_route_changed.emit(route);
+}
+
 void Train::place(Block *block, unsigned entry)
 {
        for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end();)
@@ -140,14 +147,14 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
 
        if(cur_track)
        {
-               unsigned route = 0;
+               unsigned path = 0;
                if(cur_track->get_turnout_id())
-                       route = trfc_mgr.get_control().get_turnout(cur_track->get_turnout_id()).get_route();
+                       path = trfc_mgr.get_control().get_turnout(cur_track->get_turnout_id()).get_path();
 
                offset += get_real_speed(loco.get_speed())*(dt/Time::sec);
-               if(offset>cur_track->get_type().get_route_length(route))
+               if(offset>cur_track->get_type().get_path_length(path))
                {
-                       int out = cur_track->traverse(cur_track_ep, route);
+                       int out = cur_track->traverse(cur_track_ep, path);
                        if(out>=0)
                        {
                                Track *next = cur_track->get_link(out);
@@ -161,7 +168,7 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
                }
 
                if(cur_track)
-                       pos = cur_track->get_point(cur_track_ep, route, offset);
+                       pos = cur_track->get_point(cur_track_ep, path, offset);
        }
 }
 
@@ -183,6 +190,15 @@ void Train::locomotive_reverse_changed(bool)
                i->entry = i->block->traverse(i->entry);
        reserve_more();
        update_speed();
+
+       if(cur_track)
+       {
+               unsigned path = 0;
+               if(unsigned turnout = cur_track->get_turnout_id())
+                       path = trfc_mgr.get_control().get_turnout(turnout).get_path();
+               cur_track_ep = cur_track->traverse(cur_track_ep, path);
+               offset = cur_track->get_type().get_path_length(path)-offset;
+       }
 }
 
 void Train::sensor_event(bool state, Sensor *sensor)
@@ -250,7 +266,7 @@ void Train::sensor_event(bool state, Sensor *sensor)
        }
 }
 
-void Train::turnout_route_changing(unsigned, Turnout *turnout)
+void Train::turnout_path_changing(unsigned, Turnout *turnout)
 {
        unsigned tid = turnout->get_address();
        for(list<BlockRef>::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
@@ -271,7 +287,7 @@ void Train::turnout_route_changing(unsigned, Turnout *turnout)
        }
 }
 
-void Train::turnout_route_changed(unsigned, Turnout *turnout)
+void Train::turnout_path_changed(unsigned, Turnout *turnout)
 {
        unsigned tid = turnout->get_address();
        for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
@@ -307,27 +323,29 @@ unsigned Train::reserve_more()
        while(nsens<2)
        {
                int exit = last->block->traverse(last->entry);
-               if(exit>=0) 
+               if(exit<0)
+                       break;
+
+               Block *link = last->block->get_link(exit);
+               if(!link || !link->reserve(this))
+                       break;
+
+               if(route && link->get_turnout_id())
                {
-                       Block *link = last->block->get_link(exit);
-                       if(link && link->reserve(this))
-                       {
-                               rsv_blocks.push_back(BlockRef(link, link->get_endpoint_by_link(*last->block)));
-                               last = &rsv_blocks.back();
-                               if(last->block->get_sensor_id())
-                               {
-                                       ++nsens;
-                                       result = true;
-                               }
-                       }
-                       else
-                               break;
+                       int path = route->get_turnout(link->get_turnout_id());
+                       if(path>=0)
+                               trfc_mgr.get_control().get_turnout(link->get_turnout_id()).set_path(path);
+               }
+               rsv_blocks.push_back(BlockRef(link, link->get_endpoint_by_link(*last->block)));
+               last = &rsv_blocks.back();
+               if(last->block->get_sensor_id())
+               {
+                       ++nsens;
+                       result = true;
                }
-               else
-                       break;
        }
 
-       while(last && !last->block->get_sensor_id())
+       while(!rsv_blocks.empty() && !last->block->get_sensor_id())
        {
                last->block->reserve(0);
                rsv_blocks.erase(--rsv_blocks.end());
@@ -413,14 +431,23 @@ unsigned Train::find_speed(float real) const
 {
        if(real<=real_speed[0].speed)
                return 0;
-       if(real>=real_speed[14].speed)
-               return 14;
 
        unsigned low = 0;
        unsigned high = 0;
-       for(high=0; real_speed[high].speed<real; ++high)
-               if(real_speed[high].weight)
-                       low = high;
+       for(unsigned i=0; (!high && i<=14); ++i)
+               if(real_speed[i].weight)
+               {
+                       if(real_speed[i].speed<real)
+                               low = i;
+                       else
+                               high = i;
+               }
+       if(!high)
+       {
+               if(!low)
+                       return 0;
+               return min(static_cast<unsigned>(low*real/real_speed[low].speed), 14U);
+       }
 
        float f = (real-real_speed[low].speed)/(real_speed[high].speed-real_speed[low].speed);
        return static_cast<unsigned>(low*(1-f)+high*f+0.5);