]> git.lizzy.rs Git - dragonfireclient.git/blob - src/environment.cpp
Merge branch 'master' of https://github.com/minetest/minetest
[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 "raycast.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         m_cache_abm_time_budget = g_settings->getFloat("abm_time_budget");
40
41         m_time_of_day = g_settings->getU32("world_start_time");
42         m_time_of_day_f = (float)m_time_of_day / 24000.0f;
43 }
44
45 u32 Environment::getDayNightRatio()
46 {
47         MutexAutoLock lock(this->m_time_lock);
48         if (g_settings->getBool("no_night"))
49                 return time_to_daynight_ratio(12000, m_cache_enable_shaders);
50         if (m_enable_day_night_ratio_override)
51                 return m_day_night_ratio_override;
52         return time_to_daynight_ratio(m_time_of_day_f * 24000, m_cache_enable_shaders);
53 }
54
55 void Environment::setTimeOfDaySpeed(float speed)
56 {
57         m_time_of_day_speed = speed;
58 }
59
60 void Environment::setDayNightRatioOverride(bool enable, u32 value)
61 {
62         MutexAutoLock lock(this->m_time_lock);
63         m_enable_day_night_ratio_override = enable;
64         m_day_night_ratio_override = value;
65 }
66
67 void Environment::setTimeOfDay(u32 time)
68 {
69         MutexAutoLock lock(this->m_time_lock);
70         if (m_time_of_day > time)
71                 ++m_day_count;
72         m_time_of_day = time;
73         m_time_of_day_f = (float)time / 24000.0;
74 }
75
76 u32 Environment::getTimeOfDay()
77 {
78         MutexAutoLock lock(this->m_time_lock);
79         return m_time_of_day;
80 }
81
82 float Environment::getTimeOfDayF()
83 {
84         MutexAutoLock lock(this->m_time_lock);
85         return m_time_of_day_f;
86 }
87
88 bool Environment::line_of_sight(v3f pos1, v3f pos2, v3s16 *p)
89 {
90         // Iterate trough nodes on the line
91         voxalgo::VoxelLineIterator iterator(pos1 / BS, (pos2 - pos1) / BS);
92         do {
93                 MapNode n = getMap().getNode(iterator.m_current_node_pos);
94
95                 // Return non-air
96                 if (n.param0 != CONTENT_AIR) {
97                         if (p)
98                                 *p = iterator.m_current_node_pos;
99                         return false;
100                 }
101                 iterator.next();
102         } while (iterator.m_current_index <= iterator.m_last_index);
103         return true;
104 }
105
106 /*
107         Check if a node is pointable
108 */
109 inline static bool isPointableNode(const MapNode &n,
110         const NodeDefManager *nodedef , bool liquids_pointable, bool nodes_pointable)
111 {
112         if (! nodes_pointable)
113                 return false;
114         const ContentFeatures &features = nodedef->get(n);
115         return features.pointable ||
116                ((liquids_pointable || g_settings->getBool("point_liquids")) && features.isLiquid());
117 }
118
119 void Environment::continueRaycast(RaycastState *state, PointedThing *result)
120 {
121         const NodeDefManager *nodedef = getMap().getNodeDefManager();
122         if (state->m_initialization_needed) {
123                 // Add objects
124                 if (state->m_objects_pointable) {
125                         std::vector<PointedThing> found;
126                         getSelectedActiveObjects(state->m_shootline, found);
127                         for (const PointedThing &pointed : found) {
128                                 state->m_found.push(pointed);
129                         }
130                 }
131                 // Set search range
132                 core::aabbox3d<s16> maximal_exceed = nodedef->getSelectionBoxIntUnion();
133                 state->m_search_range.MinEdge = -maximal_exceed.MaxEdge;
134                 state->m_search_range.MaxEdge = -maximal_exceed.MinEdge;
135                 // Setting is done
136                 state->m_initialization_needed = false;
137         }
138
139         // The index of the first pointed thing that was not returned
140         // before. The last index which needs to be tested.
141         s16 lastIndex = state->m_iterator.m_last_index;
142         if (!state->m_found.empty()) {
143                 lastIndex = state->m_iterator.getIndex(
144                         floatToInt(state->m_found.top().intersection_point, BS));
145         }
146
147         Map &map = getMap();
148         // If a node is found, this is the center of the
149         // first nodebox the shootline meets.
150         v3f found_boxcenter(0, 0, 0);
151         // The untested nodes are in this range.
152         core::aabbox3d<s16> new_nodes;
153         while (state->m_iterator.m_current_index <= lastIndex) {
154                 // Test the nodes around the current node in search_range.
155                 new_nodes = state->m_search_range;
156                 new_nodes.MinEdge += state->m_iterator.m_current_node_pos;
157                 new_nodes.MaxEdge += state->m_iterator.m_current_node_pos;
158
159                 // Only check new nodes
160                 v3s16 delta = state->m_iterator.m_current_node_pos
161                         - state->m_previous_node;
162                 if (delta.X > 0) {
163                         new_nodes.MinEdge.X = new_nodes.MaxEdge.X;
164                 } else if (delta.X < 0) {
165                         new_nodes.MaxEdge.X = new_nodes.MinEdge.X;
166                 } else if (delta.Y > 0) {
167                         new_nodes.MinEdge.Y = new_nodes.MaxEdge.Y;
168                 } else if (delta.Y < 0) {
169                         new_nodes.MaxEdge.Y = new_nodes.MinEdge.Y;
170                 } else if (delta.Z > 0) {
171                         new_nodes.MinEdge.Z = new_nodes.MaxEdge.Z;
172                 } else if (delta.Z < 0) {
173                         new_nodes.MaxEdge.Z = new_nodes.MinEdge.Z;
174                 }
175
176                 if (new_nodes.MaxEdge.X == S16_MAX ||
177                         new_nodes.MaxEdge.Y == S16_MAX ||
178                         new_nodes.MaxEdge.Z == S16_MAX) {
179                         break; // About to go out of bounds
180                 }
181
182                 // For each untested node
183                 for (s16 x = new_nodes.MinEdge.X; x <= new_nodes.MaxEdge.X; x++)
184                 for (s16 y = new_nodes.MinEdge.Y; y <= new_nodes.MaxEdge.Y; y++)
185                 for (s16 z = new_nodes.MinEdge.Z; z <= new_nodes.MaxEdge.Z; z++) {
186                         MapNode n;
187                         v3s16 np(x, y, z);
188                         bool is_valid_position;
189
190                         n = map.getNode(np, &is_valid_position);
191                         if (!(is_valid_position && isPointableNode(n, nodedef,
192                                         state->m_liquids_pointable, state->m_nodes_pointable))) {
193                                 continue;
194                         }
195
196                         PointedThing result;
197
198                         std::vector<aabb3f> boxes;
199                         n.getSelectionBoxes(nodedef, &boxes,
200                                 n.getNeighbors(np, &map));
201
202                         // Is there a collision with a selection box?
203                         bool is_colliding = false;
204                         // Minimal distance of all collisions
205                         float min_distance_sq = 10000000;
206                         // ID of the current box (loop counter)
207                         u16 id = 0;
208
209                         v3f npf = intToFloat(np, BS);
210                         // This loop translates the boxes to their in-world place.
211                         for (aabb3f &box : boxes) {
212                                 box.MinEdge += npf;
213                                 box.MaxEdge += npf;
214
215                                 v3f intersection_point;
216                                 v3s16 intersection_normal;
217                                 if (!boxLineCollision(box, state->m_shootline.start,
218                                                 state->m_shootline.getVector(), &intersection_point,
219                                                 &intersection_normal)) {
220                                         ++id;
221                                         continue;
222                                 }
223
224                                 f32 distanceSq = (intersection_point
225                                         - state->m_shootline.start).getLengthSQ();
226                                 // If this is the nearest collision, save it
227                                 if (min_distance_sq > distanceSq) {
228                                         min_distance_sq = distanceSq;
229                                         result.intersection_point = intersection_point;
230                                         result.intersection_normal = intersection_normal;
231                                         result.box_id = id;
232                                         found_boxcenter = box.getCenter();
233                                         is_colliding = true;
234                                 }
235                                 ++id;
236                         }
237                         // If there wasn't a collision, stop
238                         if (!is_colliding) {
239                                 continue;
240                         }
241                         result.type = POINTEDTHING_NODE;
242                         result.node_undersurface = np;
243                         result.distanceSq = min_distance_sq;
244                         // Set undersurface and abovesurface nodes
245                         f32 d = 0.002 * BS;
246                         v3f fake_intersection = result.intersection_point;
247                         // Move intersection towards its source block.
248                         if (fake_intersection.X < found_boxcenter.X) {
249                                 fake_intersection.X += d;
250                         } else {
251                                 fake_intersection.X -= d;
252                         }
253                         if (fake_intersection.Y < found_boxcenter.Y) {
254                                 fake_intersection.Y += d;
255                         } else {
256                                 fake_intersection.Y -= d;
257                         }
258                         if (fake_intersection.Z < found_boxcenter.Z) {
259                                 fake_intersection.Z += d;
260                         } else {
261                                 fake_intersection.Z -= d;
262                         }
263                         result.node_real_undersurface = floatToInt(
264                                 fake_intersection, BS);
265                         result.node_abovesurface = result.node_real_undersurface
266                                 + result.intersection_normal;
267                         // Push found PointedThing
268                         state->m_found.push(result);
269                         // If this is nearer than the old nearest object,
270                         // the search can be shorter
271                         s16 newIndex = state->m_iterator.getIndex(
272                                 result.node_real_undersurface);
273                         if (newIndex < lastIndex) {
274                                 lastIndex = newIndex;
275                         }
276                 }
277                 // Next node
278                 state->m_previous_node = state->m_iterator.m_current_node_pos;
279                 state->m_iterator.next();
280         }
281         // Return empty PointedThing if nothing left on the ray
282         if (state->m_found.empty()) {
283                 result->type = POINTEDTHING_NOTHING;
284         } else {
285                 *result = state->m_found.top();
286                 state->m_found.pop();
287         }
288 }
289
290 void Environment::stepTimeOfDay(float dtime)
291 {
292         MutexAutoLock lock(this->m_time_lock);
293
294         // Cached in order to prevent the two reads we do to give
295         // different results (can be written by code not under the lock)
296         f32 cached_time_of_day_speed = m_time_of_day_speed;
297
298         f32 speed = cached_time_of_day_speed * 24000. / (24. * 3600);
299         m_time_conversion_skew += dtime;
300         u32 units = (u32)(m_time_conversion_skew * speed);
301         bool sync_f = false;
302         if (units > 0) {
303                 // Sync at overflow
304                 if (m_time_of_day + units >= 24000) {
305                         sync_f = true;
306                         ++m_day_count;
307                 }
308                 m_time_of_day = (m_time_of_day + units) % 24000;
309                 if (sync_f)
310                         m_time_of_day_f = (float)m_time_of_day / 24000.0;
311         }
312         if (speed > 0) {
313                 m_time_conversion_skew -= (f32)units / speed;
314         }
315         if (!sync_f) {
316                 m_time_of_day_f += cached_time_of_day_speed / 24 / 3600 * dtime;
317                 if (m_time_of_day_f > 1.0)
318                         m_time_of_day_f -= 1.0;
319                 if (m_time_of_day_f < 0.0)
320                         m_time_of_day_f += 1.0;
321         }
322 }
323
324 u32 Environment::getDayCount()
325 {
326         // Atomic<u32> counter
327         return m_day_count;
328 }