]> git.lizzy.rs Git - dragonfireclient.git/blob - src/unittest/test_voxelalgorithms.cpp
Enforce hiding nametag
[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
25 class TestVoxelAlgorithms : public TestBase {
26 public:
27         TestVoxelAlgorithms() { TestManager::registerTestModule(this); }
28         const char *getName() { return "TestVoxelAlgorithms"; }
29
30         void runTests(IGameDef *gamedef);
31
32         void testPropogateSunlight(INodeDefManager *ndef);
33         void testClearLightAndCollectSources(INodeDefManager *ndef);
34 };
35
36 static TestVoxelAlgorithms g_test_instance;
37
38 void TestVoxelAlgorithms::runTests(IGameDef *gamedef)
39 {
40         INodeDefManager *ndef = gamedef->getNodeDefManager();
41
42         TEST(testPropogateSunlight, ndef);
43         TEST(testClearLightAndCollectSources, ndef);
44 }
45
46 ////////////////////////////////////////////////////////////////////////////////
47
48 void TestVoxelAlgorithms::testPropogateSunlight(INodeDefManager *ndef)
49 {
50         VoxelManipulator v;
51
52         for (u16 z = 0; z < 3; z++)
53         for (u16 y = 0; y < 3; y++)
54         for (u16 x = 0; x < 3; x++) {
55                 v3s16 p(x,y,z);
56                 v.setNodeNoRef(p, MapNode(CONTENT_AIR));
57         }
58
59         VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
60
61         {
62                 std::set<v3s16> light_sources;
63                 voxalgo::setLight(v, a, 0, ndef);
64                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
65                                 v, a, true, light_sources, ndef);
66                 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
67                 UASSERT(res.bottom_sunlight_valid == true);
68                 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
69                                 == LIGHT_SUN);
70         }
71
72         v.setNodeNoRef(v3s16(0,0,0), MapNode(t_CONTENT_STONE));
73
74         {
75                 std::set<v3s16> light_sources;
76                 voxalgo::setLight(v, a, 0, ndef);
77                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
78                                 v, a, true, light_sources, ndef);
79                 UASSERT(res.bottom_sunlight_valid == true);
80                 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
81                                 == LIGHT_SUN);
82         }
83
84         {
85                 std::set<v3s16> light_sources;
86                 voxalgo::setLight(v, a, 0, ndef);
87                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
88                                 v, a, false, light_sources, ndef);
89                 UASSERT(res.bottom_sunlight_valid == true);
90                 UASSERT(v.getNode(v3s16(2,0,2)).getLight(LIGHTBANK_DAY, ndef)
91                                 == 0);
92         }
93
94         v.setNodeNoRef(v3s16(1,3,2), MapNode(t_CONTENT_STONE));
95
96         {
97                 std::set<v3s16> light_sources;
98                 voxalgo::setLight(v, a, 0, ndef);
99                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
100                                 v, a, true, light_sources, ndef);
101                 UASSERT(res.bottom_sunlight_valid == true);
102                 UASSERT(v.getNode(v3s16(1,1,2)).getLight(LIGHTBANK_DAY, ndef)
103                                 == 0);
104         }
105
106         {
107                 std::set<v3s16> light_sources;
108                 voxalgo::setLight(v, a, 0, ndef);
109                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
110                                 v, a, false, light_sources, ndef);
111                 UASSERT(res.bottom_sunlight_valid == true);
112                 UASSERT(v.getNode(v3s16(1,0,2)).getLight(LIGHTBANK_DAY, ndef)
113                                 == 0);
114         }
115
116         {
117                 MapNode n(CONTENT_AIR);
118                 n.setLight(LIGHTBANK_DAY, 10, ndef);
119                 v.setNodeNoRef(v3s16(1,-1,2), n);
120         }
121
122         {
123                 std::set<v3s16> light_sources;
124                 voxalgo::setLight(v, a, 0, ndef);
125                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
126                                 v, a, true, light_sources, ndef);
127                 UASSERT(res.bottom_sunlight_valid == true);
128         }
129
130         {
131                 std::set<v3s16> light_sources;
132                 voxalgo::setLight(v, a, 0, ndef);
133                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
134                                 v, a, false, light_sources, ndef);
135                 UASSERT(res.bottom_sunlight_valid == true);
136         }
137
138         {
139                 MapNode n(CONTENT_AIR);
140                 n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
141                 v.setNodeNoRef(v3s16(1,-1,2), n);
142         }
143
144         {
145                 std::set<v3s16> light_sources;
146                 voxalgo::setLight(v, a, 0, ndef);
147                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
148                                 v, a, true, light_sources, ndef);
149                 UASSERT(res.bottom_sunlight_valid == false);
150         }
151
152         {
153                 std::set<v3s16> light_sources;
154                 voxalgo::setLight(v, a, 0, ndef);
155                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
156                                 v, a, false, light_sources, ndef);
157                 UASSERT(res.bottom_sunlight_valid == false);
158         }
159
160         v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_IGNORE));
161
162         {
163                 std::set<v3s16> light_sources;
164                 voxalgo::setLight(v, a, 0, ndef);
165                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
166                                 v, a, true, light_sources, ndef);
167                 UASSERT(res.bottom_sunlight_valid == true);
168         }
169 }
170
171 void TestVoxelAlgorithms::testClearLightAndCollectSources(INodeDefManager *ndef)
172 {
173         VoxelManipulator v;
174
175         for (u16 z = 0; z < 3; z++)
176         for (u16 y = 0; y < 3; y++)
177         for (u16 x = 0; x < 3; x++) {
178                 v3s16 p(x,y,z);
179                 v.setNode(p, MapNode(CONTENT_AIR));
180         }
181
182         VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
183         v.setNodeNoRef(v3s16(0,0,0), MapNode(t_CONTENT_STONE));
184         v.setNodeNoRef(v3s16(1,1,1), MapNode(t_CONTENT_TORCH));
185
186         {
187                 MapNode n(CONTENT_AIR);
188                 n.setLight(LIGHTBANK_DAY, 1, ndef);
189                 v.setNode(v3s16(1,1,2), n);
190         }
191
192         {
193                 std::set<v3s16> light_sources;
194                 std::map<v3s16, u8> unlight_from;
195                 voxalgo::clearLightAndCollectSources(v, a, LIGHTBANK_DAY,
196                                 ndef, light_sources, unlight_from);
197                 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
198                 UASSERT(v.getNode(v3s16(0,1,1)).getLight(LIGHTBANK_DAY, ndef) == 0);
199                 UASSERT(light_sources.find(v3s16(1,1,1)) != light_sources.end());
200                 UASSERT(light_sources.size() == 1);
201                 UASSERT(unlight_from.find(v3s16(1,1,2)) != unlight_from.end());
202                 UASSERT(unlight_from.size() == 1);
203         }
204 }