2017-02-20 21:05:26 +03:00
|
|
|
--[[
|
|
|
|
|
|
|
|
Copyright (C) 2016 - Auke Kok <sofar@foo-projects.org>
|
2021-05-28 01:07:06 +03:00
|
|
|
Adapted by MineClone2 contributors
|
2017-02-20 21:05:26 +03:00
|
|
|
|
|
|
|
"lightning" is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2.1
|
|
|
|
of the license, or (at your option) any later version.
|
|
|
|
|
|
|
|
--]]
|
|
|
|
|
2021-05-28 01:07:06 +03:00
|
|
|
local S = minetest.get_translator(minetest.get_current_modname())
|
2019-03-07 22:55:56 +03:00
|
|
|
|
2021-03-15 02:12:59 +03:00
|
|
|
local get_connected_players = minetest.get_connected_players
|
|
|
|
local line_of_sight = minetest.line_of_sight
|
|
|
|
local get_node = minetest.get_node
|
|
|
|
local set_node = minetest.set_node
|
|
|
|
local sound_play = minetest.sound_play
|
|
|
|
local add_particlespawner = minetest.add_particlespawner
|
|
|
|
local after = minetest.after
|
|
|
|
local add_entity = minetest.add_entity
|
|
|
|
local get_objects_inside_radius = minetest.get_objects_inside_radius
|
|
|
|
local get_item_group = minetest.get_item_group
|
|
|
|
|
2021-05-28 01:07:06 +03:00
|
|
|
lightning = {
|
2021-09-09 12:22:27 +03:00
|
|
|
interval_low = 17,
|
|
|
|
interval_high = 503,
|
|
|
|
range_h = 100,
|
|
|
|
range_v = 50,
|
|
|
|
size = 100,
|
|
|
|
-- disable this to stop lightning mod from striking
|
|
|
|
auto = true,
|
|
|
|
on_strike_functions = {},
|
2021-05-28 01:07:06 +03:00
|
|
|
}
|
2017-02-20 21:05:26 +03:00
|
|
|
|
|
|
|
local rng = PcgRandom(32321123312123)
|
|
|
|
|
|
|
|
local ps = {}
|
2017-02-21 17:00:16 +03:00
|
|
|
local ttl = -1
|
2017-02-20 21:05:26 +03:00
|
|
|
|
2021-05-28 01:07:06 +03:00
|
|
|
local function revertsky(dtime)
|
2017-02-20 21:05:26 +03:00
|
|
|
if ttl == 0 then
|
|
|
|
return
|
|
|
|
end
|
2017-02-21 06:20:43 +03:00
|
|
|
ttl = ttl - dtime
|
2017-02-20 21:05:26 +03:00
|
|
|
if ttl > 0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-11-21 03:35:31 +03:00
|
|
|
mcl_weather.skycolor.remove_layer("lightning")
|
2017-02-20 21:05:26 +03:00
|
|
|
|
|
|
|
ps = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_globalstep(revertsky)
|
|
|
|
|
2021-09-09 12:22:27 +03:00
|
|
|
-- lightning strike API
|
|
|
|
|
2021-09-09 15:35:50 +03:00
|
|
|
-- See API.md
|
2021-09-09 12:22:27 +03:00
|
|
|
--[[
|
|
|
|
lightning.register_on_strike(function(pos, pos2, objects)
|
|
|
|
-- code
|
|
|
|
end)
|
|
|
|
]]
|
|
|
|
function lightning.register_on_strike(func)
|
|
|
|
table.insert(lightning.on_strike_functions, func)
|
|
|
|
end
|
|
|
|
|
2017-02-20 21:05:26 +03:00
|
|
|
-- select a random strike point, midpoint
|
|
|
|
local function choose_pos(pos)
|
|
|
|
if not pos then
|
2021-03-15 02:12:59 +03:00
|
|
|
local playerlist = get_connected_players()
|
2017-02-20 21:05:26 +03:00
|
|
|
local playercount = table.getn(playerlist)
|
|
|
|
|
|
|
|
-- nobody on
|
|
|
|
if playercount == 0 then
|
|
|
|
return nil, nil
|
|
|
|
end
|
|
|
|
|
|
|
|
local r = rng:next(1, playercount)
|
|
|
|
local randomplayer = playerlist[r]
|
2019-02-01 08:33:07 +03:00
|
|
|
pos = randomplayer:get_pos()
|
2017-02-20 21:05:26 +03:00
|
|
|
|
|
|
|
-- avoid striking underground
|
|
|
|
if pos.y < -20 then
|
|
|
|
return nil, nil
|
|
|
|
end
|
|
|
|
|
|
|
|
pos.x = math.floor(pos.x - (lightning.range_h / 2) + rng:next(1, lightning.range_h))
|
|
|
|
pos.y = pos.y + (lightning.range_v / 2)
|
|
|
|
pos.z = math.floor(pos.z - (lightning.range_h / 2) + rng:next(1, lightning.range_h))
|
|
|
|
end
|
|
|
|
|
2021-09-09 12:22:27 +03:00
|
|
|
local b, pos2 = line_of_sight(pos, { x = pos.x, y = pos.y - lightning.range_v, z = pos.z }, 1)
|
2017-02-20 21:05:26 +03:00
|
|
|
|
|
|
|
-- nothing but air found
|
|
|
|
if b then
|
|
|
|
return nil, nil
|
|
|
|
end
|
|
|
|
|
2021-09-09 12:22:27 +03:00
|
|
|
local n = get_node({ x = pos2.x, y = pos2.y - 1/2, z = pos2.z })
|
2017-02-20 21:05:26 +03:00
|
|
|
if n.name == "air" or n.name == "ignore" then
|
|
|
|
return nil, nil
|
|
|
|
end
|
|
|
|
|
|
|
|
return pos, pos2
|
|
|
|
end
|
|
|
|
|
|
|
|
-- * pos: optional, if not given a random pos will be chosen
|
|
|
|
-- * returns: bool - success if a strike happened
|
2021-05-28 01:07:06 +03:00
|
|
|
function lightning.strike(pos)
|
2017-02-20 21:05:26 +03:00
|
|
|
if lightning.auto then
|
2021-03-15 02:12:59 +03:00
|
|
|
after(rng:next(lightning.interval_low, lightning.interval_high), lightning.strike)
|
2017-02-20 21:05:26 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
local pos2
|
|
|
|
pos, pos2 = choose_pos(pos)
|
|
|
|
|
|
|
|
if not pos then
|
|
|
|
return false
|
|
|
|
end
|
2021-09-09 12:22:27 +03:00
|
|
|
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
|
2021-03-15 02:12:59 +03:00
|
|
|
add_particlespawner({
|
2017-02-20 21:05:26 +03:00
|
|
|
amount = 1,
|
2021-09-09 12:22:27 +03:00
|
|
|
time = time,
|
2017-02-20 21:05:26 +03:00
|
|
|
-- make it hit the top of a block exactly with the bottom
|
2021-09-09 12:22:27 +03:00
|
|
|
minpos = particle_pos,
|
|
|
|
maxpos = particle_pos,
|
|
|
|
minexptime = time,
|
|
|
|
maxexptime = time,
|
|
|
|
minsize = particle_size,
|
|
|
|
maxsize = particle_size,
|
2017-02-20 21:05:26 +03:00
|
|
|
collisiondetection = true,
|
|
|
|
vertical = true,
|
|
|
|
-- to make it appear hitting the node that will get set on fire, make sure
|
|
|
|
-- to make the texture lightning bolt hit exactly in the middle of the
|
|
|
|
-- texture (e.g. 127/128 on a 256x wide texture)
|
|
|
|
texture = "lightning_lightning_" .. rng:next(1,3) .. ".png",
|
2019-03-26 14:24:30 +03:00
|
|
|
glow = minetest.LIGHT_MAX,
|
2017-02-20 21:05:26 +03:00
|
|
|
})
|
|
|
|
|
2021-03-15 02:12:59 +03:00
|
|
|
sound_play({ name = "lightning_thunder", gain = 10 }, { pos = pos, max_hear_distance = 500 }, true)
|
2017-02-20 21:05:26 +03:00
|
|
|
|
2019-03-08 21:09:40 +03:00
|
|
|
-- damage nearby objects, transform mobs
|
2021-09-09 12:22:27 +03:00
|
|
|
for _, obj in pairs(objects) do
|
2019-03-08 21:09:40 +03:00
|
|
|
local lua = obj:get_luaentity()
|
2021-09-09 16:21:33 +03:00
|
|
|
if lua and lua._on_strike then
|
|
|
|
lua._on_strike(lua, pos, pos2, objects)
|
|
|
|
end
|
|
|
|
-- remove this when mob API is done
|
2021-04-14 19:40:41 +03:00
|
|
|
if lua and lua.name == "mobs_mc:pig" then
|
2021-09-09 12:57:36 +03:00
|
|
|
mcl_util.replace_mob(obj, "mobs_mc:pigman")
|
2021-04-14 19:40:41 +03:00
|
|
|
elseif lua and lua.name == "mobs_mc:mooshroom" then
|
|
|
|
if lua.base_texture[1] == "mobs_mc_mooshroom.png" then
|
|
|
|
lua.base_texture = { "mobs_mc_mooshroom_brown.png", "mobs_mc_mushroom_brown.png" }
|
2019-03-08 21:09:40 +03:00
|
|
|
else
|
2021-04-14 19:40:41 +03:00
|
|
|
lua.base_texture = { "mobs_mc_mooshroom.png", "mobs_mc_mushroom_red.png" }
|
|
|
|
end
|
2021-09-09 12:57:36 +03:00
|
|
|
obj:set_properties({ textures = lua.base_texture })
|
|
|
|
elseif lua and lua.name == "mobs_mc:villager" then
|
|
|
|
mcl_util.replace_mob(obj, "mobs_mc:witch")
|
2021-04-14 19:40:41 +03:00
|
|
|
elseif lua and lua.name == "mobs_mc:creeper" then
|
2021-09-09 12:57:36 +03:00
|
|
|
mcl_util.replace_mob(obj, "mobs_mc:creeper_charged")
|
2021-04-14 19:40:41 +03:00
|
|
|
else
|
2021-09-09 12:22:27 +03:00
|
|
|
mcl_util.deal_damage(obj, 5, { type = "lightning_bolt" })
|
2019-03-08 21:09:40 +03:00
|
|
|
end
|
2017-02-20 21:05:26 +03:00
|
|
|
end
|
|
|
|
|
2021-03-15 02:12:59 +03:00
|
|
|
local playerlist = get_connected_players()
|
2017-02-20 21:05:26 +03:00
|
|
|
for i = 1, #playerlist do
|
|
|
|
local player = playerlist[i]
|
|
|
|
local sky = {}
|
|
|
|
|
|
|
|
sky.bgcolor, sky.type, sky.textures = player:get_sky()
|
|
|
|
|
|
|
|
local name = player:get_player_name()
|
|
|
|
if ps[name] == nil then
|
|
|
|
ps[name] = {p = player, sky = sky}
|
2021-09-09 12:57:36 +03:00
|
|
|
mcl_weather.skycolor.add_layer("lightning", { { r = 255, g = 255, b = 255 } }, true)
|
2017-11-21 03:35:31 +03:00
|
|
|
mcl_weather.skycolor.active = true
|
2017-02-20 21:05:26 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- trigger revert of skybox
|
2017-02-21 17:00:16 +03:00
|
|
|
ttl = 0.1
|
2017-02-20 21:05:26 +03:00
|
|
|
|
2017-11-12 05:24:46 +03:00
|
|
|
-- Events caused by the lightning strike: Fire, damage, mob transformations, rare skeleton spawn
|
2017-11-12 04:48:18 +03:00
|
|
|
|
2017-02-20 21:05:26 +03:00
|
|
|
pos2.y = pos2.y + 1/2
|
2017-11-12 04:48:18 +03:00
|
|
|
local skeleton_lightning = false
|
|
|
|
if rng:next(1,100) <= 3 then
|
|
|
|
skeleton_lightning = true
|
|
|
|
end
|
2021-09-09 12:57:36 +03:00
|
|
|
if get_item_group(get_node({ x = pos2.x, y = pos2.y - 1, z = pos2.z }).name, "liquid") < 1 then
|
2021-03-15 02:12:59 +03:00
|
|
|
if get_node(pos2).name == "air" then
|
2017-11-12 04:48:18 +03:00
|
|
|
-- Low chance for a lightning to spawn skeleton horse + skeletons
|
|
|
|
if skeleton_lightning then
|
2021-03-15 02:12:59 +03:00
|
|
|
add_entity(pos2, "mobs_mc:skeleton_horse")
|
2017-11-12 04:48:18 +03:00
|
|
|
|
|
|
|
local angle, posadd
|
|
|
|
angle = math.random(0, math.pi*2)
|
|
|
|
for i=1,3 do
|
2021-09-09 12:57:36 +03:00
|
|
|
posadd = { x=math.cos(angle),y=0,z=math.sin(angle) }
|
2017-11-12 04:48:18 +03:00
|
|
|
posadd = vector.normalize(posadd)
|
2021-03-15 02:12:59 +03:00
|
|
|
local mob = add_entity(vector.add(pos2, posadd), "mobs_mc:skeleton")
|
2021-11-17 02:37:11 +03:00
|
|
|
if mob then
|
|
|
|
mob:set_yaw(angle-math.pi/2)
|
|
|
|
end
|
2017-11-12 04:48:18 +03:00
|
|
|
angle = angle + (math.pi*2) / 3
|
|
|
|
end
|
|
|
|
|
2019-03-08 21:09:40 +03:00
|
|
|
-- Cause a fire
|
2017-11-12 04:48:18 +03:00
|
|
|
else
|
2021-09-09 12:57:36 +03:00
|
|
|
set_node(pos2, { name = "mcl_fire:fire" })
|
2017-11-12 04:48:18 +03:00
|
|
|
end
|
2017-02-20 21:05:26 +03:00
|
|
|
end
|
|
|
|
end
|
2021-09-09 12:22:27 +03:00
|
|
|
end)
|
2017-02-20 21:05:26 +03:00
|
|
|
|
|
|
|
-- if other mods disable auto lightning during initialization, don't trigger the first lightning.
|
2021-03-15 02:12:59 +03:00
|
|
|
after(5, function(dtime)
|
2017-02-20 21:05:26 +03:00
|
|
|
if lightning.auto then
|
2021-03-15 02:12:59 +03:00
|
|
|
after(rng:next(lightning.interval_low,
|
2017-02-20 21:05:26 +03:00
|
|
|
lightning.interval_high), lightning.strike)
|
|
|
|
end
|
|
|
|
end)
|
2019-02-21 05:36:02 +03:00
|
|
|
|
|
|
|
minetest.register_chatcommand("lightning", {
|
|
|
|
params = "[<X> <Y> <Z>]",
|
2019-03-07 22:55:56 +03:00
|
|
|
description = S("Let lightning strike at the specified position or yourself"),
|
2019-02-21 05:36:02 +03:00
|
|
|
privs = { maphack = true },
|
|
|
|
func = function(name, param)
|
|
|
|
local pos = {}
|
|
|
|
pos.x, pos.y, pos.z = string.match(param, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
|
|
|
|
pos.x = tonumber(pos.x)
|
|
|
|
pos.y = tonumber(pos.y)
|
|
|
|
pos.z = tonumber(pos.z)
|
|
|
|
if not (pos.x and pos.y and pos.z) then
|
|
|
|
pos = nil
|
|
|
|
end
|
|
|
|
if name == "" and pos == nil then
|
|
|
|
return false, "No position specified and unknown player"
|
|
|
|
end
|
|
|
|
if pos then
|
|
|
|
lightning.strike(pos)
|
|
|
|
else
|
|
|
|
local player = minetest.get_player_by_name(name)
|
|
|
|
if player then
|
|
|
|
lightning.strike(player:get_pos())
|
|
|
|
else
|
2019-03-07 22:55:56 +03:00
|
|
|
return false, S("No position specified and unknown player")
|
2019-02-21 05:36:02 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end,
|
|
|
|
})
|