]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/signal.cpp
Improve the interface and algorithms of BlockAllocator
[r2c2.git] / source / libr2c2 / signal.cpp
index 650c36232e87d82559fc36cfbb6aba2bc160ab55..a858e0dc92b99293eb142f8172f7902d563acb22 100644 (file)
@@ -1,4 +1,5 @@
 #include "blockiter.h"
+#include "catalogue.h"
 #include "driver.h"
 #include "layout.h"
 #include "signal.h"
@@ -50,64 +51,58 @@ void Signal::set_address(unsigned a)
 }
 
 void Signal::set_position(const Vector &p)
+{
+       position = p;
+
+       update_location();
+}
+
+void Signal::update_location()
 {
        const set<Track *> &tracks = layout.get_tracks();
+       float limit = layout.get_catalogue().get_gauge()*2;
        float dist = -1;
        for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
                if(!(*i)->get_type().is_turnout())
                {
                        Snap sn;
-                       sn.position = p;
+                       sn.position = position;
                        sn.rotation = rotation;
-                       (*i)->snap(sn, 1000, SNAP_SEGMENT);
-                       float d = distance(p, sn.position);
+                       (*i)->snap(sn, limit, SNAP_SEGMENT);
+                       float d = distance(position, sn.position);
                        if(d<dist || dist<0)
                        {
-                               position = sn.position;
-                               rotation = sn.rotation;
                                track = *i;
                                dist = d;
                        }
                }
 
-       normalize_location();
-}
+       block = 0;
 
-void Signal::normalize_location()
-{
-       block = &track->get_block();
+       if(!track)
+               return;
 
        unsigned n_endpoints = track->get_type().get_endpoints().size();
        for(unsigned j=0; j<n_endpoints; ++j)
        {
-               float a = track->get_snap_node(j).rotation-rotation;
-               while(a<-M_PI/2)
-                       a += M_PI*2;
-               while(a>M_PI*3/2)
-                       a -= M_PI*2;
-               if(a>=M_PI/2)
+               Angle a = wrap_with_base(track->get_snap_node(j).rotation-rotation, -Angle::quarter_turn());
+               if(a>=Angle::quarter_turn())
                {
                        BlockIter biter = TrackIter(track, j).block_iter();
-                       entry = biter.entry();
+                       if(biter)
+                       {
+                               block = &track->get_block();
+                               entry = biter.entry();
+                       }
                }
        }
 }
 
-void Signal::set_rotation(float r)
+void Signal::set_rotation(const Angle &r)
 {
-       float a = rotation-r;
-       while(a>M_PI*3/2)
-               a -= M_PI*2;
-       while(a<-M_PI/2)
-               a += M_PI*2;
-       if(a>=M_PI/2)
-       {
-               rotation += M_PI;
-               if(rotation>M_PI*2)
-                       rotation -= M_PI*2;
-       }
+       rotation = r;
 
-       normalize_location();
+       update_location();
 }
 
 unsigned Signal::get_n_snap_nodes() const
@@ -134,20 +129,6 @@ SnapType Signal::get_default_snap_type_to(const Object &other) const
        return NO_SNAP;
 }
 
-bool Signal::collide_ray(const Vector &start, const Vector &ray) const
-{
-       // XXX Totally hardcoded stuff, should be replaced with a geometry system
-       Vector center = position;
-       center.x += sin(rotation)*0.035;
-       center.y -= cos(rotation)*0.035;
-       Vector d(center.x-start.x, center.y-start.y);
-       float x = (d.x*ray.x+d.y*ray.y)/(ray.x*ray.x+ray.y*ray.y);
-       Vector nearest(start.x+ray.x*x-center.x, start.y+ray.y*x-center.y, start.z+ray.z*x-center.z);
-       if(nearest.z<0|| nearest.z>0.12)
-               return false;
-       return nearest.x*nearest.x+nearest.y*nearest.y<0.0001;
-}
-
 void Signal::tick(const Time::TimeDelta &)
 {
        if(check_allocated_blocks)
@@ -182,8 +163,8 @@ void Signal::block_reserved(const Block &b, Train *t)
        {
                if(t)
                {
-                       int train_entry = t->get_entry_to_block(*block);
-                       if(train_entry>=0 && static_cast<unsigned>(train_entry)==entry)
+                       const BlockIter &b_iter = t->get_block_allocator().iter_for(b);
+                       if(b_iter && b_iter.entry()==entry)
                        {
                                if(train_conn)
                                        train_conn.disconnect();
@@ -225,7 +206,7 @@ void Signal::reset()
 void Signal::save(list<DataFile::Statement> &st) const
 {
        st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
-       st.push_back((DataFile::Statement("rotation"), rotation));
+       st.push_back((DataFile::Statement("rotation"), rotation.radians()));
        if(address)
                st.push_back((DataFile::Statement("address"), address));
 }
@@ -251,7 +232,7 @@ void Signal::Loader::position(float x, float y, float z)
 
 void Signal::Loader::rotation(float d)
 {
-       obj.set_rotation(d);
+       obj.set_rotation(Angle::from_radians(d));
 }
 
 } // namespace R2C2