Merge pull request 'Add error handling to sunlight checking to prevent crashing and improve diagnosis.' (#3624) from fix_crash_solarpanel into master

Reviewed-on: https://git.minetest.land/MineClone2/MineClone2/pulls/3624
This commit is contained in:
ancientmarinerdev 2023-04-06 14:13:13 +00:00
commit 1b4d9cfab7
5 changed files with 28 additions and 22 deletions

View File

@ -64,6 +64,24 @@ function mcl_util.check_dtime_timer(self, dtime, timer_name, threshold)
return false
end
-- Minetest 5.3.0 or less can only measure the light level. This came in at 5.4
-- This function has been known to fail in multiple places so the error handling is added increase safety and improve
-- debugging. See:
-- https://git.minetest.land/MineClone2/MineClone2/issues/1392
function mcl_util.get_natural_light (pos, time)
local status, retVal = pcall(minetest.get_natural_light, pos, time)
if status then
return retVal
else
minetest.log("warning", "Failed to get natural light at pos: " .. dump(pos) .. ", time: " .. dump(time))
if (pos) then
local node = minetest.get_node(pos)
minetest.log("warning", "Node at pos: " .. dump(node.name))
end
end
return 0
end
function mcl_util.file_exists(name)
if type(name) ~= "string" then return end
local f = io.open(name)

View File

@ -643,7 +643,7 @@ function mob_class:do_env_damage()
--minetest.log("warning", "Pos is ignored: " .. dump(pos))
end
local sunlight = minetest.get_natural_light(pos, self.time_of_day)
local sunlight = mcl_util.get_natural_light(pos, self.time_of_day)
if self.light_damage ~= 0 and (sunlight or 0) > 12 then
if self:deal_light_damage(pos, self.light_damage) then

View File

@ -240,7 +240,7 @@ end
local function start_firework_rocket(pos)
local p = get_point_on_circle(pos,math.random(32,64),32)
local n = minetest.get_node(p)
local l = minetest.get_natural_light(pos,0.5)
local l = mcl_util.get_natural_light(pos,0.5)
if n.name ~= "air" or l <= minetest.LIGHT_MAX then return end
local o = minetest.add_entity(p,"mcl_bows:rocket_entity")
o:get_luaentity()._harmless = true

View File

@ -1,7 +1,7 @@
local zombie_siege_enabled = minetest.settings:get_bool("mcl_raids_zombie_siege", false)
local function check_spawn_pos(pos)
return minetest.get_natural_light(pos) < 7
return mcl_util.get_natural_light(pos) < 7
end
local function spawn_zombies(self)

View File

@ -35,29 +35,17 @@ local function path_to_sunlight_exists(position, light_level)
end
local function sunlight_visible(position)
local light_level
-- Minetest 5.4.0+ can measure the daylight level at a position
if nil ~= minetest.get_natural_light then
light_level = minetest.get_natural_light(
position,
nil
)
if light_level >= 12 then
return true
end
else -- Minetest 5.3.0 or less can only measure the light level
local light_level = mcl_util.get_natural_light (position)
if light_level >= 12 then
--minetest.log("Light is greater than 12")
return true
else
local time = minetest.get_timeofday() * 24000
-- only check light level during day
if time > 6000 and time < 18000 then
light_level = minetest.get_node_light(
position,
nil
)
light_level = minetest.get_node_light(position, nil)
if light_level >= 12 then
return path_to_sunlight_exists(
position,
12
)
return path_to_sunlight_exists(position, 12)
end
end
end