Skip to content

Instantly share code, notes, and snippets.

@sapier
Last active March 9, 2022 00:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sapier/7863517 to your computer and use it in GitHub Desktop.
Save sapier/7863517 to your computer and use it in GitHub Desktop.
Minetest get light from VoxelManip data
local light_lookup = {
{4250+125, 150},
{4500+125, 150},
{4750+125, 250},
{5000+125, 350},
{5250+125, 500},
{5500+125, 675},
{5750+125, 875},
{6000+125, 1000},
{6250+125, 1000}
}
function get_day_night_ratio(time)
--make sure time is between 0 and 240000
if time < 0 then
time = time - (((time*-1)/24000)*24000)
end
if time > 24000 then
time = time + ((time/24000)*24000)
end
--invert time for sunset
if time > 12000 then
time = 24000 - time
end
local dnr = 1000
for i=1,#light_lookup,1 do
if time < light_lookup[i][1] then
dnr = light_lookup[i][2]
break
end
end
return dnr
end
function get_light_from_voxelmanip(node_data,light_data,area,pos,time)
if not area:containsp(pos) then
return minetest.get_node_light(pos, time)
end
pos = vector.round(pos)
local index = area:indexp(pos)
local raw_light_value = light_data[index]
if raw_light_value == nil then
return nil
end
local light_day = nil
local light_night = nil
--read node information
local content_id = node_data[index]
local nodename = minetest.get_name_from_content_id(content_id)
local nodedef = minetest.registered_nodes[nodename]
-- check for solid node
if nodedef.paramtype ~= "light" then
light_day = 0
light_night = 0
else
light_day = raw_light_value % 16
light_night = (raw_light_value - light_day)/16
end
--check lightsource
if nodedef.light_source ~= nil then
if nodedef.light_source > light_day then
light_day = nodedef.light_source
end
if nodedef.light_source > light_night then
light_night = nodedef.light_source
end
end
time = time *24000
time = time %24000
local dnr = get_day_night_ratio(time)
local c = 1000
local current_light = ((dnr * light_day + (c-dnr) * light_night))/c
if(current_light > LIGHT_MAX+1) then
current_light = LIGHT_MAX+1
end
return math.floor(current_light)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment