minetest_mod_weather/weather/api.lua

112 lines
3.1 KiB
Lua
Raw Normal View History

2018-04-29 17:22:05 +02:00
weather_mod.registered_downfalls = {}
2020-10-07 07:49:05 +02:00
weather_mod.registered_downfall_count = 0
2020-09-13 07:58:17 +02:00
weather_mod.registered_weather_change_callbacks = {}
2018-04-29 17:22:05 +02:00
local function check_modname_prefix(name)
if name:sub(1,1) == ":" then
-- If the name starts with a colon, we can skip the modname prefix
-- mechanism.
return name:sub(2)
else
-- Enforce that the name starts with the correct mod name.
local modname = minetest.get_current_modname()
if modname == nil then
2018-08-07 10:11:27 +02:00
modname=name:split(":")[1]
2018-04-29 17:22:05 +02:00
end
local expected_prefix = modname .. ":"
if name:sub(1, #expected_prefix) ~= expected_prefix then
error("Name " .. name .. " does not follow naming conventions: " ..
"\"" .. expected_prefix .. "\" or \":\" prefix required")
end
-- Enforce that the name only contains letters, numbers and underscores.
local subname = name:sub(#expected_prefix+1)
if subname:find("[^%w_]") then
error("Name " .. name .. " does not follow naming conventions: " ..
"contains unallowed characters")
end
return name
end
end
2020-02-26 13:36:44 +01:00
local function set_defaults(vt,rt)
for i,v in pairs(rt) do
if not vt[i] then
vt[i] = v
end
end
end
local default_downfall = {
--minimum starting position
2020-10-16 10:55:49 +02:00
min_pos = {x=-15, y=15, z=-15},
2020-02-26 13:36:44 +01:00
--maximum starting position
2020-10-16 10:55:49 +02:00
max_pos = {x=15, y=15, z=15},
2020-02-26 13:36:44 +01:00
--y falling speed
falling_speed = 10,
--number of textures spawned
amount = 15,
2020-02-26 13:36:44 +01:00
--the texture size
size = 25,
2020-10-16 10:55:49 +02:00
--whether lightning should be enabled
2020-02-26 13:36:44 +01:00
enable_lightning=false,
--whether to damage the player
damage_player=false,
--how much wind is needed to trigger the weather
min_wind = 0,
2020-04-14 16:20:42 +02:00
--stops weather
disabled = false,
2020-02-26 13:36:44 +01:00
}
local default_damage = {
--how many half hearts
amount = 1,
2020-04-14 16:20:42 +02:00
--chance to damage: .5 is 50%
2020-02-26 13:36:44 +01:00
chance = 1,
--after how many steps to damage
time = 100
}
2018-04-29 17:22:05 +02:00
function weather_mod.register_downfall(id,def)
local name = check_modname_prefix(id)
if name == "none" then error("\"none\" means none, thanks") end
2020-10-16 10:55:49 +02:00
assert(not weather_mod.registered_downfalls[name], name.." is already registered")
2018-04-29 17:22:05 +02:00
local ndef = table.copy(def)
2020-02-26 13:36:44 +01:00
--what the downfall looks like
2020-10-16 10:55:49 +02:00
assert(ndef.texture,"no texture given")
2020-02-26 13:36:44 +01:00
set_defaults(ndef,default_downfall)
--when to delete the particles
if not ndef.exptime then
2018-04-29 18:50:51 +02:00
ndef.exptime = ndef.max_pos.y / (math.sqrt(ndef.falling_acceleration) + ndef.falling_speed)
2018-04-29 17:22:05 +02:00
end
2020-02-26 13:36:44 +01:00
if ndef.damage_player then
2020-10-07 07:49:05 +02:00
set_defaults(ndef.damage_player,default_damage)
end
--actually register the downfall
2018-05-08 15:24:38 +02:00
weather_mod.registered_downfalls[name]=ndef
2020-10-07 07:49:05 +02:00
weather_mod.registered_downfall_count = weather_mod.registered_downfall_count + 1
2018-05-08 15:24:38 +02:00
end
2020-09-13 07:58:17 +02:00
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
2020-04-14 16:20:42 +02:00
function weather_mod.disable_downfall(id,disable)
local state = disable
if disable == nil then
state = true
end
weather_mod.registered_downfalls[id].disabled = state
end
2020-10-16 10:55:49 +02:00
return weather_mod