spawner.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. local S = mobs.intllib
  2. -- mob spawner
  3. local spawner_default = "mobs_animal:pumba 10 15 0 0"
  4. minetest.register_node("mobs:spawner", {
  5. tiles = {"mob_spawner.png"},
  6. drawtype = "glasslike",
  7. paramtype = "light",
  8. walkable = true,
  9. description = S("Mob Spawner"),
  10. groups = {cracky = 1},
  11. on_construct = function(pos)
  12. local meta = minetest.get_meta(pos)
  13. -- text entry formspec
  14. meta:set_string("formspec",
  15. "field[text;" .. S("Mob MinLight MaxLight Amount PlayerDist")
  16. .. ";${command}]")
  17. meta:set_string("infotext", S("Spawner Not Active (enter settings)"))
  18. meta:set_string("command", spawner_default)
  19. end,
  20. on_right_click = function(pos, placer)
  21. if minetest.is_protected(pos, placer:get_player_name()) then
  22. return
  23. end
  24. end,
  25. on_receive_fields = function(pos, formname, fields, sender)
  26. if not fields.text or fields.text == "" then
  27. return
  28. end
  29. local meta = minetest.get_meta(pos)
  30. local comm = fields.text:split(" ")
  31. local name = sender:get_player_name()
  32. if minetest.is_protected(pos, name) then
  33. minetest.record_protection_violation(pos, name)
  34. return
  35. end
  36. local mob = comm[1] -- mob to spawn
  37. local mlig = tonumber(comm[2]) -- min light
  38. local xlig = tonumber(comm[3]) -- max light
  39. local num = tonumber(comm[4]) -- total mobs in area
  40. local pla = tonumber(comm[5]) -- player distance (0 to disable)
  41. local yof = tonumber(comm[6]) or 0 -- Y offset to spawn mob
  42. if mob and mob ~= "" and mobs.spawning_mobs[mob] == true
  43. and num and num >= 0 and num <= 10
  44. and mlig and mlig >= 0 and mlig <= 15
  45. and xlig and xlig >= 0 and xlig <= 15
  46. and pla and pla >=0 and pla <= 20
  47. and yof and yof > -10 and yof < 10 then
  48. meta:set_string("command", fields.text)
  49. meta:set_string("infotext", S("Spawner Active (@1)", mob))
  50. else
  51. minetest.chat_send_player(name, S("Mob Spawner settings failed!"))
  52. minetest.chat_send_player(name,
  53. S("Syntax: “name min_light[0-14] max_light[0-14] max_mobs_in_area[0 to disable] distance[1-20] y_offset[-10 to 10]”"))
  54. end
  55. end
  56. })
  57. local max_per_block = tonumber(minetest.settings:get("max_objects_per_block") or 99)
  58. -- spawner abm
  59. minetest.register_abm({
  60. label = "Mob spawner node",
  61. nodenames = {"mobs:spawner"},
  62. interval = 10,
  63. chance = 4,
  64. catch_up = false,
  65. action = function(pos, node, active_object_count, active_object_count_wider)
  66. -- return if too many entities already
  67. if active_object_count_wider >= max_per_block then
  68. return
  69. end
  70. -- get meta and command
  71. local meta = minetest.get_meta(pos)
  72. local comm = meta:get_string("command"):split(" ")
  73. -- get settings from command
  74. local mob = comm[1]
  75. local mlig = tonumber(comm[2])
  76. local xlig = tonumber(comm[3])
  77. local num = tonumber(comm[4])
  78. local pla = tonumber(comm[5]) or 0
  79. local yof = tonumber(comm[6]) or 0
  80. -- if amount is 0 then do nothing
  81. if num == 0 then
  82. return
  83. end
  84. -- are we spawning a registered mob?
  85. if not mobs.spawning_mobs[mob] then
  86. --print ("--- mob doesn't exist", mob)
  87. return
  88. end
  89. -- check objects inside 9x9 area around spawner
  90. local objs = minetest.get_objects_inside_radius(pos, 9)
  91. local count = 0
  92. local ent
  93. -- count mob objects of same type in area
  94. for _, obj in ipairs(objs) do
  95. ent = obj:get_luaentity()
  96. if ent and ent.name and ent.name == mob then
  97. count = count + 1
  98. end
  99. end
  100. -- is there too many of same type?
  101. if count >= num then
  102. return
  103. end
  104. -- spawn mob if player detected and in range
  105. if pla > 0 then
  106. local in_range = 0
  107. local objsp = minetest.get_objects_inside_radius(pos, pla)
  108. for _, oir in pairs(objsp) do
  109. if oir:is_player() then
  110. in_range = 1
  111. break
  112. end
  113. end
  114. -- player not found
  115. if in_range == 0 then
  116. return
  117. end
  118. end
  119. -- find air blocks within 5 nodes of spawner
  120. local air = minetest.find_nodes_in_area(
  121. {x = pos.x - 5, y = pos.y + yof, z = pos.z - 5},
  122. {x = pos.x + 5, y = pos.y + yof, z = pos.z + 5}, {"air"})
  123. -- spawn in random air block
  124. if air and #air > 0 then
  125. local pos2 = air[math.random(#air)]
  126. local lig = minetest.get_node_light(pos2) or 0
  127. pos2.y = pos2.y + 0.5
  128. -- only if light levels are within range
  129. if lig >= mlig and lig <= xlig
  130. and minetest.registered_entities[mob] then
  131. minetest.add_entity(pos2, mob)
  132. end
  133. end
  134. end
  135. })