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