Fog effect when camera is inside cloud
[oweals/minetest.git] / src / environment.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include <fstream>
21 #include "environment.h"
22 #include "collision.h"
23 #include "serverobject.h"
24 #include "scripting_server.h"
25 #include "server.h"
26 #include "daynightratio.h"
27 #include "emerge.h"
28
29
30 Environment::Environment(IGameDef *gamedef):
31         m_time_of_day_speed(0.0f),
32         m_day_count(0),
33         m_gamedef(gamedef)
34 {
35         m_cache_enable_shaders = g_settings->getBool("enable_shaders");
36         m_cache_active_block_mgmt_interval = g_settings->getFloat("active_block_mgmt_interval");
37         m_cache_abm_interval = g_settings->getFloat("abm_interval");
38         m_cache_nodetimer_interval = g_settings->getFloat("nodetimer_interval");
39 }
40
41 Environment::~Environment()
42 {
43 }
44
45 u32 Environment::getDayNightRatio()
46 {
47         MutexAutoLock lock(this->m_time_lock);
48         if (m_enable_day_night_ratio_override)
49                 return m_day_night_ratio_override;
50         return time_to_daynight_ratio(m_time_of_day_f * 24000, m_cache_enable_shaders);
51 }
52
53 void Environment::setTimeOfDaySpeed(float speed)
54 {
55         m_time_of_day_speed = speed;
56 }
57
58 void Environment::setDayNightRatioOverride(bool enable, u32 value)
59 {
60         MutexAutoLock lock(this->m_time_lock);
61         m_enable_day_night_ratio_override = enable;
62         m_day_night_ratio_override = value;
63 }
64
65 void Environment::setTimeOfDay(u32 time)
66 {
67         MutexAutoLock lock(this->m_time_lock);
68         if (m_time_of_day > time)
69                 ++m_day_count;
70         m_time_of_day = time;
71         m_time_of_day_f = (float)time / 24000.0;
72 }
73
74 u32 Environment::getTimeOfDay()
75 {
76         MutexAutoLock lock(this->m_time_lock);
77         return m_time_of_day;
78 }
79
80 float Environment::getTimeOfDayF()
81 {
82         MutexAutoLock lock(this->m_time_lock);
83         return m_time_of_day_f;
84 }
85
86 void Environment::stepTimeOfDay(float dtime)
87 {
88         MutexAutoLock lock(this->m_time_lock);
89
90         // Cached in order to prevent the two reads we do to give
91         // different results (can be written by code not under the lock)
92         f32 cached_time_of_day_speed = m_time_of_day_speed;
93
94         f32 speed = cached_time_of_day_speed * 24000. / (24. * 3600);
95         m_time_conversion_skew += dtime;
96         u32 units = (u32)(m_time_conversion_skew * speed);
97         bool sync_f = false;
98         if (units > 0) {
99                 // Sync at overflow
100                 if (m_time_of_day + units >= 24000) {
101                         sync_f = true;
102                         ++m_day_count;
103                 }
104                 m_time_of_day = (m_time_of_day + units) % 24000;
105                 if (sync_f)
106                         m_time_of_day_f = (float)m_time_of_day / 24000.0;
107         }
108         if (speed > 0) {
109                 m_time_conversion_skew -= (f32)units / speed;
110         }
111         if (!sync_f) {
112                 m_time_of_day_f += cached_time_of_day_speed / 24 / 3600 * dtime;
113                 if (m_time_of_day_f > 1.0)
114                         m_time_of_day_f -= 1.0;
115                 if (m_time_of_day_f < 0.0)
116                         m_time_of_day_f += 1.0;
117         }
118 }
119
120 u32 Environment::getDayCount()
121 {
122         // Atomic<u32> counter
123         return m_day_count;
124 }