From 660c09fa7ce43301cd984e90c478b754291a5b7e Mon Sep 17 00:00:00 2001 From: theFox6 Date: Sun, 13 Sep 2020 07:58:17 +0200 Subject: [PATCH] added on_weather_change callback --- weather/api.lua | 18 +++++++++++++++++- weather/command.lua | 2 ++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/weather/api.lua b/weather/api.lua index 3ee1326..3020e22 100644 --- a/weather/api.lua +++ b/weather/api.lua @@ -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 diff --git a/weather/command.lua b/weather/command.lua index 2a49168..c9fda9e 100644 --- a/weather/command.lua +++ b/weather/command.lua @@ -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