spawner.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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[10,3.5]"
  19. .. "label[0.15,0.5;" .. minetest.formspec_escape(head) .. "]"
  20. .. "field[1,2.5;8.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. -- return if too many entities already
  72. if active_object_count_wider >= max_per_block then
  73. return
  74. end
  75. -- get meta and command
  76. local meta = minetest.get_meta(pos)
  77. local comm = meta:get_string("command"):split(" ")
  78. -- get settings from command
  79. local mob = comm[1]
  80. local mlig = tonumber(comm[2])
  81. local xlig = tonumber(comm[3])
  82. local num = tonumber(comm[4])
  83. local pla = tonumber(comm[5]) or 0
  84. local yof = tonumber(comm[6]) or 0
  85. -- if amount is 0 then do nothing
  86. if num == 0 then
  87. return
  88. end
  89. -- are we spawning a registered mob?
  90. if not mobs.spawning_mobs[mob] then
  91. --print ("--- mob doesn't exist", mob)
  92. return
  93. end
  94. -- check objects inside 9x9 area around spawner
  95. local objs = minetest.get_objects_inside_radius(pos, 9)
  96. local count = 0
  97. local ent
  98. -- count mob objects of same type in area
  99. for _, obj in ipairs(objs) do
  100. ent = obj:get_luaentity()
  101. if ent and ent.name and ent.name == mob then
  102. count = count + 1
  103. end
  104. end
  105. -- is there too many of same type?
  106. if count >= num then
  107. return
  108. end
  109. -- spawn mob if player detected and in range
  110. if pla > 0 then
  111. local in_range = 0
  112. local objsp = minetest.get_objects_inside_radius(pos, pla)
  113. for _, oir in pairs(objsp) do
  114. if oir:is_player() then
  115. in_range = 1
  116. break
  117. end
  118. end
  119. -- player not found
  120. if in_range == 0 then
  121. return
  122. end
  123. end
  124. -- set medium mob usually spawns in (defaults to air)
  125. local reg = minetest.registered_entities[mob].fly_in
  126. if not reg or type(reg) == "string" then
  127. reg = {(reg or "air")}
  128. end
  129. -- find air blocks within 5 nodes of spawner
  130. local air = minetest.find_nodes_in_area(
  131. {x = pos.x - 5, y = pos.y + yof, z = pos.z - 5},
  132. {x = pos.x + 5, y = pos.y + yof, z = pos.z + 5}, reg)
  133. -- spawn in random air block
  134. if air and #air > 0 then
  135. local pos2 = air[math.random(#air)]
  136. local lig = minetest.get_node_light(pos2) or 0
  137. pos2.y = pos2.y + 0.5
  138. -- only if light levels are within range
  139. if lig >= mlig and lig <= xlig
  140. and minetest.registered_entities[mob] then
  141. minetest.add_entity(pos2, mob)
  142. end
  143. end
  144. end
  145. })