hedges.lua 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. hedges.register_leaves = function(name, leafnode, leaftex)
  2. minetest.register_node("hedges:"..name.."_leaves", {
  3. description = name.." Leaves",
  4. drawtype = "allfaces_optional",
  5. tiles = { leaftex },
  6. paramtype = "light",
  7. groups = {snappy=3, flammable=2, leaves=1, hedges_leaves=1,not_in_creatve_inventory=1},
  8. sounds = default.node_sound_leaves_defaults(),
  9. drop = {
  10. max_items = 1,
  11. items = {
  12. {items = {"hedges:"..name.."_sapling"}, rarity = 200 },
  13. {items = {leafnode} }
  14. }
  15. },
  16. })
  17. minetest.register_node("hedges:"..name.."_leaves_top", {
  18. description = name.." Leaves",
  19. drawtype = "allfaces_optional",
  20. tiles = { leaftex },
  21. paramtype = "light",
  22. groups = {snappy=3, flammable=2, leaves=1, hedges_leaves=1,not_in_creatve_inventory=1},
  23. sounds = default.node_sound_leaves_defaults(),
  24. drawtype = "nodebox",
  25. node_box = {
  26. type = "fixed",
  27. fixed = {
  28. {-0.3, -0.5, -0.5, 0.3, 0.3, 0.5},
  29. {-0.5, -0.5, -0.3, 0.5, 0.3, 0.3},
  30. {-0.3, -0.5, -0.3, 0.3, 0.5, 0.3},
  31. },
  32. },
  33. selection_box = {
  34. type = "fixed",
  35. fixed = {
  36. {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
  37. },
  38. },
  39. drop = {
  40. max_items = 1,
  41. items = {
  42. {items = {"hedges:"..name.."_sapling"}, rarity = 200 },
  43. {items = {leafnode} }
  44. }
  45. },
  46. })
  47. end
  48. hedges.register_root = function(name, leaftex, maxheight)
  49. minetest.register_node("hedges:"..name.."_root", {
  50. description = name.." Root ",
  51. drawtype = "allfaces_optional",
  52. tiles = { leaftex.."^hedges_root.png" },
  53. paramtype = "light",
  54. groups = {snappy=3, flammable=2, leaves=1, hedges_root=1},
  55. sounds = default.node_sound_leaves_defaults(),
  56. on_construct = function(pos)
  57. local meta = minetest.get_meta(pos)
  58. meta:set_string("hedgename", name)
  59. end
  60. -- drop = {
  61. -- max_items = 1,
  62. -- items = {
  63. -- {items = {"hedges:"..name.."_sapling"}, rarity = 200 },
  64. -- {items = {"hedges:"..name.."_root"} }
  65. -- }
  66. -- },
  67. })
  68. end
  69. hedges.register_sapling = function(name, maxheight)
  70. minetest.register_node("hedges:"..name.."_sapling", {
  71. description = name.." Sapling "..maxheight,
  72. drawtype = "allfaces_optional",
  73. tiles = { "hedges_root.png" },
  74. paramtype = "light",
  75. groups = {snappy=3, flammable=2, leaves=1, hedges_sapling=1},
  76. sounds = default.node_sound_leaves_defaults(),
  77. on_construct = function(pos)
  78. local meta = minetest.get_meta(pos)
  79. meta:set_string("hedgename", name)
  80. end
  81. -- drop = {
  82. -- max_items = 1,
  83. -- items = {
  84. -- {items = {"hedges:"..name.."_sapling"}, rarity = 200 },
  85. -- {items = {"hedges:"..name.."_root"} }
  86. -- }
  87. -- },
  88. })
  89. end
  90. hedges.register_hedge = function(name, leafnode, leaftex, sapling, maxheight)
  91. --register the sapling
  92. hedges.register_sapling(name, maxheight)
  93. -- register the root
  94. hedges.register_root(name, leaftex, maxheight)
  95. -- register the leaves
  96. hedges.register_leaves(name, leafnode, leaftex)
  97. minetest.register_craft({
  98. output = "hedges:"..name.."_sapling 3",
  99. recipe = {
  100. {leafnode, leafnode, leafnode},
  101. {leafnode, leafnode, leafnode},
  102. {sapling, sapling, sapling},
  103. }
  104. })
  105. end
  106. minetest.register_abm({
  107. nodenames = {"group:hedges_sapling"},
  108. interval = 5,
  109. chance = 1,
  110. action = function(pos, node)
  111. pos.y = pos.y-1
  112. local name = minetest.get_node(pos).name
  113. if name == "default:dirt"
  114. or name == "default:dirt_with_grass"
  115. or name == "default:sand"
  116. or name == "default:desert_sand"
  117. then
  118. pos.y = pos.y+1
  119. local meta = minetest.get_meta(pos)
  120. local nn = meta:get_string("hedgename")
  121. minetest.set_node(pos, {name="hedges:"..nn.."_root"})
  122. end
  123. end,
  124. })
  125. local printpos = function(p,s)
  126. print("pos: "..p.x..","..p.y..","..p.z.." "..s)
  127. end
  128. local cp = function(p)
  129. return {x=p.x, y=p.y, z=p.z}
  130. end
  131. hedges.node_in_dir = function(pos, x,z, name)
  132. pos.x = pos.x + x
  133. pos.z = pos.z + z
  134. local n = minetest.get_node(pos).name
  135. if n == name then
  136. return cp(pos)
  137. end
  138. pos.y = pos.y + 1
  139. n = minetest.get_node(pos).name
  140. if n == name then
  141. return cp(pos)
  142. end
  143. pos.y = pos.y - 2
  144. n = minetest.get_node(pos).name
  145. if n == name then
  146. return cp(pos)
  147. end
  148. return nil
  149. end
  150. hedges.can_spread = function(pos, x,z, name)
  151. local p1 = hedges.node_in_dir(pos, x,z, name)
  152. if p1 == nil then return false end
  153. local p2 = hedges.node_in_dir(p1, x,z, name)
  154. if p2 == nil then return false end
  155. return true
  156. end
  157. hedges.try_spread = function(pos, x,z, rootname)
  158. pos.x = pos.x + x
  159. pos.z = pos.z + z
  160. local ns = {}
  161. pos.y = pos.y - 2
  162. for i = 1, 3 do
  163. local dirt = minetest.get_node(pos).name
  164. local air = minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z}).name
  165. if air == "air" and
  166. (dirt == "default:dirt"
  167. or dirt == "default:dirt_with_grass"
  168. or dirt == "default:sand"
  169. or dirt == "default:desert_sand") then
  170. minetest.set_node({x=pos.x, y=pos.y+1, z=pos.z}, rootname)
  171. end
  172. pos.y = pos.y + 1
  173. end
  174. end
  175. minetest.register_abm({
  176. nodenames = {"group:hedges_root"},
  177. interval = 5,
  178. chance = 1,
  179. action = function(pos, node)
  180. local op = {
  181. x=pos.x,
  182. y=pos.y,
  183. z=pos.z
  184. }
  185. local myname = minetest.get_node(op).name
  186. pos.y = pos.y-1
  187. local name = minetest.get_node(pos).name
  188. if name == "default:dirt"
  189. or name == "default:dirt_with_grass"
  190. or name == "default:sand"
  191. or name == "default:desert_sand"
  192. then
  193. -- grow up
  194. pos.y = pos.y+1
  195. local meta = minetest.get_meta(pos)
  196. local nn = meta:get_string("hedgename")
  197. pos.y = pos.y+1
  198. local height = 1
  199. while (minetest.get_node(pos).name == "hedges:"..nn.."_leaves" or minetest.get_node(pos).name == "hedges:"..nn.."_leaves_top") and height < 3 do
  200. height = height+1
  201. pos.y = pos.y+1
  202. end
  203. if height == 3 then
  204. if minetest.get_node(pos).name == "air" then
  205. minetest.set_node(pos, {name="hedges:"..nn.."_leaves_top"})
  206. end
  207. elseif height < 3 then
  208. if minetest.get_node(pos).name == "air" then
  209. minetest.set_node(pos, {name="hedges:"..nn.."_leaves"})
  210. end
  211. end
  212. -- spread
  213. if hedges.can_spread(cp(op), 1,0, myname) then
  214. hedges.try_spread(cp(op), -1,0, {name="hedges:"..nn.."_sapling"})
  215. elseif hedges.can_spread(cp(op), -1,0, myname) then
  216. hedges.try_spread(cp(op), 1,0, {name="hedges:"..nn.."_sapling"})
  217. end
  218. if hedges.can_spread(cp(op), 0,1, myname) then
  219. hedges.try_spread(cp(op), 0,-1, {name="hedges:"..nn.."_sapling"})
  220. elseif hedges.can_spread(cp(op), 0,-1, myname) then
  221. hedges.try_spread(cp(op), 0,1, {name="hedges:"..nn.."_sapling"})
  222. end
  223. end
  224. end
  225. })
  226. for i in ipairs(moretrees.treelist) do
  227. local treename = moretrees.treelist[i][1]
  228. hedges.register_hedge(treename, "moretrees:"..treename.."_leaves", "moretrees_"..treename.."_leaves.png" , "moretrees:"..treename.."_sapling", 4)
  229. end
  230. hedges.register_hedge("defaultleaves", "default:leaves", "default_leaves.png", "default:sapling", 4)
  231. hedges.register_hedge("bananaleaves", "farming:banana_leaves", "farming_banana_leaves.png", "default:sapling", 4)
  232. hedges.register_hedge("pineneedles", "snow:needles", "snow_needles.png", "default:sapling", 4)
  233. hedges.register_hedge("poisonivy", "poisonivy:poisonivy_climbing", "poisonivy_climbing.png", "poisonivy:sproutling", 4)