From a176f9eb36033196040443991a0723c39886b8a2 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sat, 15 Jan 2011 13:50:13 +0200 Subject: generate-time lighting optimization --- src/mapblock.cpp | 87 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 47 insertions(+), 40 deletions(-) (limited to 'src/mapblock.cpp') diff --git a/src/mapblock.cpp b/src/mapblock.cpp index 68b296154..252f123ac 100644 --- a/src/mapblock.cpp +++ b/src/mapblock.cpp @@ -934,61 +934,68 @@ bool MapBlock::propagateSunlight(core::map & light_sources, s16 y = MAP_BLOCKSIZE-1; - if(no_sunlight == false) + // 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--) { - // Continue spreading sunlight downwards through transparent - // nodes - for(; y >= 0; y--) + v3s16 pos(x, y, z); + MapNode &n = getNodeRef(pos); + + if(current_light == 0) { - v3s16 pos(x, y, z); - - MapNode &n = getNodeRef(pos); - - if(n.sunlight_propagates()) - { - n.setLight(LIGHTBANK_DAY, LIGHT_SUN); - - light_sources.insert(pos_relative + pos, true); - } - else + // Do nothing + } + else if(current_light == LIGHT_SUN && n.sunlight_propagates()) + { + // Do nothing: Sunlight is continued + } + else if(n.light_propagates() == false) + { + // Turn mud into grass + if(n.d == CONTENT_MUD && current_light == LIGHT_SUN) { - // Turn mud into grass - if(n.d == CONTENT_MUD) - { - n.d = CONTENT_GRASS; - } - - // Sunlight goes no further - break; + 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); } - } - bool sunlight_should_go_down = (y==-1); - - /* - Check rest through to the bottom of the block - */ - for(; y >= 0; y--) - { - v3s16 pos(x, y, z); - MapNode &n = getNodeRef(pos); + u8 old_light = n.getLight(LIGHTBANK_DAY); - if(n.light_propagates()) + 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; } - - if(remove_light) - { - // Fill transparent nodes with black - n.setLight(LIGHTBANK_DAY, 0); - } } } + // 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: -- cgit v1.2.3