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