Add option to require nearby players for spawn

This commit is contained in:
Jordan Irwin 2021-05-08 16:01:21 -07:00
parent b0f464e7ac
commit 1aff8fee5b
4 changed files with 38 additions and 9 deletions

@ -1,4 +1,3 @@
TODO: TODO:
- check for nearbly players before spawning
- add lifetime limit - add lifetime limit

@ -3,6 +3,18 @@
local time_min = 60 local time_min = 60
--- Determines whether or not a player must be close for spawn to occur.
--
-- @setting sneeker.spawn_require_player_nearby
sneeker.spawn_require_player_nearby = core.settings:get_bool("sneeker.spawn_require_player_nearby", true)
--- Distance in nodes a player must be for spawn to occur.
--
-- Only used if `sneeker.spawn_require_player_nearby` enabled.
--
-- @setting sneeker.spawn_player_radius
sneeker.spawn_player_radius = tonumber(core.settings:get("sneeker.spawn_player_radius") or 100)
--- Sets possibility for spawn. --- Sets possibility for spawn.
-- --
-- Inverted value (e.g. 10000 = 1/10000). -- Inverted value (e.g. 10000 = 1/10000).

@ -2,6 +2,14 @@
# Extra debugging messages. # Extra debugging messages.
enable_debug_mods (Mod debugging) bool false enable_debug_mods (Mod debugging) bool false
# Determines whether or not a player must be close for spawn to occur.
sneeker.spawn_require_player_nearby (Require player nearby) bool true
# Distance in nodes a player must be for spawn to occur.
#
# Only used if `sneeker.spawn_require_player_nearby` enabled.
sneeker.spawn_player_radius (Player detect radius) int 100
# Sets possibility for spawn. # Sets possibility for spawn.
# #
# Rate is the inverted value (e.g. 1/value). # Rate is the inverted value (e.g. 1/value).

@ -28,6 +28,22 @@ core.register_abm({
-- check above target node -- check above target node
pos.y = pos.y+1 pos.y = pos.y+1
local pos_string = tostring(math.floor(pos.x))
.. "," .. tostring(math.floor(pos.y))
.. "," .. tostring(math.floor(pos.z))
if sneeker.spawn_require_player_nearby then
local player_nearby = false
for _, entity in ipairs(core.get_objects_inside_radius(pos, sneeker.spawn_player_radius)) do
if entity:is_player() then
player_nearby = true
break
end
end
if not player_nearby then return end
end
if pos.y > sneeker.spawn_maxheight or pos.y < sneeker.spawn_minheight then if pos.y > sneeker.spawn_maxheight or pos.y < sneeker.spawn_minheight then
return return
end end
@ -46,15 +62,9 @@ core.register_abm({
local spawned = core.add_entity(pos, "sneeker:sneeker") local spawned = core.add_entity(pos, "sneeker:sneeker")
if not spawned then if not spawned then
sneeker.log("warning", "Failed to spawn at: " sneeker.log("warning", "Failed to spawn at: " .. pos_string)
.. tostring(math.floor(pos.x))
.. "," .. tostring(math.floor(pos.y))
.. "," .. tostring(math.floor(pos.z)))
else else
sneeker.log("debug", "Spawned at: " sneeker.log("debug", "Spawned at: " .. pos_string)
.. tostring(math.floor(pos.x))
.. "," .. tostring(math.floor(pos.y))
.. "," .. tostring(math.floor(pos.z)))
end end
end end
}) })