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_downfalls = {}
weather_mod.registered_weather_change_callbacks = {}
local function check_modname_prefix(name) local function check_modname_prefix(name)
if name:sub(1,1) == ":" then if name:sub(1,1) == ":" then
@ -87,6 +88,18 @@ function weather_mod.register_downfall(id,def)
weather_mod.registered_downfalls[name]=ndef weather_mod.registered_downfalls[name]=ndef
end 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) function weather_mod.disable_downfall(id,disable)
local state = disable local state = disable
if disable == nil then if disable == nil then
@ -185,9 +198,11 @@ minetest.register_globalstep(function()
lightning.auto = false lightning.auto = false
--rawset(lightning,"auto",false) --rawset(lightning,"auto",false)
end end
weather_mod.handle_weather_change({type = "none", reason = "globalstep"})
else else
local cnum = 10000 * #weather_mod.registered_downfalls
for id,w in pairs(weather_mod.registered_downfalls) do 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 = { weather.wind = {
x = math.random(0,10), x = math.random(0,10),
y = 0, y = 0,
@ -196,6 +211,7 @@ minetest.register_globalstep(function()
if (not w.disabled) and vector.length(weather.wind) >= w.min_wind then if (not w.disabled) and vector.length(weather.wind) >= w.min_wind then
weather.type = id weather.type = id
weather_mod.handle_lightning(w) weather_mod.handle_lightning(w)
weather_mod.handle_weather_change({type = id, wind = true, reason = "globalstep"})
break break
end end
end end

View File

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