reflowscan.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 neighbouring 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. // neighbouring 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. bool valid_position;
  77. MapBlock *block = lookupBlock(x, y, z);
  78. if (block) {
  79. int dx = (MAP_BLOCKSIZE + x) % MAP_BLOCKSIZE;
  80. int dy = (MAP_BLOCKSIZE + y) % MAP_BLOCKSIZE;
  81. int dz = (MAP_BLOCKSIZE + z) % MAP_BLOCKSIZE;
  82. MapNode node = block->getNodeNoCheck(dx, dy, dz, &valid_position);
  83. if (node.getContent() != CONTENT_IGNORE) {
  84. const ContentFeatures &f = m_ndef->get(node);
  85. // NOTE: No need to check for flowing nodes with lower liquid level
  86. // as they should only occur on top of other columns where they
  87. // will be added to the queue themselves.
  88. return f.floodable;
  89. }
  90. }
  91. return false;
  92. }
  93. inline bool ReflowScan::isLiquidHorizontallyFlowable(int x, int y, int z)
  94. {
  95. // Check if the (x,y,z) might spread to one of the horizontally
  96. // neighbouring nodes
  97. return isLiquidFlowableTo(x - 1, y, z) ||
  98. isLiquidFlowableTo(x + 1, y, z) ||
  99. isLiquidFlowableTo(x, y, z - 1) ||
  100. isLiquidFlowableTo(x, y, z + 1);
  101. }
  102. void ReflowScan::scanColumn(int x, int z)
  103. {
  104. bool valid_position;
  105. // Is the column inside a loaded block?
  106. MapBlock *block = lookupBlock(x, 0, z);
  107. if (!block)
  108. return;
  109. MapBlock *above = lookupBlock(x, MAP_BLOCKSIZE, z);
  110. int dx = (MAP_BLOCKSIZE + x) % MAP_BLOCKSIZE;
  111. int dz = (MAP_BLOCKSIZE + z) % MAP_BLOCKSIZE;
  112. // Get the state from the node above the scanned block
  113. bool was_ignore, was_liquid;
  114. if (above) {
  115. MapNode node = above->getNodeNoCheck(dx, 0, dz, &valid_position);
  116. was_ignore = node.getContent() == CONTENT_IGNORE;
  117. was_liquid = m_ndef->get(node).isLiquid();
  118. } else {
  119. was_ignore = true;
  120. was_liquid = false;
  121. }
  122. bool was_checked = false;
  123. bool was_pushed = false;
  124. // Scan through the whole block
  125. for (s16 y = MAP_BLOCKSIZE - 1; y >= 0; y--) {
  126. MapNode node = block->getNodeNoCheck(dx, y, dz, &valid_position);
  127. const ContentFeatures &f = m_ndef->get(node);
  128. bool is_ignore = node.getContent() == CONTENT_IGNORE;
  129. bool is_liquid = f.isLiquid();
  130. if (is_ignore || was_ignore || is_liquid == was_liquid) {
  131. // Neither topmost node of liquid column nor topmost node below column
  132. was_checked = false;
  133. was_pushed = false;
  134. } else if (is_liquid) {
  135. // This is the topmost node in the column
  136. bool is_pushed = false;
  137. if (f.liquid_type == LIQUID_FLOWING ||
  138. isLiquidHorizontallyFlowable(x, y, z)) {
  139. m_liquid_queue->push_back(m_rel_block_pos + v3s16(x, y, z));
  140. is_pushed = true;
  141. }
  142. // Remember waschecked and waspushed to avoid repeated
  143. // checks/pushes in case the column consists of only this node
  144. was_checked = true;
  145. was_pushed = is_pushed;
  146. } else {
  147. // This is the topmost node below a liquid column
  148. if (!was_pushed && (f.floodable ||
  149. (!was_checked && isLiquidHorizontallyFlowable(x, y + 1, z)))) {
  150. // Activate the lowest node in the column which is one
  151. // node above this one
  152. m_liquid_queue->push_back(m_rel_block_pos + v3s16(x, y + 1, z));
  153. }
  154. }
  155. was_liquid = is_liquid;
  156. was_ignore = is_ignore;
  157. }
  158. // Check the node below the current block
  159. MapBlock *below = lookupBlock(x, -1, z);
  160. if (below) {
  161. MapNode node = below->getNodeNoCheck(dx, MAP_BLOCKSIZE - 1, dz, &valid_position);
  162. const ContentFeatures &f = m_ndef->get(node);
  163. bool is_ignore = node.getContent() == CONTENT_IGNORE;
  164. bool is_liquid = f.isLiquid();
  165. if (is_ignore || was_ignore || is_liquid == was_liquid) {
  166. // Neither topmost node of liquid column nor topmost node below column
  167. } else if (is_liquid) {
  168. // This is the topmost node in the column and might want to flow away
  169. if (f.liquid_type == LIQUID_FLOWING ||
  170. isLiquidHorizontallyFlowable(x, -1, z)) {
  171. m_liquid_queue->push_back(m_rel_block_pos + v3s16(x, -1, z));
  172. }
  173. } else {
  174. // This is the topmost node below a liquid column
  175. if (!was_pushed && (f.floodable ||
  176. (!was_checked && isLiquidHorizontallyFlowable(x, 0, z)))) {
  177. // Activate the lowest node in the column which is one
  178. // node above this one
  179. m_liquid_queue->push_back(m_rel_block_pos + v3s16(x, 0, z));
  180. }
  181. }
  182. }
  183. }