]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/train.cpp
Pull the SetFlag class from mspcore
[r2c2.git] / source / libr2c2 / train.cpp
index a45675c90c146e078427bb754848b6b907d806d5..d78e6276ec14bd98780badaef96ddece39ed2be7 100644 (file)
@@ -1,6 +1,7 @@
 #include <algorithm>
 #include <cmath>
 #include <msp/core/maputils.h>
+#include <msp/core/raii.h>
 #include <msp/strings/format.h>
 #include <msp/time/units.h>
 #include <msp/time/utils.h>
 using namespace std;
 using namespace Msp;
 
-namespace {
-
-struct SetFlag
-{
-       bool &flag;
-
-       SetFlag(bool &f): flag(f) { flag = true; }
-       ~SetFlag() { flag = false; }
-};
-
-}
-
-
 namespace R2C2 {
 
 Train::Train(Layout &l, const VehicleType &t, unsigned a, const string &p):
@@ -206,15 +194,6 @@ void Train::remove_ai(TrainAI &ai)
                ais.erase(i);
 }
 
-TrainAI *Train::get_tagged_ai(const string &tag) const
-{
-       for(list<TrainAI *>::const_iterator i=ais.begin(); i!=ais.end(); ++i)
-               if((*i)->get_tag()==tag)
-                       return *i;
-
-       return 0;
-}
-
 void Train::ai_message(const TrainAI::Message &msg)
 {
        for(list<TrainAI *>::iterator i=ais.begin(); i!=ais.end(); ++i)
@@ -267,6 +246,8 @@ void Train::unplace()
 void Train::stop_at(Block *block)
 {
        stop_at_block = block;
+       if(active && !stop_at_block)
+               reserve_more();
 }
 
 bool Train::free_block(Block &block)
@@ -674,7 +655,7 @@ void Train::reserve_more()
        else if(&*start==pending_block)
        {
                TrackIter track = start.track_iter();
-               if(!(track.endpoint().paths&(1<<track->get_active_path())))
+               if(!track.endpoint().has_path(track->get_active_path()))
                        return;
        }
 
@@ -736,11 +717,12 @@ void Train::reserve_more()
                {
                        const TrackType::Endpoint &entry_ep = track.endpoint();
                        unsigned path = track->get_active_path();
-                       if(!(entry_ep.paths&(1<<path)))
+                       if(!entry_ep.has_path(path))
                        {
                                const TrackType::Endpoint &exit_ep = track.reverse().endpoint();
-                               if(unsigned mask = entry_ep.paths&exit_ep.paths)
+                               if(entry_ep.has_common_paths(exit_ep))
                                {
+                                       unsigned mask = entry_ep.paths&exit_ep.paths;
                                        for(path=0; mask>1; ++path, mask>>=1) ;
 
                                        track->set_active_path(path);