From ea4ea3f05e82f17c8b388a8cb4df900d2a226406 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Thu, 4 May 2023 21:14:16 +0100 Subject: [PATCH] Standardise despawn logic and add asserts. Add persistent flag for mobs that have been interacted with. --- mods/ENTITIES/mcl_mobs/api.lua | 6 ++-- mods/ENTITIES/mcl_mobs/breeding.lua | 1 + mods/ENTITIES/mcl_mobs/spawning.lua | 38 ++++++++++++++++++++--- mods/ENTITIES/mobs_mc/enderman.lua | 17 +++++----- mods/ENTITIES/mobs_mc/villager_zombie.lua | 2 +- 5 files changed, 45 insertions(+), 19 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index 39d2f45a9..622366fea 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -78,11 +78,10 @@ function mob_class:get_staticdata() for _,p in pairs(minetest.get_connected_players()) do self:remove_particlespawners(p:get_player_name()) end + -- remove mob when out of range unless tamed if remove_far - and self.can_despawn - and self.remove_ok - and ((not self.nametag) or (self.nametag == "")) + and self:despawn_allowed() and self.lifetimer <= 20 then if spawn_logging then minetest.log("action", "[mcl_mobs] Mob "..tostring(self.name).." despawns at "..minetest.pos_to_string(vector.round(self.object:get_pos())) .. " - out of range") @@ -91,7 +90,6 @@ function mob_class:get_staticdata() return "remove"-- nil end - self.remove_ok = true self.attack = nil self.following = nil self.state = "stand" diff --git a/mods/ENTITIES/mcl_mobs/breeding.lua b/mods/ENTITIES/mcl_mobs/breeding.lua index a70d9cbea..c6d8f92cd 100644 --- a/mods/ENTITIES/mcl_mobs/breeding.lua +++ b/mods/ENTITIES/mcl_mobs/breeding.lua @@ -74,6 +74,7 @@ function mob_class:feed_tame(clicker, feed_count, breed, tame, notake) if self.food >= feed_count then self.food = 0 self.horny = true + self.persistent = true end end diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index be8e1028e..adff1a918 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -1071,15 +1071,43 @@ if mobs_spawn then end) end +local function despawn_allowed(self) + local nametag = self.nametag and self.nametag ~= "" + local not_busy = self.state ~= "attack" and self.following == nil + if self.can_despawn == true then + if not nametag and not_busy and not self.tamed == true and not self.persistent == true then + return true + end + end + return false +end + +function mob_class:despawn_allowed() + despawn_allowed(self) +end + + +assert(despawn_allowed({can_despawn=false}) == false, "despawn_allowed - can_despawn false failed") +assert(despawn_allowed({can_despawn=true}) == true, "despawn_allowed - can_despawn true failed") + +assert(despawn_allowed({can_despawn=true, nametag=""}) == true, "despawn_allowed - blank nametag failed") +assert(despawn_allowed({can_despawn=true, nametag=nil}) == true, "despawn_allowed - nil nametag failed") +assert(despawn_allowed({can_despawn=true, nametag="bob"}) == false, "despawn_allowed - nametag failed") + +assert(despawn_allowed({can_despawn=true, state="attack"}) == false, "despawn_allowed - attack state failed") +assert(despawn_allowed({can_despawn=true, following="blah"}) == false, "despawn_allowed - following state failed") + +assert(despawn_allowed({can_despawn=true, tamed=false}) == true, "despawn_allowed - not tamed") +assert(despawn_allowed({can_despawn=true, tamed=true}) == false, "despawn_allowed - tamed") + +assert(despawn_allowed({can_despawn=true, persistent=true}) == false, "despawn_allowed - persistent") +assert(despawn_allowed({can_despawn=true, persistent=false}) == true, "despawn_allowed - not persistent") + function mob_class:check_despawn(pos, dtime) self.lifetimer = self.lifetimer - dtime -- Despawning: when lifetimer expires, remove mob - if remove_far - and self.can_despawn == true - and ((not self.nametag) or (self.nametag == "")) - and self.state ~= "attack" - and self.following == nil then + if remove_far and despawn_allowed(self) then if self.despawn_immediately or self.lifetimer <= 0 then if logging then minetest.log("action", "[mcl_mobs] Mob "..self.name.." despawns at "..minetest.pos_to_string(pos, 1) .. " lifetimer ran out") diff --git a/mods/ENTITIES/mobs_mc/enderman.lua b/mods/ENTITIES/mobs_mc/enderman.lua index 8415084e1..a57160e4d 100644 --- a/mods/ENTITIES/mobs_mc/enderman.lua +++ b/mods/ENTITIES/mobs_mc/enderman.lua @@ -24,6 +24,12 @@ -- added rain damage. -- fixed the grass_with_dirt issue. +-- How freqeuntly to take and place blocks, in seconds +local take_frequency_min = 235 +local take_frequency_max = 245 +local place_frequency_min = 235 +local place_frequency_max = 245 + minetest.register_entity("mobs_mc:ender_eyes", { visual = "mesh", mesh = "mobs_mc_spider.b3d", @@ -60,13 +66,6 @@ end local pr = PseudoRandom(os.time()*(-334)) --- How freqeuntly to take and place blocks, in seconds -local take_frequency_min = 235 -local take_frequency_max = 245 -local place_frequency_min = 235 -local place_frequency_max = 245 - - -- Texuture overrides for enderman block. Required for cactus because it's original is a nodebox -- and the textures have tranparent pixels. local block_texture_overrides @@ -491,7 +490,7 @@ mcl_mobs.register_mob("mobs_mc:enderman", { local dug = minetest.get_node_or_nil(take_pos) if dug and dug.name == "air" then self._taken_node = node.name - self.can_despawn = false + self.persistent = true local def = minetest.registered_nodes[self._taken_node] -- Update animation and texture accordingly (adds visibly carried block) local block_type @@ -542,7 +541,7 @@ mcl_mobs.register_mob("mobs_mc:enderman", { if success then local def = minetest.registered_nodes[self._taken_node] -- Update animation accordingly (removes visible block) - self.can_despawn = true + self.persistent = false self.animation = select_enderman_animation("normal") self:set_animation(self.animation.current) if def.sounds and def.sounds.place then diff --git a/mods/ENTITIES/mobs_mc/villager_zombie.lua b/mods/ENTITIES/mobs_mc/villager_zombie.lua index 37dce5623..5af1fc879 100644 --- a/mods/ENTITIES/mobs_mc/villager_zombie.lua +++ b/mods/ENTITIES/mobs_mc/villager_zombie.lua @@ -109,7 +109,7 @@ mcl_mobs.register_mob("mobs_mc:villager_zombie", { clicker:set_wielded_item(wielditem) self._curing = math.random(3 * 60, 5 * 60) self.shaking = true - self.can_despawn = false + self.persistent = true end end end,