From 56496ad5d8a7662b0ae5c9f25d1348cb7b677b65 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Fri, 27 Jan 2012 12:58:52 +0200 Subject: Implement propagateSunlight for VoxelManipulator --- src/voxelalgorithms.cpp | 126 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/voxelalgorithms.cpp (limited to 'src/voxelalgorithms.cpp') diff --git a/src/voxelalgorithms.cpp b/src/voxelalgorithms.cpp new file mode 100644 index 000000000..ae609e96a --- /dev/null +++ b/src/voxelalgorithms.cpp @@ -0,0 +1,126 @@ +/* +Minetest-c55 +Copyright (C) 2010-2012 celeron55, Perttu Ahola + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "voxelalgorithms.h" +#include "nodedef.h" + +namespace voxalgo +{ + +void setLight(VoxelManipulator &v, VoxelArea a, u8 light, + INodeDefManager *ndef) +{ + 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); + n.setLight(LIGHTBANK_DAY, light, ndef); + n.setLight(LIGHTBANK_NIGHT, light, ndef); + } +} + +SunlightPropagateResult propagateSunlight(VoxelManipulator &v, VoxelArea a, + bool inexistent_top_provides_sunlight, + core::map & light_sources, + INodeDefManager *ndef) +{ + // Return values + bool bottom_sunlight_valid = true; + + // The full area we shall touch extends one extra at top and bottom + VoxelArea required_a = a; + required_a.pad(v3s16(0,1,0)); + // Make sure we have access to it + v.emerge(a); + + s16 max_y = a.MaxEdge.Y; + s16 min_y = a.MinEdge.Y; + + for(s32 x=a.MinEdge.X; x<=a.MaxEdge.X; x++) + for(s32 z=a.MinEdge.Z; z<=a.MaxEdge.Z; z++) + { + v3s16 p_overtop(x, max_y+1, z); + bool overtop_has_sunlight = false; + // If overtop node does not exist, trust heuristics + if(!v.exists(p_overtop)) + overtop_has_sunlight = inexistent_top_provides_sunlight; + else if(v.getNodeRefUnsafe(p_overtop).getContent() == CONTENT_IGNORE) + overtop_has_sunlight = inexistent_top_provides_sunlight; + // Otherwise refer to it's light value + else + overtop_has_sunlight = (v.getNodeRefUnsafe(p_overtop).getLight( + LIGHTBANK_DAY, ndef) == LIGHT_SUN); + + dstream<<"inexistent_top_provides_sunlight=" + <