Improve initial mummy spawn

This commit is contained in:
Wuzzy 2019-08-25 01:52:12 +02:00
parent 4fde7b9091
commit 19c81a9b4f
2 changed files with 73 additions and 56 deletions

View File

@ -77,7 +77,11 @@ end
local function add_spawner(pos, mummy_offset) local function add_spawner(pos, mummy_offset)
minetest.set_node(pos, {name="tsm_pyramids:spawner_mummy"}) minetest.set_node(pos, {name="tsm_pyramids:spawner_mummy"})
if not minetest.settings:get_bool("only_peaceful_mobs") then tsm_pyramids.spawn_mummy(vector.add(pos, mummy_offset),2) end if not minetest.settings:get_bool("only_peaceful_mobs") then
for i=1,2 do
tsm_pyramids.attempt_mummy_spawn(pos, false)
end
end
end end
local function can_replace(pos) local function can_replace(pos)

View File

@ -16,7 +16,7 @@ local sound_normal = "mummy"
local sound_hit = "mummy_hurt" local sound_hit = "mummy_hurt"
local sound_dead = "mummy_death" local sound_dead = "mummy_death"
local spawner_range = 17 local spawner_check_range = 17
local spawner_max_mobs = 6 local spawner_max_mobs = 6
local function get_animations() local function get_animations()
@ -342,7 +342,8 @@ minetest.register_craftitem("tsm_pyramids:spawn_egg", {
}) })
function tsm_pyramids.spawn_mummy (pos, number) -- Spawn a mummy at position
function tsm_pyramids.spawn_mummy_at(pos, number)
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
if node.name ~= "air" then if node.name ~= "air" then
return return
@ -383,15 +384,18 @@ minetest.register_node("tsm_pyramids:spawner_mummy", {
end, end,
sounds = spawnersounds, sounds = spawnersounds,
}) })
if not minetest.settings:get_bool("only_peaceful_mobs") then
minetest.register_abm({ -- Attempt to spawn a mummy at a random appropriate position around pos.
nodenames = {"tsm_pyramids:spawner_mummy"}, -- Criteria:
interval = 2.0, -- * Must be close to pos
chance = 20, -- * Not in sunlight
action = function(pos, node, active_object_count, active_object_count_wider) -- * Must be air on top of a non-air block
-- * No more than 6 mummies in area
-- * Player must be near is player_near_required is true
function tsm_pyramids.attempt_mummy_spawn(pos, player_near_required)
local player_near = false local player_near = false
local mobs = 0 local mobs = 0
for _,obj in ipairs(minetest.get_objects_inside_radius(pos, spawner_range)) do for _,obj in ipairs(minetest.get_objects_inside_radius(pos, spawner_check_range)) do
if obj:is_player() then if obj:is_player() then
player_near = true player_near = true
else else
@ -400,11 +404,11 @@ if not minetest.settings:get_bool("only_peaceful_mobs") then
end end
end end
end end
if player_near then if player_near or (not player_near_required) then
if mobs < spawner_max_mobs then if mobs < spawner_max_mobs then
local offset = {x=5,y=2,z=5} local offset = {x=5,y=2,z=5}
local nposses = minetest.find_nodes_in_area(vector.subtract(pos, offset), vector.add(pos,offset), "air") local nposses = minetest.find_nodes_in_area(vector.subtract(pos, offset), vector.add(pos,offset), "air")
local tries = math.max(6, #nposses) local tries = math.min(6, #nposses)
for i=1, tries do for i=1, tries do
local r = math.random(1, #nposses) local r = math.random(1, #nposses)
local npos = nposses[r] local npos = nposses[r]
@ -433,7 +437,7 @@ if not minetest.settings:get_bool("only_peaceful_mobs") then
end end
end end
if footing and two_space and light < 15 then if footing and two_space and light < 15 then
tsm_pyramids.spawn_mummy(npos, 1) tsm_pyramids.spawn_mummy_at(npos, 1)
break break
else else
table.remove(nposses, r) table.remove(nposses, r)
@ -442,6 +446,15 @@ if not minetest.settings:get_bool("only_peaceful_mobs") then
end end
end end
end end
if not minetest.settings:get_bool("only_peaceful_mobs") then
minetest.register_abm({
nodenames = {"tsm_pyramids:spawner_mummy"},
interval = 2.0,
chance = 20,
action = function(pos, node, active_object_count, active_object_count_wider)
tsm_pyramids.attempt_mummy_spawn(pos, true)
end,
}) })
end end