environment.cpp 9.2 KB

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