]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/train.cpp
Move double-address logic to drivers
[r2c2.git] / source / libr2c2 / train.cpp
index ffa436b1d6a0c07795bf876d304077bb08a80fcd..28976838fcf7da9c699ed11b8b461cdab566fd41 100644 (file)
@@ -48,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),
@@ -75,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));
 
@@ -185,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
@@ -689,10 +687,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
@@ -841,6 +837,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;
@@ -952,10 +949,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)