minor api update, change thunder max duration

This commit is contained in:
Arturas Norkus 2016-05-10 08:52:28 +03:00
parent 443741aea7
commit c364d40823
6 changed files with 49 additions and 28 deletions

1
description.txt Normal file
View File

@ -0,0 +1 @@
Set of weathers for minetest.

View File

@ -1,6 +1,6 @@
rain = {} rain = {}
rain.particles_count = 50 rain.particles_count = 35
rain.sound_handler = function(player) rain.sound_handler = function(player)
return minetest.sound_play("weather_rain", { return minetest.sound_play("weather_rain", {
@ -136,8 +136,8 @@ rain.make_weather = function()
end end
end end
if weather.known_weathers.rain == nil then if weather.reg_weathers.rain == nil then
weather.known_weathers.rain = { weather.reg_weathers.rain = {
chance = 15, chance = 15,
clear = rain.clear clear = rain.clear
} }

BIN
screenshot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

View File

@ -51,8 +51,8 @@ minetest.register_globalstep(function(dtime)
end) end)
-- register snow weather -- register snow weather
if weather.known_weathers.snow == nil then if weather.reg_weathers.snow == nil then
weather.known_weathers.snow = { weather.reg_weathers.snow = {
chance = 10, chance = 10,
clear = function() end clear = function() end
} }

View File

@ -27,9 +27,11 @@ thunder.clear = function()
end end
-- register thunderstorm weather -- register thunderstorm weather
if weather.known_weathers.thunder == nil then if weather.reg_weathers.thunder == nil then
weather.known_weathers.thunder = { weather.reg_weathers.thunder = {
chance = 5, chance = 5,
clear = thunder.clear clear = thunder.clear,
min_duration = 120,
max_duration = 600,
} }
end end

View File

@ -21,11 +21,18 @@ weather = {
end_time = nil, end_time = nil,
-- registered weathers -- registered weathers
known_weathers = {} reg_weathers = {},
-- automaticly calculates intervals and swap weathers
auto_mode = true
} }
weather.get_rand_end_time = function() weather.get_rand_end_time = function(min_duration, max_duration)
return os.time() + math.random(weather.min_duration, weather.max_duration); if min_duration ~= nil and max_duration ~= nil then
return os.time() + math.random(min_duration, max_duration);
else
return os.time() + math.random(weather.min_duration, weather.max_duration);
end
end end
-- checks if player is undewater. This is needed in order to -- checks if player is undewater. This is needed in order to
@ -84,28 +91,39 @@ function get_random_pos_by_player_look_dir(player)
end end
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
if weather.auto_mode == false then
return 0
end
-- recalculate weather only when there aren't currently any -- recalculate weather only when there aren't currently any
if (weather.state ~= "none") then if (weather.state ~= "none") then
if (weather.end_time ~= nil and weather.end_time <= os.time()) then if (weather.end_time ~= nil and weather.end_time <= os.time()) then
weather.known_weathers[weather.state].clear() weather.reg_weathers[weather.state].clear()
weather.state = "none" weather.state = "none"
end end
end elseif (weather.next_check <= os.time()) then
for weather_name, weather_meta in pairs(weather.reg_weathers) do
if (weather.next_check <= os.time()) then weather.set_random_weather(weather_name, weather_meta)
for reg_weather_name, reg_weather_obj in pairs(weather.known_weathers) do
if (reg_weather_obj ~= nil and reg_weather_obj.chance ~= nil) then
local random_roll = math.random(0,100)
if (random_roll <= reg_weather_obj.chance) then
weather.state = reg_weather_name
weather.end_time = weather.get_rand_end_time()
end
end
end end
-- fallback next_check set, weather 'none' will be.
weather.next_check = os.time() + weather.check_interval weather.next_check = os.time() + weather.check_interval
end end
end) end)
-- sets random weather (which could be 'regular' (no weather)).
weather.set_random_weather = function(weather_name, weather_meta)
if weather.next_check > os.time() then return 0 end
if (weather_meta ~= nil and weather_meta.chance ~= nil) then
local random_roll = math.random(0,100)
if (random_roll <= weather_meta.chance) then
weather.state = weather_name
weather.end_time = weather.get_rand_end_time(weather_meta.min_duration, weather_meta.max_duration)
weather.next_check = os.time() + weather.check_interval
end
end
end
minetest.register_privilege("weather_manager", { minetest.register_privilege("weather_manager", {
description = "Gives ability to control weather", description = "Gives ability to control weather",
give_to_singleplayer = false give_to_singleplayer = false
@ -118,16 +136,16 @@ minetest.register_chatcommand("set_weather", {
privs = {weather_manager = true}, privs = {weather_manager = true},
func = function(name, param) func = function(name, param)
if (param == "none") then if (param == "none") then
if (weather.state ~= nil and weather.known_weathers[weather.state] ~= nil) then if (weather.state ~= nil and weather.reg_weathers[weather.state] ~= nil) then
weather.known_weathers[weather.state].clear() weather.reg_weathers[weather.state].clear()
weather.state = param weather.state = param
end end
weather.state = "none" weather.state = "none"
end end
if (weather.known_weathers ~= nil and weather.known_weathers[param] ~= nil) then if (weather.reg_weathers ~= nil and weather.reg_weathers[param] ~= nil) then
if (weather.state ~= nil and weather.state ~= "none" and weather.known_weathers[weather.state] ~= nil) then if (weather.state ~= nil and weather.state ~= "none" and weather.reg_weathers[weather.state] ~= nil) then
weather.known_weathers[weather.state].clear() weather.reg_weathers[weather.state].clear()
end end
weather.state = param weather.state = param
end end