mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
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
This commit is contained in:
parent
c406438b95
commit
ca8e56c15a
4 changed files with 31 additions and 17 deletions
|
@ -180,20 +180,21 @@ std::vector<Player*> 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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue