Respawn spawner entity on punch/load

This commit is contained in:
Wuzzy 2022-10-16 17:06:03 +02:00
parent ac9a442a07
commit 303d3cf45c

View File

@ -14,6 +14,8 @@ local mummy_texture = {"tsm_pyramids_mummy.png"}
local mummy_hp = 20 local mummy_hp = 20
local mummy_drop = "default:papyrus" local mummy_drop = "default:papyrus"
local spawner_entity_offset = -0.28
local sound_normal = "mummy" local sound_normal = "mummy"
local sound_hit = "mummy_hurt" local sound_hit = "mummy_hurt"
local sound_dead = "mummy_death" local sound_dead = "mummy_death"
@ -96,6 +98,24 @@ local MUMMY_DEF = {
description = S("Mummy"), description = S("Mummy"),
} }
-- Returns true if a mummy spawner entity was found at pos.
-- If self is provided, this object does not count.
local function check_if_mummy_spawner_entity_exists(pos, self)
local ents = minetest.get_objects_inside_radius(pos, 0.5)
for e=1, #ents do
if (not self) or (ents[e] ~= ents[e]) then
local lua = ents[e]:get_luaentity()
if lua then
if lua.name == "tsm_pyramids:mummy_spawner" then
-- entity found
return true
end
end
end
end
return false
end
local spawner_DEF = { local spawner_DEF = {
hp_max = 1, hp_max = 1,
physical = false, physical = false,
@ -110,6 +130,13 @@ local spawner_DEF = {
} }
spawner_DEF.on_activate = function(self) spawner_DEF.on_activate = function(self)
local pos = self.object:get_pos()
local spos = vector.new(pos.x, pos.y + spawner_entity_offset, pos.z)
if check_if_mummy_spawner_entity_exists(spos, self) then
-- Remove possible duplicate entity
self.object:remove()
return
end
mummy_update_visuals_def(self) mummy_update_visuals_def(self)
self.object:set_velocity({x=0, y=0, z=0}) self.object:set_velocity({x=0, y=0, z=0})
self.object:set_acceleration({x=0, y=0, z=0}) self.object:set_acceleration({x=0, y=0, z=0})
@ -117,14 +144,18 @@ spawner_DEF.on_activate = function(self)
end end
-- Regularily check if entity is still inside spawner
spawner_DEF.on_step = function(self, dtime) spawner_DEF.on_step = function(self, dtime)
self.timer = self.timer + 0.01 self.timer = self.timer + dtime
local n = minetest.get_node_or_nil(self.object:get_pos()) local pos = self.object:get_pos()
if self.timer > 1 then pos.y = pos.y - spawner_entity_offset
local n = minetest.get_node_or_nil(pos)
if self.timer > 50 then
if n and n.name and n.name ~= "tsm_pyramids:spawner_mummy" then if n and n.name and n.name ~= "tsm_pyramids:spawner_mummy" then
self.object:remove() self.object:remove()
return return
end end
self.timer = 0
end end
end end
@ -407,6 +438,20 @@ else
spawnersounds = default.node_sound_stone_defaults() spawnersounds = default.node_sound_stone_defaults()
end end
local spawn_mummy_spawner_entity = function(pos)
local spos = vector.new(pos.x, pos.y+spawner_entity_offset, pos.z)
minetest.add_entity(spos, "tsm_pyramids:mummy_spawner")
end
-- Respawn mummy spawner entity at pos if none exists
local respawn_mummy_spawner_entity = function(pos)
local spos = vector.new(pos.x, pos.y + spawner_entity_offset, pos.z)
if check_if_mummy_spawner_entity_exists(spos) then
return
end
spawn_mummy_spawner_entity(pos)
end
minetest.register_node("tsm_pyramids:spawner_mummy", { minetest.register_node("tsm_pyramids:spawner_mummy", {
description = S("Mummy Spawner"), description = S("Mummy Spawner"),
_doc_items_longdesc = S("A mummy spawner causes hostile mummies to appear in its vicinity as long it exists."), _doc_items_longdesc = S("A mummy spawner causes hostile mummies to appear in its vicinity as long it exists."),
@ -417,13 +462,15 @@ minetest.register_node("tsm_pyramids:spawner_mummy", {
groups = {cracky=1,level=1}, groups = {cracky=1,level=1},
drop = "", drop = "",
on_construct = function(pos) on_construct = function(pos)
pos.y = pos.y - 0.28 spawn_mummy_spawner_entity(pos)
minetest.add_entity(pos,"tsm_pyramids:mummy_spawner") end,
on_punch = function(pos)
respawn_mummy_spawner_entity(pos)
end, end,
on_destruct = function(pos) on_destruct = function(pos)
for _,obj in ipairs(minetest.get_objects_inside_radius(pos, 1)) do for _,obj in ipairs(minetest.get_objects_inside_radius(pos, 0.5)) do
if not obj:is_player() then if obj ~= nil and not obj:is_player() then
if obj ~= nil and obj:get_luaentity().name == "tsm_pyramids:mummy_spawner" then if obj:get_luaentity().name == "tsm_pyramids:mummy_spawner" then
obj:remove() obj:remove()
end end
end end
@ -432,6 +479,17 @@ minetest.register_node("tsm_pyramids:spawner_mummy", {
sounds = spawnersounds, sounds = spawnersounds,
}) })
-- Neccessary in case the spawner entity got lost due to /clearobjects
minetest.register_lbm({
label = "Respawn mummy spawner entity",
name = "tsm_pyramids:respawn_mummy_spawner_entity",
nodenames = { "tsm_pyramids:spawner_mummy" },
run_at_every_load = true,
action = function(pos, node)
respawn_mummy_spawner_entity(pos)
end,
})
-- Attempt to spawn a mummy at a random appropriate position around pos. -- Attempt to spawn a mummy at a random appropriate position around pos.
-- Criteria: -- Criteria:
-- * Must be close to pos -- * Must be close to pos