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