

if minetest.get_modpath("pmb_mob_spawn") then
    pmb_mob_spawn.register_mob({
        name = "pmb_kelar:kelar",
        groups = {"hostile", "pmb_kelar"},
        biomes = {},
        on_spawn = function(self, pos)
            for i = 0, (math.random(1,4)) do
                local spos = vector.offset(
                    pos,
                    (math.random()*2-1) * 2,
                    0.5,
                    (math.random()*2-1) * 2
                )
                local obj = minetest.add_entity(spos, "pmb_kelar:kelar")
                obj:set_yaw(math.random() * math.pi * 2)
            end
        end,
        can_spawn = function(pos, spawn_info)
            if not pmb_dimensions.is_in("overworld", pos) then return false end
            if not pmb_mob_spawn.has_head_room(pos, 2) then return false end
            -- not near artificial light, not in sunlight unless it's nighttime
            if pmb_mob_spawn.has_light(pos, 0, 2, nil) then
                if pmb_mob_spawn.has_light(pos, 0, 0, 0.5)
                or pmb_entity_api.has_mobs_in_radius(pos, 80, {"pmb_kelar:kelar"}, 0, 1) then
                    return true
                end
            end
            return false
        end,
        can_despawn = function(self)
            if self._age and self._age < 120 then return false end
            return true
        end,
    })
    local biomes = {
        underworld = true,
        underworld_bridges = true,
        underworld_coral = true,
    }
    pmb_mob_spawn.register_mob({
        name = "pmb_kelar:kelar_underworld",
        groups = {"hostile", "pmb_kelar"},
        biomes = {},
        on_spawn = function(self, pos)
        end,
        can_spawn = function(pos, spawn_info)
            if math.random() < 0.90 then return false end
            if not pmb_dimensions.is_in("underworld", pos) then return false end
            if not biomes[minetest.get_biome_name(minetest.get_biome_data(pos).biome)] then return false end
            if minetest.get_item_group(minetest.get_node(vector.offset(pos, 0, -1, 0)).name, "lava_rock") == 0 then return false end
            if not pmb_mob_spawn.has_head_room(pos, 2) then return false end
            if pmb_mob_spawn.has_light(pos, 0, 7) then
                if pmb_entity_api.has_mobs_in_radius(pos, 100, {"pmb_kelar:kelar_underworld"}, 0, 1) then
                    return true
                end
            end
            return false
        end,
        can_despawn = function(self)
            if self._age and self._age < 20 then return false end
            return true
        end,
    })
    pmb_mob_spawn.cap.pmb_kelar.max = 20
end
