spawner.lua 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. -- Localize for performance.
  2. local vector_add = vector.add
  3. local set_node = minetest.set_node
  4. local get_node = minetest.get_node
  5. local remove_node = minetest.remove_node
  6. local math_random = math.random
  7. local spawner_node = {name="pm:spawner"}
  8. local quartz_node = {name="pm:quartz_ore"}
  9. function pm.on_nodespawner_construct(pos)
  10. local timer = minetest.get_node_timer(pos)
  11. timer:start(math_random(100, 500)/100)
  12. end
  13. function pm.on_nodespawner_destruct(pos)
  14. local pos = minetest.find_node_near(pos, 1, "air")
  15. if pos then
  16. -- Wisp avenges nest with its life!
  17. pm.spawn_wisp(pos, "boom")
  18. end
  19. end
  20. function pm.on_nodespawner_timer(pos, elapsed)
  21. pos.y = pos.y - 1
  22. local n = get_node(pos)
  23. pos.y = pos.y + 1
  24. if n.name == "default:gravel" then
  25. -- Storing a metadata cookie ensures we only build the nest once, even if
  26. -- this timer function is called again.
  27. local meta = minetest.get_meta(pos)
  28. if meta:get_int("nest_built") ~= 1 then
  29. pm.spawn_wisp(pos, "nest_guard")
  30. pm.spawn_wisp(pos, "nest_worker")
  31. meta:set_int("nest_built", 1)
  32. end
  33. -- Keep calling this timer function.
  34. -- This allows us to add extra behavior later.
  35. local timer = minetest.get_node_timer(pos)
  36. timer:start(10.0)
  37. else
  38. remove_node(pos)
  39. end
  40. end
  41. -- Called by the Jarkati mapgen when a candidate vent is placed.
  42. function pm.on_wisp_vent_place(pos)
  43. -- Wisp nests are very rare!
  44. if math_random(1, 100) == 1 then
  45. local minp = vector_add(pos, -3)
  46. local maxp = vector_add(pos, 3)
  47. local positions = minetest.find_nodes_in_area_under_air(minp, maxp, "default:gravel")
  48. local n = #positions
  49. for i=1, n, 1 do
  50. local p = positions[i]
  51. p.y = p.y + 1
  52. set_node(p, spawner_node)
  53. p.y = p.y - 2
  54. set_node(p, quartz_node)
  55. end
  56. end
  57. end