3 Copyright (C) 2016 MillersMan <millersman@users.noreply.github.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "reflowscan.h"
24 #include "util/timetaker.h"
27 ReflowScan::ReflowScan(Map *map, INodeDefManager *ndef) :
34 void ReflowScan::scan(MapBlock *block, UniqueQueue<v3s16> *liquid_queue)
36 m_block_pos = block->getPos();
37 m_rel_block_pos = block->getPosRelative();
38 m_liquid_queue = liquid_queue;
40 // Prepare the lookup which is a 3x3x3 array of the blocks surrounding the
41 // scanned block. Blocks are only added to the lookup if they are really
42 // needed. The lookup is indexed manually to use the same index in a
43 // bit-array (of uint32 type) which stores for unloaded blocks that they
44 // were already fetched from Map but were actually NULL.
45 memset(m_lookup, 0, sizeof(m_lookup));
46 int block_idx = 1 + (1 * 9) + (1 * 3);
47 m_lookup[block_idx] = block;
48 m_lookup_state_bitset = 1 << block_idx;
50 // Scan the columns in the block
51 for (s16 z = 0; z < MAP_BLOCKSIZE; z++)
52 for (s16 x = 0; x < MAP_BLOCKSIZE; x++) {
56 // Scan neighbouring columns from the nearby blocks as they might contain
57 // liquid nodes that weren't allowed to flow to prevent gaps.
58 for (s16 i = 0; i < MAP_BLOCKSIZE; i++) {
60 scanColumn(i, MAP_BLOCKSIZE);
62 scanColumn(MAP_BLOCKSIZE, i);
66 inline MapBlock *ReflowScan::lookupBlock(int x, int y, int z)
68 // Gets the block that contains (x,y,z) relativ to the scanned block.
69 // This uses a lookup as there might be many lookups into the same
70 // neighbouring block which makes fetches from Map costly.
71 int bx = (MAP_BLOCKSIZE + x) / MAP_BLOCKSIZE;
72 int by = (MAP_BLOCKSIZE + y) / MAP_BLOCKSIZE;
73 int bz = (MAP_BLOCKSIZE + z) / MAP_BLOCKSIZE;
74 int idx = (bx + (by * 9) + (bz * 3));
75 MapBlock *result = m_lookup[idx];
76 if (!result && (m_lookup_state_bitset & (1 << idx)) == 0) {
77 // The block wasn't requested yet so fetch it from Map and store it
79 v3s16 pos = m_block_pos + v3s16(bx - 1, by - 1, bz - 1);
80 m_lookup[idx] = result = m_map->getBlockNoCreateNoEx(pos);
81 m_lookup_state_bitset |= (1 << idx);
86 inline bool ReflowScan::isLiquidFlowableTo(int x, int y, int z)
88 // Tests whether (x,y,z) is a node to which liquid might flow.
90 MapBlock *block = lookupBlock(x, y, z);
92 int dx = (MAP_BLOCKSIZE + x) % MAP_BLOCKSIZE;
93 int dy = (MAP_BLOCKSIZE + y) % MAP_BLOCKSIZE;
94 int dz = (MAP_BLOCKSIZE + z) % MAP_BLOCKSIZE;
95 MapNode node = block->getNodeNoCheck(dx, dy, dz, &valid_position);
96 if (node.getContent() != CONTENT_IGNORE) {
97 const ContentFeatures &f = m_ndef->get(node);
98 // NOTE: No need to check for flowing nodes with lower liquid level
99 // as they should only occur on top of other columns where they
100 // will be added to the queue themselves.
107 inline bool ReflowScan::isLiquidHorizontallyFlowable(int x, int y, int z)
109 // Check if the (x,y,z) might spread to one of the horizontally
110 // neighbouring nodes
111 return isLiquidFlowableTo(x - 1, y, z) ||
112 isLiquidFlowableTo(x + 1, y, z) ||
113 isLiquidFlowableTo(x, y, z - 1) ||
114 isLiquidFlowableTo(x, y, z + 1);
117 void ReflowScan::scanColumn(int x, int z)
121 // Is the column inside a loaded block?
122 MapBlock *block = lookupBlock(x, 0, z);
126 MapBlock *above = lookupBlock(x, MAP_BLOCKSIZE, z);
127 int dx = (MAP_BLOCKSIZE + x) % MAP_BLOCKSIZE;
128 int dz = (MAP_BLOCKSIZE + z) % MAP_BLOCKSIZE;
130 // Get the state from the node above the scanned block
131 bool was_ignore, was_liquid;
133 MapNode node = above->getNodeNoCheck(dx, 0, dz, &valid_position);
134 was_ignore = node.getContent() == CONTENT_IGNORE;
135 was_liquid = m_ndef->get(node).isLiquid();
140 bool was_checked = false;
141 bool was_pushed = false;
143 // Scan through the whole block
144 for (s16 y = MAP_BLOCKSIZE - 1; y >= 0; y--) {
145 MapNode node = block->getNodeNoCheck(dx, y, dz, &valid_position);
146 const ContentFeatures &f = m_ndef->get(node);
147 bool is_ignore = node.getContent() == CONTENT_IGNORE;
148 bool is_liquid = f.isLiquid();
150 if (is_ignore || was_ignore || is_liquid == was_liquid) {
151 // Neither topmost node of liquid column nor topmost node below column
154 } else if (is_liquid) {
155 // This is the topmost node in the column
156 bool is_pushed = false;
157 if (f.liquid_type == LIQUID_FLOWING ||
158 isLiquidHorizontallyFlowable(x, y, z)) {
159 m_liquid_queue->push_back(m_rel_block_pos + v3s16(x, y, z));
162 // Remember waschecked and waspushed to avoid repeated
163 // checks/pushes in case the column consists of only this node
165 was_pushed = is_pushed;
167 // This is the topmost node below a liquid column
168 if (!was_pushed && (f.floodable ||
169 (!was_checked && isLiquidHorizontallyFlowable(x, y + 1, z)))) {
170 // Activate the lowest node in the column which is one
171 // node above this one
172 m_liquid_queue->push_back(m_rel_block_pos + v3s16(x, y + 1, z));
176 was_liquid = is_liquid;
177 was_ignore = is_ignore;
180 // Check the node below the current block
181 MapBlock *below = lookupBlock(x, -1, z);
183 MapNode node = below->getNodeNoCheck(dx, MAP_BLOCKSIZE - 1, dz, &valid_position);
184 const ContentFeatures &f = m_ndef->get(node);
185 bool is_ignore = node.getContent() == CONTENT_IGNORE;
186 bool is_liquid = f.isLiquid();
188 if (is_ignore || was_ignore || is_liquid == was_liquid) {
189 // Neither topmost node of liquid column nor topmost node below column
190 } else if (is_liquid) {
191 // This is the topmost node in the column and might want to flow away
192 if (f.liquid_type == LIQUID_FLOWING ||
193 isLiquidHorizontallyFlowable(x, -1, z)) {
194 m_liquid_queue->push_back(m_rel_block_pos + v3s16(x, -1, z));
197 // This is the topmost node below a liquid column
198 if (!was_pushed && (f.floodable ||
199 (!was_checked && isLiquidHorizontallyFlowable(x, 0, z)))) {
200 // Activate the lowest node in the column which is one
201 // node above this one
202 m_liquid_queue->push_back(m_rel_block_pos + v3s16(x, 0, z));