minetest_mod_weather/weather/api.lua

126 lines
3.9 KiB
Lua
Raw Normal View History

2018-04-29 17:22:05 +02:00
weather_mod.registered_downfalls = {}
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
modname="journal"
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
function weather_mod.register_downfall(id,def)
local name = check_modname_prefix(id)
if name == "none" then error("\"none\" means none, thanks") end
if weather_mod.registered_downfalls[name]~=nil then error(name.." is already registered") end
local ndef = table.copy(def)
if not ndef.min_pos then --minimum starting position
ndef.min_pos = {x=-9, y=10, z=-9}
end
if not ndef.maxp then --maximum starting position
ndef.max_pos = {x= 9, y=10, z= 9}
end
if not ndef.falling_speed then --y falling speed
ndef.falling_speed = 10
end
if not ndef.amount then --number of textures spawned
ndef.amount = 10
end
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
if not ndef.texture then
error("no texture given")
end
if not ndef.size then
ndef.size = 25
end
2018-05-08 15:24:38 +02:00
if not ndef.enable_lightning then
ndef.enable_lightning=false
end
weather_mod.registered_downfalls[name]=ndef
end
if minetest.get_modpath("lightning") then
2018-05-17 17:20:51 +02:00
rawset(lightning,"auto",false)
2018-05-08 15:24:38 +02:00
end
function weather_mod.handle_lightning()
if not minetest.get_modpath("lightning") then return end
local current_downfall = weather_mod.registered_downfalls[weather.type]
if not current_downfall then return end
2018-05-17 17:20:51 +02:00
rawset(lightning,"auto",current_downfall.enable_lightning)
2018-05-08 15:24:38 +02:00
if current_downfall.enable_lightning and math.random(1,2) == 1 then
local time = math.floor(math.random(lightning.interval_low/2,lightning.interval_low))
minetest.after(time, lightning.strike)
end
2018-04-29 17:22:05 +02:00
end
minetest.register_globalstep(function()
if weather.type=="none" then
2018-05-08 16:23:57 +02:00
for id,_ in pairs(weather_mod.registered_downfalls) do
2018-04-29 17:22:05 +02:00
if math.random(1, 50000) == 1 then
weather.wind = {}
weather.wind.x = math.random(0,10)
weather.wind.y = 0
weather.wind.z = math.random(0,10)
weather.type = id
2018-05-08 15:24:38 +02:00
weather_mod.handle_lightning()
2018-04-29 17:22:05 +02:00
end
end
else
if math.random(1, 10000) == 1 then
weather.type = "none"
2018-05-17 13:43:44 +02:00
if minetest.get_modpath("lightning") then
2018-05-17 17:20:51 +02:00
rawset(lightning,"auto",false)
2018-05-17 13:43:44 +02:00
end
2018-04-29 17:22:05 +02:00
end
end
2018-05-08 15:24:38 +02:00
local current_downfall = weather_mod.registered_downfalls[weather.type]
2018-04-29 17:22:05 +02:00
if current_downfall==nil then return end
for _, player in ipairs(minetest.get_connected_players()) do
local ppos = player:getpos()
2018-07-30 17:25:35 +02:00
if ppos.y > 120 then return end
2018-06-30 17:23:33 +02:00
2018-04-29 17:22:05 +02:00
local wind_pos = vector.multiply(weather.wind,-1)
local minp = vector.add(vector.add(ppos, current_downfall.min_pos),wind_pos)
local maxp = vector.add(vector.add(ppos, current_downfall.max_pos),wind_pos)
local vel = {x=weather.wind.x,y=-current_downfall.falling_speed,z=weather.wind.z}
local acc = {x=0, y=0, z=0}
local exp = current_downfall.exptime
minetest.add_particlespawner({amount=current_downfall.amount, time=0.5,
minpos=minp, maxpos=maxp,
minvel=vel, maxvel=vel,
minacc=acc, maxacc=acc,
minexptime=exp, maxexptime=exp,
minsize=current_downfall.size, maxsize=current_downfall.size,
2018-05-08 15:24:38 +02:00
collisiondetection=true, collision_removal=true,
vertical=true,
2018-04-29 17:22:05 +02:00
texture=current_downfall.texture, player=player:get_player_name()})
end
end)