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