voxalgo::clearLightAndCollectSources
authorPerttu Ahola <celeron55@gmail.com>
Fri, 27 Jan 2012 11:24:06 +0000 (13:24 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Tue, 27 Mar 2012 16:01:51 +0000 (19:01 +0300)
src/map.cpp
src/mapgen.cpp
src/test.cpp
src/voxelalgorithms.cpp
src/voxelalgorithms.h

index dce3bdc091300f31fefb3153213afa161e6083e6..1ab2b32ae55596999ffdee6f9548d17d6a8ca5fa 100644 (file)
@@ -690,9 +690,11 @@ void Map::updateLighting(enum LightBank bank,
        core::map<v3s16, bool> light_sources;
 
        core::map<v3s16, u8> unlight_from;
+
+       int num_bottom_invalid = 0;
        
        {
-       //TimeTaker t("first stuff");
+       TimeTaker t("first stuff");
 
        core::map<v3s16, MapBlock*>::Iterator i;
        i = a_blocks.getIterator();
@@ -758,6 +760,9 @@ void Map::updateLighting(enum LightBank bank,
                        {
                                bool bottom_valid = block->propagateSunlight(light_sources);
 
+                               if(!bottom_valid)
+                                       num_bottom_invalid++;
+
                                // If bottom is valid, we're done.
                                if(bottom_valid)
                                        break;
@@ -792,6 +797,8 @@ void Map::updateLighting(enum LightBank bank,
        }
 
        }
+
+       infostream<<"num_bottom_invalid="<<num_bottom_invalid<<std::endl;
        
        /*
                Enable this to disable proper lighting for speeding up map
@@ -885,15 +892,15 @@ void Map::updateLighting(enum LightBank bank,
                }
 
                {
-                       TimeTaker timer("unSpreadLight");
+                       //TimeTaker timer("unSpreadLight");
                        vmanip.unspreadLight(bank, unlight_from, light_sources, nodemgr);
                }
                {
-                       TimeTaker timer("spreadLight");
+                       //TimeTaker timer("spreadLight");
                        vmanip.spreadLight(bank, light_sources, nodemgr);
                }
                {
-                       TimeTaker timer("blitBack");
+                       //TimeTaker timer("blitBack");
                        vmanip.blitBack(modified_blocks);
                }
                /*infostream<<"emerge_time="<<emerge_time<<std::endl;
index 6fecbb45012ca7a7828586b80d7d48eddd8fb7c1..25fa427aa3ab4864c66382c3fd8652e6a26c9a1b 100644 (file)
@@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "content_sao.h"
 #include "nodedef.h"
 #include "content_mapnode.h" // For content_mapnode_get_new_name
+#include "voxelalgorithms.h"
 
 namespace mapgen
 {
@@ -2335,6 +2336,10 @@ void make_block(BlockMakeData *data)
                }
        }
 
+       /*
+               Calculate lighting
+       */
+
 }
 
 BlockMakeData::BlockMakeData():
index ecced33c341c6d7d8523f6ef540ba4a4fc4e3074..cb01edffdf1511c5b5687ca09b5114a3c2137888 100644 (file)
@@ -55,6 +55,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #define CONTENT_STONE 0
 #define CONTENT_GRASS 0x800
+#define CONTENT_TORCH 100
 
 void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *ndef)
 {
@@ -105,6 +106,22 @@ void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *n
        f.is_ground_content = true;
        idef->registerItem(itemdef);
        ndef->set(i, f);
+
+       /*
+               Torch (minimal definition for lighting tests)
+       */
+       i = CONTENT_TORCH;
+       itemdef = ItemDefinition();
+       itemdef.type = ITEM_NODE;
+       itemdef.name = "default:torch";
+       f = ContentFeatures();
+       f.name = itemdef.name;
+       f.param_type = CPT_LIGHT;
+       f.light_propagates = true;
+       f.sunlight_propagates = true;
+       f.light_source = LIGHT_MAX-1;
+       idef->registerItem(itemdef);
+       ndef->set(i, f);
 }
 
 struct TestUtilities
@@ -488,6 +505,9 @@ struct TestVoxelAlgorithms
 {
        void Run(INodeDefManager *ndef)
        {
+               /*
+                       voxalgo::propagateSunlight
+               */
                {
                        VoxelManipulator v;
                        for(u16 z=0; z<3; z++)
@@ -593,6 +613,40 @@ struct TestVoxelAlgorithms
                                assert(res.bottom_sunlight_valid == true);
                        }
                }
+               /*
+                       voxalgo::clearLightAndCollectSources
+               */
+               {
+                       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(CONTENT_STONE));
+                       v.setNodeNoRef(v3s16(1,1,1), MapNode(CONTENT_TORCH));
+                       {
+                               MapNode n(CONTENT_AIR);
+                               n.setLight(LIGHTBANK_DAY, 1, ndef);
+                               v.setNode(v3s16(1,1,2), n);
+                       }
+                       {
+                               core::map<v3s16, bool> light_sources;
+                               core::map<v3s16, u8> unlight_from;
+                               voxalgo::clearLightAndCollectSources(v, a, LIGHTBANK_DAY,
+                                               ndef, light_sources, unlight_from);
+                               //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
+                               assert(v.getNode(v3s16(0,1,1)).getLight(LIGHTBANK_DAY, ndef)
+                                               == 0);
+                               assert(light_sources.find(v3s16(1,1,1)) != NULL);
+                               assert(light_sources.size() == 1);
+                               assert(unlight_from.find(v3s16(1,1,2)) != NULL);
+                               assert(unlight_from.size() == 1);
+                       }
+               }
        }
 };
 
