diff --git a/spawners_mobs/api.lua b/spawners_mobs/api.lua index 45dd1df..d0ee771 100644 --- a/spawners_mobs/api.lua +++ b/spawners_mobs/api.lua @@ -15,15 +15,15 @@ for k, mob_mod in ipairs(ENABLED_MODS) do table.insert(spawners_mobs.mob_tables, { - name=mob.name, - mod_prefix=mob_mod, - egg_name_custom=mob.egg_name_custom, - dummy_size=mob.dummy_size, - dummy_offset=mob.dummy_offset, - dummy_mesh=mob.dummy_mesh, - dummy_texture=mob.dummy_texture, - night_only=mob.night_only, - sound_custom=mob.sound_custom + name = mob.name, + mod_prefix = mob_mod, + egg_name_custom = mob.egg_name_custom, + dummy_size = mob.dummy_size, + dummy_offset = mob.dummy_offset, + dummy_mesh = mob.dummy_mesh, + dummy_texture = mob.dummy_texture, + night_only = mob.night_only, + sound_custom = mob.sound_custom } ) -- use custom egg or create a default egg @@ -45,8 +45,6 @@ for k, mob_mod in ipairs(ENABLED_MODS) do -- ::continue:: end - else - -- print something ? end end @@ -125,46 +123,44 @@ function spawners_mobs.add_smoke_effects(pos) end -- start spawning mobs -function spawners_mobs.start_spawning(random_pos, how_many, mob_name, mod_prefix, sound_custom) +function spawners_mobs.start_spawning(random_pos, mob_name, mod_prefix, sound_custom) if not (random_pos or how_many or mob_name) then return end - local sound_name - -- remove 'spawners_mobs:' from the string - local mob_name = string.sub(mob_name,15) - + local sound_name = mod_prefix.."_"..mob_name -- use custom sounds if sound_custom ~= "" then sound_name = sound_custom - else - sound_name = mod_prefix.."_"..mob_name end - -- use random colours for sheeps + -- remove 'spawners_mobs:' from the string + print("#2 mod_prefix: "..mod_prefix) + print("#2 mob_name: "..mob_name) + -- local mob_name = string.sub(mob_name,15) + + -- use random colors for sheeps if mob_name == "sheep_white" then - local mob_name1 = "" - local sheep_colours = {"black", "blue", "brown", "cyan", "dark_green", "dark_grey", "green", "grey", "magenta", "orange", "pink", "red", "violet", "white", "yellow"} - local random_colour = math.random(1, #sheep_colours) - mob_name1 = string.split(mob_name, "_") - mob_name1 = mob_name1[1] - mob_name = mob_name1.."_"..sheep_colours[random_colour] + local sheep_colors = {"black", "blue", "brown", "cyan", "dark_green", "dark_grey", "green", "grey", "magenta", "orange", "pink", "red", "violet", "white", "yellow"} + mob_name = "sheep_"..sheep_colors[math.random(#sheep_colors)] end - for i=1,how_many do - random_pos.y = random_pos.y+0.5 + local how_many = math.random(2) + print("how_many: ", how_many) + + for i = 1, how_many do + -- spawn a bit more above the block - prevent spawning inside the block + random_pos.y = random_pos.y + 0.5 spawners_mobs.cloud_booom(random_pos) - local obj - minetest.after(1, function() - obj = minetest.add_entity(random_pos, mod_prefix..":"..mob_name) + local obj = minetest.add_entity(random_pos, mod_prefix..":"..mob_name) if obj then if sound_name then minetest.sound_play(sound_name, { pos = random_pos, - max_hear_distance = 32, - gain = 5, + max_hear_distance = 10, + gain = 0.3 }) end end @@ -205,94 +201,208 @@ function spawners_mobs.check_around_radius(pos, mob) return player_near end -function spawners_mobs.check_node_status(pos, mob, night_only) - local player_near = spawners_mobs.check_around_radius(pos, mob) - - if player_near then - local random_pos = false - local min_node_light = 10 - local tod = minetest.get_timeofday() * 24000 - local node_light = minetest.get_node_light(pos) - - if not node_light then - return false - end - - local spawn_positions = {} - local right = minetest.get_node({x=pos.x+1, y=pos.y, z=pos.z}) - local front = minetest.get_node({x=pos.x, y=pos.y, z=pos.z+1}) - local left = minetest.get_node({x=pos.x-1, y=pos.y, z=pos.z}) - local back = minetest.get_node({x=pos.x, y=pos.y, z=pos.z-1}) - local top = minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z}) - local bottom = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}) - - -- make sure that at least one side of the spawner is open - if right.name == "air" then - table.insert(spawn_positions, {x=pos.x+1.5, y=pos.y, z=pos.z}) - end - if front.name == "air" then - table.insert(spawn_positions, {x=pos.x, y=pos.y, z=pos.z+1.5}) - end - if left.name == "air" then - table.insert(spawn_positions, {x=pos.x-1.5, y=pos.y, z=pos.z}) - end - if back.name == "air" then - table.insert(spawn_positions, {x=pos.x, y=pos.y, z=pos.z-1.5}) - end - if top.name == "air" then - table.insert(spawn_positions, {x=pos.x, y=pos.y+1.5, z=pos.z}) - end - if bottom.name == "air" then - table.insert(spawn_positions, {x=pos.x, y=pos.y-1.5, z=pos.z}) - end - - if #spawn_positions < 1 then - -- spawner is closed from all sides - return false - else - -- pick random from the open sides - local pick_random - - if #spawn_positions == 1 then - pick_random = #spawn_positions - else - pick_random = math.random(1,#spawn_positions) - end - - for k, v in pairs (spawn_positions) do - if k == pick_random then - random_pos = v - end - end - end - - -- check the node above and below the found air node - local node_above = minetest.get_node({x=random_pos.x, y=random_pos.y+1, z=random_pos.z}).name - local node_below = minetest.get_node({x=random_pos.x, y=random_pos.y-1, z=random_pos.z}).name - - if not (node_above == "air" or node_below == "air") then - return false - end - - if night_only ~= "disable" then - -- spawn only at day - if not night_only and node_light < min_node_light then - return false, true - end - - -- spawn only at night - if night_only then - if not (19359 > tod and tod > 5200) or node_light < min_node_light then - return random_pos - else - return false, true - end - end - end - -- random_pos, waiting - return random_pos, false - else - -- random_pos, waiting - return false, true +local old_is_protected = minetest.is_protected +function minetest.is_protected(pos, name) + -- is area protected against name? + if not protector.can_dig(protector.radius, pos, name, false, 1) then + return true end + + -- otherwise can dig or place + return old_is_protected(pos, name) +end + +function spawners_mobs.check_node_status(pos, mob, night_only) + + -- re-factor + + local posmin = { x = pos.x - 4, y = pos.y - 1, z = pos.z - 4 } + local posmax = { x = pos.x + 4, y = pos.y + 1, z = pos.z + 4 } + local player_near = false + local entities_near = 0 + local entities_max = 6 + local node_light_min = 13 + local meta = minetest.get_meta(pos) + local owner = meta:get_string("owner") or "" + local mod_prefix = meta:get_string("mod_prefix") or false + local mob_name = meta:get_string("mob_name") or false + local sound_custom = meta:get_string("sound_custom") or "" + + print("#1 mod_prefix: "..mod_prefix) + print("#1 mob_name: "..mob_name) + + if not (mod_prefix or mob_name) then return end + + -- check spawner light + local node_light = minetest.get_node_light(pos) + + if (not node_light or node_light < node_light_min) and night_only then + -- too dark - spawn only hostile mobs + print("too dark - spawn only hostile mobs") + elseif node_light >= node_light_min and not night_only then + -- enough light - spawn only friendly mobs + print("enough light - spawn only friendly mobs") + else + -- too dark for friendly mob to spawn or too light for hostile mob to spawn + print("too dark for friendly mob to spawn or too light for hostile mob to spawn") + -- tick short + return + end + + -- positions where mobs can spawn + local spawn_area_pos = minetest.find_nodes_in_area(posmin, posmax, "air") + -- get random spawn position from spawn area + local random_idx = math.random(#spawn_area_pos) + local random_pos = spawn_area_pos[random_idx] + local random_pos_above = minetest.get_node({ x = random_pos.x, y = random_pos.y + 1, z = random_pos.z }).name + + -- check if there is enough place to spawn mob + if random_pos_above ~= "air" then + -- tick short + print("no random position found") + return + end + + -- don't do anything and try again later when random position is protected + if minetest.is_protected(random_pos, owner) then + print("random pos is protected") + minetest.record_protection_violation(random_pos, owner) + -- tick short + return + end + + -- area where player and entity count will be detected + local activation_area = minetest.get_objects_inside_radius(pos, 16) + + for k, object in ipairs(activation_area) do + -- find player inside activation area + if object:is_player() then + player_near = true + -- print("found player: "..object:get_player_name()) + end + + -- find entities inside activation area + if not object:is_player() and + object:get_luaentity() and + object:get_luaentity().name ~= "__builtin:item" then + local tmp_mob_name = string.split(object:get_luaentity().name, ":")[2] + + -- sheeps have colors in names + if string.find(tmp_mob_name, "sheep") and string.find(mob, "sheep") then + -- print("found entity: "..tmp_mob_name) + entities_near = entities_near + 1 + + elseif tmp_mob_name == mob then + -- print("found entity: "..tmp_mob_name) + entities_near = entities_near + 1 + end + end + + -- stop looping when met all conditions + if entities_near >= entities_max and player_near then + -- print("max entities reached "..entities_max.." and player_near found, breaking..") + break + end + end + + -- don't do anything and try again later when player not near or max entities reached + if entities_near >= entities_max or not player_near then + -- tick short + print("max entities reached "..entities_max.." or player not near") + return + end + + -- start spawning + -- minetest.set_node(random_pos, { name = "default:apple" }) + spawners_mobs.start_spawning(random_pos, mob_name, mod_prefix, sound_custom) + + -- /re-factor + + -- if player_near then + -- local random_pos = false + -- local min_node_light = 10 + -- local tod = minetest.get_timeofday() * 24000 + -- local node_light = minetest.get_node_light(pos) + + -- if not node_light then + -- return false + -- end + + -- local spawn_positions = {} + -- local right = minetest.get_node_or_nil({x=pos.x+1, y=pos.y, z=pos.z}) + -- local front = minetest.get_node_or_nil({x=pos.x, y=pos.y, z=pos.z+1}) + -- local left = minetest.get_node_or_nil({x=pos.x-1, y=pos.y, z=pos.z}) + -- local back = minetest.get_node_or_nil({x=pos.x, y=pos.y, z=pos.z-1}) + -- local top = minetest.get_node_or_nil({x=pos.x, y=pos.y+1, z=pos.z}) + -- local bottom = minetest.get_node_or_nil({x=pos.x, y=pos.y-1, z=pos.z}) + + -- -- make sure that at least one side of the spawner is open + -- if right ~= nil and right.name == "air" then + -- table.insert(spawn_positions, {x=pos.x+1.5, y=pos.y, z=pos.z}) + -- end + -- if front ~= nil and front.name == "air" then + -- table.insert(spawn_positions, {x=pos.x, y=pos.y, z=pos.z+1.5}) + -- end + -- if left ~= nil and left.name == "air" then + -- table.insert(spawn_positions, {x=pos.x-1.5, y=pos.y, z=pos.z}) + -- end + -- if back ~= nil and back.name == "air" then + -- table.insert(spawn_positions, {x=pos.x, y=pos.y, z=pos.z-1.5}) + -- end + -- if top ~= nil and top.name == "air" then + -- table.insert(spawn_positions, {x=pos.x, y=pos.y+1.5, z=pos.z}) + -- end + -- if bottom ~= nil and bottom.name == "air" then + -- table.insert(spawn_positions, {x=pos.x, y=pos.y-1.5, z=pos.z}) + -- end + + -- if #spawn_positions < 1 then + -- -- spawner is closed from all sides + -- return false + -- else + -- -- pick random from the open sides + -- local pick_random + + -- if #spawn_positions == 1 then + -- pick_random = #spawn_positions + -- else + -- pick_random = math.random(#spawn_positions) + -- end + + -- for k, v in pairs (spawn_positions) do + -- if k == pick_random then + -- random_pos = v + -- end + -- end + -- end + + -- -- check the node above and below the found air node + -- local node_above = minetest.get_node({x=random_pos.x, y=random_pos.y+1, z=random_pos.z}).name + -- local node_below = minetest.get_node({x=random_pos.x, y=random_pos.y-1, z=random_pos.z}).name + + -- if not (node_above == "air" or node_below == "air") then + -- return false + -- end + + -- if night_only ~= "disable" then + -- -- spawn only at day + -- if not night_only and node_light < min_node_light then + -- return false, true + -- end + + -- -- spawn only at night + -- if night_only then + -- if not (19359 > tod and tod > 5200) or node_light < min_node_light then + -- return random_pos + -- else + -- return false, true + -- end + -- end + -- end + -- -- random_pos, waiting + -- return random_pos, false + -- else + -- -- random_pos, waiting + -- return false, true + -- end end diff --git a/spawners_mobs/mob_bunny_evil.lua b/spawners_mobs/mob_bunny_evil.lua index 23e879e..56bbc6a 100644 --- a/spawners_mobs/mob_bunny_evil.lua +++ b/spawners_mobs/mob_bunny_evil.lua @@ -58,11 +58,11 @@ mobs:register_mob("spawners_mobs:bunny_evil", bunny_evil_def) mobs:spawn({ name = "spawners_mobs:bunny_evil", - nodes = {"default:snow", "default:snowblock", "default:dirt_with_snow", "default:ice"}, + nodes = {"default:snowblock", "default:dirt_with_snow", "default:ice"}, min_light = 0, max_light = 20, - chance = 700, - active_object_count = 6, + chance = 7000, + active_object_count = 3, day_toggle = false, }) diff --git a/spawners_mobs/mob_mummy.lua b/spawners_mobs/mob_mummy.lua index 0a158d5..5911d87 100644 --- a/spawners_mobs/mob_mummy.lua +++ b/spawners_mobs/mob_mummy.lua @@ -81,10 +81,10 @@ mobs:register_mob("spawners_mobs:mummy", mummy_def) mobs:spawn({ name = "spawners_mobs:mummy", - nodes = {"default:desert_sand", "default:desert_stone", "default:sand", "default:sandstone", "default:silver_sand", "spawners_mobs:deco_stone_eye", "spawners_mobs:deco_stone_men", "spawners_mobs:deco_stone_sun"}, + nodes = {"default:desert_sand", "default:sand"}, min_light = 0, max_light = 20, - chance = 2000, + chance = 7000, active_object_count = 2, day_toggle = false, }) diff --git a/spawners_mobs/spawners_mobs.lua b/spawners_mobs/spawners_mobs.lua index f60afcd..6e8c827 100644 --- a/spawners_mobs/spawners_mobs.lua +++ b/spawners_mobs/spawners_mobs.lua @@ -167,6 +167,11 @@ function spawners_mobs.create(mob_name, mod_prefix, size, offset, mesh, texture, groups = {cracky=1,level=2}, stack_max = 1, on_construct = function(pos) + local meta = minetest.get_meta(pos) + meta:set_string("status", "default") + meta:set_string("mod_prefix", mod_prefix) + meta:set_string("sound_custom", sound_custom) + meta:set_string("mob_name", mob_name) local random_pos, waiting = spawners_mobs.check_node_status(pos, mob_name, night_only) spawners_mobs.meta_set_str("infotext", mod_prefix.." "..mob_name.." spawner (inactive)", pos) @@ -182,6 +187,10 @@ function spawners_mobs.create(mob_name, mod_prefix, size, offset, mesh, texture, -- print("no position and not waiting") end end, + after_place_node = function(pos, placer, itemstack, pointed_thing) + local meta = minetest.get_meta(pos) + meta:set_string("owner", placer:get_player_name()) + end }) -- @@ -240,8 +249,10 @@ function spawners_mobs.create(mob_name, mod_prefix, size, offset, mesh, texture, "spawners_mobs:"..mod_prefix.."_"..mob_name.."_spawner_waiting" }, neighbors = {"air"}, - interval = 10, - chance = 10, + interval = 1, + chance = 1, + -- interval = 10, + -- chance = 10, catch_up = false, action = function(pos, node, active_object_count, active_object_count_wider)