mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
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.
This commit is contained in:
parent
f9b09368f0
commit
8f03995604
3 changed files with 78 additions and 49 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue