Skip to content

Instantly share code, notes, and snippets.

@corarona
Created June 3, 2023 23:55
Show Gist options
  • Save corarona/01fdba886aa237f19427c6718a1cec51 to your computer and use it in GitHub Desktop.
Save corarona/01fdba886aa237f19427c6718a1cec51 to your computer and use it in GitHub Desktop.
pestcontrol
local search_radius = 20
local limit = 15
local interval = 60
local function crowded(e)
local n = 0
local pos = e.object:get_pos()
if not pos then return end
for _,f in pairs(minetest.luaentities) do
if f.is_mob and f.name == e.name and not f.tamed and not f.nametag then
local fpos = f.object:get_pos()
if fpos and vector.distance(pos,fpos) < search_radius then
n = n + 1
end
end
end
if n > limit then return true end
end
local timer = 0
local function run_pestcontrol(dtime)
timer = timer + dtime
if timer >= interval then
for _,e in pairs(minetest.luaentities) do
if e.is_mob and crowded(e) and not f.tamed and not f.nametag and math.random(3) == 1 then
minetest.log("warning","mob "..e.name.." removing, too many here at "..minetest.pos_to_string(e.object:get_pos(),0))
mcl_burning.extinguish(e.object)
e.object:remove()
end
end
timer = 0
end
end
minetest.register_globalstep(run_pestcontrol)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment