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