From ad2fbb0008c4c5830bf5ea730c636414cad2810c Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Fri, 14 Sep 2018 14:48:48 +0200 Subject: [PATCH] Add some despawning logic (but do not enable despawning yet) --- mods/ENTITIES/mcl_mobs/api.lua | 37 ++++++++++++++++++++------------ mods/ENTITIES/mobs_mc/bat.lua | 1 + mods/ENTITIES/mobs_mc/ocelot.lua | 2 ++ mods/ENTITIES/mobs_mc/squid.lua | 1 + mods/ENTITIES/mobs_mc/wolf.lua | 3 ++- 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index 452ef55eb..846a86c27 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -60,6 +60,7 @@ local mobs_drop_items = minetest.settings:get_bool("mobs_drop_items") ~= false local mobs_griefing = minetest.settings:get_bool("mobs_griefing") ~= false local creative = minetest.settings:get_bool("creative_mode") local spawn_protected = minetest.settings:get_bool("mobs_spawn_protected") ~= false +-- TODO local remove_far = false local difficulty = tonumber(minetest.settings:get("mob_difficulty")) or 1.0 local show_health = false @@ -2537,11 +2538,10 @@ local mob_staticdata = function(self) -- remove mob when out of range unless tamed if remove_far + and self.can_despawn and self.remove_ok - and self.type ~= "npc" - and self.state ~= "attack" - and not self.tamed - and self.lifetimer < 20000 then + and ((not self.nametag) or (self.nametag == "")) + and self.lifetimer <= 20 then self.object:remove() @@ -2735,19 +2735,19 @@ local mob_step = function(self, dtime) local pos = self.object:get_pos() local yaw = 0 - -- when lifetimer expires remove mob (except npc and tamed) - if self.type ~= "npc" - and not self.tamed - and self.state ~= "attack" - and remove_far ~= true - and self.lifetimer < 20000 then + -- 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, 15) + local objs = minetest.get_objects_inside_radius(pos, 32) for n = 1, #objs do @@ -2759,8 +2759,6 @@ local mob_step = function(self, dtime) end end - effect(pos, 15, "tnt_smoke.png", 2, 4, 2, 0) - self.object:remove() return @@ -2935,6 +2933,16 @@ function mobs:register_mob(name, def) mobs.spawning_mobs[name] = true +local can_despawn +if def.can_despawn ~= nil then + can_despawn = def.can_despawn +else + if def.type == "monster" then + can_despawn = true + else + can_despawn = false + end +end minetest.register_entity(name, { stepheight = def.stepheight or 1.1, -- was 0.6 @@ -2950,7 +2958,7 @@ minetest.register_entity(name, { jump_height = def.jump_height or 4, -- was 6 drawtype = def.drawtype, -- DEPRECATED, use rotate instead rotate = math.rad(def.rotate or 0), -- 0=front, 90=side, 180=back, 270=side2 - lifetimer = def.lifetimer or 180, -- 3 minutes + lifetimer = def.lifetimer or 57.73, hp_min = max(1, (def.hp_min or 5) * difficulty), hp_max = max(1, (def.hp_max or 10) * difficulty), physical = true, @@ -3035,6 +3043,7 @@ minetest.register_entity(name, { -- MCL2 extensions ignores_nametag = def.ignores_nametag or false, rain_damage = def.rain_damage or 0, + can_despawn = can_despawn, on_spawn = def.on_spawn, diff --git a/mods/ENTITIES/mobs_mc/bat.lua b/mods/ENTITIES/mobs_mc/bat.lua index 64e0e349e..d30bc98d5 100644 --- a/mods/ENTITIES/mobs_mc/bat.lua +++ b/mods/ENTITIES/mobs_mc/bat.lua @@ -6,6 +6,7 @@ local S, NS = dofile(MP.."/intllib.lua") mobs:register_mob("mobs_mc:bat", { type = "animal", + can_despawn = true, passive = true, hp_min = 6, hp_max = 6, diff --git a/mods/ENTITIES/mobs_mc/ocelot.lua b/mods/ENTITIES/mobs_mc/ocelot.lua index a590029fd..7bfdd6741 100644 --- a/mods/ENTITIES/mobs_mc/ocelot.lua +++ b/mods/ENTITIES/mobs_mc/ocelot.lua @@ -30,6 +30,7 @@ end -- Ocelot local ocelot = { type = "animal", + can_despawn = true, hp_min = 10, hp_max = 10, collisionbox = {-0.3, -0.01, -0.3, 0.3, 0.69, 0.3}, @@ -97,6 +98,7 @@ mobs:register_mob("mobs_mc:ocelot", ocelot) -- Cat local cat = table.copy(ocelot) cat.textures = {{"mobs_mc_cat_black.png"}, {"mobs_mc_cat_red.png"}, {"mobs_mc_cat_siamese.png"}} +cat.can_despawn = false cat.owner = "" cat.order = "roam" -- "sit" or "roam" cat.owner_loyal = true diff --git a/mods/ENTITIES/mobs_mc/squid.lua b/mods/ENTITIES/mobs_mc/squid.lua index d61ae0878..a2ea34abf 100644 --- a/mods/ENTITIES/mobs_mc/squid.lua +++ b/mods/ENTITIES/mobs_mc/squid.lua @@ -10,6 +10,7 @@ local S, NS = dofile(MP.."/intllib.lua") mobs:register_mob("mobs_mc:squid", { type = "animal", + can_despawn = true, passive = true, hp_min = 10, hp_max = 10, diff --git a/mods/ENTITIES/mobs_mc/wolf.lua b/mods/ENTITIES/mobs_mc/wolf.lua index 056f3437b..934fd753c 100644 --- a/mods/ENTITIES/mobs_mc/wolf.lua +++ b/mods/ENTITIES/mobs_mc/wolf.lua @@ -22,7 +22,7 @@ end -- Wolf local wolf = { type = "animal", - + can_despawn = true, hp_min = 8, hp_max = 8, passive = false, @@ -123,6 +123,7 @@ end -- Tamed wolf (aka “dog”) local dog = table.copy(wolf) +dog.can_despawn = false dog.passive = true dog.hp_min = 20 dog.hp_max = 20