diff options
author | est31 <MTest31@outlook.com> | 2015-11-04 03:07:32 +0100 |
---|---|---|
committer | est31 <MTest31@outlook.com> | 2015-11-04 03:44:09 +0100 |
commit | 8f03995604164c891cacb41c83ff4d6e73a8535f (patch) | |
tree | 0e03e91c9ef2214122e73aab5411f529d1d0f699 /src/environment.cpp | |
parent | f9b09368f063cdace93a042d5bdd45987c084d94 (diff) | |
download | minetest-8f03995604164c891cacb41c83ff4d6e73a8535f.tar.gz minetest-8f03995604164c891cacb41c83ff4d6e73a8535f.tar.bz2 minetest-8f03995604164c891cacb41c83ff4d6e73a8535f.zip |
Time: use locks again
The Atomic implementation was only partially correct, and was very complex.
Use locks for sake of simplicity, following KISS principle.
Only remaining atomic operation use is time of day speed, because that
really is only read + written.
Also fixes a bug with m_time_conversion_skew only being decremented, never
incremented (Regresion from previous commit).
atomic.h changes:
* Add GenericAtomic<T> class for non-integral types like floats.
* Remove some last remainders from atomic.h of the volatile use.
Diffstat (limited to 'src/environment.cpp')
-rw-r--r-- | src/environment.cpp | 37 |
1 files changed, 23 insertions, 14 deletions
diff --git a/src/environment.cpp b/src/environment.cpp index 2a42f18ad..76b7ec1e0 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -49,11 +49,12 @@ with this program; if not, write to the Free Software Foundation, Inc., #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")" Environment::Environment(): + m_time_of_day_speed(0), m_time_of_day(9000), m_time_of_day_f(9000./24000), - m_time_of_day_speed(0), - m_time_conversion_skew(0), - m_day_night_ratio_override_storage(0) + m_time_conversion_skew(0.0f), + m_enable_day_night_ratio_override(false), + m_day_night_ratio_override(0.0f) { m_cache_enable_shaders = g_settings->getBool("enable_shaders"); } @@ -179,10 +180,9 @@ std::vector<Player*> Environment::getPlayers(bool ignore_disconnected) u32 Environment::getDayNightRatio() { - u64 day_night_st = m_day_night_ratio_override_storage; - if (day_night_st & ((u64)1 << 63)) - return day_night_st & U32_MAX; - MutexAutoLock lock(this->m_time_floats_lock); + MutexAutoLock lock(this->m_time_lock); + if (m_enable_day_night_ratio_override) + return m_day_night_ratio_override; return time_to_daynight_ratio(m_time_of_day_f * 24000, m_cache_enable_shaders); } @@ -196,29 +196,38 @@ float Environment::getTimeOfDaySpeed() return m_time_of_day_speed; } +void Environment::setDayNightRatioOverride(bool enable, u32 value) +{ + MutexAutoLock lock(this->m_time_lock); + m_enable_day_night_ratio_override = enable; + m_day_night_ratio_override = value; +} + void Environment::setTimeOfDay(u32 time) { - MutexAutoLock lock(this->m_time_floats_lock); + MutexAutoLock lock(this->m_time_lock); m_time_of_day = time; m_time_of_day_f = (float)time / 24000.0; } u32 Environment::getTimeOfDay() { + MutexAutoLock lock(this->m_time_lock); return m_time_of_day; } float Environment::getTimeOfDayF() { - MutexAutoLock lock(this->m_time_floats_lock); + MutexAutoLock lock(this->m_time_lock); return m_time_of_day_f; } void Environment::stepTimeOfDay(float dtime) { - MutexAutoLock lock(this->m_time_floats_lock); + MutexAutoLock lock(this->m_time_lock); f32 speed = m_time_of_day_speed * 24000. / (24. * 3600); - u32 units = (u32)((dtime + m_time_conversion_skew) * speed); + m_time_conversion_skew += dtime; + u32 units = (u32)(m_time_conversion_skew * speed); bool sync_f = false; if (units > 0) { // Sync at overflow @@ -232,7 +241,7 @@ void Environment::stepTimeOfDay(float dtime) m_time_conversion_skew -= (f32)units / speed; } if (!sync_f) { - m_time_of_day_f += m_time_of_day_speed / (24. * 3600.) * dtime; + m_time_of_day_f += speed * dtime; if (m_time_of_day_f > 1.0) m_time_of_day_f -= 1.0; if (m_time_of_day_f < 0.0) @@ -527,10 +536,10 @@ void ServerEnvironment::loadMeta() } try { - m_time_of_day = args.getU64("time_of_day"); + setTimeOfDay(args.getU64("time_of_day")); } catch (SettingNotFoundException &e) { // This is not as important - m_time_of_day = 9000; + setTimeOfDay(9000); } } |