Standardise despawn logic and add asserts. Add persistent flag for mobs that have been interacted with.

This commit is contained in:
ancientmarinerdev 2023-05-04 21:14:16 +01:00
parent 453e90741d
commit ea4ea3f05e
5 changed files with 45 additions and 19 deletions

View File

@ -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"

View File

@ -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

View File

@ -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")

View File

@ -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

View File

@ -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,