DataFile::load(layout, options.layout_fn);
layout.signal_train_added.connect(sigc::mem_fun(this, &Engineer::train_added));
- layout.signal_block_reserved.connect(sigc::mem_fun(this, &Engineer::block_reserved));
+ layout.signal_block_reserved.connect(sigc::hide<1>(sigc::mem_fun(this, &Engineer::reset_block_color)));
+ layout.signal_block_state_changed.connect(sigc::hide<1>(sigc::mem_fun(this, &Engineer::reset_block_color)));
layout.signal_emergency.connect(sigc::mem_fun(this, &Engineer::set_status));
- layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Engineer::sensor_event));
+
if(FS::exists(options.state_fn))
DataFile::load(layout, options.state_fn);
void Engineer::reset_block_color(const Block &block)
{
- bool active = false;
- if(unsigned sid=block.get_sensor_id())
- active = layout.get_driver().get_sensor(sid);
+ bool active = block.get_state()>Block::INACTIVE;
if(block.get_train())
{
set_block_color(block, GL::Color(1));
}
-void Engineer::sensor_event(unsigned addr, bool)
-{
- const set<Block *> &blocks = layout.get_blocks();
- for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
- if((*i)->get_sensor_id()==addr)
- reset_block_color(**i);
-}
-
-void Engineer::block_reserved(const Block &block, const Train *)
-{
- reset_block_color(block);
-}
-
Track *Engineer::pick_track(int x, int y)
{
const GL::Vector3 &start = camera.get_position();
*/
#include <algorithm>
+#include <msp/time/units.h>
#include "block.h"
+#include "driver.h"
#include "layout.h"
#include "route.h"
#include "trackiter.h"
id(0),
sensor_id(start.get_sensor_id()),
turnout_id(start.get_turnout_id()),
+ state(INACTIVE),
train(0)
{
tracks.insert(&start);
find_paths(TrackIter(endpoints[i].track, endpoints[i].track_ep), path);
}
+ if(sensor_id && layout.has_driver())
+ layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Block::sensor_event));
+
layout.add_block(*this);
}
if(!t || !train)
{
train = t;
- layout.signal_block_reserved.emit(*this, train);
+ signal_reserved.emit(train);
return true;
}
else
return false;
}
+void Block::tick(const Time::TimeDelta &dt)
+{
+ if(state_confirm_timeout)
+ {
+ state_confirm_timeout -= dt;
+ if(state_confirm_timeout<=Time::zero)
+ {
+ if(state==MAYBE_INACTIVE)
+ state = INACTIVE;
+ else if(state==MAYBE_ACTIVE)
+ state = ACTIVE;
+ state_confirm_timeout = Time::zero;
+ signal_state_changed.emit(state);
+ }
+ }
+}
+
void Block::find_paths(TrackIter track, unsigned path)
{
unsigned mask = track.endpoint().paths;
}
}
+void Block::sensor_event(unsigned addr, bool s)
+{
+ if(addr==sensor_id)
+ {
+ if(s && state<MAYBE_ACTIVE)
+ {
+ state = MAYBE_ACTIVE;
+ state_confirm_timeout = 300*Time::msec;
+ signal_state_changed.emit(state);
+ }
+ else if(!s && state>MAYBE_INACTIVE)
+ {
+ state = MAYBE_INACTIVE;
+ state_confirm_timeout = 700*Time::msec;
+ signal_state_changed.emit(state);
+ }
+ }
+}
+
Block::Endpoint::Endpoint(Track *t, unsigned e):
track(t),
class TrackIter;
class Train;
-class Block
+class Block: public sigc::trackable
{
public:
+ enum State
+ {
+ INACTIVE,
+ MAYBE_INACTIVE,
+ MAYBE_ACTIVE,
+ ACTIVE
+ };
+
struct Endpoint
{
Track *track;
Endpoint(Track *, unsigned);
};
+ sigc::signal<void, Train *> signal_reserved;
+ sigc::signal<void, State> signal_state_changed;
+
private:
Layout &layout;
unsigned id;
unsigned sensor_id;
unsigned turnout_id;
+ State state;
+ Msp::Time::TimeDelta state_confirm_timeout;
std::set<Track *> tracks;
std::vector<Endpoint> endpoints;
Train *train;
unsigned get_id() const { return id; }
unsigned get_sensor_id() const { return sensor_id; }
unsigned get_turnout_id() const { return turnout_id; }
+ State get_state() const { return state; }
const std::set<Track *> &get_tracks() const { return tracks; }
bool has_track(Track &) const;
const std::vector<Endpoint> &get_endpoints() const { return endpoints; }
Block *get_link(unsigned) const;
bool reserve(Train *);
Train *get_train() const { return train; }
- void print_debug();
+ void tick(const Msp::Time::TimeDelta &);
private:
void find_paths(TrackIter, unsigned);
void determine_id();
+ void sensor_event(unsigned, bool);
};
} // namespace R2C2
void CentralStation::tick()
{
- Time::TimeStamp t = Time::now();
- for(SensorMap::iterator i=sensors.begin(); i!=sensors.end(); ++i)
- if(i->second.off_timeout && t>i->second.off_timeout)
- {
- i->second.state = false;
- i->second.off_timeout = Time::TimeStamp();
- signal_sensor.emit(i->first, i->second.state);
- }
-
while(Message msg = receive())
{
if(msg.footer.code)
unsigned addr = base*16+j+1;
Sensor &sensor = sensors[addr];
bool s = state&(1<<j);
- if(s)
+ if(s!=sensor.state)
{
- sensor.off_timeout = Time::TimeStamp();
- if(!sensor.state)
- {
- sensor.state = true;
- signal_sensor.emit(addr, sensor.state);
- }
+ sensor.state = s;
+ signal_sensor.emit(addr, sensor.state);
}
- else if(sensor.state)
- sensor.off_timeout = Time::now()+700*Time::msec;
}
}
}
struct Sensor
{
bool state;
- Msp::Time::TimeStamp off_timeout;
Sensor();
};
catalogue(c),
driver(d),
next_turnout_id(0x800)
-{
- if(driver)
- driver->signal_sensor.connect(sigc::mem_fun(this, &Layout::sensor_event));
-}
+{ }
Layout::~Layout()
{
delete driver;
+ driver = 0;
+
while(!trains.empty())
delete trains.begin()->second;
while(!routes.empty())
void Layout::add_block(Block &b)
{
blocks.insert(&b);
+ b.signal_reserved.connect(sigc::bind<0>(signal_block_reserved, sigc::ref(b)));
+ if(b.get_sensor_id())
+ {
+ b.signal_state_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Layout::block_state_changed), sigc::ref(b)));
+ b.signal_state_changed.connect(sigc::bind<0>(signal_block_state_changed, sigc::ref(b)));
+ }
}
Block &Layout::get_block(unsigned id) const
dt = t-last_tick;
last_tick = t;
+ for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ (*i)->tick(dt);
for(map<unsigned, Train *>::iterator i=trains.begin(); i!=trains.end(); ++i)
i->second->tick(t, dt);
}
}
}
-void Layout::sensor_event(unsigned addr, bool state)
+void Layout::block_state_changed(Block &block, Block::State state)
{
- if(state)
- {
- for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
- if((*i)->get_sensor_id()==addr)
- {
- if(!(*i)->get_train())
- emergency(format("Unreserved sensor %d triggered", addr));
- break;
- }
- }
+ if(state==Block::ACTIVE && !block.get_train())
+ emergency(format("Unreserved sensor %d triggered", block.get_sensor_id()));
}
#include <sigc++/sigc++.h>
#include <msp/datafile/loader.h>
#include <msp/time/timestamp.h>
+#include "block.h"
namespace R2C2 {
class ArticleNumber;
-class Block;
class Catalogue;
class Driver;
class Route;
sigc::signal<void, Vehicle &> signal_vehicle_added;
sigc::signal<void, Vehicle &> signal_vehicle_removed;
sigc::signal<void, Block &, Train *> signal_block_reserved;
+ sigc::signal<void, Block &, Block::State> signal_block_state_changed;
sigc::signal<void, const std::string &> signal_emergency;
private:
void save(const std::string &) const;
void save_dynamic(const std::string &) const;
private:
- void sensor_event(unsigned, bool);
+ void block_state_changed(Block &, Block::State);
};
} // namespace R2C2
layout.get_driver().signal_loco_function.connect(sigc::mem_fun(this, &Train::loco_func_event));
layout.signal_block_reserved.connect(sigc::mem_fun(this, &Train::block_reserved));
- layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Train::sensor_event));
+ layout.signal_block_state_changed.connect(sigc::mem_fun(this, &Train::block_state_changed));
layout.get_driver().signal_halt.connect(sigc::mem_fun(this, &Train::halt_event));
}
}
-void Train::sensor_event(unsigned addr, bool state)
+void Train::block_state_changed(Block &block, Block::State state)
{
- if(state)
+ if(state==Block::MAYBE_ACTIVE)
{
// Find the first sensor block from our reserved blocks that isn't this sensor
BlockList::iterator end;
for(end=cur_blocks_end; end!=blocks.end(); ++end)
if((*end)->get_sensor_id())
{
- if((*end)->get_sensor_id()!=addr)
+ if(&**end!=&block)
{
if(result==0)
result = 2;
{
travel_dist += (*j)->get_path_length(j->entry());
- if((*j)->get_sensor_id()==addr && !advancing)
+ if(&**j==&block && !advancing)
{
TrackIter track = j->track_iter();
if(reverse)
else if(result==3)
layout.emergency("Sensor for "+name+" triggered out of order");
}
- else
+ else if(state==Block::INACTIVE)
{
const Vehicle &veh = *(reverse ? vehicles.front() : vehicles.back());
void control_changed(const Controller::Control &);
void loco_speed_event(unsigned, unsigned, bool);
void loco_func_event(unsigned, unsigned, bool);
- void sensor_event(unsigned, bool);
+ void block_state_changed(Block &, Block::State);
void turnout_path_changed(Track &);
void halt_event(bool);
void block_reserved(const Block &, const Train *);