]> git.lizzy.rs Git - dragonfireclient.git/blob - src/unittest/test_voxelalgorithms.cpp
Fix Android node selection distance (#6187)
[dragonfireclient.git] / src / unittest / test_voxelalgorithms.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.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 "test.h"
21
22 #include "gamedef.h"
23 #include "voxelalgorithms.h"
24 #include "util/numeric.h"
25
26 class TestVoxelAlgorithms : public TestBase {
27 public:
28         TestVoxelAlgorithms() { TestManager::registerTestModule(this); }
29         const char *getName() { return "TestVoxelAlgorithms"; }
30
31         void runTests(IGameDef *gamedef);
32
33         void testPropogateSunlight(INodeDefManager *ndef);
34         void testClearLightAndCollectSources(INodeDefManager *ndef);
35         void testVoxelLineIterator(INodeDefManager *ndef);
36 };
37
38 static TestVoxelAlgorithms g_test_instance;
39
40 void TestVoxelAlgorithms::runTests(IGameDef *gamedef)
41 {
42         INodeDefManager *ndef = gamedef->getNodeDefManager();
43
44         TEST(testPropogateSunlight, ndef);
45         TEST(testClearLightAndCollectSources, ndef);
46         TEST(testVoxelLineIterator, ndef);
47 }
48
49 ////////////////////////////////////////////////////////////////////////////////
50
51 void TestVoxelAlgorithms::testPropogateSunlight(INodeDefManager *ndef)
52 {
53         VoxelManipulator v;
54
55         for (u16 z = 0; z < 3; z++)
56         for (u16 y = 0; y < 3; y++)
57         for (u16 x = 0; x < 3; x++) {
58                 v3s16 p(x,y,z);
59                 v.setNodeNoRef(p, MapNode(CONTENT_AIR));
60         }
61
62         VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
63
64         {
65                 std::set<v3s16> light_sources;
66                 voxalgo::setLight(v, a, 0, ndef);
67                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
68                                 v, a, true, light_sources, ndef);
69                 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
70                 UASSERT(res.bottom_sunlight_valid == true);
71                 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
72                                 == LIGHT_SUN);
73         }
74
75         v.setNodeNoRef(v3s16(0,0,0), MapNode(t_CONTENT_STONE));
76
77         {
78                 std::set<v3s16> light_sources;
79                 voxalgo::setLight(v, a, 0, ndef);
80                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
81                                 v, a, true, light_sources, ndef);
82                 UASSERT(res.bottom_sunlight_valid == true);
83                 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
84                                 == LIGHT_SUN);
85         }
86
87         {
88                 std::set<v3s16> light_sources;
89                 voxalgo::setLight(v, a, 0, ndef);
90                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
91                                 v, a, false, light_sources, ndef);
92                 UASSERT(res.bottom_sunlight_valid == true);
93                 UASSERT(v.getNode(v3s16(2,0,2)).getLight(LIGHTBANK_DAY, ndef)
94                                 == 0);
95         }
96
97         v.setNodeNoRef(v3s16(1,3,2), MapNode(t_CONTENT_STONE));
98
99         {
100                 std::set<v3s16> light_sources;
101                 voxalgo::setLight(v, a, 0, ndef);
102                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
103                                 v, a, true, light_sources, ndef);
104                 UASSERT(res.bottom_sunlight_valid == true);
105                 UASSERT(v.getNode(v3s16(1,1,2)).getLight(LIGHTBANK_DAY, ndef)
106                                 == 0);
107         }
108
109         {
110                 std::set<v3s16> light_sources;
111                 voxalgo::setLight(v, a, 0, ndef);
112                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
113                                 v, a, false, light_sources, ndef);
114                 UASSERT(res.bottom_sunlight_valid == true);
115                 UASSERT(v.getNode(v3s16(1,0,2)).getLight(LIGHTBANK_DAY, ndef)
116                                 == 0);
117         }
118
119         {
120                 MapNode n(CONTENT_AIR);
121                 n.setLight(LIGHTBANK_DAY, 10, ndef);
122                 v.setNodeNoRef(v3s16(1,-1,2), n);
123         }
124
125         {
126                 std::set<v3s16> light_sources;
127                 voxalgo::setLight(v, a, 0, ndef);
128                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
129                                 v, a, true, light_sources, ndef);
130                 UASSERT(res.bottom_sunlight_valid == true);
131         }
132
133         {
134                 std::set<v3s16> light_sources;
135                 voxalgo::setLight(v, a, 0, ndef);
136                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
137                                 v, a, false, light_sources, ndef);
138                 UASSERT(res.bottom_sunlight_valid == true);
139         }
140
141         {
142                 MapNode n(CONTENT_AIR);
143                 n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
144                 v.setNodeNoRef(v3s16(1,-1,2), n);
145         }
146
147         {
148                 std::set<v3s16> light_sources;
149                 voxalgo::setLight(v, a, 0, ndef);
150                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
151                                 v, a, true, light_sources, ndef);
152                 UASSERT(res.bottom_sunlight_valid == false);
153         }
154
155         {
156                 std::set<v3s16> light_sources;
157                 voxalgo::setLight(v, a, 0, ndef);
158                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
159                                 v, a, false, light_sources, ndef);
160                 UASSERT(res.bottom_sunlight_valid == false);
161         }
162
163         v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_IGNORE));
164
165         {
166                 std::set<v3s16> light_sources;
167                 voxalgo::setLight(v, a, 0, ndef);
168                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
169                                 v, a, true, light_sources, ndef);
170                 UASSERT(res.bottom_sunlight_valid == true);
171         }
172 }
173
174 void TestVoxelAlgorithms::testClearLightAndCollectSources(INodeDefManager *ndef)
175 {
176         VoxelManipulator v;
177
178         for (u16 z = 0; z < 3; z++)
179         for (u16 y = 0; y < 3; y++)
180         for (u16 x = 0; x < 3; x++) {
181                 v3s16 p(x,y,z);
182                 v.setNode(p, MapNode(CONTENT_AIR));
183         }
184
185         VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
186         v.setNodeNoRef(v3s16(0,0,0), MapNode(t_CONTENT_STONE));
187         v.setNodeNoRef(v3s16(1,1,1), MapNode(t_CONTENT_TORCH));
188
189         {
190                 MapNode n(CONTENT_AIR);
191                 n.setLight(LIGHTBANK_DAY, 1, ndef);
192                 v.setNode(v3s16(1,1,2), n);
193         }
194
195         {
196                 std::set<v3s16> light_sources;
197                 std::map<v3s16, u8> unlight_from;
198                 voxalgo::clearLightAndCollectSources(v, a, LIGHTBANK_DAY,
199                                 ndef, light_sources, unlight_from);
200                 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
201                 UASSERT(v.getNode(v3s16(0,1,1)).getLight(LIGHTBANK_DAY, ndef) == 0);
202                 UASSERT(light_sources.find(v3s16(1,1,1)) != light_sources.end());
203                 UASSERT(light_sources.size() == 1);
204                 UASSERT(unlight_from.find(v3s16(1,1,2)) != unlight_from.end());
205                 UASSERT(unlight_from.size() == 1);
206         }
207 }
208
209 void TestVoxelAlgorithms::testVoxelLineIterator(INodeDefManager *ndef)
210 {
211         // Test some lines
212         // Do not test lines that start or end on the border of
213         // two voxels as rounding errors can make the test fail!
214         std::vector<core::line3d<f32> > lines;
215         for (f32 x = -9.1; x < 9; x += 3.124) {
216         for (f32 y = -9.2; y < 9; y += 3.123) {
217         for (f32 z = -9.3; z < 9; z += 3.122) {
218                 lines.emplace_back(-x, -y, -z, x, y, z);
219         }
220         }
221         }
222         lines.emplace_back(0, 0, 0, 0, 0, 0);
223         // Test every line
224         std::vector<core::line3d<f32> >::iterator it = lines.begin();
225         for (; it < lines.end(); it++) {
226                 core::line3d<f32> l = *it;
227
228                 // Initialize test
229                 voxalgo::VoxelLineIterator iterator(l.start, l.getVector());
230
231                 //Test the first voxel
232                 v3s16 start_voxel = floatToInt(l.start, 1);
233                 UASSERT(iterator.m_current_node_pos == start_voxel);
234
235                 // Values for testing
236                 v3s16 end_voxel = floatToInt(l.end, 1);
237                 v3s16 voxel_vector = end_voxel - start_voxel;
238                 int nodecount = abs(voxel_vector.X) + abs(voxel_vector.Y)
239                         + abs(voxel_vector.Z);
240                 int actual_nodecount = 0;
241                 v3s16 old_voxel = iterator.m_current_node_pos;
242
243                 while (iterator.hasNext()) {
244                         iterator.next();
245                         actual_nodecount++;
246                         v3s16 new_voxel = iterator.m_current_node_pos;
247                         // This must be a neighbor of the old voxel
248                         UASSERTEQ(f32, (new_voxel - old_voxel).getLengthSQ(), 1);
249                         // The line must intersect with the voxel
250                         v3f voxel_center = intToFloat(iterator.m_current_node_pos, 1);
251                         aabb3f box(voxel_center - v3f(0.5, 0.5, 0.5),
252                                 voxel_center + v3f(0.5, 0.5, 0.5));
253                         UASSERT(box.intersectsWithLine(l));
254                         // Update old voxel
255                         old_voxel = new_voxel;
256                 }
257
258                 // Test last node
259                 UASSERT(iterator.m_current_node_pos == end_voxel);
260                 // Test node count
261                 UASSERTEQ(int, actual_nodecount, nodecount);
262         }
263 }