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