mirror of
https://codeberg.org/tenplus1/mobs_redo.git
synced 2025-06-28 14:36:03 +02:00
tidy and tweak code, implement sound check for mineclon*
This commit is contained in:
67
spawner.lua
67
spawner.lua
@ -1,8 +1,9 @@
|
||||
|
||||
local S = mobs.translate
|
||||
local S = minetest.get_translator("mobs")
|
||||
local max_per_block = tonumber(minetest.settings:get("max_objects_per_block") or 99)
|
||||
|
||||
-- helper functions
|
||||
|
||||
-- are we a real player ?
|
||||
local function is_player(player)
|
||||
|
||||
if player and type(player) == "userdata" and minetest.is_player(player) then
|
||||
@ -21,8 +22,8 @@ local get_distance = function(a, b)
|
||||
return square(x * x + y * y + z * z)
|
||||
end
|
||||
|
||||
|
||||
-- mob spawner
|
||||
|
||||
local spawner_default = "mobs_animal:pumba 10 15 0 0 0"
|
||||
|
||||
minetest.register_node("mobs:spawner", {
|
||||
@ -35,6 +36,7 @@ minetest.register_node("mobs:spawner", {
|
||||
is_ground_content = false,
|
||||
_mcl_hardness = 1,
|
||||
_mcl_blast_resistance = 5,
|
||||
sounds = mobs.node_sound_stone_defaults(),
|
||||
|
||||
on_construct = function(pos)
|
||||
|
||||
@ -56,16 +58,12 @@ minetest.register_node("mobs:spawner", {
|
||||
|
||||
on_right_click = function(pos, placer)
|
||||
|
||||
if minetest.is_protected(pos, placer:get_player_name()) then
|
||||
return
|
||||
end
|
||||
if minetest.is_protected(pos, placer:get_player_name()) then return end
|
||||
end,
|
||||
|
||||
on_receive_fields = function(pos, formname, fields, sender)
|
||||
|
||||
if not fields.text or fields.text == "" then
|
||||
return
|
||||
end
|
||||
if not fields.text or fields.text == "" then return end
|
||||
|
||||
local meta = minetest.get_meta(pos)
|
||||
local comm = fields.text:split(" ")
|
||||
@ -76,35 +74,31 @@ minetest.register_node("mobs:spawner", {
|
||||
return
|
||||
end
|
||||
|
||||
local mob = comm[1] -- mob to spawn
|
||||
local mob = comm[1] or "" -- mob to spawn
|
||||
local mlig = tonumber(comm[2]) -- min light
|
||||
local xlig = tonumber(comm[3]) -- max light
|
||||
local num = tonumber(comm[4]) -- total mobs in area
|
||||
local pla = tonumber(comm[5]) -- player distance (0 to disable)
|
||||
local yof = tonumber(comm[6]) or 0 -- Y offset to spawn mob
|
||||
|
||||
if mob and mob ~= "" and mobs.spawning_mobs[mob]
|
||||
and num and num >= 0 and num <= 10
|
||||
and mlig and mlig >= 0 and mlig <= 15
|
||||
and xlig and xlig >= 0 and xlig <= 15
|
||||
and pla and pla >= 0 and pla <= 20
|
||||
and yof and yof > -10 and yof < 10 then
|
||||
if mob ~= "" and mobs.spawning_mobs[mob] and num and num >= 0 and num <= 10
|
||||
and mlig and mlig >= 0 and mlig <= 15 and xlig and xlig >= 0 and xlig <= 15
|
||||
and pla and pla >= 0 and pla <= 20 and yof and yof > -10 and yof < 10 then
|
||||
|
||||
meta:set_string("command", fields.text)
|
||||
meta:set_string("infotext", S("Spawner Active (@1)", mob))
|
||||
|
||||
else
|
||||
minetest.chat_send_player(name, S("Mob Spawner settings failed!"))
|
||||
minetest.chat_send_player(name,
|
||||
S("Syntax: “name min_light[0-14] max_light[0-14] max_mobs_in_area[0 to disable] player_distance[1-20] y_offset[-10 to 10]”"))
|
||||
S("Syntax: “name min_light[0-14] max_light[0-14] "
|
||||
.. "max_mobs_in_area[0 to disable] player_distance[1-20] "
|
||||
.. "y_offset[-10 to 10]”"))
|
||||
end
|
||||
end
|
||||
})
|
||||
|
||||
|
||||
local max_per_block = tonumber(minetest.settings:get("max_objects_per_block") or 99)
|
||||
|
||||
-- spawner abm
|
||||
|
||||
minetest.register_abm({
|
||||
label = "Mob spawner node",
|
||||
nodenames = {"mobs:spawner"},
|
||||
@ -115,9 +109,7 @@ minetest.register_abm({
|
||||
action = function(pos, node, active_object_count, active_object_count_wider)
|
||||
|
||||
-- return if too many entities already
|
||||
if active_object_count_wider >= max_per_block then
|
||||
return
|
||||
end
|
||||
if active_object_count_wider >= max_per_block then return end
|
||||
|
||||
-- get meta and command
|
||||
local meta = minetest.get_meta(pos)
|
||||
@ -132,9 +124,7 @@ minetest.register_abm({
|
||||
local yof = tonumber(comm[6]) or 0
|
||||
|
||||
-- if amount is 0 then do nothing
|
||||
if num == 0 then
|
||||
return
|
||||
end
|
||||
if num == 0 then return end
|
||||
|
||||
-- are we spawning a registered mob?
|
||||
if not mobs.spawning_mobs[mob] then
|
||||
@ -152,15 +142,11 @@ minetest.register_abm({
|
||||
|
||||
ent = obj:get_luaentity()
|
||||
|
||||
if ent and ent.name and ent.name == mob then
|
||||
count = count + 1
|
||||
end
|
||||
if ent and ent.name and ent.name == mob then count = count + 1 end
|
||||
end
|
||||
|
||||
-- is there too many of same type?
|
||||
if count >= num then
|
||||
return
|
||||
end
|
||||
if count >= num then return end
|
||||
|
||||
-- when player distance above 0, spawn mob if player detected and in range
|
||||
if pla > 0 then
|
||||
@ -181,22 +167,18 @@ minetest.register_abm({
|
||||
end
|
||||
|
||||
-- player not found
|
||||
if not in_range then
|
||||
return
|
||||
end
|
||||
if not in_range then return end
|
||||
end
|
||||
|
||||
-- set medium mob usually spawns in (defaults to air)
|
||||
local reg = minetest.registered_entities[mob].fly_in
|
||||
|
||||
if not reg or type(reg) == "string" then
|
||||
reg = {(reg or "air")}
|
||||
end
|
||||
if not reg or type(reg) == "string" then reg = {(reg or "air")} end
|
||||
|
||||
-- find air blocks within 5 nodes of spawner
|
||||
local air = minetest.find_nodes_in_area(
|
||||
{x = pos.x - 5, y = pos.y + yof, z = pos.z - 5},
|
||||
{x = pos.x + 5, y = pos.y + yof, z = pos.z + 5}, reg)
|
||||
{x = pos.x - 5, y = pos.y + yof, z = pos.z - 5},
|
||||
{x = pos.x + 5, y = pos.y + yof, z = pos.z + 5}, reg)
|
||||
|
||||
-- spawn in random air block
|
||||
if air and #air > 0 then
|
||||
@ -207,8 +189,7 @@ minetest.register_abm({
|
||||
pos2.y = pos2.y + 0.5
|
||||
|
||||
-- only if light levels are within range
|
||||
if lig >= mlig and lig <= xlig
|
||||
and minetest.registered_entities[mob] then
|
||||
if lig >= mlig and lig <= xlig and minetest.registered_entities[mob] then
|
||||
minetest.add_entity(pos2, mob)
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user