reflowscan.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. Minetest
  3. Copyright (C) 2016 MillersMan <millersman@users.noreply.github.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 "reflowscan.h"
  17. #include "map.h"
  18. #include "mapblock.h"
  19. #include "nodedef.h"
  20. #include "util/timetaker.h"
  21. ReflowScan::ReflowScan(Map *map, const NodeDefManager *ndef) :
  22. m_map(map),
  23. m_ndef(ndef)
  24. {
  25. }
  26. void ReflowScan::scan(MapBlock *block, UniqueQueue<v3s16> *liquid_queue)
  27. {
  28. m_block_pos = block->getPos();
  29. m_rel_block_pos = block->getPosRelative();
  30. m_liquid_queue = liquid_queue;
  31. // Prepare the lookup which is a 3x3x3 array of the blocks surrounding the
  32. // scanned block. Blocks are only added to the lookup if they are really
  33. // needed. The lookup is indexed manually to use the same index in a
  34. // bit-array (of uint32 type) which stores for unloaded blocks that they
  35. // were already fetched from Map but were actually nullptr.
  36. memset(m_lookup, 0, sizeof(m_lookup));
  37. int block_idx = 1 + (1 * 9) + (1 * 3);
  38. m_lookup[block_idx] = block;
  39. m_lookup_state_bitset = 1 << block_idx;
  40. // Scan the columns in the block
  41. for (s16 z = 0; z < MAP_BLOCKSIZE; z++)
  42. for (s16 x = 0; x < MAP_BLOCKSIZE; x++) {
  43. scanColumn(x, z);
  44. }
  45. // Scan neighboring columns from the nearby blocks as they might contain
  46. // liquid nodes that weren't allowed to flow to prevent gaps.
  47. for (s16 i = 0; i < MAP_BLOCKSIZE; i++) {
  48. scanColumn(i, -1);
  49. scanColumn(i, MAP_BLOCKSIZE);
  50. scanColumn(-1, i);
  51. scanColumn(MAP_BLOCKSIZE, i);
  52. }
  53. }
  54. inline MapBlock *ReflowScan::lookupBlock(int x, int y, int z)
  55. {
  56. // Gets the block that contains (x,y,z) relativ to the scanned block.
  57. // This uses a lookup as there might be many lookups into the same
  58. // neighboring block which makes fetches from Map costly.
  59. int bx = (MAP_BLOCKSIZE + x) / MAP_BLOCKSIZE;
  60. int by = (MAP_BLOCKSIZE + y) / MAP_BLOCKSIZE;
  61. int bz = (MAP_BLOCKSIZE + z) / MAP_BLOCKSIZE;
  62. int idx = (bx + (by * 9) + (bz * 3));
  63. MapBlock *result = m_lookup[idx];
  64. if (!result && (m_lookup_state_bitset & (1 << idx)) == 0) {
  65. // The block wasn't requested yet so fetch it from Map and store it
  66. // in the lookup
  67. v3s16 pos = m_block_pos + v3s16(bx - 1, by - 1, bz - 1);
  68. m_lookup[idx] = result = m_map->getBlockNoCreateNoEx(pos);
  69. m_lookup_state_bitset |= (1 << idx);
  70. }
  71. return result;
  72. }
  73. inline bool ReflowScan::isLiquidFlowableTo(int x, int y, int z)
  74. {
  75. // Tests whether (x,y,z) is a node to which liquid might flow.
  76. MapBlock *block = lookupBlock(x, y, z);
  77. if (block) {
  78. int dx = (MAP_BLOCKSIZE + x) % MAP_BLOCKSIZE;
  79. int dy = (MAP_BLOCKSIZE + y) % MAP_BLOCKSIZE;
  80. int dz = (MAP_BLOCKSIZE + z) % MAP_BLOCKSIZE;
  81. MapNode node = block->getNodeNoCheck(dx, dy, dz);
  82. if (node.getContent() != CONTENT_IGNORE) {
  83. const ContentFeatures &f = m_ndef->get(node);
  84. // NOTE: No need to check for flowing nodes with lower liquid level
  85. // as they should only occur on top of other columns where they
  86. // will be added to the queue themselves.
  87. return f.floodable;
  88. }
  89. }
  90. return false;
  91. }
  92. inline bool ReflowScan::isLiquidHorizontallyFlowable(int x, int y, int z)
  93. {
  94. // Check if the (x,y,z) might spread to one of the horizontally
  95. // neighboring nodes
  96. return isLiquidFlowableTo(x - 1, y, z) ||
  97. isLiquidFlowableTo(x + 1, y, z) ||
  98. isLiquidFlowableTo(x, y, z - 1) ||
  99. isLiquidFlowableTo(x, y, z + 1);
  100. }
  101. void ReflowScan::scanColumn(int x, int z)
  102. {
  103. // Is the column inside a loaded block?
  104. MapBlock *block = lookupBlock(x, 0, z);
  105. if (!block)
  106. return;
  107. MapBlock *above = lookupBlock(x, MAP_BLOCKSIZE, z);
  108. int dx = (MAP_BLOCKSIZE + x) % MAP_BLOCKSIZE;
  109. int dz = (MAP_BLOCKSIZE + z) % MAP_BLOCKSIZE;
  110. // Get the state from the node above the scanned block
  111. bool was_ignore, was_liquid;
  112. if (above) {
  113. MapNode node = above->getNodeNoCheck(dx, 0, dz);
  114. was_ignore = node.getContent() == CONTENT_IGNORE;
  115. was_liquid = m_ndef->get(node).isLiquid();
  116. } else {
  117. was_ignore = true;
  118. was_liquid = false;
  119. }
  120. bool was_checked = false;
  121. bool was_pushed = false;
  122. // Scan through the whole block
  123. for (s16 y = MAP_BLOCKSIZE - 1; y >= 0; y--) {
  124. MapNode node = block->getNodeNoCheck(dx, y, dz);
  125. const ContentFeatures &f = m_ndef->get(node);
  126. bool is_ignore = node.getContent() == CONTENT_IGNORE;
  127. bool is_liquid = f.isLiquid();
  128. if (is_ignore || was_ignore || is_liquid == was_liquid) {
  129. // Neither topmost node of liquid column nor topmost node below column
  130. was_checked = false;
  131. was_pushed = false;
  132. } else if (is_liquid) {
  133. // This is the topmost node in the column
  134. bool is_pushed = false;
  135. if (f.liquid_type == LIQUID_FLOWING ||
  136. isLiquidHorizontallyFlowable(x, y, z)) {
  137. m_liquid_queue->push_back(m_rel_block_pos + v3s16(x, y, z));
  138. is_pushed = true;
  139. }
  140. // Remember waschecked and waspushed to avoid repeated
  141. // checks/pushes in case the column consists of only this node
  142. was_checked = true;
  143. was_pushed = is_pushed;
  144. } else {
  145. // This is the topmost node below a liquid column
  146. if (!was_pushed && (f.floodable ||
  147. (!was_checked && isLiquidHorizontallyFlowable(x, y + 1, z)))) {
  148. // Activate the lowest node in the column which is one
  149. // node above this one
  150. m_liquid_queue->push_back(m_rel_block_pos + v3s16(x, y + 1, z));
  151. }
  152. }
  153. was_liquid = is_liquid;
  154. was_ignore = is_ignore;
  155. }
  156. // Check the node below the current block
  157. MapBlock *below = lookupBlock(x, -1, z);
  158. if (below) {
  159. MapNode node = below->getNodeNoCheck(dx, MAP_BLOCKSIZE - 1, dz);
  160. const ContentFeatures &f = m_ndef->get(node);
  161. bool is_ignore = node.getContent() == CONTENT_IGNORE;
  162. bool is_liquid = f.isLiquid();
  163. if (is_ignore || was_ignore || is_liquid == was_liquid) {
  164. // Neither topmost node of liquid column nor topmost node below column
  165. } else if (is_liquid) {
  166. // This is the topmost node in the column and might want to flow away
  167. if (f.liquid_type == LIQUID_FLOWING ||
  168. isLiquidHorizontallyFlowable(x, -1, z)) {
  169. m_liquid_queue->push_back(m_rel_block_pos + v3s16(x, -1, z));
  170. }
  171. } else {
  172. // This is the topmost node below a liquid column
  173. if (!was_pushed && (f.floodable ||
  174. (!was_checked && isLiquidHorizontallyFlowable(x, 0, z)))) {
  175. // Activate the lowest node in the column which is one
  176. // node above this one
  177. m_liquid_queue->push_back(m_rel_block_pos + v3s16(x, 0, z));
  178. }
  179. }
  180. }
  181. }