I'm implementing a pure Minecraft clone and I want to implement sunlight.
I have a basic algorithm of sunlight that works perfectly in 2D and I understand how to resize it in 3D. But how can I do it so I do not have to recalculate the sunlight every time I place a block?
Pseudocode, because the current algorithm is not very well written 🙂
FUNC propogateSunlight (x, y, z): IF (x, y, z) is out of range then RETURN IF light_at (x, y, z) is <= 0 then RETURN # PROPOGATE IF light_can_spread (x, y-1, z) AND light_at (x, y-1, z) <light_at (x, y, z) then: set_light_at (x, y-1, z) to light_at (x, y, z) propogateSunlight (x, y-1, z) IF light_can_spread (x-1, y, z) AND light_at (x-1, y, z) <light_at (x, y, z) then: set_light_at (x-1, y, z) to light_at (x, y, z) -1 propogateSunlight (x-1, y, z) * repeat the IF statements above for (x-1, y, z), (x + 1, y, z), (x, y, z-1), (x, y, z + 1) and ( x, y +1, z)
It then sets the top layer to 16 (maximum brightness level) and calls
propogateSunlight at the highest level.
This algorithm does what I want PERFECTLY. The only drawback is that it's really very slow.
So, what algorithm (psuedocode) is there to place / remove blocks (without transparency) that allow not to have to recalculate the whole world?
P.S. The coordinate system is a standard 3D graph, XZ is a ground plane, + Y is at the top and -Y is at the bottom.