minetest-mod-nssm/nssm_api.lua

853 lines
18 KiB
Lua
Raw Permalink Normal View History

2018-08-08 02:56:14 -07:00
-- set content id's
local c_air = minetest.get_content_id("air")
local c_ignore = minetest.get_content_id("ignore")
local c_obsidian = minetest.get_content_id("default:obsidian")
local c_brick = minetest.get_content_id("default:obsidianbrick")
local c_chest = minetest.get_content_id("default:chest_locked")
2021-01-03 13:21:53 -08:00
nssm.lessvirulent = minetest.settings:get_bool("nssm.lessvirulent") or false
nssm.safebones = minetest.settings:get_bool("nssm.safebones") or false
nssm.cryosave = minetest.settings:get_bool("nssm.cryosave") or false
2018-08-08 02:56:14 -07:00
2023-06-13 04:50:58 -07:00
local function virulence(mobe)
2018-08-08 02:56:14 -07:00
if not nssm.lessvirulent then
return 0
end
2018-08-08 02:56:14 -07:00
return math.ceil(100 / mobe.hp_max)
end
2018-08-08 02:56:14 -07:00
function nssm:affectbones(mobe) -- as function for adaptable heuristic
return not nssm.safebones
end
2023-06-13 04:50:58 -07:00
--[[
2018-08-08 02:56:14 -07:00
function drops(drop)
2018-08-08 02:56:14 -07:00
if drop then
2023-06-13 04:50:58 -07:00
drop:set_velocity({
2018-08-08 02:56:14 -07:00
x = math.random(-10, 10) / 9,
y = 5,
2022-09-27 11:26:58 -07:00
z = math.random(-10, 10) / 9
2018-08-08 02:56:14 -07:00
})
end
end
2023-06-13 04:50:58 -07:00
]]
2018-08-08 02:56:14 -07:00
2023-06-13 04:50:58 -07:00
local function perpendicular_vector(vec) --returns a vector rotated of 90° in 2D
local ang = math.pi / 2
2018-08-08 02:56:14 -07:00
local c = math.cos(ang)
local s = math.sin(ang)
local i = vec.x * c - vec.z * s
local k = vec.x * s + vec.z * c
2018-08-08 02:56:14 -07:00
local j = 0
return {x = i, y = j, z = k}
2018-08-08 02:56:14 -07:00
end
2023-06-13 04:50:58 -07:00
function nssm:add_entity_and_particles(entity, pos, particles, multiplier)
2018-08-08 02:56:14 -07:00
minetest.add_particlespawner({
2022-09-27 11:26:58 -07:00
amount = 100 * multiplier,
2018-08-08 02:56:14 -07:00
time = 2,
2022-09-27 11:26:58 -07:00
minpos = {x = pos.x - 2, y = pos.y - 1, z = pos.z - 2},
maxpos = {x = pos.x + 2, y = pos.y + 4, z = pos.z + 2},
minvel = {x = 0, y = 0, z = 0},
maxvel = {x = 1, y = 2, z = 1},
minacc = {x = -0.5, y = 0.6, z = -0.5},
maxacc = {x = 0.5, y = 0.7, z = 0.5},
2018-08-08 02:56:14 -07:00
minexptime = 2,
maxexptime = 3,
minsize = 3,
maxsize = 5,
collisiondetection = false,
vertical = false,
2022-09-27 11:26:58 -07:00
texture = particles
2018-08-08 02:56:14 -07:00
})
2018-08-08 02:56:14 -07:00
minetest.add_entity(pos, entity)
end
2018-08-08 02:56:14 -07:00
-- get node but use fallback for nil or unknown
2023-06-13 04:50:58 -07:00
function nssm:node_ok(pos, fallback)
2018-08-08 02:56:14 -07:00
fallback = fallback or "default:dirt"
2018-08-08 02:56:14 -07:00
local node = minetest.get_node_or_nil(pos)
2018-08-08 02:56:14 -07:00
if not node then
return minetest.registered_nodes[fallback]
end
2018-08-08 02:56:14 -07:00
if minetest.registered_nodes[node.name] then
return node
end
2018-08-08 02:56:14 -07:00
return minetest.registered_nodes[fallback]
end
2023-06-13 04:50:58 -07:00
function nssm:dist_pos(p, s)
2022-09-28 11:01:53 -07:00
local v = {
x = math.abs(s.x - p.x),
y = math.abs(s.y - p.y),
z = math.abs(s.z - p.z)
}
return math.sqrt(v.x ^ 2 + v.y ^ 2 + v.z ^ 2)
2018-08-08 02:56:14 -07:00
end
2023-06-13 04:50:58 -07:00
--[[
2018-08-08 02:56:14 -07:00
--check_for_death functions customized for monsters who respawns (Masticone)
function check_for_death_hydra(self)
2018-08-08 02:56:14 -07:00
local hp = self.object:get_hp()
2018-08-08 02:56:14 -07:00
if hp > 0 then
2018-08-08 02:56:14 -07:00
self.health = hp
2018-08-08 02:56:14 -07:00
if self.sounds.damage ~= nil then
2022-09-27 11:26:58 -07:00
2018-08-08 02:56:14 -07:00
minetest.sound_play(self.sounds.damage,{
object = self.object,
max_hear_distance = self.sounds.distance
2022-09-27 11:26:58 -07:00
}, true)
2018-08-08 02:56:14 -07:00
end
2018-08-08 02:56:14 -07:00
return false
end
2018-08-08 04:20:07 -07:00
local pos = self.object:get_pos()
2018-08-08 02:56:14 -07:00
local obj = nil
2018-08-08 02:56:14 -07:00
if self.sounds.death ~= nil then
2022-09-27 11:26:58 -07:00
2018-08-08 02:56:14 -07:00
minetest.sound_play(self.sounds.death,{
object = self.object,
max_hear_distance = self.sounds.distance
2022-09-27 11:26:58 -07:00
}, true)
2018-08-08 02:56:14 -07:00
end
self.object:remove()
2018-08-08 02:56:14 -07:00
return true
end
2023-06-13 04:50:58 -07:00
]]
2018-08-08 02:56:14 -07:00
2023-06-13 04:50:58 -07:00
local function round(n)
if (n > 0) then
2018-08-08 02:56:14 -07:00
return n % 1 >= 0.5 and math.ceil(n) or math.floor(n)
else
n = -n
2022-09-27 11:26:58 -07:00
2018-08-08 02:56:14 -07:00
local t = n % 1 >= 0.5 and math.ceil(n) or math.floor(n)
2022-09-27 11:26:58 -07:00
2018-08-08 02:56:14 -07:00
return -t
end
end
2023-06-13 04:50:58 -07:00
function nssm:explosion_particles(pos, exp_radius)
2022-09-27 11:26:58 -07:00
minetest.add_particlespawner({
amount = 100 * exp_radius / 2,
time = 0.1,
minpos = {x = pos.x - exp_radius, y = pos.y - exp_radius, z = pos.z - exp_radius},
maxpos = {x = pos.x + exp_radius, y = pos.y + exp_radius, z = pos.z + exp_radius},
minvel = {x = 0, y = 0, z = 0},
maxvel = {x = 0.1, y = 0.3, z = 0.1},
minacc = {x = -0.5, y = 1, z = -0.5},
maxacc = {x = 0.5, y = 1, z = 0.5},
minexptime = 0.1,
maxexptime = 4,
minsize = 6,
maxsize = 12,
collisiondetection = false,
texture = "tnt_smoke.png"
})
2018-08-08 02:56:14 -07:00
end
2018-08-09 03:36:34 -07:00
2023-06-13 04:50:58 -07:00
function nssm:digging_attack(
2018-08-08 02:56:14 -07:00
self, --the entity of the mob
group, --group of the blocks the mob can dig: nil=everything
max_vel, --max velocity of the mob
dim --vector representing the dimensions of the mob
)
if self.attack and self.attack:is_player() then
2018-08-08 04:20:07 -07:00
local s = self.object:get_pos()
local p = self.attack:get_pos()
2018-08-08 02:56:14 -07:00
local dir = vector.subtract(p,s)
2018-08-08 02:56:14 -07:00
dir = vector.normalize(dir)
local per = perpendicular_vector(dir)
2018-08-08 02:56:14 -07:00
local posp = vector.add(s,dir)
posp = vector.subtract(posp,per)
2022-09-27 11:26:58 -07:00
for j = 1, 3 do
2022-09-28 11:01:53 -07:00
2018-08-08 02:56:14 -07:00
if minetest.is_protected(posp, "") then
return
end
2018-08-08 02:56:14 -07:00
local pos1 = posp
for i = 0, dim.y do
2018-08-08 02:56:14 -07:00
local n = minetest.get_node(pos1).name
2018-08-08 02:56:14 -07:00
if group == nil then
if minetest.get_item_group(n, "unbreakable") == 1
or minetest.is_protected(pos1, "")
or (n == "bones:bones" and not nssm:affectbones(self) ) then
2018-08-08 02:56:14 -07:00
else
minetest.remove_node(pos1)
end
else
2022-09-28 11:01:53 -07:00
if ((minetest.get_item_group(n, group) == 1)
and (minetest.get_item_group(n, "unbreakable") ~= 1)
and (n ~= "bones:bones")
and not (minetest.is_protected(pos1, "")) ) then
2018-08-08 02:56:14 -07:00
minetest.remove_node(pos1)
end
end
pos1.y = pos1.y + 1
2018-08-08 02:56:14 -07:00
end
2022-09-27 11:26:58 -07:00
posp.y = s.y
posp = vector.add(posp,per)
2018-08-08 02:56:14 -07:00
end
end
end
2023-06-13 04:50:58 -07:00
function nssm:putting_ability( --puts under the mob the block defined as 'p_block'
2018-08-08 02:56:14 -07:00
self, --the entity of the mob
p_block, --definition of the block to use
max_vel --max velocity of the mob
)
2018-08-08 04:20:07 -07:00
local v = self.object:get_velocity()
2018-08-08 02:56:14 -07:00
local dx = 0
local dz = 0
2022-09-28 11:01:53 -07:00
if math.abs(v.x) > math.abs(v.z) then
if (v.x) > 0 then
2018-08-08 02:56:14 -07:00
dx = 1
else
dx = -1
end
else
2022-09-28 11:01:53 -07:00
if (v.z) > 0 then
2018-08-08 02:56:14 -07:00
dz = 1
else
dz = -1
end
end
2018-08-08 04:20:07 -07:00
local pos = self.object:get_pos()
2018-08-08 02:56:14 -07:00
local pos1
2022-09-28 11:01:53 -07:00
pos.y = pos.y - 1
pos1 = {x = pos.x + dx, y = pos.y, z = pos.z + dz}
2018-08-08 02:56:14 -07:00
local n = minetest.get_node(pos).name
local n1 = minetest.get_node(pos1).name
local oldmetainf = {
minetest.get_meta(pos):to_table(),
minetest.get_meta(pos1):to_table()
}
if n ~= p_block and not minetest.is_protected(pos, "")
2022-09-28 11:01:53 -07:00
and (n == "bones:bones" and nssm:affectbones(self) )
and n ~= "air" then
2018-08-08 02:56:14 -07:00
minetest.set_node(pos, {name=p_block})
2018-08-08 02:56:14 -07:00
if nssm.cryosave then
2018-08-08 02:56:14 -07:00
local metai = minetest.get_meta(pos)
2018-08-08 02:56:14 -07:00
metai:from_table(oldmetainf[1]) -- this is enough to save the meta
metai:set_string("nssm", n)
2018-08-08 02:56:14 -07:00
end
end
if n1 ~= p_block and not minetest.is_protected(pos1, "")
and (n == "bones:bones" and nssm:affectbones(self) )
and n ~= "air" then
minetest.set_node(pos1, {name = p_block})
2018-08-08 02:56:14 -07:00
if nssm.cryosave then
2018-08-08 02:56:14 -07:00
local metai = minetest.get_meta(pos1)
2018-08-08 02:56:14 -07:00
metai:from_table(oldmetainf[2]) -- this is enough to save the meta
2022-09-28 11:01:53 -07:00
metai:set_string("nssm", n1)
2018-08-08 02:56:14 -07:00
end
end
end
2023-06-13 04:50:58 -07:00
function nssm:webber_ability( --puts randomly around the block defined as w_block
2018-08-08 02:56:14 -07:00
self, --the entity of the mob
w_block, --definition of the block to use
radius --max distance the block can be put
)
2023-06-13 04:50:58 -07:00
if virulence(self) ~= 0
and math.random(1, virulence(self)) ~= 1 then return end
2018-08-08 02:56:14 -07:00
2018-08-08 04:20:07 -07:00
local pos = self.object:get_pos()
2022-09-28 11:01:53 -07:00
if math.random(55) == 1 then
2022-09-27 11:26:58 -07:00
local dx = math.random(radius)
local dz = math.random(radius)
local p = {x = pos.x + dx, y = pos.y - 1, z = pos.z + dz}
local t = {x = pos.x + dx, y = pos.y, z = pos.z + dz}
2018-08-08 02:56:14 -07:00
local n = minetest.get_node(p).name
local k = minetest.get_node(t).name
2022-09-28 11:01:53 -07:00
if (n ~= "air" and k == "air")
and not minetest.is_protected(t, "") then
minetest.set_node(t, {name = w_block})
2018-08-08 02:56:14 -07:00
end
end
end
2023-06-13 04:50:58 -07:00
function nssm:midas_ability( --ability to transform every blocks it touches in the m_block block
2018-08-08 02:56:14 -07:00
self, --the entity of the mob
m_block,
max_vel, --max velocity of the mob
mult, --multiplier of the dimensions of the area around that need the transformation
height --height of the mob
)
2018-08-08 04:20:07 -07:00
local v = self.object:get_velocity()
local pos = self.object:get_pos()
2018-08-08 02:56:14 -07:00
if minetest.is_protected(pos, "") then
return
end
local max = 0
2018-08-08 04:20:07 -07:00
local yaw = (self.object:get_yaw() + self.rotate) or 0
local x = math.sin(yaw) * -1
2018-08-08 02:56:14 -07:00
local z = math.cos(yaw)
local i = 1
local i1 = -1
local k = 1
local k1 = -1
local multiplier = mult
if x > 0 then
i = round(x * max_vel) * multiplier
2018-08-08 02:56:14 -07:00
else
i1 = round(x * max_vel) * multiplier
2018-08-08 02:56:14 -07:00
end
if z > 0 then
k = round(z * max_vel) * multiplier
2018-08-08 02:56:14 -07:00
else
k1 = round(z * max_vel) * multiplier
2018-08-08 02:56:14 -07:00
end
for dx = i1, i do
for dy = -1, height do
for dz = k1, k do
local p = {x = pos.x + dx, y = pos.y + dy, z = pos.z + dz}
2018-08-08 02:56:14 -07:00
local n = minetest.get_node(p).name
if minetest.get_item_group(n, "unbreakable") == 1
2022-09-28 11:01:53 -07:00
or minetest.is_protected(p, "") or n == "air"
2018-08-08 02:56:14 -07:00
or (n == "bones:bones" and not nssm:affectbones(self))
2022-09-27 11:26:58 -07:00
or n == m_block then
2018-08-08 02:56:14 -07:00
else
minetest.set_node(p, {name = m_block})
2018-08-08 02:56:14 -07:00
end
end
end
end
end
-- NEW EXPLOSION FUNCTION
-- loss probabilities array (one in X will be lost)
local loss_prob = {}
loss_prob["default:cobble"] = 3
loss_prob["default:dirt"] = 4
2021-01-03 13:21:53 -08:00
local tnt_radius = tonumber(minetest.settings:get("tnt_radius") or 3)
2018-08-08 02:56:14 -07:00
local cid_data = {}
2018-08-08 02:56:14 -07:00
minetest.after(0, function()
2018-08-08 02:56:14 -07:00
for name, def in pairs(minetest.registered_nodes) do
cid_data[minetest.get_content_id(name)] = {
name = name,
drops = def.drops,
flammable = def.groups.flammable,
on_blast = def.on_blast,
}
end
end)
2018-08-08 02:56:14 -07:00
local function rand_pos(center, pos, radius)
2018-08-08 02:56:14 -07:00
local def
local reg_nodes = minetest.registered_nodes
local i = 0
2018-08-08 02:56:14 -07:00
repeat
-- Give up and use the center if this takes too long
if i > 4 then
pos.x, pos.z = center.x, center.z
break
end
2018-08-08 02:56:14 -07:00
pos.x = center.x + math.random(-radius, radius)
pos.z = center.z + math.random(-radius, radius)
2018-08-08 02:56:14 -07:00
def = reg_nodes[minetest.get_node(pos).name]
2018-08-08 02:56:14 -07:00
i = i + 1
2018-08-08 02:56:14 -07:00
until def and not def.walkable
end
2018-08-08 02:56:14 -07:00
local function add_effects(pos, radius, drops)
2018-08-08 02:56:14 -07:00
minetest.add_particle({
pos = pos,
velocity = vector.new(),
acceleration = vector.new(),
expirationtime = 0.4,
size = radius * 10,
collisiondetection = false,
vertical = false,
2022-09-27 11:26:58 -07:00
texture = "tnt_boom.png"
2018-08-08 02:56:14 -07:00
})
2018-08-08 02:56:14 -07:00
minetest.add_particlespawner({
amount = 64,
time = 0.5,
minpos = vector.subtract(pos, radius / 2),
maxpos = vector.add(pos, radius / 2),
minvel = {x = -10, y = -10, z = -10},
maxvel = {x = 10, y = 10, z = 10},
minacc = vector.new(),
maxacc = vector.new(),
minexptime = 1,
maxexptime = 2.5,
minsize = radius * 3,
maxsize = radius * 5,
2022-09-27 11:26:58 -07:00
texture = "tnt_smoke.png"
2018-08-08 02:56:14 -07:00
})
-- we just dropped some items. Look at the items entities and pick
-- one of them to use as texture
local texture = "tnt_blast.png" --fallback texture
local most = 0
2018-08-08 02:56:14 -07:00
for name, stack in pairs(drops) do
2018-08-08 02:56:14 -07:00
local count = stack:get_count()
2018-08-08 02:56:14 -07:00
if count > most then
2018-08-08 02:56:14 -07:00
most = count
2018-08-08 02:56:14 -07:00
local def = minetest.registered_nodes[name]
2018-08-08 02:56:14 -07:00
if def and def.tiles and def.tiles[1] then
texture = def.tiles[1]
end
end
end
minetest.add_particlespawner({
amount = 64,
time = 0.1,
minpos = vector.subtract(pos, radius / 2),
maxpos = vector.add(pos, radius / 2),
minvel = {x = -3, y = 0, z = -3},
maxvel = {x = 3, y = 5, z = 3},
minacc = {x = 0, y = -10, z = 0},
maxacc = {x = 0, y = -10, z = 0},
minexptime = 0.8,
maxexptime = 2.0,
minsize = radius * 0.66,
maxsize = radius * 2,
texture = texture,
2022-09-27 11:26:58 -07:00
collisiondetection = true
2018-08-08 02:56:14 -07:00
})
end
2018-08-08 02:56:14 -07:00
local function eject_drops(drops, pos, radius)
2018-08-08 02:56:14 -07:00
local drop_pos = vector.new(pos)
2018-08-08 02:56:14 -07:00
for _, item in pairs(drops) do
2018-08-08 02:56:14 -07:00
local count = math.min(item:get_count(), item:get_stack_max())
2018-08-08 02:56:14 -07:00
while count > 0 do
2022-09-28 11:01:53 -07:00
local take = math.max(1, math.min(radius * radius,
count, item:get_stack_max()))
2018-08-08 02:56:14 -07:00
rand_pos(pos, drop_pos, radius)
2018-08-08 02:56:14 -07:00
local dropitem = ItemStack(item)
2018-08-08 02:56:14 -07:00
dropitem:set_count(take)
2018-08-08 02:56:14 -07:00
local obj = minetest.add_item(drop_pos, dropitem)
2018-08-08 02:56:14 -07:00
if obj then
obj:get_luaentity().collect = true
2018-08-08 04:20:07 -07:00
obj:set_acceleration({x = 0, y = -10, z = 0})
2022-09-28 11:01:53 -07:00
obj:set_velocity({
x = math.random(-3, 3),
y = math.random(0, 10),
z = math.random(-3, 3)
})
2018-08-08 02:56:14 -07:00
end
2018-08-08 02:56:14 -07:00
count = count - take
end
end
end
2018-08-08 02:56:14 -07:00
local function calc_velocity(pos1, pos2, old_vel, power)
2018-08-08 02:56:14 -07:00
-- Avoid errors caused by a vector of zero length
if vector.equals(pos1, pos2) then
return old_vel
end
local vel = vector.direction(pos1, pos2)
2018-08-08 02:56:14 -07:00
vel = vector.normalize(vel)
vel = vector.multiply(vel, power)
-- Divide by distance
local dist = vector.distance(pos1, pos2)
2018-08-08 02:56:14 -07:00
dist = math.max(dist, 1)
vel = vector.divide(vel, dist)
-- Add old velocity
vel = vector.add(vel, old_vel)
-- randomize it a bit
vel = vector.add(vel, {
x = math.random() - 0.5,
y = math.random() - 0.5,
2022-09-27 11:26:58 -07:00
z = math.random() - 0.5
2018-08-08 02:56:14 -07:00
})
-- Limit to terminal velocity
dist = vector.length(vel)
2018-08-08 02:56:14 -07:00
if dist > 250 then
vel = vector.divide(vel, dist / 250)
end
2018-08-08 02:56:14 -07:00
return vel
end
2018-08-08 02:56:14 -07:00
local function entity_physics(pos, radius, drops)
2018-08-08 02:56:14 -07:00
local objs = minetest.get_objects_inside_radius(pos, radius)
2018-08-08 02:56:14 -07:00
for _, obj in pairs(objs) do
2018-08-08 04:20:07 -07:00
local obj_pos = obj:get_pos()
2018-08-08 02:56:14 -07:00
local dist = math.max(1, vector.distance(pos, obj_pos))
local damage = (4 / dist) * radius
2018-08-08 02:56:14 -07:00
if obj:is_player() then
-- currently the engine has no method to set
-- player velocity. See #2960
-- instead, we knock the player back 1.0 node, and slightly upwards
local dir = vector.normalize(vector.subtract(obj_pos, pos))
local moveoff = vector.multiply(dir, dist + 1.0)
local newpos = vector.add(pos, moveoff)
2018-08-08 02:56:14 -07:00
newpos = vector.add(newpos, {x = 0, y = 0.2, z = 0})
2018-08-08 04:20:07 -07:00
obj:set_pos(newpos)
2018-08-08 02:56:14 -07:00
obj:set_hp(obj:get_hp() - damage)
else
local do_damage = true
local do_knockback = true
local entity_drops = {}
local luaobj = obj:get_luaentity()
local objdef = minetest.registered_entities[luaobj.name]
local name = luaobj.name
if objdef and objdef.on_blast then
2022-09-28 11:01:53 -07:00
if name == "nssm:pumpking"
or name == "nssm:morvalar0"
or name == "nssm:morvalar5" then
2018-08-08 02:56:14 -07:00
do_damage = false
do_knockback = false
else
do_damage, do_knockback, entity_drops = objdef.on_blast(luaobj, damage)
end
end
if do_knockback then
2018-08-08 04:20:07 -07:00
local obj_vel = obj:get_velocity()
2022-09-28 11:01:53 -07:00
obj:set_velocity(calc_velocity(pos, obj_pos, obj_vel, radius * 10))
2018-08-08 02:56:14 -07:00
end
2018-08-08 02:56:14 -07:00
if do_damage then
2018-08-08 02:56:14 -07:00
if not obj:get_armor_groups().immortal then
2018-08-08 02:56:14 -07:00
obj:punch(obj, 1.0, {
full_punch_interval = 1.0,
2022-09-27 11:26:58 -07:00
damage_groups = {fleshy = damage}
2018-08-08 02:56:14 -07:00
}, nil)
end
end
2018-08-08 02:56:14 -07:00
for _, item in pairs(entity_drops) do
add_drop(drops, item)
end
end
end
end
2018-08-08 02:56:14 -07:00
local function add_drop(drops, item)
2018-08-08 02:56:14 -07:00
item = ItemStack(item)
2018-08-08 02:56:14 -07:00
local name = item:get_name()
2018-08-08 02:56:14 -07:00
if loss_prob[name] ~= nil and math.random(1, loss_prob[name]) == 1 then
return
end
local drop = drops[name]
2018-08-08 02:56:14 -07:00
if drop == nil then
drops[name] = item
else
drop:set_count(drop:get_count() + item:get_count())
end
end
2022-09-27 11:26:58 -07:00
local function destroy(drops, npos, cid, c_air, c_fire, on_blast_queue,
ignore_protection, ignore_on_blast)
2018-08-08 02:56:14 -07:00
if not ignore_protection and minetest.is_protected(npos, "") then
return cid
end
local def = cid_data[cid]
if not def then
return c_air
elseif not ignore_on_blast and def.on_blast then
on_blast_queue[#on_blast_queue + 1] = {pos = vector.new(npos), on_blast = def.on_blast}
return cid
elseif def.flammable then
return c_fire
else
local node_drops = minetest.get_node_drops(def.name, "")
2022-09-28 11:01:53 -07:00
2018-08-08 02:56:14 -07:00
for _, item in pairs(node_drops) do
add_drop(drops, item)
end
2022-09-28 11:01:53 -07:00
2018-08-08 02:56:14 -07:00
return c_air
end
end
2018-08-08 02:56:14 -07:00
local function tnt_explode(pos, radius, ignore_protection, ignore_on_blast)
2018-08-08 02:56:14 -07:00
pos = vector.round(pos)
2018-08-08 02:56:14 -07:00
-- scan for adjacent TNT nodes first, and enlarge the explosion
local vm1 = VoxelManip()
local p1 = vector.subtract(pos, 2)
local p2 = vector.add(pos, 2)
local minp, maxp = vm1:read_from_map(p1, p2)
local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
local data = vm1:get_data()
local count = 0
2020-08-20 11:43:44 -07:00
local c_tnt
2018-08-08 02:56:14 -07:00
local c_tnt_burning = minetest.get_content_id("tnt:tnt_burning")
local c_tnt_boom = minetest.get_content_id("tnt:boom")
local c_air = minetest.get_content_id("air")
2020-08-20 11:43:44 -07:00
if minetest.registered_nodes["tnt:tnt"] then
c_tnt = minetest.get_content_id("tnt:tnt")
else
c_tnt = tnt_burning
end
2018-08-08 02:56:14 -07:00
for z = pos.z - 2, pos.z + 2 do
for y = pos.y - 2, pos.y + 2 do
2018-08-08 02:56:14 -07:00
local vi = a:index(pos.x - 2, y, z)
2018-08-08 02:56:14 -07:00
for x = pos.x - 2, pos.x + 2 do
2018-08-08 02:56:14 -07:00
local cid = data[vi]
2018-08-08 02:56:14 -07:00
if cid == c_tnt or cid == c_tnt_boom or cid == c_tnt_burning then
count = count + 1
data[vi] = c_air
end
2018-08-08 02:56:14 -07:00
vi = vi + 1
end
end
end
vm1:set_data(data)
vm1:write_to_map()
-- recalculate new radius
radius = math.floor(radius * math.pow(count, 1/3))
-- perform the explosion
local vm = VoxelManip()
local pr = PseudoRandom(os.time())
2018-08-08 02:56:14 -07:00
p1 = vector.subtract(pos, radius)
p2 = vector.add(pos, radius)
minp, maxp = vm:read_from_map(p1, p2)
a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
data = vm:get_data()
local drops = {}
local on_blast_queue = {}
local c_fire = minetest.get_content_id("fire:basic_flame")
2018-08-08 02:56:14 -07:00
for z = -radius, radius do
for y = -radius, radius do
2018-08-08 02:56:14 -07:00
local vi = a:index(pos.x + (-radius), pos.y + y, pos.z + z)
2018-08-08 02:56:14 -07:00
for x = -radius, radius do
2018-08-08 02:56:14 -07:00
local r = vector.length(vector.new(x, y, z))
2018-08-08 02:56:14 -07:00
if (radius * radius) / (r * r) >= (pr:next(80, 125) / 100) then
2018-08-08 02:56:14 -07:00
local cid = data[vi]
local p = {x = pos.x + x, y = pos.y + y, z = pos.z + z}
2018-08-08 02:56:14 -07:00
if cid ~= c_air then
data[vi] = destroy(drops, p, cid, c_air, c_fire,
2022-09-28 11:01:53 -07:00
on_blast_queue, ignore_protection, ignore_on_blast)
2018-08-08 02:56:14 -07:00
end
end
2018-08-08 02:56:14 -07:00
vi = vi + 1
end
end
end
vm:set_data(data)
vm:write_to_map()
vm:update_map()
vm:update_liquids()
-- call nodeupdate for everything within 1.5x blast radius
for y = -radius * 1.5, radius * 1.5 do
for z = -radius * 1.5, radius * 1.5 do
for x = -radius * 1.5, radius * 1.5 do
2018-08-08 02:56:14 -07:00
local rad = {x = x, y = y, z = z}
local s = vector.add(pos, rad)
local r = vector.length(rad)
2018-08-08 02:56:14 -07:00
if r / radius < 1.4 then
core.check_single_for_falling(s)
end
end
end
end
for _, queued_data in pairs(on_blast_queue) do
2018-08-08 02:56:14 -07:00
local dist = math.max(1, vector.distance(queued_data.pos, pos))
local intensity = (radius * radius) / (dist * dist)
local node_drops = queued_data.on_blast(queued_data.pos, intensity)
2018-08-08 02:56:14 -07:00
if node_drops then
2018-08-08 02:56:14 -07:00
for _, item in pairs(node_drops) do
add_drop(drops, item)
end
end
end
return drops, radius
end
2023-06-13 04:50:58 -07:00
function nssm:tnt_boom_nssm(pos, def)
2022-09-27 11:26:58 -07:00
minetest.sound_play("tnt_explode", {
pos = pos, gain = 1.5, max_hear_distance = 2 * 64}, true)
2018-08-08 02:56:14 -07:00
minetest.set_node(pos, {name = "tnt:boom"})
2018-08-08 02:56:14 -07:00
local drops, radius = tnt_explode(pos, def.radius, def.ignore_protection,
def.ignore_on_blast)
2018-08-08 02:56:14 -07:00
-- append entity drops
local damage_radius = (radius / def.radius) * def.damage_radius
2018-08-08 02:56:14 -07:00
entity_physics(pos, damage_radius, drops)
2018-08-08 02:56:14 -07:00
if not def.disable_drops then
eject_drops(drops, pos, radius)
end
2018-08-08 02:56:14 -07:00
add_effects(pos, radius, drops)
end