Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Remove unused light updating code
Also remove the unit test that tests the removed algorithms.
  • Loading branch information
juhdanad authored and rubenwardy committed Feb 4, 2018
1 parent cf0bceb commit 735fc2a
Show file tree
Hide file tree
Showing 7 changed files with 0 additions and 709 deletions.
192 changes: 0 additions & 192 deletions src/mapblock.cpp
Expand Up @@ -132,198 +132,6 @@ std::string MapBlock::getModifiedReasonString()
return reason;
}

/*
Propagates sunlight down through the block.
Doesn't modify nodes that are not affected by sunlight.
Returns false if sunlight at bottom block is invalid.
Returns true if sunlight at bottom block is valid.
Returns true if bottom block doesn't exist.
If there is a block above, continues from it.
If there is no block above, assumes there is sunlight, unless
is_underground is set or highest node is water.
All sunlighted nodes are added to light_sources.
if remove_light==true, sets non-sunlighted nodes black.
if black_air_left!=NULL, it is set to true if non-sunlighted
air is left in block.
*/
bool MapBlock::propagateSunlight(std::set<v3s16> & light_sources,
bool remove_light, bool *black_air_left)
{
INodeDefManager *nodemgr = m_gamedef->ndef();

// Whether the sunlight at the top of the bottom block is valid
bool block_below_is_valid = true;

v3s16 pos_relative = getPosRelative();

for(s16 x=0; x<MAP_BLOCKSIZE; x++)
{
for(s16 z=0; z<MAP_BLOCKSIZE; z++)
{
#if 1
bool no_sunlight = false;
//bool no_top_block = false;

// Check if node above block has sunlight

bool is_valid_position;
MapNode n = getNodeParent(v3s16(x, MAP_BLOCKSIZE, z),
&is_valid_position);
if (is_valid_position)
{
if(n.getContent() == CONTENT_IGNORE)
{
// Trust heuristics
no_sunlight = is_underground;
}
else if(n.getLight(LIGHTBANK_DAY, m_gamedef->ndef()) != LIGHT_SUN)
{
no_sunlight = true;
}
}
else
{
//no_top_block = true;

// NOTE: This makes over-ground roofed places sunlighted
// Assume sunlight, unless is_underground==true
if(is_underground)
{
no_sunlight = true;
}
else
{
MapNode n = getNodeNoEx(v3s16(x, MAP_BLOCKSIZE-1, z));
if (!m_gamedef->ndef()->get(n).sunlight_propagates) {
no_sunlight = true;
}
}
// NOTE: As of now, this just would make everything dark.
// No sunlight here
//no_sunlight = true;
}
#endif
#if 0 // Doesn't work; nothing gets light.
bool no_sunlight = true;
bool no_top_block = false;
// Check if node above block has sunlight
try{
MapNode n = getNodeParent(v3s16(x, MAP_BLOCKSIZE, z));
if(n.getLight(LIGHTBANK_DAY) == LIGHT_SUN)
{
no_sunlight = false;
}
}
catch(InvalidPositionException &e)
{
no_top_block = true;
}
#endif

/*std::cout<<"("<<x<<","<<z<<"): "
<<"no_top_block="<<no_top_block
<<", is_underground="<<is_underground
<<", no_sunlight="<<no_sunlight
<<std::endl;*/

s16 y = MAP_BLOCKSIZE-1;

// This makes difference to diminishing in water.
bool stopped_to_solid_object = false;

u8 current_light = no_sunlight ? 0 : LIGHT_SUN;

for(; y >= 0; y--)
{
v3s16 pos(x, y, z);
MapNode &n = getNodeRef(pos);

if(current_light == 0)
{
// Do nothing
}
else if(current_light == LIGHT_SUN && nodemgr->get(n).sunlight_propagates)
{
// Do nothing: Sunlight is continued
} else if (!nodemgr->get(n).light_propagates) {
// A solid object is on the way.
stopped_to_solid_object = true;

// Light stops.
current_light = 0;
}
else
{
// Diminish light
current_light = diminish_light(current_light);
}

u8 old_light = n.getLight(LIGHTBANK_DAY, nodemgr);

if(current_light > old_light || remove_light)
{
n.setLight(LIGHTBANK_DAY, current_light, nodemgr);
}

if(diminish_light(current_light) != 0)
{
light_sources.insert(pos_relative + pos);
}

if(current_light == 0 && stopped_to_solid_object)
{
if(black_air_left)
{
*black_air_left = true;
}
}
}

// Whether or not the block below should see LIGHT_SUN
bool sunlight_should_go_down = (current_light == LIGHT_SUN);

/*
If the block below hasn't already been marked invalid:
Check if the node below the block has proper sunlight at top.
If not, the block below is invalid.
Ignore non-transparent nodes as they always have no light
*/

if(block_below_is_valid)
{
MapNode n = getNodeParent(v3s16(x, -1, z), &is_valid_position);
if (is_valid_position) {
if(nodemgr->get(n).light_propagates)
{
if(n.getLight(LIGHTBANK_DAY, nodemgr) == LIGHT_SUN
&& !sunlight_should_go_down)
block_below_is_valid = false;
else if(n.getLight(LIGHTBANK_DAY, nodemgr) != LIGHT_SUN
&& sunlight_should_go_down)
block_below_is_valid = false;
}
}
else
{
/*std::cout<<"InvalidBlockException for bottom block node"
<<std::endl;*/
// Just no block below, no need to panic.
}
}
}
}

return block_below_is_valid;
}