index ae609e96ad93e95f26ef63761d7b7c04314d653b..ab6cbdfa2cf2aef1a45bf3649804dfd29ee404cc 100644 (file)
@@ -37,6 +37,42 @@ void setLight(VoxelManipulator &v, VoxelArea a, u8 light,
        }
 }
 
+void clearLightAndCollectSources(VoxelManipulator &v, VoxelArea a,
+               enum LightBank bank, INodeDefManager *ndef,
+               core::map<v3s16, bool> & light_sources,
+               core::map<v3s16, u8> & unlight_from)
+{
+       // The full area we shall touch
+       VoxelArea required_a = a;
+       required_a.pad(v3s16(0,0,0));
+       // Make sure we have access to it
+       v.emerge(a);
+
+       for(s32 x=a.MinEdge.X; x<=a.MaxEdge.X; x++)
+       for(s32 z=a.MinEdge.Z; z<=a.MaxEdge.Z; z++)
+       for(s32 y=a.MinEdge.Y; y<=a.MaxEdge.Y; y++)
+       {
+               v3s16 p(x,y,z);
+               MapNode &n = v.getNodeRefUnsafe(p);
+               u8 oldlight = n.getLight(bank, ndef);
+               n.setLight(bank, 0, ndef);
+
+               // If node sources light, add to list
+               u8 source = ndef->get(n).light_source;
+               if(source != 0)
+                       light_sources[p] = true;
+
+               // Collect borders for unlighting
+               if((x==a.MinEdge.X || x == a.MaxEdge.X
+               || y==a.MinEdge.Y || y == a.MaxEdge.Y
+               || z==a.MinEdge.Z || z == a.MaxEdge.Z)
+               && oldlight != 0)
+               {
+                       unlight_from.insert(p, oldlight);
+               }
+       }
+}
+
 SunlightPropagateResult propagateSunlight(VoxelManipulator &v, VoxelArea a,
                bool inexistent_top_provides_sunlight,
                core::map<v3s16, bool> & light_sources,
@@ -69,11 +105,6 @@ SunlightPropagateResult propagateSunlight(VoxelManipulator &v, VoxelArea a,
                        overtop_has_sunlight = (v.getNodeRefUnsafe(p_overtop).getLight(
                                        LIGHTBANK_DAY, ndef) == LIGHT_SUN);
 
-               dstream<<"inexistent_top_provides_sunlight="
-                               <<inexistent_top_provides_sunlight<<std::endl;
-               dstream<<"v.exists(p_overtop)="
-                               <<v.exists(p_overtop)<<std::endl;
-
                // Copy overtop's sunlight all over the place
                u8 incoming_light = overtop_has_sunlight ? LIGHT_SUN : 0;
                for(s32 y=max_y; y>=min_y; y--)
index 3dea233dc9989bd1073c1523a393a194a875e013..9c90e18a9d9dba9c5bfe07649af07acfdf20ca7a 100644 (file)
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #define VOXELALGORITHMS_HEADER
 
 #include "voxel.h"
+#include "mapnode.h"
 
 namespace voxalgo
 {
@@ -30,6 +31,11 @@ namespace voxalgo
 void setLight(VoxelManipulator &v, VoxelArea a, u8 light,
                INodeDefManager *ndef);
 
+void clearLightAndCollectSources(VoxelManipulator &v, VoxelArea a,
+               enum LightBank bank, INodeDefManager *ndef,
+               core::map<v3s16, bool> & light_sources,
+               core::map<v3s16, u8> & unlight_from);
+
 struct SunlightPropagateResult
 {
        bool bottom_sunlight_valid;