]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/train.cpp
Emit signals from loco status command only when all data has been gathered
[r2c2.git] / source / libr2c2 / train.cpp
index dd7139f864824c7aac2e80b1eea4f8c40691c6ab..0eaf1e9a0ec91fb62e6947ff15ae2a38851d378e 100644 (file)
@@ -21,6 +21,7 @@ Distributed under the GPL
 #include "train.h"
 #include "vehicle.h"
 #include "vehicletype.h"
+#include "zone.h"
 
 using namespace std;
 using namespace Msp;
@@ -47,6 +48,7 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a, const string &p):
        protocol(p),
        priority(0),
        yielding_to(0),
+       preceding_train(0),
        cur_blocks_end(blocks.end()),
        clear_blocks_end(blocks.end()),
        pending_block(0),
@@ -74,7 +76,7 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a, const string &p):
 
        layout.add_train(*this);
 
-       layout.get_driver().add_loco(address, protocol);
+       layout.get_driver().add_loco(address, protocol, loco_type);
        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));
 
@@ -184,10 +186,7 @@ void Train::set_function(unsigned func, bool state)
 {
        if(!loco_type.get_functions().count(func))
                throw InvalidParameterValue("Invalid function");
-       if(func<5)
-               layout.get_driver().set_loco_function(address, func, state);
-       else
-               layout.get_driver().set_loco_function(address+1, func-4, state);
+       layout.get_driver().set_loco_function(address, func, state);
 }
 
 float Train::get_control(const string &ctrl) const
@@ -266,6 +265,35 @@ bool Train::go_to(Track &to)
        return set_route(route);
 }
 
+bool Train::go_to(const Zone &to)
+{
+       set<Track *> tracks;
+       for(BlockList::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
+               tracks.insert((*i)->get_tracks().begin(), (*i)->get_tracks().end());
+
+       const Zone::TrackSet &ztracks = to.get_tracks();
+       unsigned union_size = 0;
+       for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i)
+               union_size += tracks.count(*i);
+
+       if(union_size==tracks.size() || union_size==ztracks.size())
+       {
+               signal_arrived.emit();
+               return set_route(0);
+       }
+
+       free_noncritical_blocks();
+
+       TrackIter next = blocks.back().next().track_iter();
+
+       Route *route = Route::find(next, to);
+       if(!route)
+               return false;
+       create_lead_route(route, route);
+       route->add_tracks(ztracks);
+       return set_route(route);
+}
+
 bool Train::divert(Track &from)
 {
        if(!from.get_turnout_id())
@@ -647,11 +675,13 @@ void Train::control_changed(const Controller::Control &ctrl)
        signal_control_changed.emit(ctrl.name, ctrl.value);
 }
 
-void Train::loco_speed_event(unsigned addr, unsigned speed, bool)
+void Train::loco_speed_event(unsigned addr, unsigned speed, bool rev)
 {
        if(addr==address)
        {
                current_speed_step = speed;
+               if(rev!=reverse)
+                       layout.get_driver().set_loco_reverse(address, reverse);
                speed_changing = false;
                pure_speed = false;
        }
@@ -659,10 +689,8 @@ void Train::loco_speed_event(unsigned addr, unsigned speed, bool)
 
 void Train::loco_func_event(unsigned addr, unsigned func, bool state)
 {
-       if(addr==address || (addr==address+1 && loco_type.get_max_function()>4))
+       if(addr==address)
        {
-               if(addr==address+1)
-                       func += 4;
                if(state)
                        functions |= 1<<func;
                else
@@ -811,6 +839,7 @@ void Train::reserve_more()
        BlockIter start = blocks.back();
 
        pending_block = 0;
+       preceding_train = 0;
 
        // See how many sensor blocks and how much track we already have
        unsigned nsens = 0;
@@ -922,10 +951,15 @@ void 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();
 
+                               if(static_cast<unsigned>(other_entry)==block.entry())
+                                       preceding_train = other_train;
+                       }
+
                        int other_prio = other_train->get_priority();
 
                        if(!entry_conflict && !exit_conflict && other_prio<priority)