environment.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 how a node can be pointed at
  90. */
  91. inline static PointabilityType isPointableNode(const MapNode &n,
  92. const NodeDefManager *nodedef, bool liquids_pointable,
  93. const std::optional<Pointabilities> &pointabilities)
  94. {
  95. const ContentFeatures &features = nodedef->get(n);
  96. if (pointabilities) {
  97. std::optional<PointabilityType> match =
  98. pointabilities->matchNode(features.name, features.groups);
  99. if (match)
  100. return match.value();
  101. }
  102. if (features.isLiquid() && liquids_pointable)
  103. return PointabilityType::POINTABLE;
  104. return features.pointable;
  105. }
  106. void Environment::continueRaycast(RaycastState *state, PointedThing *result_p)
  107. {
  108. const NodeDefManager *nodedef = getMap().getNodeDefManager();
  109. if (state->m_initialization_needed) {
  110. // Add objects
  111. if (state->m_objects_pointable) {
  112. std::vector<PointedThing> found;
  113. getSelectedActiveObjects(state->m_shootline, found, state->m_pointabilities);
  114. for (const PointedThing &pointed : found) {
  115. state->m_found.push(pointed);
  116. }
  117. }
  118. // Set search range
  119. core::aabbox3d<s16> maximal_exceed = nodedef->getSelectionBoxIntUnion();
  120. state->m_search_range.MinEdge = -maximal_exceed.MaxEdge;
  121. state->m_search_range.MaxEdge = -maximal_exceed.MinEdge;
  122. // Setting is done
  123. state->m_initialization_needed = false;
  124. }
  125. // The index of the first pointed thing that was not returned
  126. // before. The last index which needs to be tested.
  127. s16 lastIndex = state->m_iterator.m_last_index;
  128. if (!state->m_found.empty()) {
  129. lastIndex = state->m_iterator.getIndex(
  130. floatToInt(state->m_found.top().intersection_point, BS));
  131. }
  132. Map &map = getMap();
  133. // If a node is found, this is the center of the
  134. // first nodebox the shootline meets.
  135. v3f found_boxcenter(0, 0, 0);
  136. // The untested nodes are in this range.
  137. core::aabbox3d<s16> new_nodes;
  138. while (state->m_iterator.m_current_index <= lastIndex) {
  139. // Test the nodes around the current node in search_range.
  140. new_nodes = state->m_search_range;
  141. new_nodes.MinEdge += state->m_iterator.m_current_node_pos;
  142. new_nodes.MaxEdge += state->m_iterator.m_current_node_pos;
  143. // Only check new nodes
  144. v3s16 delta = state->m_iterator.m_current_node_pos
  145. - state->m_previous_node;
  146. if (delta.X > 0) {
  147. new_nodes.MinEdge.X = new_nodes.MaxEdge.X;
  148. } else if (delta.X < 0) {
  149. new_nodes.MaxEdge.X = new_nodes.MinEdge.X;
  150. } else if (delta.Y > 0) {
  151. new_nodes.MinEdge.Y = new_nodes.MaxEdge.Y;
  152. } else if (delta.Y < 0) {
  153. new_nodes.MaxEdge.Y = new_nodes.MinEdge.Y;
  154. } else if (delta.Z > 0) {
  155. new_nodes.MinEdge.Z = new_nodes.MaxEdge.Z;
  156. } else if (delta.Z < 0) {
  157. new_nodes.MaxEdge.Z = new_nodes.MinEdge.Z;
  158. }
  159. if (new_nodes.MaxEdge.X == S16_MAX ||
  160. new_nodes.MaxEdge.Y == S16_MAX ||
  161. new_nodes.MaxEdge.Z == S16_MAX) {
  162. break; // About to go out of bounds
  163. }
  164. // For each untested node
  165. for (s16 x = new_nodes.MinEdge.X; x <= new_nodes.MaxEdge.X; x++)
  166. for (s16 y = new_nodes.MinEdge.Y; y <= new_nodes.MaxEdge.Y; y++)
  167. for (s16 z = new_nodes.MinEdge.Z; z <= new_nodes.MaxEdge.Z; z++) {
  168. MapNode n;
  169. v3s16 np(x, y, z);
  170. bool is_valid_position;
  171. n = map.getNode(np, &is_valid_position);
  172. if (!is_valid_position)
  173. continue;
  174. PointabilityType pointable = isPointableNode(n, nodedef,
  175. state->m_liquids_pointable,
  176. state->m_pointabilities);
  177. // If it can be pointed through skip
  178. if (pointable == PointabilityType::POINTABLE_NOT)
  179. continue;
  180. PointedThing result;
  181. std::vector<aabb3f> boxes;
  182. n.getSelectionBoxes(nodedef, &boxes,
  183. n.getNeighbors(np, &map));
  184. // Is there a collision with a selection box?
  185. bool is_colliding = false;
  186. // Minimal distance of all collisions
  187. float min_distance_sq = 10000000;
  188. // ID of the current box (loop counter)
  189. u16 id = 0;
  190. // Do calculations relative to the node center
  191. // to translate the ray rather than the boxes
  192. v3f npf = intToFloat(np, BS);
  193. v3f rel_start = state->m_shootline.start - npf;
  194. for (aabb3f &box : boxes) {
  195. v3f intersection_point;
  196. v3f intersection_normal;
  197. if (!boxLineCollision(box, rel_start,
  198. state->m_shootline.getVector(), &intersection_point,
  199. &intersection_normal)) {
  200. ++id;
  201. continue;
  202. }
  203. intersection_point += npf; // translate back to world coords
  204. f32 distanceSq = (intersection_point
  205. - state->m_shootline.start).getLengthSQ();
  206. // If this is the nearest collision, save it
  207. if (min_distance_sq > distanceSq) {
  208. min_distance_sq = distanceSq;
  209. result.intersection_point = intersection_point;
  210. result.intersection_normal = intersection_normal;
  211. result.box_id = id;
  212. found_boxcenter = box.getCenter();
  213. is_colliding = true;
  214. }
  215. ++id;
  216. }
  217. // If there wasn't a collision, stop
  218. if (!is_colliding) {
  219. continue;
  220. }
  221. result.pointability = pointable;
  222. result.type = POINTEDTHING_NODE;
  223. result.node_undersurface = np;
  224. result.distanceSq = min_distance_sq;
  225. // Set undersurface and abovesurface nodes
  226. f32 d = 0.002 * BS;
  227. v3f fake_intersection = result.intersection_point;
  228. found_boxcenter += npf; // translate back to world coords
  229. // Move intersection towards its source block.
  230. if (fake_intersection.X < found_boxcenter.X) {
  231. fake_intersection.X += d;
  232. } else {
  233. fake_intersection.X -= d;
  234. }
  235. if (fake_intersection.Y < found_boxcenter.Y) {
  236. fake_intersection.Y += d;
  237. } else {
  238. fake_intersection.Y -= d;
  239. }
  240. if (fake_intersection.Z < found_boxcenter.Z) {
  241. fake_intersection.Z += d;
  242. } else {
  243. fake_intersection.Z -= d;
  244. }
  245. result.node_real_undersurface = floatToInt(
  246. fake_intersection, BS);
  247. result.node_abovesurface = result.node_real_undersurface
  248. + floatToInt(result.intersection_normal, 1.0f);
  249. // Push found PointedThing
  250. state->m_found.push(result);
  251. // If this is nearer than the old nearest object,
  252. // the search can be shorter
  253. s16 newIndex = state->m_iterator.getIndex(
  254. result.node_real_undersurface);
  255. if (newIndex < lastIndex) {
  256. lastIndex = newIndex;
  257. }
  258. }
  259. // Next node
  260. state->m_previous_node = state->m_iterator.m_current_node_pos;
  261. state->m_iterator.next();
  262. }
  263. // Return empty PointedThing if nothing left on the ray or it is blocking pointable
  264. if (state->m_found.empty()) {
  265. result_p->type = POINTEDTHING_NOTHING;
  266. } else {
  267. *result_p = state->m_found.top();
  268. state->m_found.pop();
  269. if (result_p->pointability == PointabilityType::POINTABLE_BLOCKING) {
  270. result_p->type = POINTEDTHING_NOTHING;
  271. }
  272. }
  273. }
  274. void Environment::stepTimeOfDay(float dtime)
  275. {
  276. MutexAutoLock lock(this->m_time_lock);
  277. // Cached in order to prevent the two reads we do to give
  278. // different results (can be written by code not under the lock)
  279. f32 cached_time_of_day_speed = m_time_of_day_speed;
  280. f32 speed = cached_time_of_day_speed * 24000. / (24. * 3600);
  281. m_time_conversion_skew += dtime;
  282. u32 units = (u32)(m_time_conversion_skew * speed);
  283. bool sync_f = false;
  284. if (units > 0) {
  285. // Sync at overflow
  286. if (m_time_of_day + units >= 24000) {
  287. sync_f = true;
  288. ++m_day_count;
  289. }
  290. m_time_of_day = (m_time_of_day + units) % 24000;
  291. if (sync_f)
  292. m_time_of_day_f = (float)m_time_of_day / 24000.0;
  293. }
  294. if (speed > 0) {
  295. m_time_conversion_skew -= (f32)units / speed;
  296. }
  297. if (!sync_f) {
  298. m_time_of_day_f += cached_time_of_day_speed / 24 / 3600 * dtime;
  299. if (m_time_of_day_f > 1.0)
  300. m_time_of_day_f -= 1.0;
  301. if (m_time_of_day_f < 0.0)
  302. m_time_of_day_f += 1.0;
  303. }
  304. }
  305. u32 Environment::getDayCount()
  306. {
  307. // Atomic<u32> counter
  308. return m_day_count;
  309. }