add basic lightning API

This commit is contained in:
NO11 2021-09-09 09:22:27 +00:00 committed by Gitea
parent e9437e9e1e
commit 8e3f9d2169

View file

@ -31,6 +31,7 @@ lightning = {
size = 100, size = 100,
-- disable this to stop lightning mod from striking -- disable this to stop lightning mod from striking
auto = true, auto = true,
on_strike_functions = {},
} }
local rng = PcgRandom(32321123312123) local rng = PcgRandom(32321123312123)
@ -54,6 +55,18 @@ end
minetest.register_globalstep(revertsky) minetest.register_globalstep(revertsky)
-- lightning strike API
-- See README.md
--[[
lightning.register_on_strike(function(pos, pos2, objects)
-- code
end)
]]
function lightning.register_on_strike(func)
table.insert(lightning.on_strike_functions, func)
end
-- select a random strike point, midpoint -- select a random strike point, midpoint
local function choose_pos(pos) local function choose_pos(pos)
if not pos then if not pos then
@ -94,7 +107,6 @@ local function choose_pos(pos)
return pos, pos2 return pos, pos2
end end
-- lightning strike API
-- * pos: optional, if not given a random pos will be chosen -- * pos: optional, if not given a random pos will be chosen
-- * returns: bool - success if a strike happened -- * returns: bool - success if a strike happened
function lightning.strike(pos) function lightning.strike(pos)
@ -108,21 +120,30 @@ function lightning.strike(pos)
if not pos then if not pos then
return false return false
end end
local objects = get_objects_inside_radius(pos2, 3.5)
if lightning.on_strike_functions then
for _, func in pairs(lightning.on_strike_functions) do
func(pos, pos2, objects)
end
end
end
lightning.register_on_strike(function(pos, pos2, objects)
local particle_pos = vector.offset(pos2, 0, (lightning.size / 2) + 0.5, 0)
local particle_size = lightning.size * 10
local time = 0.2
add_particlespawner({ add_particlespawner({
amount = 1, amount = 1,
time = 0.2, time = time,
-- make it hit the top of a block exactly with the bottom -- make it hit the top of a block exactly with the bottom
minpos = {x = pos2.x, y = pos2.y + (lightning.size / 2) + 1/2, z = pos2.z }, minpos = particle_pos,
maxpos = {x = pos2.x, y = pos2.y + (lightning.size / 2) + 1/2, z = pos2.z }, maxpos = particle_pos,
minvel = {x = 0, y = 0, z = 0}, minexptime = time,
maxvel = {x = 0, y = 0, z = 0}, maxexptime = time,
minacc = {x = 0, y = 0, z = 0}, minsize = particle_size,
maxacc = {x = 0, y = 0, z = 0}, maxsize = particle_size,
minexptime = 0.2,
maxexptime = 0.2,
minsize = lightning.size * 10,
maxsize = lightning.size * 10,
collisiondetection = true, collisiondetection = true,
vertical = true, vertical = true,
-- to make it appear hitting the node that will get set on fire, make sure -- to make it appear hitting the node that will get set on fire, make sure
@ -135,10 +156,7 @@ function lightning.strike(pos)
sound_play({ name = "lightning_thunder", gain = 10 }, { pos = pos, max_hear_distance = 500 }, true) sound_play({ name = "lightning_thunder", gain = 10 }, { pos = pos, max_hear_distance = 500 }, true)
-- damage nearby objects, transform mobs -- damage nearby objects, transform mobs
-- TODO: use an API insteed of hardcoding this behaviour for _, obj in pairs(objects) do
local objs = get_objects_inside_radius(pos2, 3.5)
for o=1, #objs do
local obj = objs[o]
local lua = obj:get_luaentity() local lua = obj:get_luaentity()
-- pig → zombie pigman (no damage) -- pig → zombie pigman (no damage)
if lua and lua.name == "mobs_mc:pig" then if lua and lua.name == "mobs_mc:pig" then
@ -223,8 +241,7 @@ function lightning.strike(pos)
end end
end end
end end
end)
end
-- if other mods disable auto lightning during initialization, don't trigger the first lightning. -- if other mods disable auto lightning during initialization, don't trigger the first lightning.
after(5, function(dtime) after(5, function(dtime)