void MapBlock::copyTo(VoxelManipulator &dst)
{
v3s16 data_size(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
Expand Down
4 changes: 0 additions & 4 deletions src/mapblock.h
Expand Up @@ -348,10 +348,6 @@ class MapBlock
setNode(x0 + x, y0 + y, z0 + z, node);
}

// See comments in mapblock.cpp
bool propagateSunlight(std::set<v3s16> &light_sources,
bool remove_light=false, bool *black_air_left=NULL);

// Copies data to VoxelManipulator to getPosRelative()
void copyTo(VoxelManipulator &dst);

Expand Down
162 changes: 0 additions & 162 deletions src/unittest/test_voxelalgorithms.cpp
Expand Up @@ -30,8 +30,6 @@ class TestVoxelAlgorithms : public TestBase {

void runTests(IGameDef *gamedef);

void testPropogateSunlight(INodeDefManager *ndef);
void testClearLightAndCollectSources(INodeDefManager *ndef);
void testVoxelLineIterator(INodeDefManager *ndef);
};

Expand All @@ -41,171 +39,11 @@ void TestVoxelAlgorithms::runTests(IGameDef *gamedef)
{
INodeDefManager *ndef = gamedef->getNodeDefManager();

TEST(testPropogateSunlight, ndef);
TEST(testClearLightAndCollectSources, ndef);
TEST(testVoxelLineIterator, ndef);
}

////////////////////////////////////////////////////////////////////////////////

void TestVoxelAlgorithms::testPropogateSunlight(INodeDefManager *ndef)
{
VoxelManipulator v;

for (u16 z = 0; z < 3; z++)
for (u16 y = 0; y < 3; y++)
for (u16 x = 0; x < 3; x++) {
v3s16 p(x,y,z);
v.setNodeNoRef(p, MapNode(CONTENT_AIR));
}

VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));

{
std::set<v3s16> light_sources;
voxalgo::setLight(v, a, 0, ndef);
voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
v, a, true, light_sources, ndef);
//v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
UASSERT(res.bottom_sunlight_valid == true);
UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
== LIGHT_SUN);
}

v.setNodeNoRef(v3s16(0,0,0), MapNode(t_CONTENT_STONE));

{
std::set<v3s16> light_sources;
voxalgo::setLight(v, a, 0, ndef);
voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
v, a, true, light_sources, ndef);
UASSERT(res.bottom_sunlight_valid == true);
UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
== LIGHT_SUN);
}

