functions.lua 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. -- Function for spawn ABM
  2. -- For the node in question find an air node nearby and spawn there
  3. -- Based on the mobs_redo spawner
  4. local max_per_block = tonumber(minetest.settings:get("max_objects_per_block") or 99)
  5. function = mobs_flat.spawn(pos, node, active_object_count, active_object_count_wider, mob, mlig, xlig, num, yof)
  6. -- return if too many entities already
  7. if active_object_count_wider >= max_per_block then
  8. return
  9. end
  10. --[[
  11. -- get settings from command
  12. local mob = comm[1]
  13. local mlig = tonumber(comm[2]) -- min light
  14. local xlig = tonumber(comm[3]) -- max light
  15. local num = tonumber(comm[4]) -- total mobs in area
  16. local yof = tonumber(comm[6]) or 0 -- Y offset to spawn mob
  17. --]]
  18. -- if amount is 0 then do nothing
  19. if num == 0 then
  20. return
  21. end
  22. -- are we spawning a registered mob?
  23. if not mobs.spawning_mobs[mob] then
  24. --print ("--- mob doesn't exist", mob)
  25. return
  26. end
  27. -- check objects inside 9x9 area around spawner
  28. local objs = minetest.get_objects_inside_radius(pos, 9)
  29. local count = 0
  30. local ent = nil
  31. -- count mob objects of same type in area
  32. for k, obj in ipairs(objs) do
  33. ent = obj:get_luaentity()
  34. if ent and ent.name and ent.name == mob then
  35. count = count + 1
  36. end
  37. end
  38. -- is there too many of same type?
  39. if count >= num then
  40. return
  41. end
  42. -- find air blocks within 5 nodes of spawner
  43. local air = minetest.find_nodes_in_area(
  44. {x = pos.x - 5, y = pos.y + yof, z = pos.z - 5},
  45. {x = pos.x + 5, y = pos.y + yof, z = pos.z + 5},
  46. {"air"})
  47. -- spawn in random air block
  48. if air and #air > 0 then
  49. local pos2 = air[math.random(#air)]
  50. local lig = minetest.get_node_light(pos2) or 0
  51. pos2.y = pos2.y + 0.5
  52. -- only if light levels are within range
  53. if lig >= mlig and lig <= xlig
  54. and minetest.registered_entities[mob] then
  55. minetest.add_entity(pos2, mob)
  56. end
  57. end
  58. end
  59. --[[ Example
  60. -- spawner abm
  61. minetest.register_abm({
  62. label = "Mob spawner node",
  63. nodenames = {"mobs:spawner"},
  64. interval = 10,
  65. chance = 4,
  66. catch_up = false,
  67. action = function(pos, node, active_object_count, active_object_count_wider)
  68. mobs_flat.spawn(pos, node, active_object_count, active_object_count_wider,
  69. mob,
  70. mlig,
  71. xlig,
  72. num,
  73. yof
  74. )
  75. end
  76. })
  77. --]]