]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/train.cpp
Make the simulation independent of wall clock time
[r2c2.git] / source / libr2c2 / train.cpp
index 91387d85297e7f2a6b5bed13a1b620cf01f687b9..8cfa2d0ee4817158b11086ea318bdeef2c50db83 100644 (file)
@@ -6,13 +6,14 @@
 #include <msp/time/units.h>
 #include <msp/time/utils.h>
 #include "aicontrol.h"
+#include "block.h"
 #include "catalogue.h"
 #include "driver.h"
 #include "layout.h"
 #include "route.h"
 #include "simplecontroller.h"
 #include "speedquantizer.h"
-#include "timetable.h"
+#include "trackcircuit.h"
 #include "trackiter.h"
 #include "tracktype.h"
 #include "train.h"
@@ -60,7 +61,7 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a, const string &p):
        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));
 
-       layout.signal_block_state_changed.connect(sigc::mem_fun(this, &Train::block_state_changed));
+       layout.signal_sensor_state_changed.connect(sigc::mem_fun(this, &Train::sensor_state_changed));
 
        layout.get_driver().signal_halt.connect(sigc::mem_fun(this, &Train::halt_event));
 
@@ -135,11 +136,11 @@ void Train::set_active(bool a)
        active = a;
        if(active)
        {
-               stop_timeout = Time::TimeStamp();
+               stop_timeout = Time::zero;
                allocator.reserve_more();
        }
        else
-               stop_timeout = Time::now()+2*Time::sec;
+               stop_timeout = 2*Time::sec;
 }
 
 void Train::set_function(unsigned func, bool state)
@@ -288,18 +289,24 @@ void Train::reserve_more()
        allocator.reserve_more();
 }
 
-void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
+void Train::tick(const Time::TimeDelta &dt)
 {
-       if(!active && stop_timeout && t>=stop_timeout)
+       if(!active && stop_timeout)
        {
-               allocator.release_noncurrent();
-               stop_timeout = Time::TimeStamp();
+               stop_timeout -= dt;
+               if(stop_timeout<=Time::zero)
+               {
+                       allocator.release_noncurrent();
+                       stop_timeout = Time::TimeDelta();
+               }
        }
 
+       travel_time += dt;
+
        Driver &driver = layout.get_driver();
 
        for(list<TrainAI *>::iterator i=ais.begin(); i!=ais.end(); ++i)
-               (*i)->tick(t, dt);
+               (*i)->tick(dt);
        controller->tick(dt);
        float speed = controller->get_speed();
        bool moving = speed>0;
@@ -386,12 +393,6 @@ void Train::save(list<DataFile::Statement> &st) const
                        router->save(ss.sub);
                        st.push_back(ss);
                }
-               else if(Timetable *timetable = dynamic_cast<Timetable *>(*i))
-               {
-                       DataFile::Statement ss("timetable");
-                       timetable->save(ss.sub);
-                       st.push_back(ss);
-               }
        }
 }
 
@@ -428,9 +429,15 @@ void Train::loco_func_event(unsigned addr, unsigned func, bool state)
        }
 }
 
-void Train::block_state_changed(Block &block, Block::State state)
+void Train::sensor_state_changed(Sensor &sensor, Sensor::State state)
 {
-       if(block.get_train()==this && state==Block::MAYBE_ACTIVE)
+       Block *block = 0;
+       if(TrackCircuit *tc = dynamic_cast<TrackCircuit *>(&sensor))
+               block = &tc->get_block();
+       else
+               return;
+
+       if(block->get_train()==this && state==Sensor::MAYBE_ACTIVE)
        {
                if(last_entry_block)
                {
@@ -438,7 +445,7 @@ void Train::block_state_changed(Block &block, Block::State state)
                        if(pure_speed && speed_quantizer && current_speed_step>0)
                                travel_distance = 0;
 
-                       for(BlockIter i=last_entry_block; &*i!=&block; i=i.next())
+                       for(BlockIter i=last_entry_block; &*i!=block; i=i.next())
                        {
                                if(i->get_sensor_id())
                                        return;
@@ -448,15 +455,15 @@ void Train::block_state_changed(Block &block, Block::State state)
 
                        if(travel_distance>0)
                        {
-                               float travel_time_secs = (Time::now()-last_entry_time)/Time::sec;
+                               float travel_time_secs = travel_time/Time::sec;
 
                                if(travel_time_secs>=2)
                                        speed_quantizer->learn(current_speed_step, travel_distance/travel_time_secs, travel_time_secs);
                        }
                }
 
-               last_entry_block = allocator.iter_for(block);
-               last_entry_time = Time::now();
+               last_entry_block = allocator.iter_for(*block);
+               travel_time = Time::zero;
                pure_speed = true;
                accurate_position = true;
                overshoot_dist = 0;
@@ -534,7 +541,6 @@ Train::Loader::Loader(Train &t):
        add("name",        &Loader::name);
        add("quantized_speed",  &Loader::quantized_speed);
        add("router",      &Loader::router);
-       add("timetable",   &Loader::timetable);
        add("vehicle",     &Loader::vehicle);
 }
 
@@ -570,12 +576,6 @@ void Train::Loader::router()
        load_sub(*rtr);
 }
 
-void Train::Loader::timetable()
-{
-       Timetable *ttbl = new Timetable(obj);
-       load_sub(*ttbl);
-}
-
 void Train::Loader::vehicle(ArticleNumber art_nr)
 {
        const VehicleType &vtype = obj.layout.get_catalogue().get_vehicle(art_nr);