3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
21 #include "environment.h"
22 #include "collision.h"
24 #include "serverobject.h"
25 #include "scripting_server.h"
27 #include "daynightratio.h"
31 Environment::Environment(IGameDef *gamedef):
32 m_time_of_day_speed(0.0f),
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");
42 u32 Environment::getDayNightRatio()
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);
50 void Environment::setTimeOfDaySpeed(float speed)
52 m_time_of_day_speed = speed;
55 void Environment::setDayNightRatioOverride(bool enable, u32 value)
57 MutexAutoLock lock(this->m_time_lock);
58 m_enable_day_night_ratio_override = enable;
59 m_day_night_ratio_override = value;
62 void Environment::setTimeOfDay(u32 time)
64 MutexAutoLock lock(this->m_time_lock);
65 if (m_time_of_day > time)
68 m_time_of_day_f = (float)time / 24000.0;
71 u32 Environment::getTimeOfDay()
73 MutexAutoLock lock(this->m_time_lock);
77 float Environment::getTimeOfDayF()
79 MutexAutoLock lock(this->m_time_lock);
80 return m_time_of_day_f;
84 Check if a node is pointable
86 inline static bool isPointableNode(const MapNode &n,
87 INodeDefManager *nodedef , bool liquids_pointable)
89 const ContentFeatures &features = nodedef->get(n);
90 return features.pointable ||
91 (liquids_pointable && features.isLiquid());
94 void Environment::continueRaycast(RaycastState *state, PointedThing *result)
96 INodeDefManager *nodedef = getMap().getNodeDefManager();
97 if (state->m_initialization_needed) {
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);
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;
111 state->m_initialization_needed = false;
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));
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;
134 // Only check new nodes
135 v3s16 delta = state->m_iterator.m_current_node_pos
136 - state->m_previous_node;
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;
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++) {
157 bool is_valid_position;
159 n = map.getNodeNoEx(np, &is_valid_position);
160 if (!(is_valid_position && isPointableNode(n, nodedef,
161 state->m_liquids_pointable))) {
167 std::vector<aabb3f> boxes;
168 n.getSelectionBoxes(nodedef, &boxes,
169 n.getNeighbors(np, &map));
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;
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
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))
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();
202 // If there wasn't a collision, stop
206 result.type = POINTEDTHING_NODE;
207 result.node_undersurface = np;
208 result.distanceSq = min_distance_sq;
209 // Set undersurface and abovesurface nodes
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;
216 fake_intersection.X -= d;
218 if (fake_intersection.Y < found_boxcenter.Y) {
219 fake_intersection.Y += d;
221 fake_intersection.Y -= d;
223 if (fake_intersection.Z < found_boxcenter.Z) {
224 fake_intersection.Z += d;
226 fake_intersection.Z -= d;
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;
243 state->m_previous_node = state->m_iterator.m_current_node_pos;
244 state->m_iterator.next();
246 // Return empty PointedThing if nothing left on the ray
247 if (state->m_found.empty()) {
248 result->type = POINTEDTHING_NOTHING;
250 *result = state->m_found.top();
251 state->m_found.pop();
255 void Environment::stepTimeOfDay(float dtime)
257 MutexAutoLock lock(this->m_time_lock);
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;
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);
269 if (m_time_of_day + units >= 24000) {
273 m_time_of_day = (m_time_of_day + units) % 24000;
275 m_time_of_day_f = (float)m_time_of_day / 24000.0;
278 m_time_conversion_skew -= (f32)units / speed;
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;
289 u32 Environment::getDayCount()
291 // Atomic<u32> counter