voxalgo::clearLightAndCollectSources

master
Perttu Ahola 2012-01-27 13:24:06 +02:00
parent 56496ad5d8
commit 0f3c2f6541
5 changed files with 112 additions and 9 deletions

View 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;

View 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():

View 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);
}
}
}
};

View 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--)

View 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;