From 1aff8fee5b935155048cde5855833a929bf8ccb4 Mon Sep 17 00:00:00 2001 From: Jordan Irwin Date: Sat, 8 May 2021 16:01:21 -0700 Subject: [PATCH] Add option to require nearby players for spawn --- TODO.txt | 1 - settings.lua | 12 ++++++++++++ settingtypes.txt | 8 ++++++++ spawn.lua | 26 ++++++++++++++++++-------- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/TODO.txt b/TODO.txt index c39760f..a05f351 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,4 +1,3 @@ TODO: -- check for nearbly players before spawning - add lifetime limit diff --git a/settings.lua b/settings.lua index 87ba5e3..ee6f413 100644 --- a/settings.lua +++ b/settings.lua @@ -3,6 +3,18 @@ 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. -- -- Inverted value (e.g. 10000 = 1/10000). diff --git a/settingtypes.txt b/settingtypes.txt index 113397b..3f95686 100644 --- a/settingtypes.txt +++ b/settingtypes.txt @@ -2,6 +2,14 @@ # Extra debugging messages. 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. # # Rate is the inverted value (e.g. 1/value). diff --git a/spawn.lua b/spawn.lua index 099c59d..65803cb 100644 --- a/spawn.lua +++ b/spawn.lua @@ -28,6 +28,22 @@ core.register_abm({ -- check above target node 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 return end @@ -46,15 +62,9 @@ core.register_abm({ local spawned = core.add_entity(pos, "sneeker:sneeker") if not spawned then - sneeker.log("warning", "Failed to spawn at: " - .. tostring(math.floor(pos.x)) - .. "," .. tostring(math.floor(pos.y)) - .. "," .. tostring(math.floor(pos.z))) + sneeker.log("warning", "Failed to spawn at: " .. pos_string) else - sneeker.log("debug", "Spawned at: " - .. tostring(math.floor(pos.x)) - .. "," .. tostring(math.floor(pos.y)) - .. "," .. tostring(math.floor(pos.z))) + sneeker.log("debug", "Spawned at: " .. pos_string) end end })