]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/train.cpp
Add reverse and arrival row types to Timetable
[r2c2.git] / source / libmarklin / train.cpp
index 93470997133a6e534736f6f6d71b256e0533bc13..c3affef1fdb48dbb0fbe18ff7d9adefda12a9b70 100644 (file)
@@ -14,7 +14,7 @@ Distributed under the GPL
 #include "driver.h"
 #include "layout.h"
 #include "route.h"
-#include "simplephysics.h"
+#include "simplecontroller.h"
 #include "timetable.h"
 #include "tracktype.h"
 #include "train.h"
@@ -32,7 +32,7 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a):
        address(a),
        priority(0),
        pending_block(0),
-       control(new AIControl(*this, new SimplePhysics)),
+       controller(new AIControl(*this, new SimpleController)),
        timetable(0),
        active(false),
        current_speed(0),
@@ -66,12 +66,12 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a):
 
        layout.get_driver().signal_halt.connect(sigc::mem_fun(this, &Train::halt_event));
 
-       control->signal_control_changed.connect(sigc::mem_fun(this, &Train::control_changed));
+       controller->signal_control_changed.connect(sigc::mem_fun(this, &Train::control_changed));
 }
 
 Train::~Train()
 {
-       delete control;
+       delete controller;
        delete timetable;
        for(vector<Vehicle *>::iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
                delete *i;
@@ -130,14 +130,14 @@ const Vehicle &Train::get_vehicle(unsigned i) const
 
 void Train::set_control(const string &n, float v)
 {
-       control->set_control(n, v);
+       controller->set_control(n, v);
 }
 
 void Train::set_active(bool a)
 {
        if(a==active)
                return;
-       if(!a && control->get_speed())
+       if(!a && controller->get_speed())
                throw InvalidState("Can't deactivate while moving");
 
        active = a;
@@ -165,12 +165,12 @@ void Train::set_function(unsigned func, bool state)
 
 float Train::get_control(const string &ctrl) const
 {
-       return control->get_control(ctrl).value;
+       return controller->get_control(ctrl).value;
 }
 
 float Train::get_speed() const
 {
-       return control->get_speed();
+       return controller->get_speed();
 }
 
 bool Train::get_function(unsigned func) const
@@ -245,7 +245,7 @@ void Train::go_to(const Track &to)
 
 void Train::place(Block &block, unsigned entry)
 {
-       if(control->get_speed())
+       if(controller->get_speed())
                throw InvalidState("Must be stopped before placing");
 
        release_blocks(rsv_blocks);
@@ -272,7 +272,7 @@ void Train::place(Block &block, unsigned entry)
        else
        {
                const Block::Endpoint &bep = block.get_endpoints()[entry];
-               vehicles.front()->place(bep.track, bep.track_ep, 0, Vehicle::BACK_BUFFER);
+               vehicles.back()->place(bep.track, bep.track_ep, 0, Vehicle::BACK_BUFFER);
        }
 }
 
@@ -366,13 +366,13 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
 
        if(timetable)
                timetable->tick(t);
-       control->tick(dt);
-       float speed = control->get_speed();
+       controller->tick(dt);
+       float speed = controller->get_speed();
        unsigned speed_notch = find_speed(speed);
 
-       if(control->get_reverse()!=reverse)
+       if(controller->get_reverse()!=reverse)
        {
-               reverse = control->get_reverse();
+               reverse = controller->get_reverse();
                driver.set_loco_reverse(address, reverse);
 
                release_blocks(rsv_blocks);
@@ -435,7 +435,10 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
                unsigned entry = bep.track_ep;
 
                bool found = false;
-               float dist = veh.get_offset()-veh.get_type().get_length()/2;
+               float dist = veh.get_offset();
+               if(reverse)
+                       dist = veh.get_track()->get_type().get_path_length(veh.get_track()->get_active_path())-dist;
+               dist -= veh.get_type().get_length()/2;
                while(1)
                {
                        if(track==veh.get_track())
@@ -514,7 +517,7 @@ void Train::save(list<DataFile::Statement> &st) const
        }
 }
 
-void Train::control_changed(const TrainControl &ctrl)
+void Train::control_changed(const Controller::Control &ctrl)
 {
        signal_control_changed.emit(ctrl.name, ctrl.value);
 }