environment.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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(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(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(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(m_time_lock);
  65. return m_time_of_day;
  66. }
  67. float Environment::getTimeOfDayF()
  68. {
  69. MutexAutoLock lock(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. if (new_nodes.MaxEdge.X == S16_MAX ||
  152. new_nodes.MaxEdge.Y == S16_MAX ||
  153. new_nodes.MaxEdge.Z == S16_MAX) {
  154. break; // About to go out of bounds
  155. }
  156. // For each untested node
  157. for (s16 x = new_nodes.MinEdge.X; x <= new_nodes.MaxEdge.X; x++)
  158. for (s16 y = new_nodes.MinEdge.Y; y <= new_nodes.MaxEdge.Y; y++)
  159. for (s16 z = new_nodes.MinEdge.Z; z <= new_nodes.MaxEdge.Z; z++) {
  160. MapNode n;
  161. v3s16 np(x, y, z);
  162. bool is_valid_position;
  163. n = map.getNode(np, &is_valid_position);
  164. if (!(is_valid_position && isPointableNode(n, nodedef,
  165. state->m_liquids_pointable))) {
  166. continue;
  167. }
  168. PointedThing result;
  169. std::vector<aabb3f> boxes;
  170. n.getSelectionBoxes(nodedef, &boxes,
  171. n.getNeighbors(np, &map));
  172. // Is there a collision with a selection box?
  173. bool is_colliding = false;
  174. // Minimal distance of all collisions
  175. float min_distance_sq = 10000000;
  176. // ID of the current box (loop counter)
  177. u16 id = 0;
  178. // Do calculations relative to the node center
  179. // to translate the ray rather than the boxes
  180. v3f npf = intToFloat(np, BS);
  181. v3f rel_start = state->m_shootline.start - npf;
  182. for (aabb3f &box : boxes) {
  183. v3f intersection_point;
  184. v3f intersection_normal;
  185. if (!boxLineCollision(box, rel_start,
  186. state->m_shootline.getVector(), &intersection_point,
  187. &intersection_normal)) {
  188. ++id;
  189. continue;
  190. }
  191. intersection_point += npf; // translate back to world coords
  192. f32 distanceSq = (intersection_point
  193. - state->m_shootline.start).getLengthSQ();
  194. // If this is the nearest collision, save it
  195. if (min_distance_sq > distanceSq) {
  196. min_distance_sq = distanceSq;
  197. result.intersection_point = intersection_point;
  198. result.intersection_normal = intersection_normal;
  199. result.box_id = id;
  200. found_boxcenter = box.getCenter();
  201. is_colliding = true;
  202. }
  203. ++id;
  204. }
  205. // If there wasn't a collision, stop
  206. if (!is_colliding) {
  207. continue;
  208. }
  209. result.type = POINTEDTHING_NODE;
  210. result.node_undersurface = np;
  211. result.distanceSq = min_distance_sq;
  212. // Set undersurface and abovesurface nodes
  213. f32 d = 0.002 * BS;
  214. v3f fake_intersection = result.intersection_point;
  215. found_boxcenter += npf; // translate back to world coords
  216. // Move intersection towards its source block.
  217. if (fake_intersection.X < found_boxcenter.X) {
  218. fake_intersection.X += d;
  219. } else {
  220. fake_intersection.X -= d;
  221. }
  222. if (fake_intersection.Y < found_boxcenter.Y) {
  223. fake_intersection.Y += d;
  224. } else {
  225. fake_intersection.Y -= d;
  226. }
  227. if (fake_intersection.Z < found_boxcenter.Z) {
  228. fake_intersection.Z += d;
  229. } else {
  230. fake_intersection.Z -= d;
  231. }
  232. result.node_real_undersurface = floatToInt(
  233. fake_intersection, BS);
  234. result.node_abovesurface = result.node_real_undersurface
  235. + floatToInt(result.intersection_normal, 1.0f);
  236. // Push found PointedThing
  237. state->m_found.push(result);
  238. // If this is nearer than the old nearest object,
  239. // the search can be shorter
  240. s16 newIndex = state->m_iterator.getIndex(
  241. result.node_real_undersurface);
  242. if (newIndex < lastIndex) {
  243. lastIndex = newIndex;
  244. }
  245. }
  246. // Next node
  247. state->m_previous_node = state->m_iterator.m_current_node_pos;
  248. state->m_iterator.next();
  249. }
  250. // Return empty PointedThing if nothing left on the ray
  251. if (state->m_found.empty()) {
  252. result->type = POINTEDTHING_NOTHING;
  253. } else {
  254. *result = state->m_found.top();
  255. state->m_found.pop();
  256. }
  257. }
  258. void Environment::stepTimeOfDay(float dtime)
  259. {
  260. MutexAutoLock lock(this->m_time_lock);
  261. // Cached in order to prevent the two reads we do to give
  262. // different results (can be written by code not under the lock)
  263. f32 cached_time_of_day_speed = m_time_of_day_speed;
  264. f32 speed = cached_time_of_day_speed * 24000. / (24. * 3600);
  265. m_time_conversion_skew += dtime;
  266. u32 units = (u32)(m_time_conversion_skew * speed);
  267. bool sync_f = false;
  268. if (units > 0) {
  269. // Sync at overflow
  270. if (m_time_of_day + units >= 24000) {
  271. sync_f = true;
  272. ++m_day_count;
  273. }
  274. m_time_of_day = (m_time_of_day + units) % 24000;
  275. if (sync_f)
  276. m_time_of_day_f = (float)m_time_of_day / 24000.0;
  277. }
  278. if (speed > 0) {
  279. m_time_conversion_skew -= (f32)units / speed;
  280. }
  281. if (!sync_f) {
  282. m_time_of_day_f += cached_time_of_day_speed / 24 / 3600 * dtime;
  283. if (m_time_of_day_f > 1.0)
  284. m_time_of_day_f -= 1.0;
  285. if (m_time_of_day_f < 0.0)
  286. m_time_of_day_f += 1.0;
  287. }
  288. }
  289. u32 Environment::getDayCount()
  290. {
  291. // Atomic<u32> counter
  292. return m_day_count;
  293. }