3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "environment.h"
22 #include "collision.h"
23 #include "serverobject.h"
24 #include "scripting_server.h"
26 #include "daynightratio.h"
30 Environment::Environment(IGameDef *gamedef):
31 m_time_of_day_speed(0),
33 m_time_of_day_f(9000./24000),
34 m_time_conversion_skew(0.0f),
35 m_enable_day_night_ratio_override(false),
36 m_day_night_ratio_override(0.0f),
39 m_cache_enable_shaders = g_settings->getBool("enable_shaders");
40 m_cache_active_block_mgmt_interval = g_settings->getFloat("active_block_mgmt_interval");
41 m_cache_abm_interval = g_settings->getFloat("abm_interval");
42 m_cache_nodetimer_interval = g_settings->getFloat("nodetimer_interval");
45 Environment::~Environment()
49 u32 Environment::getDayNightRatio()
51 MutexAutoLock lock(this->m_time_lock);
52 if (m_enable_day_night_ratio_override)
53 return m_day_night_ratio_override;
54 return time_to_daynight_ratio(m_time_of_day_f * 24000, m_cache_enable_shaders);
57 void Environment::setTimeOfDaySpeed(float speed)
59 m_time_of_day_speed = speed;
62 void Environment::setDayNightRatioOverride(bool enable, u32 value)
64 MutexAutoLock lock(this->m_time_lock);
65 m_enable_day_night_ratio_override = enable;
66 m_day_night_ratio_override = value;
69 void Environment::setTimeOfDay(u32 time)
71 MutexAutoLock lock(this->m_time_lock);
72 if (m_time_of_day > time)
75 m_time_of_day_f = (float)time / 24000.0;
78 u32 Environment::getTimeOfDay()
80 MutexAutoLock lock(this->m_time_lock);
84 float Environment::getTimeOfDayF()
86 MutexAutoLock lock(this->m_time_lock);
87 return m_time_of_day_f;
90 void Environment::stepTimeOfDay(float dtime)
92 MutexAutoLock lock(this->m_time_lock);
94 // Cached in order to prevent the two reads we do to give
95 // different results (can be written by code not under the lock)
96 f32 cached_time_of_day_speed = m_time_of_day_speed;
98 f32 speed = cached_time_of_day_speed * 24000. / (24. * 3600);
99 m_time_conversion_skew += dtime;
100 u32 units = (u32)(m_time_conversion_skew * speed);
104 if (m_time_of_day + units >= 24000) {
108 m_time_of_day = (m_time_of_day + units) % 24000;
110 m_time_of_day_f = (float)m_time_of_day / 24000.0;
113 m_time_conversion_skew -= (f32)units / speed;
116 m_time_of_day_f += cached_time_of_day_speed / 24 / 3600 * dtime;
117 if (m_time_of_day_f > 1.0)
118 m_time_of_day_f -= 1.0;
119 if (m_time_of_day_f < 0.0)
120 m_time_of_day_f += 1.0;
124 u32 Environment::getDayCount()
126 // Atomic<u32> counter