// // Created by Micha on 29/09/2021. // #include "DecisionRegulator.h" DecisionRegulator::DecisionRegulator(float NumUpdatesPerSecondRqd) { std::random_device rdevice{}; std::default_random_engine num{rdevice()}; std::uniform_real_distribution random{0.0f, 1000.0f}; m_NextUpdateTime = SDL_GetTicks() + static_cast(random(num)); if (NumUpdatesPerSecondRqd > 0) { m_UpdatePeriod = 1000.0f / NumUpdatesPerSecondRqd; } // check if 0.0(remember floats never quite 0.0 else if ((NumUpdatesPerSecondRqd * NumUpdatesPerSecondRqd) < 0.0000001) { m_UpdatePeriod = 0.0f; } else if (NumUpdatesPerSecondRqd < 0) { m_UpdatePeriod = -1; } } // returns true if the current time exceeds m_dwNextUpdateTime bool DecisionRegulator::isReady() { // if a regulator is instantiated with a zero freq then it goes into // stealth mode (doesn't regulate) if ((m_UpdatePeriod * m_UpdatePeriod) < 0.0000001) return true; // if the regulator is instantiated with a negative freq then it will // never allow the code to flow if (m_UpdatePeriod < 0) return false; Uint32 CurrentTime = SDL_GetTicks(); // the number of milliseconds the update period can vary per required // update-step. This is here to make sure any multiple clients of this class // have their updates spread evenly static const float UpdatePeriodVariator = 10.0; if (CurrentTime >= m_NextUpdateTime) { std::random_device rdevice{}; std::default_random_engine num{rdevice()}; std::uniform_real_distribution random{-UpdatePeriodVariator, UpdatePeriodVariator}; m_NextUpdateTime = CurrentTime + (Uint32)m_UpdatePeriod + static_cast(random(num)); return true; } return false; }