2017-11-21 00:35:31 +00:00
|
|
|
mcl_weather = {
|
2017-02-20 18:05:26 +00:00
|
|
|
-- weather states, 'none' is default, other states depends from active mods
|
|
|
|
state = "none",
|
|
|
|
|
|
|
|
-- player list for saving player meta info
|
|
|
|
players = {},
|
|
|
|
|
|
|
|
-- default weather recalculation interval
|
|
|
|
check_interval = 300,
|
|
|
|
|
|
|
|
-- weather min duration
|
2017-11-12 04:27:18 +00:00
|
|
|
min_duration = 600,
|
2017-02-20 18:05:26 +00:00
|
|
|
|
|
|
|
-- weather max duration
|
2017-11-12 04:02:20 +00:00
|
|
|
max_duration = 9000,
|
2017-02-20 18:05:26 +00:00
|
|
|
|
|
|
|
-- weather calculated end time
|
|
|
|
end_time = nil,
|
|
|
|
|
|
|
|
-- registered weathers
|
|
|
|
reg_weathers = {},
|
|
|
|
|
|
|
|
-- automaticly calculates intervals and swap weathers
|
|
|
|
auto_mode = true,
|
|
|
|
|
|
|
|
-- global flag to disable/enable ABM logic.
|
|
|
|
allow_abm = true,
|
|
|
|
}
|
|
|
|
|
2017-11-21 00:35:31 +00:00
|
|
|
mcl_weather.reg_weathers["none"] = {
|
|
|
|
min_duration = mcl_weather.min_duration,
|
|
|
|
max_duration = mcl_weather.max_duration,
|
2017-11-22 00:41:23 +00:00
|
|
|
light_factor = nil,
|
2017-11-12 05:57:11 +00:00
|
|
|
transitions = {
|
|
|
|
[50] = "rain",
|
|
|
|
[100] = "snow",
|
|
|
|
},
|
|
|
|
clear = function() end,
|
|
|
|
}
|
|
|
|
|
2017-11-21 00:35:31 +00:00
|
|
|
mcl_weather.get_rand_end_time = function(min_duration, max_duration)
|
2017-11-12 05:57:11 +00:00
|
|
|
local r
|
2017-02-20 18:05:26 +00:00
|
|
|
if min_duration ~= nil and max_duration ~= nil then
|
2017-11-12 05:57:11 +00:00
|
|
|
r = math.random(min_duration, max_duration);
|
2017-02-20 18:05:26 +00:00
|
|
|
else
|
2017-11-21 00:35:31 +00:00
|
|
|
r = math.random(mcl_weather.min_duration, mcl_weather.max_duration);
|
2017-02-20 18:05:26 +00:00
|
|
|
end
|
2017-11-12 05:57:11 +00:00
|
|
|
return minetest.get_gametime() + r
|
2017-02-20 18:05:26 +00:00
|
|
|
end
|
|
|
|
|
2017-11-22 00:41:23 +00:00
|
|
|
mcl_weather.get_current_light_factor = function()
|
|
|
|
if mcl_weather.state == "none" then
|
2017-11-12 03:36:26 +00:00
|
|
|
return nil
|
|
|
|
else
|
2017-11-22 00:41:23 +00:00
|
|
|
return mcl_weather.reg_weathers[mcl_weather.state].light_factor
|
2017-11-12 03:36:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-11 20:14:03 +00:00
|
|
|
-- Returns true if pos is outdoor.
|
|
|
|
-- 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.
|
2017-11-21 00:35:31 +00:00
|
|
|
mcl_weather.is_outdoor = function(pos)
|
2017-11-11 20:14:03 +00:00
|
|
|
local cpos = {x=pos.x, y=pos.y+1, z=pos.z}
|
2017-11-24 02:10:02 +00:00
|
|
|
local dim = mcl_worlds.pos_to_dimension(cpos)
|
2017-11-11 20:14:03 +00:00
|
|
|
if minetest.get_node_light(cpos, 0.5) == 15 and dim == "overworld" then
|
2017-02-20 18:05:26 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- checks if player is undewater. This is needed in order to
|
|
|
|
-- turn off weather particles generation.
|
2017-11-21 00:35:31 +00:00
|
|
|
mcl_weather.is_underwater = function(player)
|
2017-02-20 18:05:26 +00:00
|
|
|
local ppos = player:getpos()
|
|
|
|
local offset = player:get_eye_offset()
|
|
|
|
local player_eye_pos = {x = ppos.x + offset.x,
|
|
|
|
y = ppos.y + offset.y + 1.5,
|
|
|
|
z = ppos.z + offset.z}
|
|
|
|
local node_level = minetest.get_node_level(player_eye_pos)
|
|
|
|
if node_level == 8 or node_level == 7 then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- 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.
|
2017-11-21 00:35:31 +00:00
|
|
|
mcl_weather.get_random_pos_by_player_look_dir = function(player)
|
2017-02-20 18:05:26 +00:00
|
|
|
local look_dir = player:get_look_dir()
|
|
|
|
local player_pos = player:getpos()
|
|
|
|
|
|
|
|
local random_pos_x = 0
|
|
|
|
local random_pos_y = 0
|
|
|
|
local random_pos_z = 0
|
|
|
|
|
|
|
|
if look_dir.x > 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_z = math.random() + math.random(player_pos.z - 2.5, player_pos.z + 5)
|
|
|
|
else
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if look_dir.z > 0 then
|
|
|
|
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)
|
|
|
|
else
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
random_pos_y = math.random() + math.random(player_pos.y + 1, player_pos.y + 3)
|
|
|
|
return random_pos_x, random_pos_y, random_pos_z
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_globalstep(function(dtime)
|
2017-11-21 00:35:31 +00:00
|
|
|
if mcl_weather.auto_mode == false then
|
2017-02-20 18:05:26 +00:00
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2017-11-21 00:35:31 +00:00
|
|
|
if mcl_weather.end_time == nil then
|
|
|
|
mcl_weather.end_time = mcl_weather.get_rand_end_time()
|
2017-11-12 04:14:25 +00:00
|
|
|
end
|
2017-11-12 05:57:11 +00:00
|
|
|
-- recalculate weather
|
2017-11-21 00:35:31 +00:00
|
|
|
if mcl_weather.end_time <= minetest.get_gametime() then
|
|
|
|
mcl_weather.set_random_weather(mcl_weather.state, mcl_weather.reg_weathers[mcl_weather.state])
|
2017-02-20 18:05:26 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2017-11-20 03:16:49 +00:00
|
|
|
-- Sets random weather (which could be 'none' (no weather)).
|
2017-11-21 00:35:31 +00:00
|
|
|
mcl_weather.set_random_weather = function(weather_name, weather_meta)
|
2017-11-12 05:57:11 +00:00
|
|
|
if (weather_meta ~= nil) then
|
|
|
|
local transitions = weather_meta.transitions
|
2017-02-20 18:05:26 +00:00
|
|
|
local random_roll = math.random(0,100)
|
2017-11-12 05:57:11 +00:00
|
|
|
local new_weather
|
|
|
|
for v, weather in pairs(transitions) do
|
|
|
|
if random_roll < v then
|
|
|
|
new_weather = weather
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if new_weather then
|
2017-11-21 00:35:31 +00:00
|
|
|
mcl_weather.change_weather(new_weather)
|
2017-02-20 18:05:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-21 00:35:31 +00:00
|
|
|
mcl_weather.change_weather = function(new_weather)
|
|
|
|
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
|
|
|
|
mcl_weather.reg_weathers[mcl_weather.state].clear()
|
2017-11-20 03:16:49 +00:00
|
|
|
end
|
2017-11-21 00:35:31 +00:00
|
|
|
mcl_weather.state = new_weather
|
|
|
|
local weather_meta = mcl_weather.reg_weathers[mcl_weather.state]
|
|
|
|
mcl_weather.end_time = mcl_weather.get_rand_end_time(weather_meta.min_duration, weather_meta.max_duration)
|
2017-11-22 00:41:23 +00:00
|
|
|
mcl_weather.skycolor.update_sky_color()
|
2017-11-20 03:16:49 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2017-11-21 00:35:31 +00:00
|
|
|
mcl_weather.get_weather = function()
|
|
|
|
return mcl_weather.state
|
2017-11-20 03:16:49 +00:00
|
|
|
end
|
|
|
|
|
2017-02-20 18:05:26 +00:00
|
|
|
minetest.register_privilege("weather_manager", {
|
|
|
|
description = "Gives ability to control weather",
|
|
|
|
give_to_singleplayer = false
|
|
|
|
})
|
|
|
|
|
|
|
|
-- Weather command definition. Set
|
2017-02-20 18:24:23 +00:00
|
|
|
minetest.register_chatcommand("weather", {
|
2017-05-14 19:57:25 +00:00
|
|
|
params = "clear | rain | snow | thunder",
|
2017-02-20 18:24:23 +00:00
|
|
|
description = "Changes the weather to the specified parameter.",
|
2017-02-20 18:05:26 +00:00
|
|
|
privs = {weather_manager = true},
|
|
|
|
func = function(name, param)
|
2017-05-14 20:04:38 +00:00
|
|
|
if (param == "") then
|
2017-11-20 03:16:49 +00:00
|
|
|
return false, "Error: No weather specified."
|
2017-05-14 20:04:38 +00:00
|
|
|
end
|
2017-11-20 03:16:49 +00:00
|
|
|
local new_weather
|
|
|
|
if param == "clear" then
|
|
|
|
new_weather = "none"
|
|
|
|
else
|
|
|
|
new_weather = param
|
2017-02-20 18:05:26 +00:00
|
|
|
end
|
2017-11-21 00:35:31 +00:00
|
|
|
local success = mcl_weather.change_weather(new_weather)
|
2017-11-20 03:16:49 +00:00
|
|
|
if success then
|
|
|
|
return true
|
2017-05-14 20:04:38 +00:00
|
|
|
else
|
2017-11-20 03:16:49 +00:00
|
|
|
return false, "Error: Invalid weather specified. Use “clear”, “rain”, “snow” or “thunder”."
|
2017-02-20 18:05:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
2017-05-23 22:39:28 +00:00
|
|
|
minetest.register_chatcommand("toggledownfall", {
|
|
|
|
params = "",
|
|
|
|
description = "Toggles between clear weather and weather with downfall (randomly rain, thunderstorm or snow)",
|
|
|
|
privs = {weather_manager = true},
|
|
|
|
func = function(name, param)
|
|
|
|
-- Currently rain/thunder/snow: Set weather to clear
|
2017-11-21 00:35:31 +00:00
|
|
|
if mcl_weather.state ~= "none" then
|
|
|
|
return mcl_weather.change_weather("none")
|
2017-11-12 04:02:20 +00:00
|
|
|
|
2017-05-23 22:39:28 +00:00
|
|
|
-- Currently clear: Set weather randomly to rain/thunder/snow
|
|
|
|
else
|
|
|
|
local new = { "rain", "thunder", "snow" }
|
|
|
|
local r = math.random(1, #new)
|
2017-11-21 00:35:31 +00:00
|
|
|
return mcl_weather.change_weather(new[r])
|
2017-05-23 22:39:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
2017-02-20 18:05:26 +00:00
|
|
|
-- 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.
|
2017-08-09 14:17:00 +00:00
|
|
|
local weather_allow_abm = minetest.settings:get_bool("weather_allow_abm")
|
2017-02-20 18:05:26 +00:00
|
|
|
if weather_allow_abm ~= nil and weather_allow_abm == false then
|
2017-11-21 00:35:31 +00:00
|
|
|
mcl_weather.allow_abm = false
|
2017-02-20 18:05:26 +00:00
|
|
|
end
|
2017-11-24 02:48:32 +00:00
|
|
|
|
|
|
|
|