nodes.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. -- support for i18n
  2. local S = minetest.get_translator("bushes_classic")
  3. plantlife_bushes = {}
  4. -- TODO: add support for nodebreakers? those dig like mese picks
  5. plantlife_bushes.after_dig_node = function(pos, oldnode, oldmetadata, digger)
  6. if not (digger and pos and oldnode) then
  7. return
  8. end
  9. -- find out which bush type we are dealing with
  10. local bush_name = ""
  11. local can_harvest = false
  12. if oldnode.name == "bushes:fruitless_bush" then
  13. -- this bush has not grown fruits yet (but will eventually)
  14. bush_name = oldmetadata.fields.bush_type
  15. -- no fruits to be found, so can_harvest stays false
  16. else
  17. local name_parts = oldnode.name:split(":")
  18. if #name_parts >= 2 and name_parts[2] ~= nil then
  19. name_parts = name_parts[2]:split("_")
  20. if #name_parts >= 2 and name_parts[1] ~= nil then
  21. bush_name = name_parts[1]
  22. -- this bush really carries fruits
  23. can_harvest = true
  24. end
  25. end
  26. end
  27. -- find out which tool the digger was wielding (if any)
  28. local toolstack = digger:get_wielded_item()
  29. local capabilities = toolstack:get_tool_capabilities()
  30. -- what the player will get
  31. local harvested
  32. -- failure to find out what the tool can do: destroy the bush and return nothing
  33. local groupcaps = capabilities.groupcaps
  34. if not groupcaps then
  35. return
  36. -- digging with the hand or something like that
  37. elseif groupcaps.snappy then
  38. -- plant a new bush without fruits
  39. minetest.swap_node(pos, {type = "node", name = "bushes:fruitless_bush"})
  40. local meta = minetest.get_meta(pos)
  41. meta:set_string('bush_type', bush_name)
  42. -- construct the stack of fruits the player will get
  43. -- only bushes that have grown fruits can actually give fruits
  44. if can_harvest then
  45. local amount = "4"
  46. harvested = "bushes:" .. bush_name .. " " .. amount
  47. end
  48. -- something like a shovel
  49. elseif groupcaps.crumbly then
  50. -- with a chance of 1/3, return 2 bushes
  51. local amount
  52. if math.random(1,3) == 1 then
  53. amount = "2"
  54. else
  55. amount = "1"
  56. end
  57. -- return the bush itself
  58. harvested = "bushes:" .. bush_name .. "_bush "..amount
  59. -- something like an axe
  60. elseif groupcaps.choppy then
  61. -- the amount of sticks may vary
  62. local amount = math.random(4, 20)
  63. -- return some sticks
  64. harvested = "default:stick " .. amount
  65. -- nothing known - destroy the plant
  66. else
  67. return
  68. end
  69. -- give the harvested result to the player
  70. if harvested then
  71. --minetest.chat_send_player("singleplayer","you would now get "..tostring( harvested ) );
  72. local itemstack = ItemStack(harvested)
  73. local inventory = digger:get_inventory()
  74. if inventory:room_for_item("main", itemstack) then
  75. inventory:add_item("main", itemstack)
  76. else
  77. minetest.item_drop(itemstack, digger, pos)
  78. end
  79. end
  80. end
  81. plantlife_bushes.after_place_node = function(pos, placer, itemstack)
  82. if not (itemstack and pos) then
  83. return
  84. end
  85. local name_parts = itemstack:get_name():split(":")
  86. if #name_parts < 2 or name_parts[2] == nil then
  87. return
  88. end
  89. name_parts = name_parts[2]:split("_")
  90. if #name_parts < 2 or name_parts[1] == nil then
  91. return
  92. end
  93. minetest.swap_node(pos, {name = "bushes:fruitless_bush"})
  94. local meta = minetest.get_meta(pos)
  95. meta:set_string("bush_type", name_parts[1])
  96. end
  97. -- regrow berries (uses a base abm instead of biome_lib because of the use of metadata).
  98. minetest.register_abm({
  99. nodenames = {"bushes:fruitless_bush"},
  100. neighbors = {"group:soil", "group:potting_soil"},
  101. interval = 500,
  102. chance = 5,
  103. action = function(pos, node, active_object_count, active_object_count_wider)
  104. local meta = minetest.get_meta(pos)
  105. local bush_name = meta:get_string("bush_type")
  106. if bush_name and bush_name ~= "" then
  107. local dirtpos = {x = pos.x, y = pos.y-1, z = pos.z}
  108. local dirt = minetest.get_node(dirtpos)
  109. local is_soil = minetest.get_item_group(dirt.name, "soil") or minetest.get_item_group(dirt.name, "potting_soil")
  110. if is_soil and (dirt.name == "farming:soil_wet" or math.random(1,3) == 1) then
  111. minetest.swap_node( pos, {name = "bushes:" .. bush_name .. "_bush"})
  112. end
  113. end
  114. end
  115. })
  116. -- Define the basket and bush nodes
  117. for i, bush_name in ipairs(bushes_classic.bushes) do
  118. minetest.register_node(":bushes:basket_"..bush_name, {
  119. description = bushes_classic.bushes_descriptions[i][5],
  120. drawtype = "mesh",
  121. mesh = "bushes_basket_full.obj",
  122. tiles = {
  123. "bushes_basket_pie_"..bush_name..".png",
  124. "bushes_basket.png"
  125. },
  126. paramtype = "light",
  127. paramtype2 = "facedir",
  128. on_use = minetest.item_eat(18),
  129. groups = { dig_immediate = 3 },
  130. })
  131. local texture_top, texture_bottom
  132. local groups = {snappy = 3, bush = 1, flammable = 2, attached_node=1}
  133. if bush_name == "mixed_berry" then
  134. bush_name = "fruitless";
  135. texture_top = "bushes_fruitless_bush_top.png"
  136. texture_bottom = "bushes_fruitless_bush_bottom.png"
  137. groups.not_in_creative_inventory = 1
  138. else
  139. texture_top = "bushes_bush_top.png"
  140. texture_bottom = "bushes_bush_bottom.png"
  141. end
  142. minetest.register_node(":bushes:" .. bush_name .. "_bush", {
  143. description = bushes_classic.bushes_descriptions[i][6],
  144. drawtype = "mesh",
  145. mesh = "bushes_bush.obj",
  146. tiles = {"bushes_bush_"..bush_name..".png"},
  147. paramtype = "light",
  148. sunlight_propagates = true,
  149. walkable = false,
  150. groups = groups,
  151. sounds = default.node_sound_leaves_defaults(),
  152. drop = "",
  153. after_dig_node = function( pos, oldnode, oldmetadata, digger )
  154. return plantlife_bushes.after_dig_node(pos, oldnode, oldmetadata, digger);
  155. end,
  156. after_place_node = function( pos, placer, itemstack )
  157. return plantlife_bushes.after_place_node(pos, placer, itemstack);
  158. end,
  159. })
  160. -- do not spawn fruitless bushes
  161. if bush_name ~= "fruitless" then
  162. table.insert(bushes_classic.spawn_list, "bushes:"..bush_name.."_bush")
  163. end
  164. end
  165. minetest.register_node(":bushes:basket_empty", {
  166. description = S("Basket"),
  167. drawtype = "mesh",
  168. mesh = "bushes_basket_empty.obj",
  169. tiles = { "bushes_basket.png" },
  170. paramtype = "light",
  171. paramtype2 = "facedir",
  172. groups = { dig_immediate = 3 },
  173. })