{
std::set<v3s16> light_sources;
voxalgo::setLight(v, a, 0, ndef);
voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
v, a, false, light_sources, ndef);
UASSERT(res.bottom_sunlight_valid == true);
UASSERT(v.getNode(v3s16(2,0,2)).getLight(LIGHTBANK_DAY, ndef)
== 0);
}

v.setNodeNoRef(v3s16(1,3,2), MapNode(t_CONTENT_STONE));

{
std::set<v3s16> light_sources;
voxalgo::setLight(v, a, 0, ndef);
voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
v, a, true, light_sources, ndef);
UASSERT(res.bottom_sunlight_valid == true);
UASSERT(v.getNode(v3s16(1,1,2)).getLight(LIGHTBANK_DAY, ndef)
== 0);
}

{
std::set<v3s16> light_sources;
voxalgo::setLight(v, a, 0, ndef);
voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
v, a, false, light_sources, ndef);
UASSERT(res.bottom_sunlight_valid == true);
UASSERT(v.getNode(v3s16(1,0,2)).getLight(LIGHTBANK_DAY, ndef)
== 0);
}

{
MapNode n(CONTENT_AIR);
n.setLight(LIGHTBANK_DAY, 10, ndef);
v.setNodeNoRef(v3s16(1,-1,2), n);
}

{
std::set<v3s16> light_sources;
voxalgo::setLight(v, a, 0, ndef);
voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
v, a, true, light_sources, ndef);
UASSERT(res.bottom_sunlight_valid == true);
}

{
std::set<v3s16> light_sources;
voxalgo::setLight(v, a, 0, ndef);
voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
v, a, false, light_sources, ndef);
UASSERT(res.bottom_sunlight_valid == true);
}

{
MapNode n(CONTENT_AIR);
n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
v.setNodeNoRef(v3s16(1,-1,2), n);
}

{
std::set<v3s16> light_sources;
voxalgo::setLight(v, a, 0, ndef);
voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
v, a, true, light_sources, ndef);
UASSERT(res.bottom_sunlight_valid == false);
}

{
std::set<v3s16> light_sources;
voxalgo::setLight(v, a, 0, ndef);
voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
v, a, false, light_sources, ndef);
UASSERT(res.bottom_sunlight_valid == false);
}

v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_IGNORE));

{
std::set<v3s16> light_sources;
voxalgo::setLight(v, a, 0, ndef);
voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
v, a, true, light_sources, ndef);
UASSERT(res.bottom_sunlight_valid == true);
}
}

void TestVoxelAlgorithms::testClearLightAndCollectSources(INodeDefManager *ndef)
{
VoxelManipulator v;

for (u16 z = 0; z < 3; z++)
for (u16 y = 0; y < 3; y++)
for (u16 x = 0; x < 3; x++) {
v3s16 p(x,y,z);
v.setNode(p, MapNode(CONTENT_AIR));
}

VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
v.setNodeNoRef(v3s16(0,0,0), MapNode(t_CONTENT_STONE));
v.setNodeNoRef(v3s16(1,1,1), MapNode(t_CONTENT_TORCH));

{
MapNode n(CONTENT_AIR);
n.setLight(LIGHTBANK_DAY, 1, ndef);
v.setNode(v3s16(1,1,2), n);
}

{
std::set<v3s16> light_sources;
std::map<v3s16, u8> unlight_from;
voxalgo::clearLightAndCollectSources(v, a, LIGHTBANK_DAY,
ndef, light_sources, unlight_from);
//v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
UASSERT(v.getNode(v3s16(0,1,1)).getLight(LIGHTBANK_DAY, ndef) == 0);
UASSERT(light_sources.find(v3s16(1,1,1)) != light_sources.end());
UASSERT(light_sources.size() == 1);
UASSERT(unlight_from.find(v3s16(1,1,2)) != unlight_from.end());
UASSERT(unlight_from.size() == 1);
}
}

void TestVoxelAlgorithms::testVoxelLineIterator(INodeDefManager *ndef)
{
// Test some lines
Expand Down

0 comments on commit 735fc2a

Please sign in to comment.