spawner.lua 4.7 KB

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