Allow mods to strike with our code, optionally at a certain pos.

This commit is contained in:
Auke Kok 2016-03-10 12:56:10 -08:00
parent 3c4c9d0e75
commit 9bdcdafae2

View File

@ -43,26 +43,25 @@ end
minetest.register_globalstep(revertsky) minetest.register_globalstep(revertsky)
lightning.strike = function() -- select a random strike point, midpoint
if lightning.auto then local function choose_pos(pos)
minetest.after(rng:next(lightning.interval_low, lightning.interval_high), lightning.strike) if not pos then
fi local playerlist = minetest.get_connected_players()
local playercount = table.getn(playerlist)
local playerlist = minetest.get_connected_players() -- nobody on
local playercount = table.getn(playerlist) if playercount == 0 then
return nil, nil
end
-- nobody on local r = rng:next(1, playercount)
if playercount == 0 then local randomplayer = playerlist[r]
return pos = randomplayer:getpos()
end end
local r = rng:next(1, playercount)
local randomplayer = playerlist[r]
local pos = randomplayer:getpos()
-- avoid striking underground -- avoid striking underground
if pos.y < -20 then if pos.y < -20 then
return return nil, nil
end end
pos.x = math.floor(pos.x - (lightning.range_h / 2) + rng:next(1, lightning.range_h)) pos.x = math.floor(pos.x - (lightning.range_h / 2) + rng:next(1, lightning.range_h))
@ -72,12 +71,31 @@ lightning.strike = function()
local b, pos2 = minetest.line_of_sight(pos, {x = pos.x, y = pos.y - lightning.range_v, z = pos.z}, 1) local b, pos2 = minetest.line_of_sight(pos, {x = pos.x, y = pos.y - lightning.range_v, z = pos.z}, 1)
-- nothing but air found -- nothing but air found
if b then if b then
return return nil, nil
end end
local n = minetest.get_node({x = pos2.x, y = pos2.y - 1/2, z = pos2.z}) local n = minetest.get_node({x = pos2.x, y = pos2.y - 1/2, z = pos2.z})
if n.name == "air" or n.name == "ignore" then if n.name == "air" or n.name == "ignore" then
return return nil, nil
end
return pos, pos2
end
-- lightning strike API
-- * pos: optional, if not given a random pos will be chosen
-- * returns: bool - success if a strike happened
lightning.strike = function(pos)
if lightning.auto then
minetest.after(rng:next(lightning.interval_low, lightning.interval_high), lightning.strike)
end
local pos2
if not pos then
pos, pos2 = choose_pos()
if not pos then
return false
end
end end
minetest.add_particlespawner({ minetest.add_particlespawner({
@ -104,7 +122,8 @@ lightning.strike = function()
minetest.sound_play({ pos = pos, name = "lightning_thunder", gain = 10, max_hear_distance = 500 }) minetest.sound_play({ pos = pos, name = "lightning_thunder", gain = 10, max_hear_distance = 500 })
for i = 1, playercount do local playerlist = minetest.get_connected_players()
for i = 1, #playerlist do
local sky = {} local sky = {}
sky.bgcolor, sky.type, sky.textures = playerlist[i]:get_sky() sky.bgcolor, sky.type, sky.textures = playerlist[i]:get_sky()
table.insert(ps, { p = playerlist[i], sky = sky}) table.insert(ps, { p = playerlist[i], sky = sky})