init.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. -- butterflies/init.lua
  2. -- Load support for MT game translation.
  3. local S = minetest.get_translator("butterflies")
  4. -- register butterflies
  5. local butter_list = {
  6. {"white", S("White Butterfly")},
  7. {"red", S("Red Butterfly")},
  8. {"violet", S("Violet Butterfly")}
  9. }
  10. for i in ipairs (butter_list) do
  11. local name = butter_list[i][1]
  12. local desc = butter_list[i][2]
  13. minetest.register_node("butterflies:butterfly_"..name, {
  14. description = desc,
  15. drawtype = "plantlike",
  16. tiles = {{
  17. name = "butterflies_butterfly_"..name.."_animated.png",
  18. animation = {
  19. type = "vertical_frames",
  20. aspect_w = 16,
  21. aspect_h = 16,
  22. length = 3
  23. },
  24. }},
  25. inventory_image = "butterflies_butterfly_"..name..".png",
  26. wield_image = "butterflies_butterfly_"..name..".png",
  27. waving = 1,
  28. paramtype = "light",
  29. sunlight_propagates = true,
  30. buildable_to = true,
  31. walkable = false,
  32. groups = {catchable = 1},
  33. selection_box = {
  34. type = "fixed",
  35. fixed = {-0.1, -0.1, -0.1, 0.1, 0.1, 0.1},
  36. },
  37. floodable = true,
  38. on_place = function(itemstack, placer, pointed_thing)
  39. local player_name = placer:get_player_name()
  40. local pos = pointed_thing.above
  41. if not minetest.is_protected(pos, player_name) and
  42. not minetest.is_protected(pointed_thing.under, player_name) and
  43. minetest.get_node(pos).name == "air" then
  44. minetest.set_node(pos, {name = "butterflies:butterfly_"..name})
  45. minetest.get_node_timer(pos):start(1)
  46. itemstack:take_item()
  47. end
  48. return itemstack
  49. end,
  50. on_timer = function(pos, elapsed)
  51. if minetest.get_node_light(pos) < 11 then
  52. minetest.set_node(pos, {name = "butterflies:hidden_butterfly_"..name})
  53. end
  54. minetest.get_node_timer(pos):start(30)
  55. end,
  56. on_punch = function(pos, node, puncher, pointed_thing)
  57. minetest.remove_node(pos)
  58. end
  59. })
  60. minetest.register_node("butterflies:hidden_butterfly_"..name, {
  61. drawtype = "airlike",
  62. inventory_image = "butterflies_butterfly_"..name..".png",
  63. wield_image = "butterflies_butterfly_"..name..".png",
  64. paramtype = "light",
  65. sunlight_propagates = true,
  66. walkable = false,
  67. pointable = false,
  68. diggable = false,
  69. drop = "",
  70. groups = {not_in_creative_inventory = 1},
  71. floodable = true,
  72. on_place = function(itemstack, placer, pointed_thing)
  73. local player_name = placer:get_player_name()
  74. local pos = pointed_thing.above
  75. if not minetest.is_protected(pos, player_name) and
  76. not minetest.is_protected(pointed_thing.under, player_name) and
  77. minetest.get_node(pos).name == "air" then
  78. minetest.set_node(pos, {name = "butterflies:hidden_butterfly_"..name})
  79. minetest.get_node_timer(pos):start(1)
  80. itemstack:take_item()
  81. end
  82. return itemstack
  83. end,
  84. on_timer = function(pos, elapsed)
  85. if minetest.get_node_light(pos) >= 11 then
  86. minetest.set_node(pos, {name = "butterflies:butterfly_"..name})
  87. end
  88. minetest.get_node_timer(pos):start(30)
  89. end,
  90. on_punch = function(pos, node, puncher, pointed_thing)
  91. minetest.remove_node(pos)
  92. end
  93. })
  94. end
  95. -- register decoration
  96. minetest.register_decoration({
  97. name = "butterflies:butterfly",
  98. deco_type = "simple",
  99. place_on = {"default:dirt_with_grass"},
  100. place_offset_y = 2,
  101. sidelen = 80,
  102. fill_ratio = 0.005,
  103. biomes = {"grassland", "deciduous_forest"},
  104. y_max = 31000,
  105. y_min = 1,
  106. decoration = {
  107. "butterflies:butterfly_white",
  108. "butterflies:butterfly_red",
  109. "butterflies:butterfly_violet"
  110. },
  111. spawn_by = "group:flower",
  112. num_spawn_by = 1
  113. })
  114. -- get decoration ID
  115. local butterflies = minetest.get_decoration_id("butterflies:butterfly")
  116. minetest.set_gen_notify({decoration = true}, {butterflies})
  117. -- start nodetimers
  118. minetest.register_on_generated(function(minp, maxp, blockseed)
  119. local gennotify = minetest.get_mapgen_object("gennotify")
  120. local poslist = {}
  121. for _, pos in ipairs(gennotify["decoration#"..butterflies] or {}) do
  122. local deco_pos = {x = pos.x, y = pos.y + 3, z = pos.z}
  123. table.insert(poslist, deco_pos)
  124. end
  125. if #poslist ~= 0 then
  126. for i = 1, #poslist do
  127. local pos = poslist[i]
  128. minetest.get_node_timer(pos):start(1)
  129. end
  130. end
  131. end)