init.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. })
  57. minetest.register_node("butterflies:hidden_butterfly_"..name, {
  58. drawtype = "airlike",
  59. inventory_image = "butterflies_butterfly_"..name..".png^default_invisible_node_overlay.png",
  60. wield_image = "butterflies_butterfly_"..name..".png^default_invisible_node_overlay.png",
  61. paramtype = "light",
  62. sunlight_propagates = true,
  63. walkable = false,
  64. pointable = false,
  65. diggable = false,
  66. drop = "",
  67. groups = {not_in_creative_inventory = 1},
  68. floodable = true,
  69. on_place = function(itemstack, placer, pointed_thing)
  70. local player_name = placer:get_player_name()
  71. local pos = pointed_thing.above
  72. if not minetest.is_protected(pos, player_name) and
  73. not minetest.is_protected(pointed_thing.under, player_name) and
  74. minetest.get_node(pos).name == "air" then
  75. minetest.set_node(pos, {name = "butterflies:hidden_butterfly_"..name})
  76. minetest.get_node_timer(pos):start(1)
  77. itemstack:take_item()
  78. end
  79. return itemstack
  80. end,
  81. on_timer = function(pos, elapsed)
  82. if minetest.get_node_light(pos) >= 11 then
  83. minetest.set_node(pos, {name = "butterflies:butterfly_"..name})
  84. end
  85. minetest.get_node_timer(pos):start(30)
  86. end
  87. })
  88. end
  89. -- register decoration
  90. minetest.register_decoration({
  91. name = "butterflies:butterfly",
  92. deco_type = "simple",
  93. place_on = {"default:dirt_with_grass"},
  94. place_offset_y = 2,
  95. sidelen = 80,
  96. fill_ratio = 0.005,
  97. biomes = {"grassland", "deciduous_forest"},
  98. y_max = 31000,
  99. y_min = 1,
  100. decoration = {
  101. "butterflies:butterfly_white",
  102. "butterflies:butterfly_red",
  103. "butterflies:butterfly_violet"
  104. },
  105. spawn_by = "group:flower",
  106. num_spawn_by = 1
  107. })
  108. -- get decoration ID
  109. local butterflies = minetest.get_decoration_id("butterflies:butterfly")
  110. minetest.set_gen_notify({decoration = true}, {butterflies})
  111. -- start nodetimers
  112. minetest.register_on_generated(function(minp, maxp, blockseed)
  113. local gennotify = minetest.get_mapgen_object("gennotify")
  114. local poslist = {}
  115. for _, pos in ipairs(gennotify["decoration#"..butterflies] or {}) do
  116. local deco_pos = {x = pos.x, y = pos.y + 3, z = pos.z}
  117. table.insert(poslist, deco_pos)
  118. end
  119. if #poslist ~= 0 then
  120. for i = 1, #poslist do
  121. local pos = poslist[i]
  122. minetest.get_node_timer(pos):start(1)
  123. end
  124. end
  125. end)