From: Mikko Rasa Date: Mon, 9 Feb 2015 17:33:41 +0000 (+0200) Subject: Instantly set turnout paths when loading state X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=08dea8dfcf923e9296bcd3910455540895d0439b;p=r2c2.git Instantly set turnout paths when loading state Vehicles are placed immediately and will follow turnout paths. Having them in the wrong places confuses the route planner. --- diff --git a/source/libr2c2/layout.cpp b/source/libr2c2/layout.cpp index 72f083d..fe21881 100644 --- a/source/libr2c2/layout.cpp +++ b/source/libr2c2/layout.cpp @@ -525,7 +525,12 @@ void Layout::save_dynamic(const string &fn) const const set &tracks = objects.get(); for(set::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::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 &tracks = obj.objects.get(); + for(set::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); } diff --git a/source/libr2c2/layout.h b/source/libr2c2/layout.h index 5a0804f..a1c37d3 100644 --- a/source/libr2c2/layout.h +++ b/source/libr2c2/layout.h @@ -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(); }; diff --git a/source/libr2c2/track.cpp b/source/libr2c2/track.cpp index f1fdd99..0411f15 100644 --- a/source/libr2c2/track.cpp +++ b/source/libr2c2/track.cpp @@ -417,6 +417,12 @@ void Track::save(list &st) const st.push_back((DataFile::Statement("flex"), true)); } +void Track::save_dynamic(list &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(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)); diff --git a/source/libr2c2/track.h b/source/libr2c2/track.h index bac4712..ba59da0 100644 --- a/source/libr2c2/track.h +++ b/source/libr2c2/track.h @@ -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 &) const; + void save_dynamic(std::list &) const; private: void turnout_event(unsigned, unsigned); void turnout_failed(unsigned);