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