added 'mob_active_limit' to limit mobs in game

This commit is contained in:
tenplus1 2020-05-16 10:44:38 +01:00
parent 53cc10a16f
commit 85faeea604
4 changed files with 41 additions and 43 deletions

76
api.lua
View File

@ -6,16 +6,11 @@ local use_cmi = minetest.global_exists("cmi")
mobs = {
mod = "redo",
version = "20200515",
version = "20200516",
intllib = S,
invis = minetest.global_exists("invisibility") and invisibility or {}
}
-- mob table and limit (not active)
local active_mobs = 0
local active_limit = 99
-- creative check
local creative_cache = minetest.settings:get_bool("creative_mode")
function mobs.is_creative(name)
@ -59,7 +54,11 @@ local difficulty = tonumber(minetest.settings:get("mob_difficulty")) or 1.0
local show_health = minetest.settings:get_bool("mob_show_health") ~= false
local max_per_block = tonumber(minetest.settings:get("max_objects_per_block") or 99)
local mob_nospawn_range = tonumber(minetest.settings:get("mob_nospawn_range") or 12)
local mob_chance_multiplier = tonumber(minetest.settings:get("mob_chance_multiplier") or 1)
local active_limit = tonumber(minetest.settings:get("mob_active_limit") or 0)
local mob_chance_multiplier =
tonumber(minetest.settings:get("mob_chance_multiplier") or 1)
local active_mobs = 0
-- Peaceful mode message so players will know there are no monsters
if peaceful_only then
@ -763,9 +762,9 @@ local remove_mob = function(self, decrease)
self.object:remove()
if decrease then
if decrease and active_limit > 0 then
-- active_mobs = active_mobs - 1
active_mobs = active_mobs - 1
if active_mobs < 0 then
active_mobs = 0
@ -1361,7 +1360,7 @@ function mob_class:breed()
ent.hornytimer = 41
-- have we reached active mob limit
if active_mobs >= active_limit then
if active_limit > 0 and active_mobs >= active_limit then
minetest.chat_send_player(self.owner,
S("Active Mob Limit Reached!")
.. " (" .. active_mobs
@ -1391,8 +1390,6 @@ function mob_class:breed()
local ent2 = mob:get_luaentity()
local textures = self.base_texture
-- active_mobs = active_mobs + 1
-- using specific child texture (if found)
if self.child_texture then
textures = self.child_texture[1]
@ -2969,6 +2966,13 @@ end
-- get entity staticdata
function mob_class:mob_staticdata()
-- this handles mob count for mobs activated, unloaded, reloaded
if active_limit > 0 and self.active_toggle then
active_mobs = active_mobs + self.active_toggle
self.active_toggle = -self.active_toggle
--print("-- staticdata", active_mobs, active_limit, self.active_toggle)
end
-- remove mob when out of range unless tamed
if remove_far
and self.remove_ok
@ -3005,9 +3009,7 @@ function mob_class:mob_staticdata()
local t = type(stat)
if t ~= "function"
and t ~= "nil"
and t ~= "userdata"
if t ~= "function" and t ~= "nil" and t ~= "userdata"
and _ ~= "_cmi_components" then
tmp[_] = self[_]
end
@ -3022,6 +3024,20 @@ end
-- activate mob and reload settings
function mob_class:mob_activate(staticdata, def, dtime)
-- if dtime == 0 then entity has just been created
-- anything higher means it is respawning (thanks SorceryKid)
if dtime == 0 and active_limit > 0 then
self.active_toggle = 1
end
-- remove mob if not tamed and mob total reached
if active_limit > 0 and active_mobs >= active_limit and not self.tamed then
remove_mob(self)
--print("-- mob limit reached, removing " .. self.name)
return
end
-- remove monsters in peaceful mode
if self.type == "monster" and peaceful_only then
@ -3030,14 +3046,6 @@ function mob_class:mob_activate(staticdata, def, dtime)
return
end
-- remove mob if not tamed and mob total reached
if active_mobs >= active_limit and not self.tamed then
remove_mob(self)
--print("-- mob limit reached, removing " .. self.name)
return
end
-- load entity variables
local tmp = minetest.deserialize(staticdata)
@ -3047,13 +3055,6 @@ function mob_class:mob_activate(staticdata, def, dtime)
end
end
-- add currently spawned mobs to total
-- this is buggy as it doubles count when mobs unloaded and reloaded
if self.standing_in then
-- active_mobs = active_mobs + 1
--print("-- active mobs: " .. active_mobs .. " / " .. active_limit)
end
-- force current model into mob
self.mesh = def.mesh
self.base_mesh = def.mesh
@ -3101,10 +3102,7 @@ function mob_class:mob_activate(staticdata, def, dtime)
-- set child objects to half size
if self.child == true then
vis_size = {
x = self.base_size.x * .5,
y = self.base_size.y * .5,
}
vis_size = {x = self.base_size.x * .5, y = self.base_size.y * .5}
if def.child_texture then
textures = def.child_texture[1]
@ -3632,7 +3630,7 @@ function mobs:spawn_specific(name, nodes, neighbors, min_light, max_light,
end
-- are we over active mob limit
if active_mobs >= active_limit then
if active_limit > 0 and active_mobs >= active_limit then
--print("--- active mob limit reached", active_mobs, active_limit)
return
end
@ -3780,8 +3778,6 @@ function mobs:spawn_specific(name, nodes, neighbors, min_light, max_light,
local mob = minetest.add_entity(pos, name)
-- active_mobs = active_mobs + 1
-- print("[mobs] Spawned " .. name .. " at "
-- .. minetest.pos_to_string(pos) .. " on "
-- .. node.name .. " near " .. neighbors[1])
@ -4056,8 +4052,6 @@ function mobs:register_egg(mob, desc, background, addegg, no_creative)
if not ent then return end -- sanity check
-- active_mobs = active_mobs + 1
-- set owner if not a monster
if ent.type ~= "monster" then
ent.owner = placer:get_player_name()
@ -4102,7 +4096,7 @@ function mobs:register_egg(mob, desc, background, addegg, no_creative)
end
-- have we reached active mob limit
if active_mobs >= active_limit then
if active_limit > 0 and active_mobs >= active_limit then
minetest.chat_send_player(placer:get_player_name(),
S("Active Mob Limit Reached!")
.. " (" .. active_mobs
@ -4117,8 +4111,6 @@ function mobs:register_egg(mob, desc, background, addegg, no_creative)
if not ent then return end -- sanity check
-- active_mobs = active_mobs + 1
-- don't set owner if monster or sneak pressed
if ent.type ~= "monster"
and not placer:get_player_control().sneak then

View File

@ -636,6 +636,7 @@ External Settings for "minetest.conf"
pathfinding level 2, replace functions or mobs:boom
function.
'mob_nospawn_range' Minimum range a mob can spawn near player (def: 12)
'mob_active_limit' Number of active mobs in game, 0 for unlimited
Players can override the spawn chance for each mob registered by adding a line
to their minetest.conf file with a new value, the lower the value the more each

View File

@ -23,6 +23,8 @@ Lucky Blocks: 9
Changelog:
- 1.52 - Added 'mob_active_limit' in settings to set number of mobs in game
(default is 0 for unlimited)
- 1.51 - Added some node checks for dangerous nodes, jumping and falling tweaks, spawn area check (thx for idea wuzzy), re-enabled mob suffocation, add 'mob_nospawn_range' setting
- 1.50 - Added new line_of_sight function that uses raycasting if mt5.0 is found, (thanks Astrobe), dont spawn mobs if world anchor nearby (technic or simple_anchor mods), chinese local added
- 1.49- Added mobs:force_capture(self, player) function, api functions now use metatables thanks to bell07

View File

@ -28,5 +28,8 @@ mob_chance_multiplier (Mob chance multiplier) float 1.0
# When false Mob no longer drop items when killed
mobs_drop_items (Mob drops) bool true
#....Sets minimum distance around player that mobs cannot spawn
# Sets minimum distance around player that mobs cannot spawn
mob_nospawn_range (Mob no-spawn range) float 12.0
# Sets maximum number of active mobs in game (0 for unlimited)
mob_active_limit (Mob Active Limit) float 0