added on_weather_change callback

This commit is contained in:
theFox6 2020-09-13 07:58:17 +02:00
parent 9f4664dddd
commit 660c09fa7c
Signed by: theFox6
GPG Key ID: C884FE8D3BCE128A
2 changed files with 19 additions and 1 deletions

View File

@ -1,4 +1,5 @@
weather_mod.registered_downfalls = {}
weather_mod.registered_weather_change_callbacks = {}
local function check_modname_prefix(name)
if name:sub(1,1) == ":" then
@ -87,6 +88,18 @@ function weather_mod.register_downfall(id,def)
weather_mod.registered_downfalls[name]=ndef
end
function weather_mod.register_on_weather_change(callback)
local ct = type(callback)
assert(ct == "function", "on_weather_change callback must be a function, got a " + ct)
table.insert(weather_mod.registered_weather_change_callbacks,callback)
end
function weather_mod.handle_weather_change(changes)
for _,c in pairs(weather_mod.registered_weather_change_callbacks) do
c(changes)
end
end
function weather_mod.disable_downfall(id,disable)
local state = disable
if disable == nil then
@ -185,9 +198,11 @@ minetest.register_globalstep(function()
lightning.auto = false
--rawset(lightning,"auto",false)
end
weather_mod.handle_weather_change({type = "none", reason = "globalstep"})
else
local cnum = 10000 * #weather_mod.registered_downfalls
for id,w in pairs(weather_mod.registered_downfalls) do
if math.random(1, 50000) == 1 then
if math.random(1, cnum) == 1 then
weather.wind = {
x = math.random(0,10),
y = 0,
@ -196,6 +211,7 @@ minetest.register_globalstep(function()
if (not w.disabled) and vector.length(weather.wind) >= w.min_wind then
weather.type = id
weather_mod.handle_lightning(w)
weather_mod.handle_weather_change({type = id, wind = true, reason = "globalstep"})
break
end
end

View File

@ -23,6 +23,7 @@ minetest.register_chatcommand("setweather", {
else
weather.type = param
weather_mod.handle_lightning()
weather_mod.handle_weather_change({type = param, reason = "command", player = name})
end
end
end
@ -46,6 +47,7 @@ minetest.register_chatcommand("setwind", {
end
if x and z then
weather.wind = vector.new(x,0,z)
weather_mod.handle_weather_change({wind = true, reason = "command", player = name})
else
minetest.chat_send_player(name, param.." are not two comma seperated numbers")
end