]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/train.cpp
Add vehicles
[r2c2.git] / source / libmarklin / train.cpp
index f745313233d57ce7e2b513ee2628c815c69dade2..a87717480bf7806d77b0e014bb2baf2b54e94d0b 100644 (file)
@@ -15,6 +15,7 @@ Distributed under the GPL
 #include "route.h"
 #include "tracktype.h"
 #include "train.h"
+#include "vehicle.h"
 
 using namespace std;
 using namespace Msp;
@@ -37,9 +38,10 @@ Train::Train(Layout &l, const LocoType &t, unsigned a):
        travel_dist(0),
        travel_speed(0),
        pure_speed(false),
-       real_speed(15),
-       cur_track(0)
+       real_speed(15)
 {
+       vehicles.push_back(new Vehicle(layout, loco_type));
+
        layout.add_train(*this);
 
        layout.get_driver().add_loco(address);
@@ -53,6 +55,8 @@ Train::Train(Layout &l, const LocoType &t, unsigned a):
 
 Train::~Train()
 {
+       for(vector<Vehicle *>::iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
+               delete *i;
        layout.remove_train(*this);
 }
 
@@ -63,6 +67,20 @@ void Train::set_name(const string &n)
        signal_name_changed.emit(name);
 }
 
+Vehicle &Train::get_vehicle(unsigned i)
+{
+       if(i>=vehicles.size())
+               throw InvalidParameterValue("Vehicle index out of range");
+       return *vehicles[i];
+}
+
+const Vehicle &Train::get_vehicle(unsigned i) const
+{
+       if(i>=vehicles.size())
+               throw InvalidParameterValue("Vehicle index out of range");
+       return *vehicles[i];
+}
+
 void Train::set_speed(unsigned speed)
 {
        if(speed==target_speed)
@@ -102,12 +120,7 @@ void Train::set_reverse(bool rev)
        release_blocks(rsv_blocks);
        reverse_blocks(cur_blocks);
 
-       if(cur_track)
-       {
-               unsigned path = cur_track->get_active_path();
-               cur_track_ep = cur_track->traverse(cur_track_ep, path);
-               offset = cur_track->get_type().get_path_length(path)-offset;
-       }
+       // XXX Do something about the vehicles
 }
 
 void Train::set_function(unsigned func, bool state)
@@ -245,34 +258,16 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
                stop_timeout = Time::TimeStamp();
        }
 
-       if(cur_track)
+       if(current_speed)
        {
-               unsigned path = cur_track->get_active_path();
-
-               offset += get_real_speed(current_speed)*(dt/Time::sec);
-               float path_len = cur_track->get_type().get_path_length(path);
-               if(offset>path_len)
-               {
-                       unsigned out = cur_track->traverse(cur_track_ep, path);
-                       Track *next = cur_track->get_link(out);
-
-                       bool ok = false;
-                       for(list<BlockRef>::const_iterator i=cur_blocks.begin(); (!ok && i!=cur_blocks.end()); ++i)
-                               ok = i->block->get_tracks().count(next);
+               Track *track = vehicles[0]->get_track();
 
-                       if(ok)
-                       {
-                               if(next)
-                                       cur_track_ep = next->get_endpoint_by_link(*cur_track);
-                               cur_track = next;
-                               offset = 0;
-                       }
-                       else
-                               offset = path_len-0.001;
-               }
+               bool ok = false;
+               for(list<BlockRef>::const_iterator i=cur_blocks.begin(); (!ok && i!=cur_blocks.end()); ++i)
+                       ok = i->block->get_tracks().count(track);
 
-               if(cur_track)
-                       pos = cur_track->get_point(cur_track_ep, path, offset).pos;
+               if(ok)
+                       vehicles[0]->advance(get_real_speed(current_speed)*(dt/Time::sec));
        }
 }
 
@@ -669,10 +664,7 @@ void Train::set_status(const string &s)
 
 void Train::set_position(const Block::Endpoint &bep)
 {
-       cur_track = bep.track;
-       cur_track_ep = bep.track_ep;
-       offset = 0;
-       pos = cur_track->get_endpoint_position(cur_track_ep);
+       vehicles[0]->place(bep.track, bep.track_ep, 0, Vehicle::FRONT_AXLE);
 }
 
 void Train::release_blocks(list<BlockRef> &blocks)