Line data Source code
1 : /*
2 : Minetest
3 : Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 :
5 : This program is free software; you can redistribute it and/or modify
6 : it under the terms of the GNU Lesser General Public License as published by
7 : the Free Software Foundation; either version 2.1 of the License, or
8 : (at your option) any later version.
9 :
10 : This program is distributed in the hope that it will be useful,
11 : but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : GNU Lesser General Public License for more details.
14 :
15 : You should have received a copy of the GNU Lesser General Public License along
16 : with this program; if not, write to the Free Software Foundation, Inc.,
17 : 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 : */
19 :
20 : #include "voxelalgorithms.h"
21 : #include "nodedef.h"
22 :
23 : namespace voxalgo
24 : {
25 :
26 0 : void setLight(VoxelManipulator &v, VoxelArea a, u8 light,
27 : INodeDefManager *ndef)
28 : {
29 0 : for(s32 x=a.MinEdge.X; x<=a.MaxEdge.X; x++)
30 0 : for(s32 z=a.MinEdge.Z; z<=a.MaxEdge.Z; z++)
31 0 : for(s32 y=a.MinEdge.Y; y<=a.MaxEdge.Y; y++)
32 : {
33 0 : v3s16 p(x,y,z);
34 0 : MapNode &n = v.getNodeRefUnsafe(p);
35 0 : n.setLight(LIGHTBANK_DAY, light, ndef);
36 0 : n.setLight(LIGHTBANK_NIGHT, light, ndef);
37 : }
38 0 : }
39 :
40 0 : void clearLightAndCollectSources(VoxelManipulator &v, VoxelArea a,
41 : enum LightBank bank, INodeDefManager *ndef,
42 : std::set<v3s16> & light_sources,
43 : std::map<v3s16, u8> & unlight_from)
44 : {
45 : // The full area we shall touch
46 0 : VoxelArea required_a = a;
47 0 : required_a.pad(v3s16(0,0,0));
48 : // Make sure we have access to it
49 0 : v.addArea(a);
50 :
51 0 : for(s32 x=a.MinEdge.X; x<=a.MaxEdge.X; x++)
52 0 : for(s32 z=a.MinEdge.Z; z<=a.MaxEdge.Z; z++)
53 0 : for(s32 y=a.MinEdge.Y; y<=a.MaxEdge.Y; y++)
54 : {
55 0 : v3s16 p(x,y,z);
56 0 : MapNode &n = v.getNodeRefUnsafe(p);
57 0 : u8 oldlight = n.getLight(bank, ndef);
58 0 : n.setLight(bank, 0, ndef);
59 :
60 : // If node sources light, add to list
61 0 : u8 source = ndef->get(n).light_source;
62 0 : if(source != 0)
63 0 : light_sources.insert(p);
64 :
65 : // Collect borders for unlighting
66 0 : if((x==a.MinEdge.X || x == a.MaxEdge.X
67 0 : || y==a.MinEdge.Y || y == a.MaxEdge.Y
68 0 : || z==a.MinEdge.Z || z == a.MaxEdge.Z)
69 0 : && oldlight != 0)
70 : {
71 0 : unlight_from[p] = oldlight;
72 : }
73 : }
74 0 : }
75 :
76 0 : SunlightPropagateResult propagateSunlight(VoxelManipulator &v, VoxelArea a,
77 : bool inexistent_top_provides_sunlight,
78 : std::set<v3s16> & light_sources,
79 : INodeDefManager *ndef)
80 : {
81 : // Return values
82 0 : bool bottom_sunlight_valid = true;
83 :
84 : // The full area we shall touch extends one extra at top and bottom
85 0 : VoxelArea required_a = a;
86 0 : required_a.pad(v3s16(0,1,0));
87 : // Make sure we have access to it
88 0 : v.addArea(a);
89 :
90 0 : s16 max_y = a.MaxEdge.Y;
91 0 : s16 min_y = a.MinEdge.Y;
92 :
93 0 : for(s32 x=a.MinEdge.X; x<=a.MaxEdge.X; x++)
94 0 : for(s32 z=a.MinEdge.Z; z<=a.MaxEdge.Z; z++)
95 : {
96 0 : v3s16 p_overtop(x, max_y+1, z);
97 0 : bool overtop_has_sunlight = false;
98 : // If overtop node does not exist, trust heuristics
99 0 : if(!v.exists(p_overtop))
100 0 : overtop_has_sunlight = inexistent_top_provides_sunlight;
101 0 : else if(v.getNodeRefUnsafe(p_overtop).getContent() == CONTENT_IGNORE)
102 0 : overtop_has_sunlight = inexistent_top_provides_sunlight;
103 : // Otherwise refer to it's light value
104 : else
105 0 : overtop_has_sunlight = (v.getNodeRefUnsafe(p_overtop).getLight(
106 0 : LIGHTBANK_DAY, ndef) == LIGHT_SUN);
107 :
108 : // Copy overtop's sunlight all over the place
109 0 : u8 incoming_light = overtop_has_sunlight ? LIGHT_SUN : 0;
110 0 : for(s32 y=max_y; y>=min_y; y--)
111 : {
112 0 : v3s16 p(x,y,z);
113 0 : MapNode &n = v.getNodeRefUnsafe(p);
114 0 : if(incoming_light == 0){
115 : // Do nothing
116 0 : } else if(incoming_light == LIGHT_SUN &&
117 0 : ndef->get(n).sunlight_propagates){
118 : // Do nothing
119 0 : } else if(ndef->get(n).sunlight_propagates == false){
120 0 : incoming_light = 0;
121 : } else {
122 0 : incoming_light = diminish_light(incoming_light);
123 : }
124 0 : u8 old_light = n.getLight(LIGHTBANK_DAY, ndef);
125 :
126 0 : if(incoming_light > old_light)
127 0 : n.setLight(LIGHTBANK_DAY, incoming_light, ndef);
128 :
129 0 : if(diminish_light(incoming_light) != 0)
130 0 : light_sources.insert(p);
131 : }
132 :
133 : // Check validity of sunlight at top of block below if it
134 : // hasn't already been proven invalid
135 0 : if(bottom_sunlight_valid)
136 : {
137 0 : bool sunlight_should_continue_down = (incoming_light == LIGHT_SUN);
138 0 : v3s16 p_overbottom(x, min_y-1, z);
139 0 : if(!v.exists(p_overbottom) ||
140 : v.getNodeRefUnsafe(p_overbottom
141 0 : ).getContent() == CONTENT_IGNORE){
142 : // Is not known, cannot compare
143 : } else {
144 : bool overbottom_has_sunlight = (v.getNodeRefUnsafe(p_overbottom
145 0 : ).getLight(LIGHTBANK_DAY, ndef) == LIGHT_SUN);
146 0 : if(sunlight_should_continue_down != overbottom_has_sunlight){
147 0 : bottom_sunlight_valid = false;
148 : }
149 : }
150 : }
151 : }
152 :
153 0 : return SunlightPropagateResult(bottom_sunlight_valid);
154 : }
155 :
156 3 : } // namespace voxalgo
157 :
|