Implement mob despawner/mob limiter

This commit is contained in:
jordan4ibanez 2021-04-23 19:49:35 -04:00
parent 19c8dd1dd4
commit 5ee6cf6c9b
5 changed files with 45 additions and 26 deletions

View file

@ -186,15 +186,6 @@ function mobs:register_mob(name, def)
mobs.spawning_mobs[name] = true mobs.spawning_mobs[name] = true
local can_despawn
if def.can_despawn ~= nil then
can_despawn = def.can_despawn
elseif def.spawn_class == "passive" then
can_despawn = false
else
can_despawn = true
end
local function scale_difficulty(value, default, min, special) local function scale_difficulty(value, default, min, special)
if (not value) or (value == default) or (value == special) then if (not value) or (value == default) or (value == special) then
return default return default
@ -219,7 +210,6 @@ function mobs:register_mob(name, def)
do_custom = def.do_custom, do_custom = def.do_custom,
jump_height = def.jump_height or 4, -- was 6 jump_height = def.jump_height or 4, -- was 6
rotate = def.rotate or 0, -- 0=front, 90=side, 180=back, 270=side2 rotate = def.rotate or 0, -- 0=front, 90=side, 180=back, 270=side2
lifetimer = def.lifetimer or 57.73,
hp_min = scale_difficulty(def.hp_min, 5, 1), hp_min = scale_difficulty(def.hp_min, 5, 1),
hp_max = scale_difficulty(def.hp_max, 10, 1), hp_max = scale_difficulty(def.hp_max, 10, 1),
xp_min = def.xp_min or 0, xp_min = def.xp_min or 0,
@ -338,6 +328,8 @@ function mobs:register_mob(name, def)
projectile_cooldown_min = def.projectile_cooldown_min or 2, projectile_cooldown_min = def.projectile_cooldown_min or 2,
projectile_cooldown_max = def.projectile_cooldown_max or 6, projectile_cooldown_max = def.projectile_cooldown_max or 6,
skittish = def.skittish, skittish = def.skittish,
lifetimer_reset = 30, --30 seconds
lifetimer = 30, --30 seconds
--end j4i stuff --end j4i stuff
-- MCL2 extensions -- MCL2 extensions
@ -347,7 +339,7 @@ function mobs:register_mob(name, def)
ignores_nametag = def.ignores_nametag or false, ignores_nametag = def.ignores_nametag or false,
rain_damage = def.rain_damage or 0, rain_damage = def.rain_damage or 0,
glow = def.glow, glow = def.glow,
can_despawn = can_despawn, --can_despawn = can_despawn,
child = def.child or false, child = def.child or false,
texture_mods = {}, texture_mods = {},
shoot_arrow = def.shoot_arrow, shoot_arrow = def.shoot_arrow,

View file

@ -696,6 +696,19 @@ mobs.mob_step = function(self, dtime)
return false return false
end end
--despawn mechanism
--don't despawned tamed mobs
if not self.tamed then
self.lifetimer = self.lifetimer - dtime
if self.lifetimer <= 0 then
self.lifetimer = self.lifetimer_reset
if not mobs.check_for_player_within_area(self, 64) then
--print("removing in MAIN LOGIC!")
self.object:remove()
return
end
end
end
--color modifier which coincides with the pause_timer --color modifier which coincides with the pause_timer
if self.old_health and self.health < self.old_health then if self.old_health and self.health < self.old_health then

View file

@ -6,9 +6,11 @@ local minetest_get_item_group = minetest.get_item_group
local minetest_get_objects_inside_radius = minetest.get_objects_inside_radius local minetest_get_objects_inside_radius = minetest.get_objects_inside_radius
local minetest_get_node_or_nil = minetest.get_node_or_nil local minetest_get_node_or_nil = minetest.get_node_or_nil
local minetest_registered_nodes = minetest.registered_nodes local minetest_registered_nodes = minetest.registered_nodes
local minetest_get_connected_players = minetest.get_connected_players
local vector_new = vector.new local vector_new = vector.new
local vector_multiply = vector.multiply local vector_multiply = vector.multiply
local vector_distance = vector.distance
local table_copy = table.copy local table_copy = table.copy
@ -215,3 +217,21 @@ mobs.teleport = function(self, target)
end end
end end
end end
--a function used for despawning mobs
mobs.check_for_player_within_area = function(self, radius)
local pos1 = self.object:get_pos()
--get players in radius
for _,player in pairs(minetest_get_connected_players()) do
if player and player:get_hp() > 0 then
local pos2 = player:get_pos()
local distance = vector_distance(pos1,pos2)
if distance < radius then
--found a player
return(true)
end
end
end
--did not find a player
return(false)
end

View file

@ -5,21 +5,15 @@ local minetest_settings = minetest.settings
-- get entity staticdata -- get entity staticdata
mobs.mob_staticdata = function(self) mobs.mob_staticdata = function(self)
--[[ --despawn mechanism
-- remove mob when out of range unless tamed --don't despawned tamed mobs
if remove_far if not self.tamed then
and self.can_despawn if not mobs.check_for_player_within_area(self, 64) then
and self.remove_ok --print("removing SERIALIZED!")
and ((not self.nametag) or (self.nametag == ""))
and self.lifetimer <= 20 then
minetest.log("action", "Mob "..name.." despawns in mob_staticdata at "..minetest.pos_to_string(self.object.get_pos(), 1))
mcl_burning.extinguish(self.object)
self.object:remove() self.object:remove()
return
return ""-- nil end
end end
--]]
self.remove_ok = true self.remove_ok = true
self.attack = nil self.attack = nil

View file

@ -20,7 +20,7 @@ local table_remove = table.remove
-- range for mob count -- range for mob count
local aoc_range = 32 local aoc_range = 48
--[[ --[[
THIS IS THE BIG LIST OF ALL BIOMES - used for programming/updating mobs THIS IS THE BIG LIST OF ALL BIOMES - used for programming/updating mobs