]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/layout.cpp
Handle sensors in a separate class
[r2c2.git] / source / libr2c2 / layout.cpp
index fefa3e9b3fb4045e2d6ecf84280c0319fa2431ed..16542953a76320697ece0cb037767c70d9793ef3 100644 (file)
@@ -13,6 +13,7 @@
 #include "signal.h"
 #include "signaltype.h"
 #include "track.h"
+#include "trackcircuit.h"
 #include "tracktype.h"
 #include "train.h"
 #include "vehicletype.h"
@@ -50,6 +51,8 @@ Layout::~Layout()
                delete *routes.begin();
        while(!zones.empty())
                delete *zones.begin();
+       while(!signals.empty())
+               delete *signals.begin();
        while(!tracks.empty())
                delete *tracks.begin();
        while(!blocks.empty())
@@ -72,6 +75,11 @@ void Layout::add_track(Track &t)
        }
 }
 
+Track *Layout::pick_track(const Ray &ray)
+{
+       return pick(tracks, ray);
+}
+
 void Layout::remove_track(Track &t)
 {
        if(tracks.erase(&t))
@@ -81,51 +89,61 @@ void Layout::remove_track(Track &t)
        }
 }
 
+unsigned Layout::allocate_turnout_id()
+{
+       set<unsigned> used_ids;
+       for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+               if((*i)->get_turnout_id())
+                       used_ids.insert((*i)->get_turnout_id());
+
+       unsigned result = next_turnout_id;
+       while(used_ids.count(result))
+               ++result;
+       next_turnout_id = result+1;
+
+       return result;
+}
+
 void Layout::add_signal(Signal &s)
 {
        if(signals.insert(&s).second)
                signal_signal_added.emit(s);
 }
 
+Signal *Layout::pick_signal(const Ray &ray)
+{
+       return pick(signals, ray);
+}
+
 void Layout::remove_signal(Signal &s)
 {
        if(signals.erase(&s))
                signal_signal_removed.emit(s);
 }
 
-Track *Layout::pick_track(const Vector &start, const Vector &ray)
+Object *Layout::pick_object(const Ray &ray)
 {
-       for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
-               if((*i)->collide_ray(start, ray))
-                       return *i;
-
+       if(Object *obj = pick_track(ray))
+               return obj;
+       else if((obj = pick_signal(ray)))
+               return obj;
        return 0;
 }
 
-unsigned Layout::allocate_turnout_id()
+template<typename T>
+T *Layout::pick(const set<T *> &objs, const Ray &ray)
 {
-       set<unsigned> used_ids;
-       for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
-               if((*i)->get_turnout_id())
-                       used_ids.insert((*i)->get_turnout_id());
-
-       unsigned result = next_turnout_id;
-       while(used_ids.count(result))
-               ++result;
-       next_turnout_id = result+1;
+       for(typename set<T *>::const_iterator i=objs.begin(); i!=objs.end(); ++i)
+               if((*i)->collide_ray(ray))
+                       return *i;
 
-       return result;
+       return 0;
 }
 
 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
@@ -271,6 +289,20 @@ void Layout::remove_vehicle(Vehicle &v)
                signal_vehicle_removed.emit(v);
 }
 
+void Layout::add_sensor(Sensor &s)
+{
+       if(sensors.insert(&s).second)
+       {
+               s.signal_state_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Layout::sensor_state_changed), sigc::ref(s)));
+               s.signal_state_changed.connect(sigc::bind<0>(signal_sensor_state_changed, sigc::ref(s)));
+       }
+}
+
+void Layout::remove_sensor(Sensor &s)
+{
+       sensors.erase(&s);
+}
+
 void Layout::tick()
 {
        if(driver)
@@ -282,7 +314,7 @@ void Layout::tick()
                dt = t-last_tick;
        last_tick = t;
 
-       for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
+       for(set<Sensor *>::iterator i=sensors.begin(); i!=sensors.end(); ++i)
                (*i)->tick(dt);
        for(set<Signal *>::iterator i=signals.begin(); i!=signals.end(); ++i)
                (*i)->tick(dt);
@@ -360,10 +392,17 @@ void Layout::save_dynamic(const string &fn) const
        }
 }
 
-void Layout::block_state_changed(Block &block, Block::State state)
+void Layout::sensor_state_changed(Sensor &sensor, Sensor::State state)
 {
-       if(state==Block::ACTIVE && !block.get_train())
-               emergency(format("Unreserved sensor %d triggered", block.get_sensor_id()));
+       if(state==Sensor::ACTIVE)
+       {
+               Block *block = 0;
+               if(TrackCircuit *tc = dynamic_cast<TrackCircuit *>(&sensor))
+                       block = &tc->get_block();
+
+               if(block && !block->get_train())
+                       emergency(format("Unreserved sensor %d triggered", sensor.get_address()));
+       }
 }
 
 
@@ -385,12 +424,6 @@ Layout::Loader::Loader(Layout &l):
        add("train", static_cast<void (Loader::*)(unsigned, unsigned, const std::string &)>(&Loader::train));
 }
 
-void Layout::Loader::finish()
-{
-       for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
-               (*i)->check_slope();
-}
-
 void Layout::Loader::route()
 {
        Route *rte = new Route(obj);
@@ -422,7 +455,7 @@ void Layout::Loader::track(ArticleNumber art_nr)
        new_tracks = true;
        for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
                if(*i!=trk)
-                       trk->snap_to(**i, true);
+                       trk->link_to(**i);
 }
 
 void Layout::Loader::train(unsigned art_nr, unsigned addr, const std::string &proto)