1
0
Fork 0

Refactor weather change code

This commit is contained in:
Wuzzy 2017-11-20 04:16:49 +01:00
parent fd32ee0318
commit 547eb893af
1 changed files with 32 additions and 31 deletions

View File

@ -120,7 +120,7 @@ minetest.register_globalstep(function(dtime)
end end
end) end)
-- sets random weather (which could be 'regular' (no weather)). -- Sets random weather (which could be 'none' (no weather)).
weather.set_random_weather = function(weather_name, weather_meta) weather.set_random_weather = function(weather_name, weather_meta)
if (weather_meta ~= nil) then if (weather_meta ~= nil) then
local transitions = weather_meta.transitions local transitions = weather_meta.transitions
@ -133,11 +133,26 @@ weather.set_random_weather = function(weather_name, weather_meta)
end end
end end
if new_weather then if new_weather then
weather.change_weather(new_weather)
end
end
end
weather.change_weather = function(new_weather)
if (weather.reg_weathers ~= nil and weather.reg_weathers[new_weather] ~= nil) then
if (weather.state ~= nil and weather.reg_weathers[weather.state] ~= nil) then
weather.reg_weathers[weather.state].clear()
end
weather.state = new_weather weather.state = new_weather
weather.end_time = weather.get_rand_end_time(weather.reg_weathers[weather.state].min_duration, weather.reg_weathers[weather.state].max_duration) local weather_meta = weather.reg_weathers[weather.state]
weather.reg_weathers[weather_name].clear() weather.end_time = weather.get_rand_end_time(weather_meta.min_duration, weather_meta.max_duration)
return true
end end
return false
end end
weather.get_weather = function()
return weather.state
end end
minetest.register_privilege("weather_manager", { minetest.register_privilege("weather_manager", {
@ -152,28 +167,19 @@ minetest.register_chatcommand("weather", {
privs = {weather_manager = true}, privs = {weather_manager = true},
func = function(name, param) func = function(name, param)
if (param == "") then if (param == "") then
minetest.chat_send_player(name, "Error: No weather specified.") return false, "Error: No weather specified."
return
end end
local success = false local new_weather
if (param == "clear") then if param == "clear" then
weather.reg_weathers[weather.state].clear() new_weather = "none"
weather.state = "none"
weather.end_time = weather.get_rand_end_time()
success = true
return
end
if (weather.reg_weathers ~= nil and weather.reg_weathers[param] ~= nil) then
if (weather.state ~= nil and weather.state ~= "none" and weather.reg_weathers[weather.state] ~= nil) then
weather.reg_weathers[weather.state].clear()
local weather_meta = weather.reg_weathers[weather.state]
weather.end_time = weather.get_rand_end_time(weather_meta.min_duration, weather_meta.max_duration)
end
weather.state = param
return
else else
minetest.chat_send_player(name, "Error: Invalid weather specified. Use “clear”, “rain”, “snow” or “thunder”.") new_weather = param
end
local success = weather.change_weather(new_weather)
if success then
return true
else
return false, "Error: Invalid weather specified. Use “clear”, “rain”, “snow” or “thunder”."
end end
end end
}) })
@ -185,18 +191,13 @@ minetest.register_chatcommand("toggledownfall", {
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 weather.state ~= "none" then if weather.state ~= "none" then
weather.reg_weathers[weather.state].clear() return weather.change_weather("none")
weather.state = "none"
weather.end_time = weather.get_rand_end_time()
-- 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)
weather.reg_weathers[weather.state].clear() return weather.change_weather(new[r])
weather.state = new[r]
local weather_meta = weather.reg_weathers[weather.state]
weather.end_time = weather.get_rand_end_time(weather_meta.min_duration, weather_meta.max_duration)
end end
end end
}) })