]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/train.cpp
Redesign the train activation system
[r2c2.git] / source / libr2c2 / train.cpp
index 8cfa2d0ee4817158b11086ea318bdeef2c50db83..6cb3205eb175fad4f6e6d8b18997a725df2da7b5 100644 (file)
@@ -36,7 +36,6 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a, const string &p):
        allocator(*this),
        advancing(false),
        controller(new SimpleController),
-       active(false),
        current_speed_step(0),
        speed_changing(false),
        reverse(false),
@@ -126,23 +125,6 @@ void Train::set_control(const string &n, float v)
        controller->set_control(n, v);
 }
 
-void Train::set_active(bool a)
-{
-       if(a==active)
-               return;
-       if(!a && controller->get_speed())
-               throw logic_error("moving");
-
-       active = a;
-       if(active)
-       {
-               stop_timeout = Time::zero;
-               allocator.reserve_more();
-       }
-       else
-               stop_timeout = 2*Time::sec;
-}
-
 void Train::set_function(unsigned func, bool state)
 {
        if(!loco_type.get_functions().count(func))
@@ -199,10 +181,8 @@ void Train::place(const BlockIter &block)
        if(controller->get_speed())
                throw logic_error("moving");
 
-       set_active(false);
-       accurate_position = false;
-
        allocator.start_from(block);
+       accurate_position = false;
 
        if(reverse)
                vehicles.front()->place(block.reverse().track_iter(), 0, Vehicle::FRONT_BUFFER);
@@ -216,8 +196,6 @@ void Train::unplace()
                throw logic_error("moving");
 
        allocator.clear();
-
-       set_active(false);
        accurate_position = false;
 
        for(vector<Vehicle *>::iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
@@ -227,48 +205,48 @@ void Train::unplace()
 void Train::stop_at(Block *block)
 {
        allocator.stop_at(block);
-       if(active && !block)
-               allocator.reserve_more();
 }
 
-bool Train::free_block(Block &block)
+bool Train::is_block_critical(const Block &block) const
 {
-       if(get_reserved_distance_until(&block)<controller->get_braking_distance()*1.3)
-               return false;
-
-       return allocator.release_from(block);
+       return get_reserved_distance_until(&block)<controller->get_braking_distance()*1.3;
 }
 
-void Train::free_noncritical_blocks()
+BlockIter Train::get_first_noncritical_block() const
 {
        if(allocator.empty())
-               return;
+               return BlockIter();
+
+       BlockIter i = allocator.last_current().next();
 
        if(controller->get_speed()==0)
-       {
-               allocator.release_noncurrent();
-               return;
-       }
+               return i;
 
        float margin = 10*layout.get_catalogue().get_scale();
        float min_dist = controller->get_braking_distance()*1.3+margin;
 
-       BlockIter i = allocator.last_current().next();
        float dist = 0;
        bool sensor_seen = false;
        for(; i->get_train()==this; i=i.next())
        {
                if(dist>min_dist && sensor_seen)
-               {
-                       allocator.release_from(*i);
-                       return;
-               }
+                       return i;
 
                dist += i->get_path_length(i.entry());
 
                if(i->get_sensor_id())
                        sensor_seen = true;
        }
+
+       return i;
+}
+
+void Train::refresh_blocks_from(Block &block)
+{
+       if(is_block_critical(block))
+               allocator.rewind_to(*get_first_noncritical_block());
+       else
+               allocator.rewind_to(block);
 }
 
 float Train::get_reserved_distance() const
@@ -284,19 +262,14 @@ float Train::get_reserved_distance() const
        return max(get_reserved_distance_until(0)-margin, 0.0f);
 }
 
-void Train::reserve_more()
-{
-       allocator.reserve_more();
-}
-
 void Train::tick(const Time::TimeDelta &dt)
 {
-       if(!active && stop_timeout)
+       if(stop_timeout)
        {
                stop_timeout -= dt;
                if(stop_timeout<=Time::zero)
                {
-                       allocator.release_noncurrent();
+                       allocator.set_active(false);
                        stop_timeout = Time::TimeDelta();
                }
        }
@@ -305,8 +278,14 @@ void Train::tick(const Time::TimeDelta &dt)
 
        Driver &driver = layout.get_driver();
 
+       bool intent_to_move = false;
        for(list<TrainAI *>::iterator i=ais.begin(); i!=ais.end(); ++i)
+       {
                (*i)->tick(dt);
+               if((*i)->has_intent_to_move())
+                       intent_to_move = true;
+       }
+
        controller->tick(dt);
        float speed = controller->get_speed();
        bool moving = speed>0;
@@ -320,8 +299,6 @@ void Train::tick(const Time::TimeDelta &dt)
                driver.set_loco_reverse(address, r);
 
                allocator.reverse();
-               if(active)
-                       allocator.reserve_more();
        }
 
        if(speed_quantizer)
@@ -340,8 +317,8 @@ void Train::tick(const Time::TimeDelta &dt)
 
        if(moving)
        {
-               if(!active)
-                       set_active(true);
+               if(!allocator.is_active())
+                       allocator.set_active(true);
 
                Vehicle &vehicle = *(reverse ? vehicles.back() : vehicles.front());
 
@@ -361,6 +338,8 @@ void Train::tick(const Time::TimeDelta &dt)
                        }
                }
        }
+       else if(allocator.is_active() && !intent_to_move && !stop_timeout)
+               stop_timeout = 2*Time::sec;
 }
 
 void Train::save(list<DataFile::Statement> &st) const