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