init.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. -- boilerplate to support localized strings if intllib mod is installed
  2. local S
  3. if minetest.global_exists('intllib') then
  4. if intllib.make_gettext_pair then
  5. -- New method using gettext.
  6. S = intllib.make_gettext_pair()
  7. else
  8. -- Old method using text files.
  9. S = intllib.Getter()
  10. end
  11. else
  12. S = function(s) return s end
  13. end
  14. -- WORMS --------------------------------------------------
  15. minetest.register_node('glow:cave_worms', {
  16. description = S('Glow Worms'),
  17. drawtype = 'nodebox',
  18. tiles = {'glow_worms.png'},
  19. inventory_image = 'glow_worms.png',
  20. groups = {dig_immediate = 2, flammable = 1},
  21. sounds = default.node_sound_stone_defaults(),
  22. drop = 'glow:cave_worms',
  23. paramtype = 'light',
  24. light_source = 4,
  25. paramtype2 = 'facedir',
  26. sunlight_propagates = true,
  27. walkable = false,
  28. node_box = {
  29. type = 'fixed',
  30. fixed = { -1/2, -1/2, -1/2, 1/2, -15/32, 1/2 },
  31. },
  32. selection_box = {
  33. type = 'fixed',
  34. fixed = { -1/2, -1/2, -1/2, 1/2, -7/16, 1/2 },
  35. },
  36. on_place = minetest.rotate_node,
  37. })
  38. local function near_surface(pos)
  39. for dx = -1, 1, 1 do
  40. for dy = -1, 1, 1 do
  41. for dz = -1, 1, 1 do
  42. local dpos = { x=pos.x+dx, y=pos.y+dy, z=pos.z+dz }
  43. local light = minetest.get_node_light(dpos, 0.5) -- 0.5 means noon
  44. if light and light > 5 then
  45. return true
  46. end
  47. end
  48. end
  49. end
  50. return false
  51. end
  52. local function place_worms(pos)
  53. local axes = {
  54. { x=pos.x, y=pos.y-1, z=pos.z },
  55. { x=pos.x, y=pos.y, z=pos.z-1 },
  56. { x=pos.x, y=pos.y, z=pos.z+1 },
  57. { x=pos.x-1, y=pos.y, z=pos.z },
  58. { x=pos.x+1, y=pos.y, z=pos.z },
  59. { x=pos.x, y=pos.y+1, z=pos.z },
  60. }
  61. for i, cpos in ipairs(axes) do
  62. if minetest.get_node(cpos).name == 'default:stone' then
  63. local facedir = (i-1) * 4 + math.random(0, 3) -- see 6d facedir info
  64. minetest.set_node(pos, { name = 'glow:cave_worms', param2 = facedir })
  65. return
  66. end
  67. end
  68. end
  69. local function make_worms(pos)
  70. local spot = minetest.find_node_near(pos, 1, 'air')
  71. if not spot or near_surface(spot) then
  72. return
  73. end
  74. local minp = vector.subtract(pos, 6)
  75. local maxp = vector.add(pos, 6)
  76. if #(minetest.find_nodes_in_area(minp, maxp, 'default:lava_source')) == 0
  77. and #(minetest.find_nodes_in_area(minp, maxp, 'glow:cave_worms')) == 0
  78. and #(minetest.find_nodes_in_area(minp, maxp, 'group:water')) > 1 then
  79. place_worms(spot)
  80. end
  81. end
  82. minetest.register_abm({
  83. label = 'Make glowworms on stone.',
  84. nodenames = { 'default:stone' },
  85. neighbors = { 'air' },
  86. interval = 120,
  87. chance = 200,
  88. action = make_worms,
  89. })
  90. minetest.register_abm({
  91. label = 'add/remove glowworms',
  92. nodenames = { 'glow:cave_worms' },
  93. interval = 60,
  94. chance = 10,
  95. action = function(pos)
  96. if math.random() < 0.7 then
  97. local minp = vector.subtract(pos, 2)
  98. local maxp = vector.add(pos, 2)
  99. local worms_count = #(minetest.find_nodes_in_area(minp, maxp, 'glow:cave_worms'))
  100. if worms_count < 20 then
  101. local spot = minetest.find_node_near(pos, 3, 'air')
  102. if spot and not near_surface(spot) then
  103. place_worms(spot)
  104. return
  105. end
  106. end
  107. end
  108. minetest.remove_node(pos)
  109. end,
  110. })
  111. -- SHROOMS -------------------------------------------------
  112. minetest.register_node('glow:shrooms', {
  113. description = S('Glow Shrooms'),
  114. drawtype = 'plantlike',
  115. tiles = {'glow_shrooms.png'},
  116. inventory_image = 'glow_shrooms.png',
  117. groups = { snappy=3, flammable=2, flower=1, flora=1, attached_node=1 },
  118. sunlight_propagates = true,
  119. walkable = false,
  120. pointable = true,
  121. diggable = true,
  122. climbable = false,
  123. buildable_to = true,
  124. paramtype = 'light',
  125. light_source = 3,
  126. sounds = default.node_sound_leaves_defaults(),
  127. selection_box = {
  128. type = 'fixed',
  129. fixed = { -0.4, -0.5, -0.4, 0.4, 0.0, 0.4 },
  130. },
  131. })
  132. local function add_shrooms(pos)
  133. if minetest.find_node_near(pos, 4, 'glow:shrooms') then
  134. return
  135. end
  136. for nx = -1, 1, 2 do
  137. for nz = -1, 1, 2 do
  138. for ny = 1, -1, -1 do
  139. if math.random() < 0.2 then
  140. local p = { x=pos.x+nx, y=pos.y-1+ny, z=pos.z+nz }
  141. if minetest.get_item_group(minetest.get_node(p).name, 'soil') ~= 0 then
  142. p.y = p.y+1
  143. if minetest.get_node(p).name == 'air' then
  144. minetest.set_node(p, { name = 'glow:shrooms' })
  145. end
  146. break
  147. end
  148. end
  149. end
  150. end
  151. end
  152. end
  153. minetest.register_abm({
  154. label = 'Add glow shrooms',
  155. nodenames = {'default:tree'},
  156. neighbors = {
  157. 'air',
  158. 'group:soil',
  159. },
  160. interval = 163,
  161. chance = 60,
  162. action = function(pos)
  163. local minp = vector.subtract(pos, 2)
  164. local maxp = vector.add(pos, 2)
  165. local shroom_count = #(minetest.find_nodes_in_area(minp, maxp, 'glow:shrooms'))
  166. if shroom_count == 0 then
  167. add_shrooms(pos)
  168. end
  169. end,
  170. })
  171. minetest.register_abm({
  172. label = 'Remove glow shrooms',
  173. nodenames = {'glow:shrooms'},
  174. neighbors = {
  175. 'air',
  176. 'group:soil',
  177. },
  178. interval = 91,
  179. chance = 100,
  180. action = function(pos)
  181. minetest.remove_node(pos)
  182. end,
  183. })