set_reverse(!reverse);
}
-void AIControl::tick(const Time::TimeStamp &, const Time::TimeDelta &)
+void AIControl::tick(const Time::TimeDelta &)
{
float scale = train.get_layout().get_catalogue().get_scale();
float rsv_dist = train.get_reserved_distance();
bool get_reverse() const { return reverse; }
virtual void message(const Message &);
- virtual void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
+ virtual void tick(const Msp::Time::TimeDelta &);
private:
void event(TrainAI &, const Message &);
--- /dev/null
+#include "clock.h"
+
+using namespace Msp;
+
+namespace R2C2 {
+
+Clock::Clock():
+ rate(1)
+{ }
+
+void Clock::set_rate(float s)
+{
+ rate = s;
+}
+
+void Clock::tick(const Time::TimeDelta &dt)
+{
+ current_time += dt*rate;
+}
+
+} // namespace R2C2
--- /dev/null
+#ifndef LIBR2C2_CLOCK_H_
+#define LIBR2C2_CLOCK_H_
+
+#include <msp/time/timedelta.h>
+
+namespace R2C2 {
+
+class Clock
+{
+private:
+ Msp::Time::TimeDelta current_time;
+ float rate;
+
+public:
+ Clock();
+
+ void set_rate(float);
+ const Msp::Time::TimeDelta &get_current_time() const { return current_time; }
+ void tick(const Msp::Time::TimeDelta &);
+};
+
+} // namespace R2C2
+
+#endif
catalogue(c),
driver(d),
next_turnout_id(0x800)
-{ }
+{
+ clock.set_rate(60);
+}
Layout::~Layout()
{
dt = t-last_tick;
last_tick = t;
+ clock.tick(dt);
+
for(set<Sensor *>::iterator i=sensors.get().begin(); i!=sensors.get().end(); ++i)
(*i)->tick(dt);
const set<Signal *> &signals = objects.get<Signal>();
for(set<Signal *>::iterator i=signals.begin(); i!=signals.end(); ++i)
(*i)->tick(dt);
for(map<unsigned, Train *>::iterator i=trains.begin(); i!=trains.end(); ++i)
- i->second->tick(t, dt);
+ i->second->tick(dt);
}
void Layout::emergency(const string &msg)
#include <msp/time/timestamp.h>
#include "geometry.h"
#include "sensor.h"
+#include "clock.h"
namespace R2C2 {
private:
Catalogue &catalogue;
Driver *driver;
+ Clock clock;
std::string base;
Storage<Object> objects;
Storage<TrackChain> track_chains;
Catalogue &get_catalogue() const { return catalogue; }
bool has_driver() const { return driver; }
Driver &get_driver() const;
+ const Clock &get_clock() const { return clock; }
const std::string &get_base() const { return base; }
void add(Object &);
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)
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;
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();
+ travel_time = Time::zero;
pure_speed = true;
accurate_position = true;
overshoot_dist = 0;
#include <sigc++/signal.h>
#include <sigc++/trackable.h>
#include <msp/datafile/objectloader.h>
-#include <msp/time/timestamp.h>
+#include <msp/time/timedelta.h>
#include "blockallocator.h"
#include "controller.h"
#include "sensor.h"
unsigned current_speed_step;
bool speed_changing;
bool reverse;
- Msp::Time::TimeStamp stop_timeout;
+ Msp::Time::TimeDelta stop_timeout;
unsigned functions;
BlockIter last_entry_block;
- Msp::Time::TimeStamp last_entry_time;
+ Msp::Time::TimeDelta travel_time;
bool pure_speed;
SpeedQuantizer *speed_quantizer;
bool accurate_position;
const BlockAllocator &get_block_allocator() const { return allocator; }
float get_reserved_distance() const;
- void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
+ void tick(const Msp::Time::TimeDelta &);
void save(std::list<Msp::DataFile::Statement> &) const;
private:
virtual ~TrainAI();
virtual void message(const Message &) { }
- virtual void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &) { }
+ virtual void tick(const Msp::Time::TimeDelta &) = 0;
};
} // namespace R2C2
}
}
-void TrainRouter::tick(const Time::TimeStamp &, const Time::TimeDelta &)
+void TrainRouter::tick(const Time::TimeDelta &)
{
if(update_pending)
create_plans(train.get_layout());
bool is_destination(Track &) const;
virtual void message(const Message &);
- virtual void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
+ virtual void tick(const Msp::Time::TimeDelta &);
void save(std::list<Msp::DataFile::Statement> &) const;
check();
}
-void TrainStatus::tick(const Time::TimeStamp &, const Time::TimeDelta &)
+void TrainStatus::tick(const Time::TimeDelta &)
{
check();
}
TrainStatus(Train &);
const std::string &get_status() const { return status; }
- void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
+ void tick(const Msp::Time::TimeDelta &);
private:
void check();
};