]> git.tdb.fi Git - r2c2.git/commitdiff
Fix some stuff in Timetable
authorMikko Rasa <tdb@tdb.fi>
Fri, 15 Apr 2011 20:14:31 +0000 (20:14 +0000)
committerMikko Rasa <tdb@tdb.fi>
Fri, 15 Apr 2011 20:14:31 +0000 (20:14 +0000)
source/libr2c2/timetable.cpp
source/libr2c2/timetable.h

index c2a86a6e23db26d4da3dda44de08d79e715b7a2c..dcdbb00bbc66c5f78dd21fcd4cb426131d6b3171 100644 (file)
@@ -31,7 +31,7 @@ Timetable::Timetable(Train &t):
        train.signal_advanced.connect(sigc::mem_fun(this, &Timetable::train_advanced));
        train.signal_arrived.connect(sigc::mem_fun(this, &Timetable::train_arrived));
        Layout &layout = train.get_layout();
-       layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Timetable::sensor_event));
+       layout.signal_block_state_changed.connect(sigc::mem_fun(this, &Timetable::block_state_changed));
        layout.signal_block_reserved.connect(sigc::mem_fun(this, &Timetable::block_reserved));
 }
 
@@ -104,9 +104,15 @@ void Timetable::tick(const Time::TimeStamp &t, const Time::TimeDelta &)
                                set_enabled(false);
                        break;
                case TRAVEL_TO:
-                       pending_block = &get_sensor(row.get_param<unsigned>(0)).get_block();
-                       pending_train = &train;
-                       executing = false;
+                       {
+                               Block *block = &get_sensor(row.get_param<unsigned>(0)).get_block();
+                               if(block->get_train()!=&train || block->get_state()<Block::MAYBE_ACTIVE)
+                               {
+                                       pending_block = block;
+                                       pending_train = &train;
+                                       executing = false;
+                               }
+                       }
                        break;
                case TRAVEL_PAST:
                        pending_block = &get_turnout(row.get_param<unsigned>(0)).get_block();
@@ -127,9 +133,16 @@ void Timetable::tick(const Time::TimeStamp &t, const Time::TimeDelta &)
                        }
                        break;
                case WAIT_TRAIN:
-                       pending_train = &train.get_layout().get_train(row.get_param<unsigned>(0));
-                       pending_block = &get_sensor(row.get_param<unsigned>(1)).get_block();
-                       executing = false;
+                       {
+                               Train *other_train = &train.get_layout().get_train(row.get_param<unsigned>(0));
+                               Block *block = &get_sensor(row.get_param<unsigned>(1)).get_block();
+                               if(block->get_train()!=other_train || block->get_state()<Block::MAYBE_ACTIVE)
+                               {
+                                       pending_train = other_train;
+                                       pending_block = block;
+                                       executing = false;
+                               }
+                       }
                        break;
                case ARRIVE:
                        if(!arrived)
@@ -186,9 +199,12 @@ Zone &Timetable::get_zone(const string &name)
        return train.get_layout().get_zone(name.substr(0, space), number);
 }
 
-void Timetable::sensor_event(unsigned addr, bool state)
+void Timetable::block_state_changed(Block &block, Block::State state)
 {
-       if(pending_block && pending_block->get_train()==pending_train && addr==pending_block->get_sensor_id() && state)
+       if(rows.empty() || !enabled)
+               return;
+
+       if(&block==pending_block && block.get_train()==pending_train && state>=Block::MAYBE_ACTIVE)
        {
                pending_block = 0;
                current_row = (current_row+1)%rows.size();
@@ -198,6 +214,9 @@ void Timetable::sensor_event(unsigned addr, bool state)
 
 void Timetable::block_reserved(Block &block, Train *trn)
 {
+       if(rows.empty() || !enabled)
+               return;
+
        if(&block==pending_block && trn==pending_train)
        {
                Row &row = rows[current_row];
@@ -212,6 +231,9 @@ void Timetable::block_reserved(Block &block, Train *trn)
 
 void Timetable::train_advanced(Block &block)
 {
+       if(rows.empty() || !enabled)
+               return;
+
        Row &row = rows[current_row];
        if(row.type==TRAVEL_PAST && &block==pending_block && pending_train)
                pending_train = 0;
@@ -219,6 +241,9 @@ void Timetable::train_advanced(Block &block)
 
 void Timetable::train_arrived()
 {
+       if(rows.empty() || !enabled)
+               return;
+
        Row &row = rows[current_row];
        if(row.type==ARRIVE)
        {
@@ -266,7 +291,7 @@ string Timetable::Row::str() const
        case WAIT_UNTIL:
                return format("wait until %d mod %d seconds", get_param<unsigned>(0), get_param<unsigned>(1));
        case WAIT_TRAIN:
-               return format("wait for train %d at %s", get_param<unsigned>(0), get_param<string>(1));
+               return format("wait for train %d at sensor %d", get_param<unsigned>(0), get_param<unsigned>(1));
        case ARRIVE:
                return "travel until arrival";
        case SPEED:
index 1bf8f4fe6c0b000db6823edb1f699dc369010682..b2f4fae1e2dd3ddf2cd70385dd8752b55d09817b 100644 (file)
@@ -12,11 +12,11 @@ Distributed under the GPL
 #include <vector>
 #include <sigc++/trackable.h>
 #include <msp/datafile/objectloader.h>
+#include "block.h"
 #include "trainai.h"
 
 namespace R2C2 {
 
-class Block;
 class Track;
 class Train;
 class Zone;
@@ -108,7 +108,7 @@ private:
        Track &get_sensor(unsigned);
        Track &get_turnout(unsigned);
        Zone &get_zone(const std::string &);
-       void sensor_event(unsigned, bool);
+       void block_state_changed(Block &, Block::State);
        void block_reserved(Block &, Train *);
        void train_advanced(Block &);
        void train_arrived();