]> git.tdb.fi Git - r2c2.git/commitdiff
Use max/min for range capping
authorMikko Rasa <tdb@tdb.fi>
Fri, 6 Feb 2015 21:55:14 +0000 (23:55 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 6 Feb 2015 21:58:55 +0000 (23:58 +0200)
source/libr2c2/sensor.cpp
source/libr2c2/simplecontroller.cpp
source/libr2c2/train.cpp

index ecd06473fb4bdf2234a2c149f19d0b3ae1af849e..83dcb6d1be3b6bae9610101672bf21973674e655 100644 (file)
@@ -2,6 +2,7 @@
 #include "layout.h"
 #include "sensor.h"
 
+using namespace std;
 using namespace Msp;
 
 namespace R2C2 {
@@ -42,14 +43,13 @@ void Sensor::tick(const Time::TimeDelta &dt)
 {
        if(state_confirm_timeout)
        {
-               state_confirm_timeout -= dt;
+               state_confirm_timeout = max(state_confirm_timeout-dt, Time::zero);
                if(state_confirm_timeout<=Time::zero)
                {
                        if(state==MAYBE_INACTIVE)
                                state = INACTIVE;
                        else if(state==MAYBE_ACTIVE)
                                state = ACTIVE;
-                       state_confirm_timeout = Time::zero;
                        signal_state_changed.emit(state);
                }
        }
index d04acab4a045502d6326927429c625fd568a2d29..df828d5498832bf480cea9767518dae92dc6014a 100644 (file)
@@ -61,17 +61,9 @@ void SimpleController::tick(const Time::TimeDelta &dt)
 {
        float secs = dt/Time::sec;
        if(speed<target_speed.value)
-       {
-               speed += secs*accel;
-               if(speed>target_speed.value)
-                       speed = target_speed.value;
-       }
+               speed = min(speed+secs*accel, target_speed.value);
        else if(speed>target_speed.value)
-       {
-               speed -= secs*accel;
-               if(speed<target_speed.value)
-                       speed = target_speed.value;
-       }
+               speed = max(speed-secs*accel, target_speed.value);
 }
 
 } // namespace R2C2
index e9a539a4e77158bc911eebc44c9a437564db4b29..858cbbc9e8c8d093971af814a3acb049717ffc37 100644 (file)
@@ -298,12 +298,9 @@ void Train::tick(const Time::TimeDelta &dt)
 {
        if(stop_timeout)
        {
-               stop_timeout -= dt;
+               stop_timeout = max(stop_timeout-dt, Time::zero);
                if(stop_timeout<=Time::zero)
-               {
                        allocator.set_active(false);
-                       stop_timeout = Time::TimeDelta();
-               }
        }
 
        travel_time += dt;