environment.cpp 8.8 KB

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