]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/timetable.cpp
Better algorithms for dealing with turnouts in a Route
[r2c2.git] / source / libr2c2 / timetable.cpp
index d5a09517ef045c830a0903cdb96efa4fa2be6a23..5be3a03ce07c893df50b05ea4b4985d9e22606b9 100644 (file)
@@ -5,6 +5,7 @@
 #include "driver.h"
 #include "layout.h"
 #include "timetable.h"
+#include "trackcircuit.h"
 #include "train.h"
 
 using namespace std;
@@ -24,7 +25,7 @@ Timetable::Timetable(Train &t):
        train.signal_advanced.connect(sigc::mem_fun(this, &Timetable::train_advanced));
        train.signal_ai_event.connect(sigc::mem_fun(this, &Timetable::event));
        Layout &layout = train.get_layout();
-       layout.signal_block_state_changed.connect(sigc::mem_fun(this, &Timetable::block_state_changed));
+       layout.signal_sensor_state_changed.connect(sigc::mem_fun(this, &Timetable::sensor_state_changed));
        layout.signal_block_reserved.connect(sigc::mem_fun(this, &Timetable::block_reserved));
 }
 
@@ -88,16 +89,16 @@ void Timetable::tick(const Time::TimeStamp &t, const Time::TimeDelta &)
                {
                case GOTO_SENSOR:
                        arrived = false;
-                       train.ai_message(Message("go-to-track", &get_sensor(row.get_param<unsigned>(0))));
+                       train.ai_message(Message("set-destination-block", &get_sensor(row.get_param<unsigned>(0))));
                        break;
                case GOTO_ZONE:
                        arrived = false;
-                       train.ai_message(Message("go-to-zone", &get_zone(row.get_param<string>(0))));
+                       train.ai_message(Message("set-destination-zone", &get_zone(row.get_param<string>(0))));
                        break;
                case TRAVEL_TO:
                        {
-                               Block *block = &get_sensor(row.get_param<unsigned>(0)).get_block();
-                               if(block->get_train()!=&train || block->get_state()<Block::MAYBE_ACTIVE)
+                               Block *block = &get_sensor(row.get_param<unsigned>(0));
+                               if(block->get_train()!=&train || block->get_sensor().get_state()<Sensor::MAYBE_ACTIVE)
                                {
                                        pending_block = block;
                                        pending_train = &train;
@@ -126,8 +127,8 @@ void Timetable::tick(const Time::TimeStamp &t, const Time::TimeDelta &)
                case WAIT_TRAIN:
                        {
                                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)
+                               Block *block = &get_sensor(row.get_param<unsigned>(1));
+                               if(block->get_train()!=other_train || block->get_sensor().get_state()<Sensor::MAYBE_ACTIVE)
                                {
                                        pending_train = other_train;
                                        pending_block = block;
@@ -162,16 +163,13 @@ void Timetable::tick(const Time::TimeStamp &t, const Time::TimeDelta &)
 
 void Timetable::save(list<DataFile::Statement> &st) const
 {
-       if(!tag.empty())
-               st.push_back((DataFile::Statement("tag"), tag));
        for(vector<Row>::const_iterator i=rows.begin(); i!=rows.end(); ++i)
                st.push_back(i->save());
 }
 
-Track &Timetable::get_sensor(unsigned id)
+Block &Timetable::get_sensor(unsigned id)
 {
-       Block &block = train.get_layout().get_block(id|0x1000);
-       return **block.get_tracks().begin();
+       return train.get_layout().get_block(id|0x1000);
 }
 
 Track &Timetable::get_turnout(unsigned id)
@@ -189,12 +187,18 @@ Zone &Timetable::get_zone(const string &name)
        return train.get_layout().get_zone(name.substr(0, space), number);
 }
 
-void Timetable::block_state_changed(Block &block, Block::State state)
+void Timetable::sensor_state_changed(Sensor &sensor, Sensor::State state)
 {
        if(rows.empty() || !enabled)
                return;
 
-       if(&block==pending_block && block.get_train()==pending_train && state>=Block::MAYBE_ACTIVE)
+       Block *block = 0;
+       if(TrackCircuit *tc = dynamic_cast<TrackCircuit *>(&sensor))
+               block = &tc->get_block();
+       else
+               return;
+
+       if(block==pending_block && block->get_train()==pending_train && state>=Sensor::MAYBE_ACTIVE)
        {
                pending_block = 0;
                current_row = (current_row+1)%rows.size();
@@ -415,7 +419,6 @@ Timetable::Loader::Loader(Timetable &tt):
        add("route",       &Loader::route);
        add("speed",       &Loader::speed);
        add("reverse",     &Loader::reverse);
-       add("tag",         &Loader::tag);
        add("travel_to",   &Loader::travel_to);
        add("travel_past", &Loader::travel_past);
        add("wait",        &Loader::wait);
@@ -463,11 +466,6 @@ void Timetable::Loader::speed(unsigned s)
        obj.rows.push_back(Row(SPEED, s));
 }
 
-void Timetable::Loader::tag(const string &t)
-{
-       obj.tag = t;
-}
-
 void Timetable::Loader::travel_to(unsigned s)
 {
        obj.rows.push_back(Row(TRAVEL_TO, s));