-/*
- 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(core::map<v3s16, bool> & light_sources,
- bool remove_light, bool *black_air_left)
-{
- // 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
- try{
- MapNode n = getNodeParent(v3s16(x, MAP_BLOCKSIZE, z));
- if(n.d == CONTENT_IGNORE || n.getLight(LIGHTBANK_DAY) != LIGHT_SUN)
- {
- no_sunlight = true;
- }
- }
- catch(InvalidPositionException &e)
- {
- 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 = getNode(v3s16(x, MAP_BLOCKSIZE-1, z));
- //if(n.d == CONTENT_WATER || n.d == CONTENT_WATERSOURCE)
- if(content_features(n.d).sunlight_propagates == false)
- {
- 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 && n.sunlight_propagates())
- {
- // Do nothing: Sunlight is continued
- }
- else if(n.light_propagates() == false)
- {
- /*// DEPRECATED TODO: REMOVE
- if(grow_grass)
- {
- bool upper_is_air = false;
- try
- {
- if(getNodeParent(pos+v3s16(0,1,0)).d == CONTENT_AIR)
- upper_is_air = true;
- }
- catch(InvalidPositionException &e)
- {
- }
- // Turn mud into grass
- if(upper_is_air && n.d == CONTENT_MUD
- && current_light == LIGHT_SUN)
- {
- n.d = CONTENT_GRASS;
- }
- }*/
-
- // 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);
-
- if(current_light > old_light || remove_light)
- {
- n.setLight(LIGHTBANK_DAY, current_light);
- }
-
- if(diminish_light(current_light) != 0)
- {
- light_sources.insert(pos_relative + pos, true);
- }
-
- 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
- */
- try
- {
- if(block_below_is_valid)
- {
- MapNode n = getNodeParent(v3s16(x, -1, z));
- if(n.light_propagates())
- {
- if(n.getLight(LIGHTBANK_DAY) == LIGHT_SUN
- && sunlight_should_go_down == false)
- block_below_is_valid = false;
- else if(n.getLight(LIGHTBANK_DAY) != LIGHT_SUN
- && sunlight_should_go_down == true)
- block_below_is_valid = false;
- }
- }//if
- }//try
- catch(InvalidPositionException &e)
- {
- /*std::cout<<"InvalidBlockException for bottom block node"
- <<std::endl;*/
- // Just no block below, no need to panic.
- }
- }
- }
-
- return block_below_is_valid;