From fb51ab05ea2ccc2073e6e4225f9c6095e7f54ff8 Mon Sep 17 00:00:00 2001 From: MysticTempest Date: Thu, 17 Dec 2020 09:22:34 -0600 Subject: [PATCH] Enable basic mob despawning. --- mods/ENTITIES/mcl_mobs/api.lua | 70 ++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index a6f0fd09..3d8e2b52 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -67,7 +67,7 @@ local disable_blood = minetest.settings:get_bool("mobs_disable_blood") local mobs_drop_items = minetest.settings:get_bool("mobs_drop_items") ~= false local mobs_griefing = minetest.settings:get_bool("mobs_griefing") ~= false local spawn_protected = minetest.settings:get_bool("mobs_spawn_protected") ~= false -local remove_far = false +local remove_far = true local difficulty = tonumber(minetest.settings:get("mob_difficulty")) or 1.0 local show_health = false local max_per_block = tonumber(minetest.settings:get("max_objects_per_block") or 64) @@ -3113,6 +3113,7 @@ end -- get entity staticdata local mob_staticdata = function(self) +--[[ -- remove mob when out of range unless tamed if remove_far and self.can_despawn @@ -3125,7 +3126,7 @@ local mob_staticdata = function(self) return ""-- nil end - +--]] self.remove_ok = true self.attack = nil self.following = nil @@ -3334,37 +3335,6 @@ local mob_step = function(self, dtime) update_tag(self) end - -- Despawning: when lifetimer expires, remove mob - if remove_far - and self.can_despawn == true - and ((not self.nametag) or (self.nametag == "")) then - - -- TODO: Finish up implementation of despawning rules - - self.lifetimer = self.lifetimer - dtime - - if self.lifetimer <= 0 then - - -- only despawn away from player - local objs = minetest.get_objects_inside_radius(pos, 32) - - for n = 1, #objs do - - if objs[n]:is_player() then - - self.lifetimer = 20 - - return - end - end - - minetest.log("action", "Mob "..name.." despawns in mob_step at "..minetest.pos_to_string(pos)) - self.object:remove() - - return - end - end - if self.state == "die" then return end @@ -3548,6 +3518,40 @@ local mob_step = function(self, dtime) set_animation(self, "stand") end + -- Despawning: when lifetimer expires, remove mob + if remove_far + and self.can_despawn == true + and ((not self.nametag) or (self.nametag == "")) then + + self.lifetimer = self.lifetimer - dtime + if self.lifetimer <= 10 then + + -- only despawn away from player + local far_objs = minetest.get_objects_inside_radius(pos, 48) + for n = 1, #far_objs do + + if far_objs[n]:is_player() then + + local close_objs = minetest.get_objects_inside_radius(pos, 16) + for n = 1, #close_objs do + if close_objs[n]:is_player() then + self.lifetimer = 20 + else + if math.random(1,10) <= 3 then + minetest.log("action", "Mob "..self.name.." despawns in mob_step at "..minetest.pos_to_string(pos)) + self.object:remove() + return + end + end + end + else + minetest.log("action", "Mob "..self.name.." despawns in mob_step at "..minetest.pos_to_string(pos)) + self.object:remove() + return + end + end + end + end end