environment.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include <fstream>
  17. #include "environment.h"
  18. #include "collision.h"
  19. #include "raycast.h"
  20. #include "serverobject.h"
  21. #include "scripting_server.h"
  22. #include "server.h"
  23. #include "daynightratio.h"
  24. #include "emerge.h"
  25. Environment::Environment(IGameDef *gamedef):
  26. m_time_of_day_speed(0.0f),
  27. m_day_count(0),
  28. m_gamedef(gamedef)
  29. {
  30. m_cache_enable_shaders = g_settings->getBool("enable_shaders");
  31. m_cache_active_block_mgmt_interval = g_settings->getFloat("active_block_mgmt_interval");
  32. m_cache_abm_interval = g_settings->getFloat("abm_interval");
  33. m_cache_nodetimer_interval = g_settings->getFloat("nodetimer_interval");
  34. }
  35. u32 Environment::getDayNightRatio()
  36. {
  37. MutexAutoLock lock(this->m_time_lock);
  38. if (m_enable_day_night_ratio_override)
  39. return m_day_night_ratio_override;
  40. return time_to_daynight_ratio(m_time_of_day_f * 24000, m_cache_enable_shaders);
  41. }
  42. void Environment::setTimeOfDaySpeed(float speed)
  43. {
  44. m_time_of_day_speed = speed;
  45. }
  46. void Environment::setDayNightRatioOverride(bool enable, u32 value)
  47. {
  48. MutexAutoLock lock(this->m_time_lock);
  49. m_enable_day_night_ratio_override = enable;
  50. m_day_night_ratio_override = value;
  51. }
  52. void Environment::setTimeOfDay(u32 time)
  53. {
  54. MutexAutoLock lock(this->m_time_lock);
  55. if (m_time_of_day > time)
  56. ++m_day_count;
  57. m_time_of_day = time;
  58. m_time_of_day_f = (float)time / 24000.0;
  59. }
  60. u32 Environment::getTimeOfDay()
  61. {
  62. MutexAutoLock lock(this->m_time_lock);
  63. return m_time_of_day;
  64. }
  65. float Environment::getTimeOfDayF()
  66. {
  67. MutexAutoLock lock(this->m_time_lock);
  68. return m_time_of_day_f;
  69. }
  70. /*
  71. Check if a node is pointable
  72. */
  73. inline static bool isPointableNode(const MapNode &n,
  74. INodeDefManager *nodedef , bool liquids_pointable)
  75. {
  76. const ContentFeatures &features = nodedef->get(n);
  77. return features.pointable ||
  78. (liquids_pointable && features.isLiquid());
  79. }
  80. void Environment::continueRaycast(RaycastState *state, PointedThing *result)
  81. {
  82. INodeDefManager *nodedef = getMap().getNodeDefManager();
  83. if (state->m_initialization_needed) {
  84. // Add objects
  85. if (state->m_objects_pointable) {
  86. std::vector<PointedThing> found;
  87. getSelectedActiveObjects(state->m_shootline, found);
  88. for (const PointedThing &pointed : found) {
  89. state->m_found.push(pointed);
  90. }
  91. }
  92. // Set search range
  93. core::aabbox3d<s16> maximal_exceed = nodedef->getSelectionBoxIntUnion();
  94. state->m_search_range.MinEdge = -maximal_exceed.MaxEdge;
  95. state->m_search_range.MaxEdge = -maximal_exceed.MinEdge;
  96. // Setting is done
  97. state->m_initialization_needed = false;
  98. }
  99. // The index of the first pointed thing that was not returned
  100. // before. The last index which needs to be tested.
  101. s16 lastIndex = state->m_iterator.m_last_index;
  102. if (!state->m_found.empty()) {
  103. lastIndex = state->m_iterator.getIndex(
  104. floatToInt(state->m_found.top().intersection_point, BS));
  105. }
  106. Map &map = getMap();
  107. // If a node is found, this is the center of the
  108. // first nodebox the shootline meets.
  109. v3f found_boxcenter(0, 0, 0);
  110. // The untested nodes are in this range.
  111. core::aabbox3d<s16> new_nodes;
  112. while (state->m_iterator.m_current_index <= lastIndex) {
  113. // Test the nodes around the current node in search_range.
  114. new_nodes = state->m_search_range;
  115. new_nodes.MinEdge += state->m_iterator.m_current_node_pos;
  116. new_nodes.MaxEdge += state->m_iterator.m_current_node_pos;
  117. // Only check new nodes
  118. v3s16 delta = state->m_iterator.m_current_node_pos
  119. - state->m_previous_node;
  120. if (delta.X > 0) {
  121. new_nodes.MinEdge.X = new_nodes.MaxEdge.X;
  122. } else if (delta.X < 0) {
  123. new_nodes.MaxEdge.X = new_nodes.MinEdge.X;
  124. } else if (delta.Y > 0) {
  125. new_nodes.MinEdge.Y = new_nodes.MaxEdge.Y;
  126. } else if (delta.Y < 0) {
  127. new_nodes.MaxEdge.Y = new_nodes.MinEdge.Y;
  128. } else if (delta.Z > 0) {
  129. new_nodes.MinEdge.Z = new_nodes.MaxEdge.Z;
  130. } else if (delta.Z < 0) {
  131. new_nodes.MaxEdge.Z = new_nodes.MinEdge.Z;
  132. }
  133. // For each untested node
  134. for (s16 x = new_nodes.MinEdge.X; x <= new_nodes.MaxEdge.X; x++)
  135. for (s16 y = new_nodes.MinEdge.Y; y <= new_nodes.MaxEdge.Y; y++)
  136. for (s16 z = new_nodes.MinEdge.Z; z <= new_nodes.MaxEdge.Z; z++) {
  137. MapNode n;
  138. v3s16 np(x, y, z);
  139. bool is_valid_position;
  140. n = map.getNodeNoEx(np, &is_valid_position);
  141. if (!(is_valid_position && isPointableNode(n, nodedef,
  142. state->m_liquids_pointable))) {
  143. continue;
  144. }
  145. PointedThing result;
  146. std::vector<aabb3f> boxes;
  147. n.getSelectionBoxes(nodedef, &boxes,
  148. n.getNeighbors(np, &map));
  149. // Is there a collision with a selection box?
  150. bool is_colliding = false;
  151. // Minimal distance of all collisions
  152. float min_distance_sq = 10000000;
  153. v3f npf = intToFloat(np, BS);
  154. for (std::vector<aabb3f>::const_iterator i = boxes.begin();
  155. i != boxes.end(); ++i) {
  156. // Get current collision box
  157. aabb3f box = *i;
  158. box.MinEdge += npf;
  159. box.MaxEdge += npf;
  160. v3f intersection_point;
  161. v3s16 intersection_normal;
  162. if (!boxLineCollision(box, state->m_shootline.start,
  163. state->m_shootline.getVector(), &intersection_point,
  164. &intersection_normal))
  165. continue;
  166. f32 distanceSq = (intersection_point
  167. - state->m_shootline.start).getLengthSQ();
  168. // If this is the nearest collision, save it
  169. if (min_distance_sq > distanceSq) {
  170. min_distance_sq = distanceSq;
  171. result.intersection_point = intersection_point;
  172. result.intersection_normal = intersection_normal;
  173. found_boxcenter = box.getCenter();
  174. is_colliding = true;
  175. }
  176. }
  177. // If there wasn't a collision, stop
  178. if (!is_colliding) {
  179. continue;
  180. }
  181. result.type = POINTEDTHING_NODE;
  182. result.node_undersurface = np;
  183. result.distanceSq = min_distance_sq;
  184. // Set undersurface and abovesurface nodes
  185. f32 d = 0.002 * BS;
  186. v3f fake_intersection = result.intersection_point;
  187. // Move intersection towards its source block.
  188. if (fake_intersection.X < found_boxcenter.X) {
  189. fake_intersection.X += d;
  190. } else {
  191. fake_intersection.X -= d;
  192. }
  193. if (fake_intersection.Y < found_boxcenter.Y) {
  194. fake_intersection.Y += d;
  195. } else {
  196. fake_intersection.Y -= d;
  197. }
  198. if (fake_intersection.Z < found_boxcenter.Z) {
  199. fake_intersection.Z += d;
  200. } else {
  201. fake_intersection.Z -= d;
  202. }
  203. result.node_real_undersurface = floatToInt(
  204. fake_intersection, BS);
  205. result.node_abovesurface = result.node_real_undersurface
  206. + result.intersection_normal;
  207. // Push found PointedThing
  208. state->m_found.push(result);
  209. // If this is nearer than the old nearest object,
  210. // the search can be shorter
  211. s16 newIndex = state->m_iterator.getIndex(
  212. result.node_real_undersurface);
  213. if (newIndex < lastIndex) {
  214. lastIndex = newIndex;
  215. }
  216. }
  217. // Next node
  218. state->m_previous_node = state->m_iterator.m_current_node_pos;
  219. state->m_iterator.next();
  220. }
  221. // Return empty PointedThing if nothing left on the ray
  222. if (state->m_found.empty()) {
  223. result->type = POINTEDTHING_NOTHING;
  224. } else {
  225. *result = state->m_found.top();
  226. state->m_found.pop();
  227. }
  228. }
  229. void Environment::stepTimeOfDay(float dtime)
  230. {
  231. MutexAutoLock lock(this->m_time_lock);
  232. // Cached in order to prevent the two reads we do to give
  233. // different results (can be written by code not under the lock)
  234. f32 cached_time_of_day_speed = m_time_of_day_speed;
  235. f32 speed = cached_time_of_day_speed * 24000. / (24. * 3600);
  236. m_time_conversion_skew += dtime;
  237. u32 units = (u32)(m_time_conversion_skew * speed);
  238. bool sync_f = false;
  239. if (units > 0) {
  240. // Sync at overflow
  241. if (m_time_of_day + units >= 24000) {
  242. sync_f = true;
  243. ++m_day_count;
  244. }
  245. m_time_of_day = (m_time_of_day + units) % 24000;
  246. if (sync_f)
  247. m_time_of_day_f = (float)m_time_of_day / 24000.0;
  248. }
  249. if (speed > 0) {
  250. m_time_conversion_skew -= (f32)units / speed;
  251. }
  252. if (!sync_f) {
  253. m_time_of_day_f += cached_time_of_day_speed / 24 / 3600 * dtime;
  254. if (m_time_of_day_f > 1.0)
  255. m_time_of_day_f -= 1.0;
  256. if (m_time_of_day_f < 0.0)
  257. m_time_of_day_f += 1.0;
  258. }
  259. }
  260. u32 Environment::getDayCount()
  261. {
  262. // Atomic<u32> counter
  263. return m_day_count;
  264. }