]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/train.cpp
Fix a compile error
[r2c2.git] / source / libmarklin / train.cpp
index 1abf535c06b6d03a0649c95162b7264da1802c73..efe1a142cb6daefc29162f1a99bd578f55de7f9b 100644 (file)
@@ -30,6 +30,7 @@ Train::Train(Layout &l, const LocoType &t, unsigned a):
        layout(l),
        loco_type(t),
        address(a),
+       priority(0),
        pending_block(0),
        control(new AIControl(*this, new SimplePhysics)),
        timetable(0),
@@ -81,6 +82,11 @@ void Train::set_name(const string &n)
        signal_name_changed.emit(name);
 }
 
+void Train::set_priority(int p)
+{
+       priority = p;
+}
+
 Vehicle &Train::get_vehicle(unsigned i)
 {
        if(i>=vehicles.size())
@@ -163,8 +169,7 @@ void Train::set_route(const Route *r)
                }
        }
 
-       if(active)
-               reserve_more();
+       reserve_more();
 
        signal_route_changed.emit(route);
 }
@@ -424,6 +429,8 @@ void Train::save(list<DataFile::Statement> &st) const
 {
        st.push_back((DataFile::Statement("name"), name));
 
+       st.push_back((DataFile::Statement("priority"), priority));
+
        for(vector<Vehicle *>::const_iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
                if(i!=vehicles.begin())
                        st.push_back((DataFile::Statement("vehicle"), (*i)->get_type().get_article_number()));
@@ -516,8 +523,11 @@ void Train::sensor_event(unsigned addr, bool state)
 
                        if(pure_speed)
                        {
-                               RealSpeed &rs = real_speed[current_speed];
-                               rs.add(travel_dist/travel_time_secs, travel_time_secs);
+                               if(current_speed)
+                               {
+                                       RealSpeed &rs = real_speed[current_speed];
+                                       rs.add(travel_dist/travel_time_secs, travel_time_secs);
+                               }
                                set_status(format("Traveling %d kmh", get_travel_speed()));
                        }
 
@@ -622,6 +632,9 @@ void Train::block_reserved(const Block &block, const Train *train)
 
 unsigned Train::reserve_more()
 {
+       if(!active)
+               return 0;
+
        BlockRef *last = 0;
        if(!rsv_blocks.empty())
                last = &rsv_blocks.back();
@@ -694,19 +707,28 @@ unsigned Train::reserve_more()
                else if(route && route->get_tracks().count(entry_ep.track))
                        cur_route = route;
 
-               if(!link->reserve(this))
+               bool reserved = link->reserve(this);
+               if(!reserved)
                {
-                       // If we found another train and it's not headed straight for us, we can keep the blocks we got
-                       int other_entry = link->get_train()->get_entry_to_block(*link);
-                       if(other_entry<0)
-                               throw LogicError("Block reservation inconsistency");
-                       if(static_cast<unsigned>(entry)!=link->traverse(other_entry))
+                       // Ask a lesser priority train to free the block for us
+                       if(link->get_train()->get_priority()<priority)
+                               if(link->get_train()->free_block(*link))
+                                       reserved = link->reserve(this);
+
+                       if(!reserved)
                        {
-                               good = last;
-                               good_sens = nsens;
+                               // If we found another train and it's not headed straight for us, we can keep the blocks we got
+                               int other_entry = link->get_train()->get_entry_to_block(*link);
+                               if(other_entry<0)
+                                       throw LogicError("Block reservation inconsistency");
+                               if(static_cast<unsigned>(entry)!=link->traverse(other_entry))
+                               {
+                                       good = last;
+                                       good_sens = nsens;
+                               }
+                               pending_block = link;
+                               break;
                        }
-                       pending_block = link;
-                       break;
                }
 
                if(link->get_turnout_id())
@@ -815,7 +837,12 @@ unsigned Train::find_speed(float real) const
        if(!high)
        {
                if(!low)
-                       return 0;
+               {
+                       if(real)
+                               return 3;
+                       else
+                               return 0;
+               }
                return min(static_cast<unsigned>(low*real/real_speed[low].speed), 14U);
        }
 
@@ -897,6 +924,7 @@ Train::Loader::Loader(Train &t):
        add("block",       &Loader::block);
        add("block_hint",  &Loader::block_hint);
        add("name",        &Loader::name);
+       add("priority",    &Train::priority);
        add("real_speed",  &Loader::real_speed);
        add("route",       &Loader::route);
        add("timetable",   &Loader::timetable);