Added command based mob spawner node, tidied code

This commit is contained in:
TenPlus1 2016-01-04 12:37:55 +00:00
parent feadb888b2
commit 0c34bc26b4
5 changed files with 135 additions and 6 deletions

View File

@ -28,6 +28,7 @@ This mod contains the following additions:
Changelog: Changelog:
1.23- Added mob spawner block for admin to setup spawners in-game (place and right click to enter settings)
1.22- Added ability to name tamed animals and npc using nametags, also npc will attack anyone who punches them apart from owner 1.22- Added ability to name tamed animals and npc using nametags, also npc will attack anyone who punches them apart from owner
1.21- Added some more error checking to reduce serialize.h error and added height checks for falling off cliffs (thanks cmdskp) 1.21- Added some more error checking to reduce serialize.h error and added height checks for falling off cliffs (thanks cmdskp)
1.20- Error checking added to remove bad mobs, out of map limit mobs and stop serialize.h error 1.20- Error checking added to remove bad mobs, out of map limit mobs and stop serialize.h error

12
api.lua
View File

@ -1,4 +1,4 @@
-- Mobs Api (31st December 2015) -- Mobs Api (4th January 2016)
mobs = {} mobs = {}
mobs.mod = "redo" mobs.mod = "redo"
@ -1833,13 +1833,13 @@ function mobs:spawn_specific(name, nodes, neighbors, min_light, max_light,
local mob = minetest.add_entity(pos, name) local mob = minetest.add_entity(pos, name)
--local ent = mob:get_luaentity() --local ent = mob:get_luaentity()
if mob == false then if mob and mob:get_luaentity() then
print ("[mobs]" .. name .. " failed to spawn at "
.. minetest.pos_to_string(pos))
else
-- print ("[mobs] Spawned " .. name .. " at " -- print ("[mobs] Spawned " .. name .. " at "
-- .. minetest.pos_to_string(pos) .. " on " -- .. minetest.pos_to_string(pos) .. " on "
-- .. node.name .. " near " .. neighbors[1]) -- .. node.name .. " near " .. neighbors[1])
else
print ("[mobs]" .. name .. " failed to spawn at "
.. minetest.pos_to_string(pos))
end end
end end
@ -2265,7 +2265,7 @@ function mobs:feed_tame(self, clicker, feed_count, breed, tame)
local name = clicker:get_player_name() local name = clicker:get_player_name()
-- store mob and nametag stack in external variable -- store mob and nametag stack in external variables
mob_obj[name] = self mob_obj[name] = self
mob_sta[name] = item mob_sta[name] = item

View File

@ -33,4 +33,7 @@ dofile(path.."/npc.lua") -- TenPlus1
-- Mob Items -- Mob Items
dofile(path.."/crafts.lua") dofile(path.."/crafts.lua")
-- Spawner
dofile(path.."/spawner.lua")
print ("[MOD] Mobs Redo loaded") print ("[MOD] Mobs Redo loaded")

125
spawner.lua Normal file
View File

@ -0,0 +1,125 @@
-- mob spawner
local spawner_default = "mobs:pumba 10 15 0"
minetest.register_node("mobs:spawner", {
tiles = {"mob_spawner.png"},
drawtype = "glasslike",
paramtype = "light",
walkable = true,
description = "Mob Spawner",
groups = {cracky = 1},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
-- text entry formspec
meta:set_string("formspec", "field[text;mob_name min_light max_light amount;${command}]")
meta:set_string("infotext", "Spawner Not Active (enter settings)")
meta:set_string("command", spawner_default)
end,
on_right_click = function(pos, placer)
local meta = minetest.get_meta(pos)
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]
local mlig = tonumber(comm[2])
local xlig = tonumber(comm[3])
local num = tonumber(comm[4])
if mob and 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 then
meta:set_string("command", fields.text)
meta:set_string("infotext", "Spawner Active (" .. mob .. ")")
else
minetest.chat_send_player(name, "Mob Spawner settings failed!")
end
end,
})
-- spawner abm
minetest.register_abm({
nodenames = {"mobs:spawner"},
interval = 10,
chance = 4,
catch_up = false,
action = function(pos, node, active_object_count, active_object_count_wider)
-- check objects inside 9x9 area around spawner
local objs = minetest.get_objects_inside_radius(pos, 9)
-- get meta and command
local meta = minetest.get_meta(pos)
local comm = meta:get_string("command"):split(" ")
-- get settings from command
local mob = comm[1]
local mlig = tonumber(comm[2])
local xlig = tonumber(comm[3])
local num = tonumber(comm[4])
-- if amount is 0 then do nothing
if num == 0 then
return
end
local count = 0
local ent = nil
-- count objects of same type in area
for k, obj in pairs(objs) do
ent = obj:get_luaentity()
if ent and ent.name == mob then
count = count + 1
end
end
-- is there too many of same type?
if count >= num then
return
end
-- find air blocks within 5 nodes of spawner
local air = minetest.find_nodes_in_area(
{x = pos.x - 5, y = pos.y, z = pos.z - 5},
{x = pos.x + 5, y = pos.y, 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)
-- only if light levels are within range
if lig and lig >= mlig and lig <= xlig then
minetest.add_entity(pos2, mob)
end
end
end
})

BIN
textures/mob_spawner.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 108 B