From ca8e56c15a26bc5f3d1dffe5fd39e1ca4b82d6f8 Mon Sep 17 00:00:00 2001 From: est31 Date: Tue, 27 Oct 2015 21:03:59 +0100 Subject: Environment: Time of day fixes and add serverside getter -> Put access to time variables under the time lock. -> Merge both time locks, there is no point to have two locks. -> Fix the lock being released too early in Environment::setTimeOfDay -> Add serverside getter so that you don't have to get the environment if you only have the server --- src/environment.cpp | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'src/environment.cpp') diff --git a/src/environment.cpp b/src/environment.cpp index 0b37212e5..c15fe795e 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -180,20 +180,21 @@ std::vector Environment::getPlayers(bool ignore_disconnected) u32 Environment::getDayNightRatio() { - if(m_enable_day_night_ratio_override) + MutexAutoLock(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); + return time_to_daynight_ratio(m_time_of_day_f * 24000, m_cache_enable_shaders); } void Environment::setTimeOfDaySpeed(float speed) { - MutexAutoLock(this->m_timeofday_lock); + MutexAutoLock(this->m_time_lock); m_time_of_day_speed = speed; } float Environment::getTimeOfDaySpeed() { - MutexAutoLock(this->m_timeofday_lock); + MutexAutoLock(this->m_time_lock); float retval = m_time_of_day_speed; return retval; } @@ -221,29 +222,28 @@ float Environment::getTimeOfDayF() void Environment::stepTimeOfDay(float dtime) { - // getTimeOfDaySpeed lock the value we need to prevent MT problems - float day_speed = getTimeOfDaySpeed(); + MutexAutoLock(this->m_time_lock); m_time_counter += dtime; - f32 speed = day_speed * 24000./(24.*3600); - u32 units = (u32)(m_time_counter*speed); + f32 speed = m_time_of_day_speed * 24000. / (24. * 3600); + u32 units = (u32)(m_time_counter * speed); bool sync_f = false; - if(units > 0){ + if (units > 0) { // Sync at overflow - if(m_time_of_day + units >= 24000) + if (m_time_of_day + units >= 24000) sync_f = true; m_time_of_day = (m_time_of_day + units) % 24000; - if(sync_f) + if (sync_f) m_time_of_day_f = (float)m_time_of_day / 24000.0; } if (speed > 0) { m_time_counter -= (f32)units / speed; } - if(!sync_f){ - m_time_of_day_f += day_speed/24/3600*dtime; - if(m_time_of_day_f > 1.0) + if (!sync_f) { + m_time_of_day_f += m_time_of_day_speed / 24 / 3600 * dtime; + if (m_time_of_day_f > 1.0) m_time_of_day_f -= 1.0; - if(m_time_of_day_f < 0.0) + if (m_time_of_day_f < 0.0) m_time_of_day_f += 1.0; } } -- cgit v1.2.3