1
0
Fork 0
kay27 2021-01-28 02:02:52 +04:00
parent a3ccb54376
commit 1f18c2438c
2 changed files with 176 additions and 173 deletions

View File

@ -81,17 +81,16 @@ end)
-- register snow weather -- register snow weather
if mcl_weather.reg_weathers.snow == nil then if mcl_weather.reg_weathers.snow == nil then
mcl_weather.reg_weathers.snow = { mcl_weather.reg_weathers.snow = {
clear = mcl_weather.snow.clear, clear = mcl_weather.snow.clear,
light_factor = 0.6, light_factor = 0.6,
-- 10min - 20min -- 10min - 20min
min_duration = 600, min_duration = 600,
max_duration = 1200, max_duration = 1200,
transitions = { transitions = {
[65] = "none", [65] = "none",
[80] = "rain", [80] = "rain",
[100] = "thunder", [100] = "thunder",
} }
} }
end end

View File

@ -6,8 +6,8 @@ mcl_weather.state = "none"
-- player list for saving player meta info -- player list for saving player meta info
mcl_weather.players = {} mcl_weather.players = {}
-- default weather recalculation interval -- default weather check interval for global step
mcl_weather.check_interval = 300 mcl_weather.check_interval = 5
-- weather min duration -- weather min duration
mcl_weather.min_duration = 600 mcl_weather.min_duration = 600
@ -25,14 +25,14 @@ mcl_weather.reg_weathers = {}
mcl_weather.allow_abm = true mcl_weather.allow_abm = true
mcl_weather.reg_weathers["none"] = { mcl_weather.reg_weathers["none"] = {
min_duration = mcl_weather.min_duration, min_duration = mcl_weather.min_duration,
max_duration = mcl_weather.max_duration, max_duration = mcl_weather.max_duration,
light_factor = nil, light_factor = nil,
transitions = { transitions = {
[50] = "rain", [50] = "rain",
[100] = "snow", [100] = "snow",
}, },
clear = function() end, clear = function() end,
} }
local storage = minetest.get_mod_storage() local storage = minetest.get_mod_storage()
@ -40,216 +40,220 @@ local storage = minetest.get_mod_storage()
local save_weather = function() local save_weather = function()
storage:set_string("mcl_weather_state", mcl_weather.state) storage:set_string("mcl_weather_state", mcl_weather.state)
storage:set_int("mcl_weather_end_time", mcl_weather.end_time) storage:set_int("mcl_weather_end_time", mcl_weather.end_time)
minetest.log("verbose", "[mcl_weather] Weather data saved: state="..mcl_weather.state.." end_time="..mcl_weather.end_time) minetest.log("action", "[mcl_weather] Weather data saved: state="..mcl_weather.state.." end_time="..mcl_weather.end_time)
end end
minetest.register_on_shutdown(save_weather) minetest.register_on_shutdown(save_weather)
mcl_weather.get_rand_end_time = function(min_duration, max_duration) mcl_weather.get_rand_end_time = function(min_duration, max_duration)
local r local r
if min_duration ~= nil and max_duration ~= nil then if min_duration ~= nil and max_duration ~= nil then
r = math.random(min_duration, max_duration); r = math.random(min_duration, max_duration)
else else
r = math.random(mcl_weather.min_duration, mcl_weather.max_duration); r = math.random(mcl_weather.min_duration, mcl_weather.max_duration)
end end
return minetest.get_gametime() + r return minetest.get_gametime() + r
end end
mcl_weather.get_current_light_factor = function() mcl_weather.get_current_light_factor = function()
if mcl_weather.state == "none" then if mcl_weather.state == "none" then
return nil return nil
else else
return mcl_weather.reg_weathers[mcl_weather.state].light_factor return mcl_weather.reg_weathers[mcl_weather.state].light_factor
end end
end end
-- Returns true if pos is outdoor. -- Returns true if pos is outdoor.
-- Outdoor is defined as any node in the Overworld under open sky. -- Outdoor is defined as any node in the Overworld under open sky.
-- FIXME: Nodes below glass also count as “outdoor”, this should not be the case. -- FIXME: Nodes below glass also count as “outdoor”, this should not be the case.
mcl_weather.is_outdoor = function(pos) mcl_weather.is_outdoor = function(pos)
local cpos = {x=pos.x, y=pos.y+1, z=pos.z} local cpos = {x=pos.x, y=pos.y+1, z=pos.z}
local dim = mcl_worlds.pos_to_dimension(cpos) local dim = mcl_worlds.pos_to_dimension(cpos)
if minetest.get_node_light(cpos, 0.5) == 15 and dim == "overworld" then if minetest.get_node_light(cpos, 0.5) == 15 and dim == "overworld" then
return true return true
end end
return false return false
end end
-- checks if player is undewater. This is needed in order to -- checks if player is undewater. This is needed in order to
-- turn off weather particles generation. -- turn off weather particles generation.
mcl_weather.is_underwater = function(player) mcl_weather.is_underwater = function(player)
local ppos = player:get_pos() local ppos = player:get_pos()
local offset = player:get_eye_offset() local offset = player:get_eye_offset()
local player_eye_pos = {x = ppos.x + offset.x, local player_eye_pos = {x = ppos.x + offset.x,
y = ppos.y + offset.y + 1.5, y = ppos.y + offset.y + 1.5,
z = ppos.z + offset.z} z = ppos.z + offset.z}
local node_level = minetest.get_node_level(player_eye_pos) local node_level = minetest.get_node_level(player_eye_pos)
if node_level == 8 or node_level == 7 then if node_level == 8 or node_level == 7 then
return true return true
end end
return false return false
end end
-- trying to locate position for particles by player look direction for performance reason. -- trying to locate position for particles by player look direction for performance reason.
-- it is costly to generate many particles around player so goal is focus mainly on front view. -- it is costly to generate many particles around player so goal is focus mainly on front view.
mcl_weather.get_random_pos_by_player_look_dir = function(player) mcl_weather.get_random_pos_by_player_look_dir = function(player)
local look_dir = player:get_look_dir() local look_dir = player:get_look_dir()
local player_pos = player:get_pos() local player_pos = player:get_pos()
local random_pos_x = 0 local random_pos_x = 0
local random_pos_y = 0 local random_pos_y = 0
local random_pos_z = 0 local random_pos_z = 0
if look_dir.x > 0 then if look_dir.x > 0 then
if look_dir.z > 0 then if look_dir.z > 0 then
random_pos_x = math.random() + math.random(player_pos.x - 2.5, player_pos.x + 5) random_pos_x = math.random() + math.random(player_pos.x - 2.5, player_pos.x + 5)
random_pos_z = math.random() + math.random(player_pos.z - 2.5, player_pos.z + 5) random_pos_z = math.random() + math.random(player_pos.z - 2.5, player_pos.z + 5)
else else
random_pos_x = math.random() + math.random(player_pos.x - 2.5, player_pos.x + 5) random_pos_x = math.random() + math.random(player_pos.x - 2.5, player_pos.x + 5)
random_pos_z = math.random() + math.random(player_pos.z - 5, player_pos.z + 2.5) random_pos_z = math.random() + math.random(player_pos.z - 5, player_pos.z + 2.5)
end end
else else
if look_dir.z > 0 then if look_dir.z > 0 then
random_pos_x = math.random() + math.random(player_pos.x - 5, player_pos.x + 2.5) random_pos_x = math.random() + math.random(player_pos.x - 5, player_pos.x + 2.5)
random_pos_z = math.random() + math.random(player_pos.z - 2.5, player_pos.z + 5) random_pos_z = math.random() + math.random(player_pos.z - 2.5, player_pos.z + 5)
else else
random_pos_x = math.random() + math.random(player_pos.x - 5, player_pos.x + 2.5) random_pos_x = math.random() + math.random(player_pos.x - 5, player_pos.x + 2.5)
random_pos_z = math.random() + math.random(player_pos.z - 5, player_pos.z + 2.5) random_pos_z = math.random() + math.random(player_pos.z - 5, player_pos.z + 2.5)
end end
end end
random_pos_y = math.random() + math.random(player_pos.y + 10, player_pos.y + 15) random_pos_y = math.random() + math.random(player_pos.y + 10, player_pos.y + 15)
return random_pos_x, random_pos_y, random_pos_z return random_pos_x, random_pos_y, random_pos_z
end end
local t, wci = 0, mcl_weather.check_interval
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
if mcl_weather.end_time == nil then t = t + dtime
mcl_weather.end_time = mcl_weather.get_rand_end_time() if t < wci then return end
end t = 0
-- recalculate weather
if mcl_weather.end_time <= minetest.get_gametime() then if mcl_weather.end_time == nil then
local changeWeather = minetest.settings:get_bool("mcl_doWeatherCycle") mcl_weather.end_time = mcl_weather.get_rand_end_time()
if changeWeather == nil then end
changeWeather = true -- recalculate weather
end if mcl_weather.end_time <= minetest.get_gametime() then
if changeWeather then local changeWeather = minetest.settings:get_bool("mcl_doWeatherCycle")
mcl_weather.set_random_weather(mcl_weather.state, mcl_weather.reg_weathers[mcl_weather.state]) if changeWeather == nil then
else changeWeather = true
mcl_weather.end_time = mcl_weather.get_rand_end_time() end
end if changeWeather then
end mcl_weather.set_random_weather(mcl_weather.state, mcl_weather.reg_weathers[mcl_weather.state])
else
mcl_weather.end_time = mcl_weather.get_rand_end_time()
end
end
end) end)
-- Sets random weather (which could be 'none' (no weather)). -- Sets random weather (which could be 'none' (no weather)).
mcl_weather.set_random_weather = function(weather_name, weather_meta) mcl_weather.set_random_weather = function(weather_name, weather_meta)
if (weather_meta ~= nil) then if weather_meta == nil then return end
local transitions = weather_meta.transitions local transitions = weather_meta.transitions
local random_roll = math.random(0,100) local random_roll = math.random(0,100)
local new_weather local new_weather
for v, weather in pairs(transitions) do for v, weather in pairs(transitions) do
if random_roll < v then if random_roll < v then
new_weather = weather new_weather = weather
break break
end end
end end
if new_weather then if new_weather then
mcl_weather.change_weather(new_weather) mcl_weather.change_weather(new_weather)
end end
end
end end
-- Change weather to new_weather. -- Change weather to new_weather.
-- * explicit_end_time is OPTIONAL. If specified, explicitly set the -- * explicit_end_time is OPTIONAL. If specified, explicitly set the
-- gametime (minetest.get_gametime) in which the weather ends. -- gametime (minetest.get_gametime) in which the weather ends.
mcl_weather.change_weather = function(new_weather, explicit_end_time) mcl_weather.change_weather = function(new_weather, explicit_end_time)
if (mcl_weather.reg_weathers ~= nil and mcl_weather.reg_weathers[new_weather] ~= nil) then if (mcl_weather.reg_weathers ~= nil and mcl_weather.reg_weathers[new_weather] ~= nil) then
if (mcl_weather.state ~= nil and mcl_weather.reg_weathers[mcl_weather.state] ~= nil) then if (mcl_weather.state ~= nil and mcl_weather.reg_weathers[mcl_weather.state] ~= nil) then
mcl_weather.reg_weathers[mcl_weather.state].clear() mcl_weather.reg_weathers[mcl_weather.state].clear()
end end
mcl_weather.state = new_weather mcl_weather.state = new_weather
local weather_meta = mcl_weather.reg_weathers[mcl_weather.state] local weather_meta = mcl_weather.reg_weathers[mcl_weather.state]
if explicit_end_time then if explicit_end_time then
mcl_weather.end_time = explicit_end_time mcl_weather.end_time = explicit_end_time
else else
mcl_weather.end_time = mcl_weather.get_rand_end_time(weather_meta.min_duration, weather_meta.max_duration) mcl_weather.end_time = mcl_weather.get_rand_end_time(weather_meta.min_duration, weather_meta.max_duration)
end end
mcl_weather.skycolor.update_sky_color() mcl_weather.skycolor.update_sky_color()
save_weather() save_weather()
return true return true
end end
return false return false
end end
mcl_weather.get_weather = function() mcl_weather.get_weather = function()
return mcl_weather.state return mcl_weather.state
end end
minetest.register_privilege("weather_manager", { minetest.register_privilege("weather_manager", {
description = S("Gives ability to control weather"), description = S("Gives ability to control weather"),
give_to_singleplayer = false give_to_singleplayer = false
}) })
-- Weather command definition. Set -- Weather command definition. Set
minetest.register_chatcommand("weather", { minetest.register_chatcommand("weather", {
params = "(clear | rain | snow | thunder) [<duration>]", params = "(clear | rain | snow | thunder) [<duration>]",
description = S("Changes the weather to the specified parameter."), description = S("Changes the weather to the specified parameter."),
privs = {weather_manager = true}, privs = {weather_manager = true},
func = function(name, param) func = function(name, param)
if (param == "") then if (param == "") then
return false, S("Error: No weather specified.") return false, S("Error: No weather specified.")
end end
local new_weather, end_time local new_weather, end_time
local parse1, parse2 = string.match(param, "(%w+) ?(%d*)") local parse1, parse2 = string.match(param, "(%w+) ?(%d*)")
if parse1 then if parse1 then
if parse1 == "clear" then if parse1 == "clear" then
new_weather = "none" new_weather = "none"
else else
new_weather = parse1 new_weather = parse1
end end
else else
return false, S("Error: Invalid parameters.") return false, S("Error: Invalid parameters.")
end end
if parse2 then if parse2 then
if type(tonumber(parse2)) == "number" then if type(tonumber(parse2)) == "number" then
local duration = tonumber(parse2) local duration = tonumber(parse2)
if duration < 1 then if duration < 1 then
return false, S("Error: Duration can't be less than 1 second.") return false, S("Error: Duration can't be less than 1 second.")
end end
end_time = minetest.get_gametime() + duration end_time = minetest.get_gametime() + duration
end end
end end
local success = mcl_weather.change_weather(new_weather, end_time) local success = mcl_weather.change_weather(new_weather, end_time)
if success then if success then
return true return true
else else
return false, S("Error: Invalid weather specified. Use “clear”, “rain”, “snow” or “thunder”.") return false, S("Error: Invalid weather specified. Use “clear”, “rain”, “snow” or “thunder”.")
end end
end end
}) })
minetest.register_chatcommand("toggledownfall", { minetest.register_chatcommand("toggledownfall", {
params = "", params = "",
description = S("Toggles between clear weather and weather with downfall (randomly rain, thunderstorm or snow)"), description = S("Toggles between clear weather and weather with downfall (randomly rain, thunderstorm or snow)"),
privs = {weather_manager = true}, privs = {weather_manager = true},
func = function(name, param) func = function(name, param)
-- Currently rain/thunder/snow: Set weather to clear -- Currently rain/thunder/snow: Set weather to clear
if mcl_weather.state ~= "none" then if mcl_weather.state ~= "none" then
return mcl_weather.change_weather("none") return mcl_weather.change_weather("none")
-- Currently clear: Set weather randomly to rain/thunder/snow -- Currently clear: Set weather randomly to rain/thunder/snow
else else
local new = { "rain", "thunder", "snow" } local new = { "rain", "thunder", "snow" }
local r = math.random(1, #new) local r = math.random(1, #new)
return mcl_weather.change_weather(new[r]) return mcl_weather.change_weather(new[r])
end end
end end
}) })
-- Configuration setting which allows user to disable ABM for weathers (if they use it). -- Configuration setting which allows user to disable ABM for weathers (if they use it).
-- Weather mods expected to be use this flag before registering ABM. -- Weather mods expected to be use this flag before registering ABM.
local weather_allow_abm = minetest.settings:get_bool("weather_allow_abm") local weather_allow_abm = minetest.settings:get_bool("weather_allow_abm")
if weather_allow_abm ~= nil and weather_allow_abm == false then if weather_allow_abm ~= nil and weather_allow_abm == false then
mcl_weather.allow_abm = false mcl_weather.allow_abm = false
end end