]> git.tdb.fi Git - r2c2.git/commitdiff
Instantly set turnout paths when loading state
authorMikko Rasa <tdb@tdb.fi>
Mon, 9 Feb 2015 17:33:41 +0000 (19:33 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 9 Feb 2015 17:38:01 +0000 (19:38 +0200)
Vehicles are placed immediately and will follow turnout paths.  Having
them in the wrong places confuses the route planner.

source/libr2c2/layout.cpp
source/libr2c2/layout.h
source/libr2c2/track.cpp
source/libr2c2/track.h

index 72f083d57cc2da491dead13077ad495b716f5942..fe21881a2e4ab46a5f7f1e3b6a6e48c75bf7c2af 100644 (file)
@@ -525,7 +525,12 @@ void Layout::save_dynamic(const string &fn) const
        const set<Track *> &tracks = objects.get<Track>();
        for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
                if((*i)->get_type().is_turnout())
-                       writer.write((DataFile::Statement("turnout"), (*i)->get_turnout_address(), (*i)->get_active_path()));
+               {
+                       DataFile::Statement st("turnout");
+                       st.append((*i)->get_turnout_address());
+                       (*i)->save_dynamic(st.sub);
+                       writer.write(st);
+               }
 
        for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
        {
@@ -638,6 +643,9 @@ Layout::Loader::Loader(Layout &l):
        add("train", &Loader::train);
        add("turnout", &Loader::turnout);
        add("zone",  &Loader::zone);
+
+       // Deprecated
+       add("turnout", &Loader::turnout2);
 }
 
 void Layout::Loader::beamgate()
@@ -685,8 +693,24 @@ void Layout::Loader::train(const string &n, unsigned addr, const std::string &pr
        load_sub(*trn);
 }
 
-void Layout::Loader::turnout(unsigned addr, unsigned path)
+void Layout::Loader::turnout(unsigned addr)
+{
+       /* Kinda slow, but maintaining a map of turnouts seems more trouble than
+       it's worth at this point. */
+       const set<Track *> &tracks = obj.objects.get<Track>();
+       for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+               if((*i)->get_turnout_address()==addr)
+               {
+                       load_sub(**i);
+                       return;
+               }
+
+       throw key_error(addr);
+}
+
+void Layout::Loader::turnout2(unsigned addr, unsigned path)
 {
+       // XXX Discrepancy between ids and addresses
        if(obj.driver)
                obj.driver->set_turnout(addr, path);
 }
index 5a0804fc77329b1c8714b3ce2d27112d1ff14b13..a1c37d312b682ed6fabb086018824a0876d54d2e 100644 (file)
@@ -41,7 +41,8 @@ public:
                void terrain(const std::string &);
                void track(const std::string &);
                void train(const std::string &, unsigned, const std::string &);
-               void turnout(unsigned, unsigned);
+               void turnout(unsigned);
+               void turnout2(unsigned, unsigned);
                void zone();
        };
 
index f1fdd992822180c26ddd5c39a6095bb8145516e4..0411f1586c86fd89bc21f3be534905f363995dec 100644 (file)
@@ -417,6 +417,12 @@ void Track::save(list<DataFile::Statement> &st) const
                st.push_back((DataFile::Statement("flex"), true));
 }
 
+void Track::save_dynamic(list<DataFile::Statement> &st) const
+{
+       if(turnout_addr)
+               st.push_back((DataFile::Statement("path"), active_path));
+}
+
 void Track::turnout_event(unsigned id, unsigned state)
 {
        if(id==turnout_id)
@@ -440,6 +446,7 @@ void Track::turnout_failed(unsigned id)
 Track::Loader::Loader(Track &t):
        DataFile::ObjectLoader<Track>(t)
 {
+       add("path",       &Loader::path);
        add("position",   &Loader::position);
        add("rotation",   &Loader::rotation);
        add("tilt",       &Loader::tilt);
@@ -453,6 +460,16 @@ Track::Loader::Loader(Track &t):
        add("slope",      &Loader::slope);
 }
 
+void Track::Loader::path(unsigned p)
+{
+       obj.set_active_path(p);
+       if(obj.path_changing)
+       {
+               obj.active_path = p;
+               obj.signal_path_changed.emit(p);
+       }
+}
+
 void Track::Loader::position(float x, float y, float z)
 {
        obj.set_position(Vector(x, y, z));
index bac4712322ba38ed46f2416fd3276d3e450c1902..ba59da0ad95b9badc3e55401c7b95a7bd764bf33 100644 (file)
@@ -23,6 +23,7 @@ public:
        public:
                Loader(Track &);
        private:
+               void path(unsigned);
                void position(float, float, float);
                void rotation(float);
                void sensor_address(unsigned);
@@ -104,6 +105,7 @@ public:
        AttachmentList get_attachments_ordered(unsigned) const;
 
        void save(std::list<Msp::DataFile::Statement> &) const;
+       void save_dynamic(std::list<Msp::DataFile::Statement> &) const;
 private:
        void turnout_event(unsigned, unsigned);
        void turnout_failed(unsigned);