Fix random crash in darkness effect in mcl_potions, finish refactoring of mcl_weather.skycolor that also makes darkness effect more reliable

This commit is contained in:
teknomunk 2024-05-27 10:37:42 +00:00
parent 161dd7d379
commit f2a638f8e9
3 changed files with 261 additions and 195 deletions

@ -124,33 +124,16 @@ end
-- Wrapper for updating day/night ratio that respects night vision -- Wrapper for updating day/night ratio that respects night vision
function skycolor.override_day_night_ratio(player, ratio) function skycolor.override_day_night_ratio(player, ratio)
local meta = player:get_meta() player._skycolor_day_night_ratio = ratio
local has_night_vision = meta:get_int("night_vision") == 1 skycolor.update_player_sky_color(player)
local has_darkness = meta:get_int("darkness") == 1 player._skycolor_day_night_ratio = nil
local is_visited_shepherd = meta:get_int("mcl_shepherd:special") == 1
local arg
if has_darkness and not is_visited_shepherd then
if has_night_vision then arg = 0.1
else arg = 0 end
else
-- Apply night vision only for dark sky
local is_dark = minetest.get_timeofday() > 0.8 or minetest.get_timeofday() < 0.2 or mcl_weather.state ~= "none"
local pos = player:get_pos()
local dim = mcl_worlds.pos_to_dimension(pos)
if (has_night_vision or is_visited_shepherd) and is_dark and dim ~= "nether" and dim ~= "end" then
if ratio == nil then
arg = NIGHT_VISION_RATIO
else
arg = math.max(ratio, NIGHT_VISION_RATIO)
end
else
arg = ratio
end
end
player:override_day_night_ratio(arg)
end end
function water_sky(player)
function water_sky(player, sky_data)
local pos = player:get_pos() local pos = player:get_pos()
local water_color = DEFAULT_WATER_COLOR local water_color = DEFAULT_WATER_COLOR
@ -165,8 +148,7 @@ function water_sky(player)
if checkname == "mclx_core:river_water_source" or checkname == "mclx_core:river_water_flowing" then water_color = "#0084FF" end if checkname == "mclx_core:river_water_source" or checkname == "mclx_core:river_water_flowing" then water_color = "#0084FF" end
return { sky_data.sky = { type = "regular",
sky = { type = "regular",
sky_color = { sky_color = {
day_sky = water_color, day_sky = water_color,
day_horizon = water_color, day_horizon = water_color,
@ -181,25 +163,17 @@ function water_sky(player)
}, },
clouds = false, clouds = false,
} }
}
end end
-- Update sky color. If players not specified update sky for all players.
function skycolor.update_sky_color(players)
-- Override day/night ratio as well
players = mcl_weather.skycolor.utils.get_players(players)
for _, player in ipairs(players) do
local dimension_handlers = {}
function dimension_handlers.overworld(player, sky_data)
local pos = player:get_pos() local pos = player:get_pos()
local dim = mcl_worlds.pos_to_dimension(pos)
local has_weather = (mcl_worlds.has_weather(pos) and (mcl_weather.state == "snow" or mcl_weather.state =="rain" or mcl_weather.state == "thunder") and mcl_weather.has_snow(pos)) or ((mcl_weather.state =="rain" or mcl_weather.state == "thunder") and mcl_weather.has_rain(pos)) local has_weather = (mcl_worlds.has_weather(pos) and (mcl_weather.state == "snow" or mcl_weather.state =="rain" or mcl_weather.state == "thunder") and mcl_weather.has_snow(pos)) or ((mcl_weather.state =="rain" or mcl_weather.state == "thunder") and mcl_weather.has_rain(pos))
local res = water_sky(player)
if res and res.sky then
player:set_sky(res.sky)
end
if dim == "overworld" then
local biomesky local biomesky
local biomefog local biomefog
if mg_name ~= "v6" and mg_name ~= "singlenode" then if mg_name ~= "v6" and mg_name ~= "singlenode" then
@ -217,15 +191,14 @@ function skycolor.update_sky_color(players)
if (mcl_weather.state == "none") then if (mcl_weather.state == "none") then
-- Clear weather -- Clear weather
mcl_weather.set_sky_box_clear(player,biomesky,biomefog) mcl_weather.set_sky_box_clear(player,biomesky,biomefog)
player:set_sun({visible = true, sunrise_visible = true}) sky_data.sun = {visible = true, sunrise_visible = true}
player:set_moon({visible = true}) sky_data.moon = {visible = true}
player:set_stars({visible = true}) sky_data.stars = {visible = true}
mcl_weather.skycolor.override_day_night_ratio(player, nil)
elseif not has_weather then elseif not has_weather then
local day_color = mcl_weather.skycolor.get_sky_layer_color(0.15) local day_color = mcl_weather.skycolor.get_sky_layer_color(0.15)
local dawn_color = mcl_weather.skycolor.get_sky_layer_color(0.27) local dawn_color = mcl_weather.skycolor.get_sky_layer_color(0.27)
local night_color = mcl_weather.skycolor.get_sky_layer_color(0.1) local night_color = mcl_weather.skycolor.get_sky_layer_color(0.1)
mcl_weather.set_sky_color(player, { sky_data.sky = {
type = "regular", type = "regular",
sky_color = { sky_color = {
day_sky = day_color, day_sky = day_color,
@ -236,16 +209,16 @@ function skycolor.update_sky_color(players)
night_horizon = night_color, night_horizon = night_color,
}, },
clouds = true, clouds = true,
}) }
player:set_sun({visible = false, sunrise_visible = false}) sky_data.sun = {visible = false, sunrise_visible = false}
player:set_moon({visible = false}) sky_data.moon = {visible = false}
player:set_stars({visible = false}) sky_data.stars = {visible = false}
elseif has_weather then elseif has_weather then
-- Weather skies -- Weather skies
local day_color = mcl_weather.skycolor.get_sky_layer_color(0.5) local day_color = mcl_weather.skycolor.get_sky_layer_color(0.5)
local dawn_color = mcl_weather.skycolor.get_sky_layer_color(0.75) local dawn_color = mcl_weather.skycolor.get_sky_layer_color(0.75)
local night_color = mcl_weather.skycolor.get_sky_layer_color(0) local night_color = mcl_weather.skycolor.get_sky_layer_color(0)
mcl_weather.set_sky_color(player, { sky_data.sky = {
type = "regular", type = "regular",
sky_color = { sky_color = {
day_sky = day_color, day_sky = day_color,
@ -256,24 +229,23 @@ function skycolor.update_sky_color(players)
night_horizon = night_color, night_horizon = night_color,
}, },
clouds = true, clouds = true,
}) }
player:set_sun({visible = false, sunrise_visible = false}) sky_data.sun = {visible = false, sunrise_visible = false}
player:set_moon({visible = false}) sky_data.moon = {visible = false}
player:set_stars({visible = false}) sky_data.stars = {visible = false}
local light_factor = mcl_weather.get_current_light_factor() local light_factor = mcl_weather.get_current_light_factor()
if mcl_weather.skycolor.current_layer_name() == "lightning" then if mcl_weather.skycolor.current_layer_name() == "lightning" then
mcl_weather.skycolor.override_day_night_ratio(player, 1) sky_data.day_night_ratio = 1
elseif light_factor then elseif light_factor then
local time = minetest.get_timeofday() local time = minetest.get_timeofday()
local light_multiplier = get_light_modifier(time) local light_multiplier = get_light_modifier(time)
local new_light = math.max(light_factor * light_multiplier, MINIMUM_LIGHT_LEVEL) local new_light = math.max(light_factor * light_multiplier, MINIMUM_LIGHT_LEVEL)
mcl_weather.skycolor.override_day_night_ratio(player, new_light) sky_data.day_night_ratio = new_light
else
mcl_weather.skycolor.override_day_night_ratio(player, nil)
end end
end end
elseif dim == "end" then end
dimension_handlers["end"] = function(player, sky_data)
local biomesky = "#000000" local biomesky = "#000000"
local biomefog = "#A080A0" local biomefog = "#A080A0"
if mg_name ~= "v6" and mg_name ~= "singlenode" then if mg_name ~= "v6" and mg_name ~= "singlenode" then
@ -289,16 +261,17 @@ function skycolor.update_sky_color(players)
end end
end end
local t = "mcl_playerplus_end_sky.png" local t = "mcl_playerplus_end_sky.png"
player:set_sky({ type = "skybox", sky_data.sky = { type = "skybox",
base_color = biomesky, base_color = biomesky,
textures = {t,t,t,t,t,t}, textures = {t,t,t,t,t,t},
clouds = false, clouds = false,
}) }
player:set_sun({visible = false , sunrise_visible = false}) sky_data.sun = {visible = false , sunrise_visible = false}
player:set_moon({visible = false}) sky_data.moon = {visible = false}
player:set_stars({visible = false}) sky_data.stars = {visible = false}
mcl_weather.skycolor.override_day_night_ratio(player, 0.5) sky_data.day_night_ratio = 0.5
elseif dim == "nether" then end
function dimension_handlers.nether(player, sky_data)
local biomesky = "#6EB1FF" local biomesky = "#6EB1FF"
local biomefog = "#330808" local biomefog = "#330808"
if mg_name ~= "v6" and mg_name ~= "singlenode" then if mg_name ~= "v6" and mg_name ~= "singlenode" then
@ -313,7 +286,7 @@ function skycolor.update_sky_color(players)
--minetest.log("action", string.format("No biome for number: %s in biome: %s", tostring(biome_index), biome_name)) --minetest.log("action", string.format("No biome for number: %s in biome: %s", tostring(biome_index), biome_name))
end end
end end
mcl_weather.set_sky_color(player, { sky_data.sky = {
type = "regular", type = "regular",
sky_color = { sky_color = {
day_sky = biomefog, day_sky = biomefog,
@ -328,20 +301,112 @@ function skycolor.update_sky_color(players)
fog_tint_type = "custom" fog_tint_type = "custom"
}, },
clouds = false, clouds = false,
}) }
player:set_sun({visible = false , sunrise_visible = false}) sky_data.sun = {visible = false , sunrise_visible = false}
player:set_moon({visible = false}) sky_data.moon = {visible = false}
player:set_stars({visible = false}) sky_data.stars = {visible = false}
mcl_weather.skycolor.override_day_night_ratio(player, nil) end
elseif dim == "void" then function dimension_handlers.void(player, sky_data)
player:set_sky({ type = "plain", sky_data.sky = { type = "plain",
base_color = "#000000", base_color = "#000000",
clouds = false, clouds = false,
}) }
player:set_sun({visible = false, sunrise_visible = false}) sky_data.sun = {visible = false, sunrise_visible = false}
player:set_moon({visible = false}) sky_data.moon = {visible = false}
player:set_stars({visible = false}) sky_data.stars = {visible = false}
end
function dimension(player, sky_data)
local pos = player:get_pos()
local dim = mcl_worlds.pos_to_dimension(pos)
local handler = dimension_handlers[dim]
if handler then return handler(player, sky_data) end
end
local effects_handlers = {}
function effects_handlers.darkness(player, meta, effect, sky_data)
-- No darkness effect if visited by shepherd
if meta:get_int("mcl_shepherd:special") == 1 then return end
-- High stars
sky_data.stars = {visible = false}
-- Minor visibility if the player has the night vision effect
if mcl_potions.has_effect(player, "night_vision") then
sky_data.day_night_ratio = 0.1
else
sky_data.day_night_ratio = 0
end end
end
local DIM_ALLOW_NIGHT_VISION = {
overworld = true,
void = true,
}
function effects_handlers.night_vision(player, meta, effect, sky_data)
-- Apply night vision only for dark sky
if not (minetest.get_timeofday() > 0.8 or minetest.get_timeofday() < 0.2 or mcl_weather.state ~= "none") then return end
-- Only some dimensions allow night vision
local pos = player:get_pos()
local dim = mcl_worlds.pos_to_dimension(pos)
if not DIM_ALLOW_NIGHT_VISION[dim] then return end
-- Apply night vision
sky_data.day_night_ratio = math.max(sky_data.day_night_ratio or 0, NIGHT_VISION_RATIO)
end
local has_mcl_potions = false
local function effects(player, sky_data)
if not has_mcl_potions then
if not minetest.get_modpath("mcl_potions") then return end
has_mcl_potions = true
end
local meta = player:get_meta()
for name,effect in pairs(mcl_potions.registered_effects) do
local effect_data = mcl_potions.get_effect(player, name)
if effect_data then
local hook = effect.mcl_weather_skycolor or effects_handlers[name]
if hook then hook(player, meta, effect_data, sky_data) end
end
end
-- Handle night vision for shepheard
if meta:get_int("mcl_shepherd:special") == 1 then
return effects_handlers.night_vision(player, meta, {}, sky_data)
end
end
function skycolor.update_player_sky_color(player)
local sky_data = {
day_night_ratio = player._skycolor_day_night_ratio
}
water_sky(player, sky_data)
dimension(player, sky_data)
effects(player, sky_data)
if sky_data.sky then player:set_sky(sky_data.sky) end
if sky_data.sun then player:set_sun(sky_data.sun) end
if sky_data.moon then player:set_moon(sky_data.moon) end
if sky_data.stars then player:set_stars(sky_data.stars) end
player:override_day_night_ratio(sky_data.day_night_ratio)
end
-- Update sky color. If players not specified update sky for all players.
function skycolor.update_sky_color(players)
-- Override day/night ratio as well
players = mcl_weather.skycolor.utils.get_players(players)
local update = skycolor.update_player_sky_color
for _, player in ipairs(players) do
update(player)
end end
end -- END function skycolor.update_sky_color(players) end -- END function skycolor.update_sky_color(players)

@ -155,6 +155,7 @@ local function drink_milk_delayed(itemstack, player, pointed_thing)
mcl_hunger.stop_poison(player) mcl_hunger.stop_poison(player)
end end
mcl_potions._reset_effects(player) mcl_potions._reset_effects(player)
mcl_weather.skycolor.update_player_sky_color(player)
end end
-- Wrapper for handling mcl_hunger delayed eating -- Wrapper for handling mcl_hunger delayed eating

@ -492,7 +492,7 @@ mcl_potions.register_effect({
end, end,
on_step = function(dtime, object, factor, duration) on_step = function(dtime, object, factor, duration)
if object:get_meta():get_int("night_vision") ~= 1 then if object:get_meta():get_int("night_vision") ~= 1 then
local flash = EF.darkness[object].flash local flash = EF.darkness[object].flash or 0
if flash < 0.2 then EF.darkness[object].flashdir = true if flash < 0.2 then EF.darkness[object].flashdir = true
elseif flash > 0.6 then EF.darkness[object].flashdir = false end elseif flash > 0.6 then EF.darkness[object].flashdir = false end
flash = EF.darkness[object].flashdir and (flash + dtime) or (flash - dtime) flash = EF.darkness[object].flashdir and (flash + dtime) or (flash - dtime)