functions.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. local random = math.random
  2. local set_node = minetest.set_node
  3. local get_node = minetest.get_node
  4. local crystal_directions = {
  5. {x=1, y=0, z=0},
  6. {x=-1, y=0, z=0},
  7. {x=0, y=0, z=1},
  8. {x=0, y=0, z=-1},
  9. -- Duplicate entries increase the chance that this direction is taken.
  10. {x=0, y=-1, z=0},
  11. {x=0, y=-1, z=0},
  12. --{x=0, y=-1, z=0},
  13. }
  14. local generate_crystal -- Forward declaration needed for recursion.
  15. generate_crystal = function(pos, rec_, tot_)
  16. -- Enabling this causes crystals to fail to generate on the netherealm roof.
  17. --if get_node(pos).name ~= "air" then return end
  18. set_node(pos, {name="glowstone:minerals"})
  19. local rec = rec_ or 0
  20. local tot = tot_ or random(3, 10)
  21. if rec >= tot then return end
  22. local d1 = crystal_directions[random(1, #crystal_directions)]
  23. local p1 = {x=pos.x+d1.x, y=pos.y+d1.y, z=pos.z+d1.z}
  24. generate_crystal(p1, rec+1, tot)
  25. if random(1, 4) == 1 then
  26. local d2 = crystal_directions[random(1, #crystal_directions)]
  27. local p2 = {x=pos.x+d2.x, y=pos.y+d2.y, z=pos.z+d2.z}
  28. generate_crystal(p2, rec+2, tot)
  29. end
  30. end
  31. nethermapgen.place_crystal = function(pos)
  32. generate_crystal(pos)
  33. end
  34. local find_floor_up = function(pos, limit)
  35. for i = 0, limit, 1 do
  36. local p1 = {x=pos.x, y=pos.y+i, z=pos.z} -- Node
  37. local p2 = {x=p1.x, y=p1.y+1, z=p1.z} -- Node above
  38. local n1 = get_node(p1).name
  39. local n2 = get_node(p2).name
  40. if n2 == "air" and (n1 == "rackstone:redrack" or
  41. n1 == "rackstone:mg_redrack" or
  42. n1 == "rackstone:rackstone" or
  43. n1 == "rackstone:mg_rackstone") then
  44. return p2
  45. end
  46. end
  47. end
  48. local find_floor_down = function(pos, limit)
  49. for i = 0, limit, 1 do
  50. local p1 = {x=pos.x, y=pos.y-i, z=pos.z} -- Node above
  51. local p2 = {x=p1.x, y=p1.y-1, z=p1.z} -- Node
  52. local n1 = get_node(p1).name
  53. local n2 = get_node(p2).name
  54. if n1 == "air" and (n2 == "rackstone:redrack" or
  55. n2 == "rackstone:mg_redrack" or
  56. n2 == "rackstone:rackstone" or
  57. n2 == "rackstone:mg_rackstone") then
  58. return p1
  59. end
  60. end
  61. end
  62. local fire_directions = {
  63. {x=1, y=0, z=0},
  64. {x=-1, y=0, z=0},
  65. {x=0, y=0, z=1},
  66. {x=0, y=0, z=-1},
  67. }
  68. local scatter_nether_fire
  69. scatter_nether_fire = function(pos, rec_, tot_)
  70. local p1 = find_floor_up(pos, 2)
  71. if p1 == nil then p1 = find_floor_down(pos, 2) end
  72. if p1 == nil then return end -- No floor found.
  73. pos = p1
  74. set_node(pos, {name="fire:nether_flame"})
  75. local rec = rec_ or 0
  76. local tot = tot_ or random(5, 20)
  77. if rec >= tot then return end
  78. local d1 = fire_directions[random(1, #fire_directions)]
  79. local p1 = {x=pos.x+d1.x, y=pos.y+d1.y, z=pos.z+d1.z}
  80. scatter_nether_fire(p1, rec+1, tot)
  81. if random(1, 4) == 1 then
  82. local d2 = fire_directions[random(1, #fire_directions)]
  83. local p2 = {x=pos.x+d2.x, y=pos.y+d2.y, z=pos.z+d2.z}
  84. scatter_nether_fire(p2, rec+1, tot)
  85. end
  86. end
  87. nethermapgen.scatter_flames = function(pos)
  88. scatter_nether_fire(pos)
  89. end