3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.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.
23 #include "voxelalgorithms.h"
25 class TestVoxelAlgorithms : public TestBase {
27 TestVoxelAlgorithms() { TestManager::registerTestModule(this); }
28 const char *getName() { return "TestVoxelAlgorithms"; }
30 void runTests(IGameDef *gamedef);
32 void testPropogateSunlight(INodeDefManager *ndef);
33 void testClearLightAndCollectSources(INodeDefManager *ndef);
36 static TestVoxelAlgorithms g_test_instance;
38 void TestVoxelAlgorithms::runTests(IGameDef *gamedef)
40 INodeDefManager *ndef = gamedef->getNodeDefManager();
42 TEST(testPropogateSunlight, ndef);
43 TEST(testClearLightAndCollectSources, ndef);
46 ////////////////////////////////////////////////////////////////////////////////
48 void TestVoxelAlgorithms::testPropogateSunlight(INodeDefManager *ndef)
52 for (u16 z = 0; z < 3; z++)
53 for (u16 y = 0; y < 3; y++)
54 for (u16 x = 0; x < 3; x++) {
56 v.setNodeNoRef(p, MapNode(CONTENT_AIR));
59 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
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)
72 v.setNodeNoRef(v3s16(0,0,0), MapNode(t_CONTENT_STONE));
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)
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)
94 v.setNodeNoRef(v3s16(1,3,2), MapNode(t_CONTENT_STONE));
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)
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)
117 MapNode n(CONTENT_AIR);
118 n.setLight(LIGHTBANK_DAY, 10, ndef);
119 v.setNodeNoRef(v3s16(1,-1,2), n);
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);
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);
139 MapNode n(CONTENT_AIR);
140 n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
141 v.setNodeNoRef(v3s16(1,-1,2), n);
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);
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);
160 v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_IGNORE));
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);
171 void TestVoxelAlgorithms::testClearLightAndCollectSources(INodeDefManager *ndef)
175 for (u16 z = 0; z < 3; z++)
176 for (u16 y = 0; y < 3; y++)
177 for (u16 x = 0; x < 3; x++) {
179 v.setNode(p, MapNode(CONTENT_AIR));
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));
187 MapNode n(CONTENT_AIR);
188 n.setLight(LIGHTBANK_DAY, 1, ndef);
189 v.setNode(v3s16(1,1,2), n);
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);