]> git.tdb.fi Git - r2c2.git/commitdiff
Mark generated routes as temporary and don't show or save them
authorMikko Rasa <tdb@tdb.fi>
Fri, 23 Apr 2010 17:05:18 +0000 (17:05 +0000)
committerMikko Rasa <tdb@tdb.fi>
Fri, 23 Apr 2010 17:05:18 +0000 (17:05 +0000)
Fix a segfault with unplaced trains
Handle end-of-line gracefully in various places

source/engineer/routeselect.cpp
source/libmarklin/layout.cpp
source/libmarklin/route.cpp
source/libmarklin/route.h
source/libmarklin/train.cpp

index 3413220a46aea4c8cac349230c29cdc486cd7850..c285f35f7031f7610551511d05bb12c4fb9432f8 100644 (file)
@@ -32,12 +32,14 @@ RouteSelect::RouteSelect(Engineer &e, const GLtk::Resources &r, Train &t):
        drp_route->set_selected_index(0);
        const map<string, Route *> &routes = engineer.get_layout().get_routes();
        unsigned n = 1;
-       for(map<string, Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i, ++n)
-       {
-               drp_route->append(i->second->get_name());
-               if(i->second==train.get_route())
-                       drp_route->set_selected_index(n);
-       }
+       for(map<string, Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
+               if(!i->second->is_temporary())
+               {
+                       drp_route->append(i->second->get_name());
+                       if(i->second==train.get_route())
+                               drp_route->set_selected_index(n);
+                       ++n;
+               }
 }
 
 void RouteSelect::on_ok_clicked()
@@ -46,7 +48,17 @@ void RouteSelect::on_ok_clicked()
        {
                const map<string, Route *> &routes = engineer.get_layout().get_routes();
                map<string, Route *>::const_iterator i = routes.begin();
-               advance(i, drp_route->get_selected_index()-1);
+               unsigned n = drp_route->get_selected_index()-1;
+               while(i!=routes.end())
+               {
+                       if(!i->second->is_temporary())
+                       {
+                               if(!n)
+                                       break;
+                               --n;
+                       }
+                       ++i;
+               }
                
                train.set_route(i->second);
        }
index af88d7d8fd2e35f3a44af86ff24640c85c003d1d..e67fbc26ebc0443440900293bd72555184e439f0 100644 (file)
@@ -237,6 +237,9 @@ void Layout::save(const string &fn)
 
        for(map<string, Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
        {
+               if(i->second->is_temporary())
+                       continue;
+
                DataFile::Statement st("route");
                st.append(i->first);
                i->second->save(st.sub);
@@ -287,6 +290,9 @@ void Layout::check_routes()
 {
        for(map<string, Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
        {
+               if(i->second->is_temporary())
+                       continue;
+
                /* We must copy the turnout map, since adding tracks to the route will
                (temporarily) mess it up */
                const map<unsigned, int> turnouts = i->second->get_turnouts();
@@ -322,6 +328,8 @@ void Layout::check_routes()
                while(1)
                {
                        // Traverse the track and get the next one
+                       if(track->get_type().get_endpoints().size()<2)
+                               break;
                        unsigned out_ep = track->traverse(ep, trk_path);
                        Track *next = track->get_links()[out_ep];
                        if(!next || next == start)
index ace8d0b950812c56a22e23a0ba381f4d389b9f7b..7ecd473694926ea306067f1993e03ad16b08a0cf 100644 (file)
@@ -130,6 +130,8 @@ Route *create_route(const Track &from, unsigned ep, const Pred &goal)
        for(list<const Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
                route->add_track(**i);
 
+       route->set_temporary(true);
+
        return route;
 }
 
@@ -140,7 +142,8 @@ namespace Marklin {
 
 Route::Route(Layout &l, const string &n):
        layout(l),
-       name(n)
+       name(n),
+       temporary(false)
 {
        layout.add_route(*this);
        layout.signal_track_removed.connect(sigc::mem_fun(this, &Route::track_removed));
@@ -151,6 +154,11 @@ Route::~Route()
        layout.remove_route(*this);
 }
 
+void Route::set_temporary(bool t)
+{
+       temporary = t;
+}
+
 int Route::get_turnout(unsigned id) const
 {
        map<unsigned, int>::const_iterator i = turnouts.find(id);
index 89788e3b62b477fc945229a96f4e1ede0ecbfc7f..010bb873c8e0971ae0a8022677df26b001b04a7a 100644 (file)
@@ -33,6 +33,7 @@ public:
 private:
        Layout &layout;
        std::string name;
+       bool temporary;
        std::set<const Track *> tracks;
        std::map<unsigned, int> turnouts;
 
@@ -41,6 +42,8 @@ public:
        ~Route();
 
        const std::string &get_name() const { return name; }
+       void set_temporary(bool);
+       bool is_temporary() const { return temporary; }
        int get_turnout(unsigned) const;
        const std::map<unsigned, int> &get_turnouts() const { return turnouts; }
        void add_track(const Track &);
index 0c741722d7b7dfecf749be196183bbb1df3a3e58..ed57dd7ee143c7f1ec8eb1a5b1081f85d9ad4881 100644 (file)
@@ -262,6 +262,8 @@ float Train::get_reserved_distance() const
        const VehicleType &vtype = veh.get_type();
 
        Track *track = veh.get_track();
+       if(!track)
+               return 0;
        unsigned entry = veh.get_entry();
 
        float result = -vtype.get_length()/2;
@@ -281,6 +283,9 @@ float Train::get_reserved_distance() const
                        result += track->get_type().get_path_length(track->get_active_path());
                first = false;
 
+               if(track->get_type().get_endpoints().size()<2)
+                       return result;
+
                unsigned exit = track->traverse(entry);
                Track *next = track->get_link(exit);
 
@@ -390,7 +395,12 @@ void Train::save(list<DataFile::Statement> &st) const
        }
 
        if(route)
-               st.push_back((DataFile::Statement("route"), route->get_name()));
+       {
+               if(!route->is_temporary())
+                       st.push_back((DataFile::Statement("route"), route->get_name()));
+               else if(next_route && !next_route->is_temporary())
+                       st.push_back((DataFile::Statement("route"), next_route->get_name()));
+       }
 
        if(timetable)
        {
@@ -592,6 +602,13 @@ unsigned Train::reserve_more()
        unsigned good_sens = nsens;
        while(good_sens<3)
        {
+               if(last->block->get_endpoints().size()<2)
+               {
+                       good = last;
+                       good_sens = nsens;
+                       break;
+               }
+
                // Traverse to the next block
                unsigned exit = last->block->traverse(last->entry);
                Block *link = last->block->get_link(exit);