2017-01-16 19:40:08 +03:00
|
|
|
local S = mobs.intllib
|
|
|
|
|
2017-05-25 01:59:41 +03:00
|
|
|
local default_mob = "mobs_mc:chicken"
|
2017-01-16 19:40:08 +03:00
|
|
|
|
2017-05-25 01:59:41 +03:00
|
|
|
-- mob spawner
|
|
|
|
local spawner_default = default_mob.." 10 15 3 0"
|
|
|
|
|
|
|
|
local function get_mob_textures(mob)
|
|
|
|
-- FIXME: Ummm … wtf? Why isn't there a textures attribute?
|
|
|
|
return minetest.registered_entities[mob].texture_list[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Find doll entity at pos
|
|
|
|
local function find_doll(pos)
|
|
|
|
for _,obj in ipairs(minetest.env:get_objects_inside_radius(pos, 1)) do
|
|
|
|
if not obj:is_player() then
|
|
|
|
if obj ~= nil and obj:get_luaentity().name == "mobs:spawner_mob_doll" then
|
|
|
|
return obj
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
2017-01-16 19:40:08 +03:00
|
|
|
|
2017-05-25 02:14:18 +03:00
|
|
|
local function set_doll_properties(doll, mob)
|
|
|
|
local mobinfo = minetest.registered_entities[mob]
|
|
|
|
local prop = {
|
|
|
|
_mob = mob,
|
|
|
|
mesh = mobinfo.mesh,
|
|
|
|
textures = get_mob_textures(mob),
|
|
|
|
visual_size = {
|
|
|
|
x = mobinfo.visual_size.x * 0.5,
|
|
|
|
y = mobinfo.visual_size.y * 0.5,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
doll:set_properties(prop)
|
|
|
|
end
|
|
|
|
|
2017-01-16 19:40:08 +03:00
|
|
|
minetest.register_node("mobs:spawner", {
|
|
|
|
tiles = {"mob_spawner.png"},
|
|
|
|
drawtype = "glasslike",
|
|
|
|
paramtype = "light",
|
2017-03-20 22:27:10 +03:00
|
|
|
sunlight_propagates = true,
|
2017-01-16 19:40:08 +03:00
|
|
|
walkable = true,
|
2017-01-17 01:58:09 +03:00
|
|
|
description = S("Monster Spawner"),
|
2017-03-11 21:05:29 +03:00
|
|
|
_doc_items_longdesc = S("A monster spawner is a block which regularily causes monsters and animals to appear around it."),
|
2017-03-11 07:34:58 +03:00
|
|
|
groups = {pickaxey=1, not_in_creative_inventory = 1, material_stone=1},
|
2017-03-11 18:36:05 +03:00
|
|
|
is_ground_content = false,
|
2017-02-01 23:09:24 +03:00
|
|
|
drop = "",
|
2017-01-16 19:40:08 +03:00
|
|
|
|
|
|
|
on_construct = function(pos)
|
|
|
|
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
|
|
|
|
-- text entry formspec
|
|
|
|
meta:set_string("formspec",
|
|
|
|
"field[text;" .. S("Mob MinLight MaxLight Amount PlayerDist") .. ";${command}]")
|
2017-05-25 02:29:54 +03:00
|
|
|
meta:set_string("infotext", S("Monster spawner not active (Rightclick to enter settings)"))
|
2017-01-16 19:40:08 +03:00
|
|
|
meta:set_string("command", spawner_default)
|
2017-05-25 02:29:54 +03:00
|
|
|
meta:set_int("active", 0)
|
2017-05-25 01:59:41 +03:00
|
|
|
|
|
|
|
end,
|
|
|
|
|
|
|
|
on_destruct = function(pos)
|
|
|
|
local obj = find_doll(pos)
|
|
|
|
obj:remove()
|
2017-01-16 19:40:08 +03:00
|
|
|
end,
|
|
|
|
|
|
|
|
on_right_click = function(pos, placer)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local comm = fields.text:split(" ")
|
|
|
|
local name = sender:get_player_name()
|
|
|
|
|
|
|
|
if minetest.is_protected(pos, name) then
|
|
|
|
minetest.record_protection_violation(pos, name)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local mob = comm[1] -- 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] == true
|
|
|
|
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
|
|
|
|
|
2017-05-25 02:29:54 +03:00
|
|
|
-- Activate monster spawner and disable editing functionality
|
2017-05-25 02:45:16 +03:00
|
|
|
meta:set_string("Mob", mob)
|
|
|
|
meta:set_int("MinLight", mlig)
|
|
|
|
meta:set_int("MaxLight", xlig)
|
|
|
|
meta:set_int("MaxMobsInArea", num)
|
|
|
|
meta:set_int("PlayerDistance", pla)
|
|
|
|
meta:set_int("YOffset", yof)
|
|
|
|
|
2017-05-25 02:29:54 +03:00
|
|
|
meta:set_int("active", 1)
|
|
|
|
meta:set_string("infotext", "")
|
|
|
|
meta:set_string("formspec", "")
|
2017-05-25 02:45:16 +03:00
|
|
|
meta:set_string("command", "")
|
2017-01-16 19:40:08 +03:00
|
|
|
|
2017-05-25 01:59:41 +03:00
|
|
|
-- Create or update doll
|
|
|
|
local doll = find_doll(pos)
|
|
|
|
if not doll then
|
2017-05-25 02:14:18 +03:00
|
|
|
doll = minetest.add_entity({x=pos.x, y=pos.y-0.3, z=pos.z}, "mobs:spawner_mob_doll")
|
2017-05-25 01:59:41 +03:00
|
|
|
end
|
2017-05-25 02:14:18 +03:00
|
|
|
set_doll_properties(doll, mob)
|
2017-01-16 19:40:08 +03:00
|
|
|
else
|
|
|
|
minetest.chat_send_player(name, S("Mob Spawner settings failed!"))
|
|
|
|
minetest.chat_send_player(name,
|
|
|
|
S("> name min_light[0-14] max_light[0-14] max_mobs_in_area[0 to disable] distance[1-20] y_offset[-10 to 10]"))
|
|
|
|
end
|
|
|
|
end,
|
2017-02-11 20:46:23 +03:00
|
|
|
sounds = mcl_sounds.node_sound_metal_defaults(),
|
2017-02-22 18:22:28 +03:00
|
|
|
_mcl_blast_resistance = 25,
|
2017-02-27 20:32:35 +03:00
|
|
|
_mcl_hardness = 5,
|
2017-01-16 19:40:08 +03:00
|
|
|
})
|
|
|
|
|
2017-05-25 01:59:41 +03:00
|
|
|
-- Mob spawner doll (rotating icon inside cage)
|
|
|
|
|
|
|
|
local spawner_mob_doll_def = {
|
|
|
|
hp_max = 1,
|
|
|
|
physical = true,
|
|
|
|
collisionbox = {0,0,0,0,0,0},
|
|
|
|
visual = "mesh",
|
|
|
|
makes_footstep_sound = false,
|
|
|
|
timer = 0,
|
|
|
|
automatic_rotate = math.pi * 2.9,
|
|
|
|
|
|
|
|
_mob = default_mob, -- name of the mob this doll represents
|
|
|
|
}
|
|
|
|
|
|
|
|
spawner_mob_doll_def.get_staticdata = function(self)
|
|
|
|
return self._mob
|
|
|
|
end
|
|
|
|
|
|
|
|
spawner_mob_doll_def.on_activate = function(self, staticdata, dtime_s)
|
|
|
|
local mob = staticdata
|
2017-05-25 02:14:18 +03:00
|
|
|
if mob == "" or mob == nil then
|
|
|
|
mob = default_mob
|
2017-05-25 01:59:41 +03:00
|
|
|
end
|
2017-05-25 02:14:18 +03:00
|
|
|
set_doll_properties(self.object, mob)
|
2017-05-25 01:59:41 +03:00
|
|
|
self.object:setvelocity({x=0, y=0, z=0})
|
|
|
|
self.object:setacceleration({x=0, y=0, z=0})
|
|
|
|
self.object:set_armor_groups({immortal=1})
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
spawner_mob_doll_def.on_step = function(self, dtime)
|
|
|
|
-- Check if spawner is still present. If not, delete the entity
|
|
|
|
self.timer = self.timer + 0.01
|
|
|
|
local n = minetest.get_node_or_nil(self.object:getpos())
|
|
|
|
if self.timer > 1 then
|
|
|
|
if n and n.name and n.name ~= "mobs:spawner" then
|
|
|
|
self.object:remove()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
spawner_mob_doll_def.on_punch = function(self, hitter) end
|
|
|
|
|
|
|
|
minetest.register_entity("mobs:spawner_mob_doll", spawner_mob_doll_def)
|
|
|
|
|
|
|
|
|
2017-05-25 00:12:13 +03:00
|
|
|
|
|
|
|
local max_per_block = tonumber(minetest.setting_get("max_objects_per_block") or 99)
|
|
|
|
|
2017-01-16 19:40:08 +03:00
|
|
|
-- spawner abm
|
|
|
|
minetest.register_abm({
|
2017-05-15 01:45:54 +03:00
|
|
|
label = "Monster Spawner spawning a monster",
|
2017-01-16 19:40:08 +03:00
|
|
|
nodenames = {"mobs:spawner"},
|
|
|
|
interval = 10,
|
|
|
|
chance = 4,
|
|
|
|
catch_up = false,
|
|
|
|
|
|
|
|
action = function(pos, node, active_object_count, active_object_count_wider)
|
|
|
|
|
2017-05-25 00:12:13 +03:00
|
|
|
-- return if too many entities already
|
|
|
|
if active_object_count_wider >= max_per_block then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-01-16 19:40:08 +03:00
|
|
|
-- get meta and command
|
|
|
|
local meta = minetest.get_meta(pos)
|
2017-05-25 02:29:54 +03:00
|
|
|
local active = meta:get_int("active")
|
|
|
|
if active == 0 then
|
|
|
|
-- Spawner not active yet, do nothing
|
|
|
|
return
|
|
|
|
end
|
2017-05-25 02:45:16 +03:00
|
|
|
|
|
|
|
-- get settings
|
|
|
|
local mob = meta:get_string("Mob")
|
|
|
|
local mlig = meta:get_int("MinLight")
|
|
|
|
local xlig = meta:get_int("MaxLight")
|
|
|
|
local num = meta:get_int("MaxMobsInArea")
|
|
|
|
local pla = meta:get_int("PlayerDistance")
|
|
|
|
local yof = meta:get_int("YOffset")
|
2017-01-16 19:40:08 +03:00
|
|
|
|
|
|
|
-- if amount is 0 then do nothing
|
|
|
|
if num == 0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-05-25 00:12:13 +03:00
|
|
|
-- are we spawning a registered mob?
|
|
|
|
if not mobs.spawning_mobs[mob] then
|
2017-05-25 00:40:48 +03:00
|
|
|
minetest.log("error", "[mobs] Monster Spawner: Mob doesn't exist: "..mob)
|
2017-05-25 00:12:13 +03:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-01-16 19:40:08 +03:00
|
|
|
-- check objects inside 9x9 area around spawner
|
|
|
|
local objs = minetest.get_objects_inside_radius(pos, 9)
|
|
|
|
local count = 0
|
|
|
|
local ent = nil
|
|
|
|
|
|
|
|
-- count mob objects of same type in area
|
2017-05-25 00:12:13 +03:00
|
|
|
for k, obj in ipairs(objs) do
|
2017-01-16 19:40:08 +03:00
|
|
|
|
|
|
|
ent = obj:get_luaentity()
|
|
|
|
|
2017-05-25 00:12:13 +03:00
|
|
|
if ent and ent.name and ent.name == mob then
|
2017-01-16 19:40:08 +03:00
|
|
|
count = count + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- is there too many of same type?
|
|
|
|
if count >= num then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- spawn mob if player detected and in range
|
|
|
|
if pla > 0 then
|
|
|
|
|
|
|
|
local in_range = 0
|
|
|
|
local objs = minetest.get_objects_inside_radius(pos, pla)
|
|
|
|
|
|
|
|
for _,oir in pairs(objs) do
|
|
|
|
|
|
|
|
if oir:is_player() then
|
|
|
|
|
|
|
|
in_range = 1
|
|
|
|
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- player not found
|
|
|
|
if in_range == 0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
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},
|
|
|
|
{"air"})
|
|
|
|
|
|
|
|
-- spawn in random air block
|
|
|
|
if air and #air > 0 then
|
|
|
|
|
|
|
|
local pos2 = air[math.random(#air)]
|
|
|
|
local lig = minetest.get_node_light(pos2) or 0
|
|
|
|
|
|
|
|
pos2.y = pos2.y + 0.5
|
|
|
|
|
|
|
|
-- only if light levels are within range
|
|
|
|
if lig >= mlig and lig <= xlig then
|
|
|
|
minetest.add_entity(pos2, mob)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
})
|