add mobs:add_mob() function

This commit is contained in:
tenplus1 2020-06-22 21:24:06 +01:00
parent ec122aa6de
commit e63b1b478e
2 changed files with 117 additions and 3 deletions

108
api.lua
View File

@ -6,7 +6,7 @@ local use_cmi = minetest.global_exists("cmi")
mobs = {
mod = "redo",
version = "20200620",
version = "20200622",
intllib = S,
invis = minetest.global_exists("invisibility") and invisibility or {}
}
@ -3464,7 +3464,7 @@ mobs.spawning_mobs = {}
-- register mob entity
function mobs:register_mob(name, def)
mobs.spawning_mobs[name] = true
mobs.spawning_mobs[name] = {}
minetest.register_entity(name, setmetatable({
@ -3611,6 +3611,106 @@ end
-- global functions
function mobs:add_mob(pos, def)
-- is mob actually registered?
if not mobs.spawning_mobs[def.name]
or not minetest.registered_entities[def.name] then
--print("--- mob doesn't exist", def.name)
return
end
-- are we over active mob limit
if active_limit > 0 and active_mobs >= active_limit then
--print("--- active mob limit reached", active_mobs, active_limit)
return
end
-- get total number of this mob in area
local num_mob, is_pla = count_mobs(pos, def.name)
if not is_pla then
--print("--- no players within active area, will not spawn " .. def.name)
return
end
local aoc = mobs.spawning_mobs[def.name]
and mobs.spawning_mobs[def.name].aoc or 1
if def.ignore_count ~= true and num_mob >= aoc then
--print("--- too many " .. def.name .. " in area", num_mob .. "/" .. aoc)
return
end
local mob = minetest.add_entity(pos, def.name)
--print("[mobs] Spawned " .. def.name .. " at " .. minetest.pos_to_string(pos))
local ent = mob:get_luaentity()
if not ent then
--print("[mobs] entity not found " .. def.name)
return false
end
if def.child then
local textures = ent.base_texture
-- using specific child texture (if found)
if ent.child_texture then
textures = ent.child_texture[1]
end
-- and resize to half height
mob:set_properties({
textures = textures,
visual_size = {
x = ent.base_size.x * .5,
y = ent.base_size.y * .5
},
collisionbox = {
ent.base_colbox[1] * .5,
ent.base_colbox[2] * .5,
ent.base_colbox[3] * .5,
ent.base_colbox[4] * .5,
ent.base_colbox[5] * .5,
ent.base_colbox[6] * .5
},
selectionbox = {
ent.base_selbox[1] * .5,
ent.base_selbox[2] * .5,
ent.base_selbox[3] * .5,
ent.base_selbox[4] * .5,
ent.base_selbox[5] * .5,
ent.base_selbox[6] * .5
},
})
ent.child = true
end
if def.owner then
ent.tamed = true
ent.owner = def.owner
end
if def.nametag then
-- limit name entered to 64 characters long
if def.nametag:len() > 64 then
def.nametag = def.nametag:sub(1, 64)
end
ent.nametag = def.nametag
ent:update_tag()
end
return true
end
function mobs:spawn_abm_check(pos, node, name)
-- global function to add additional spawn checks
-- return true to stop spawning mob
@ -3621,7 +3721,7 @@ function mobs:spawn_specific(name, nodes, neighbors, min_light, max_light,
interval, chance, aoc, min_height, max_height, day_toggle, on_spawn)
-- Do mobs spawn at all?
if not mobs_spawn then
if not mobs_spawn or not mobs.spawning_mobs[name] then
return
end
@ -3645,6 +3745,8 @@ function mobs:spawn_specific(name, nodes, neighbors, min_light, max_light,
end
mobs.spawning_mobs[name].aoc = aoc
minetest.register_abm({
label = name .. " spawning",

12
api.txt
View File

@ -328,6 +328,18 @@ for each mob.
'self.nametag' contains the name of the mob which it can show above
Adding Mobs in World
--------------------
mobs:add_mob(pos, {
name = "mobs_animal:chicken",
child = true,
owner = "singleplayer",
nametag = "Bessy",
ignore_count = true -- ignores mob count per map area
})
Spawning Mobs in World
----------------------