voxelalgorithms.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #pragma once
  17. #include "voxel.h"
  18. #include "mapnode.h"
  19. #include "util/container.h"
  20. class Map;
  21. class MapBlock;
  22. class MMVManip;
  23. namespace voxalgo
  24. {
  25. /*!
  26. * Updates the lighting on the map.
  27. * The result will be correct only if
  28. * no nodes were changed except the given ones.
  29. * Before calling this procedure make sure that all new nodes on
  30. * the map have zero light level!
  31. *
  32. * \param oldnodes contains the MapNodes that were replaced by the new
  33. * MapNodes and their positions
  34. * \param modified_blocks output, contains all map blocks that
  35. * the function modified
  36. */
  37. void update_lighting_nodes(
  38. Map *map,
  39. const std::vector<std::pair<v3s16, MapNode>> &oldnodes,
  40. std::map<v3s16, MapBlock*> &modified_blocks);
  41. /*!
  42. * Updates borders of the given mapblock.
  43. * Only updates if the block was marked with incomplete
  44. * lighting and the neighbor is also loaded.
  45. *
  46. * \param block the block to update
  47. * \param modified_blocks output, contains all map blocks that
  48. * the function modified
  49. */
  50. void update_block_border_lighting(Map *map, MapBlock *block,
  51. std::map<v3s16, MapBlock*> &modified_blocks);
  52. /*!
  53. * Copies back nodes from a voxel manipulator
  54. * to the map and updates lighting.
  55. * For server use only.
  56. *
  57. * \param modified_blocks output, contains all map blocks that
  58. * the function modified
  59. */
  60. void blit_back_with_light(Map *map, MMVManip *vm,
  61. std::map<v3s16, MapBlock*> *modified_blocks);
  62. /*!
  63. * Corrects the light in a map block.
  64. * For server use only.
  65. *
  66. * \param block the block to update
  67. */
  68. void repair_block_light(Map *map, MapBlock *block,
  69. std::map<v3s16, MapBlock*> *modified_blocks);
  70. /*!
  71. * This class iterates trough voxels that intersect with
  72. * a line. The collision detection does not see nodeboxes,
  73. * every voxel is a cube and is returned.
  74. * This iterator steps to all nodes exactly once.
  75. */
  76. struct VoxelLineIterator
  77. {
  78. public:
  79. //! Starting position of the line in world coordinates.
  80. v3f m_start_position;
  81. //! Direction and length of the line in world coordinates.
  82. v3f m_line_vector;
  83. /*!
  84. * Each component stores the next smallest positive number, by
  85. * which multiplying the line's vector gives a vector that ends
  86. * on the intersection of two nodes.
  87. */
  88. v3f m_next_intersection_multi { 10000.0f, 10000.0f, 10000.0f };
  89. /*!
  90. * Each component stores the smallest positive number, by which
  91. * m_next_intersection_multi's components can be increased.
  92. */
  93. v3f m_intersection_multi_inc { 10000.0f, 10000.0f, 10000.0f };
  94. /*!
  95. * Direction of the line. Each component can be -1 or 1 (if a
  96. * component of the line's vector is 0, then there will be 1).
  97. */
  98. v3s16 m_step_directions { 1, 1, 1 };
  99. //! Position of the current node.
  100. v3s16 m_current_node_pos;
  101. //! Index of the current node
  102. s16 m_current_index = 0;
  103. //! Position of the start node.
  104. v3s16 m_start_node_pos;
  105. //! Index of the last node
  106. s16 m_last_index;
  107. /*!
  108. * Creates a voxel line iterator with the given line.
  109. * @param start_position starting point of the line
  110. * in voxel coordinates
  111. * @param line_vector length and direction of the
  112. * line in voxel coordinates. start_position+line_vector
  113. * is the end of the line
  114. */
  115. VoxelLineIterator(const v3f &start_position,const v3f &line_vector);
  116. /*!
  117. * Steps to the next voxel.
  118. * Updates m_current_node_pos and
  119. * m_previous_node_pos.
  120. * Note that it works even if hasNext() is false,
  121. * continuing the line as a ray.
  122. */
  123. void next();
  124. /*!
  125. * Returns true if the next voxel intersects the given line.
  126. */
  127. inline bool hasNext() const
  128. {
  129. return m_current_index < m_last_index;
  130. }
  131. /*!
  132. * Returns how many times next() must be called until
  133. * voxel==m_current_node_pos.
  134. * If voxel does not intersect with the line,
  135. * the result is undefined.
  136. */
  137. s16 getIndex(v3s16 voxel);
  138. };
  139. } // namespace voxalgo