init.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. if not minetest.global_exists("jungletree") then jungletree = {} end
  2. jungletree.modpath = minetest.get_modpath("jungletree")
  3. -- Localize for performance.
  4. local math_random = math.random
  5. local SAPLING_TIME_MIN = 60*15
  6. local SAPLING_TIME_MAX = 60*20
  7. local SAPLING_CHANCE = 25
  8. local SCHEMATIC_MINP = {x=-2, y=0, z=-2}
  9. local SCHEMATIC_MAXP = {x=2, y=12, z=2}
  10. local SCHEMATIC_RELP = {x=-2, y=0, z=-2}
  11. jungletree.can_grow = function(pos)
  12. return basictrees.can_grow(pos)
  13. end
  14. jungletree.sapling_selection_box = {
  15. type = "fixed",
  16. fixed = {-0.3, -0.5, -0.3, 0.3, 0.35, 0.3},
  17. }
  18. jungletree.sapling_groups = utility.dig_groups("plant", {
  19. flammable = 2,
  20. attached_node = 1,
  21. sapling = 1,
  22. })
  23. jungletree.tree_groups = utility.dig_groups("tree", {
  24. flammable = 2,
  25. tree = 1,
  26. })
  27. jungletree.get_wood_groups = function(extra)
  28. local groups = utility.dig_groups("wood", extra or {})
  29. groups.flammable = 2
  30. groups.wood = 1
  31. return groups
  32. end
  33. jungletree.stair_groups = utility.dig_groups("wood", {
  34. flammable = 2,
  35. })
  36. jungletree.leaves_groups = utility.dig_groups("leaves", {
  37. leafdecay = 3,
  38. flammable = 2,
  39. leaves = 1,
  40. green_leaves = 1,
  41. })
  42. jungletree.get_leafdrop_table = function(chance, sapling, leaves)
  43. local drop = {
  44. max_items = 1,
  45. items = {
  46. {items={sapling}, rarity=chance},
  47. {items={"default:stick"}, rarity=10},
  48. -- Player will get leaves only if he gets nothing else; this is because 'max_items' is 1.
  49. {items={leaves}},
  50. }
  51. }
  52. return drop
  53. end
  54. minetest.register_node("jungletree:jungletree_tree", {
  55. drawtype = "nodebox",
  56. paramtype = "light",
  57. node_box = {
  58. type = "fixed",
  59. fixed = basictrees.trunk_nodebox,
  60. },
  61. description = "White Jungletree",
  62. tiles = {
  63. "jungletree_jungletree_top.png",
  64. "jungletree_jungletree_top.png",
  65. "jungletree_jungletree_side.png"
  66. },
  67. paramtype2 = "facedir",
  68. groups = jungletree.tree_groups,
  69. sounds = default.node_sound_wood_defaults(),
  70. on_place = minetest.rotate_node,
  71. movement_speed_multiplier = default.NORM_SPEED,
  72. on_destruct = enhanced_leafdecay.make_tree_destructor({
  73. leaves = {
  74. "jungletree:jungletree_leaves",
  75. "group:dry_leaves",
  76. },
  77. }),
  78. })
  79. minetest.register_node("jungletree:jungletree_leaves", {
  80. description = "Jungletree Leaves",
  81. drawtype = "allfaces_optional",
  82. tiles = {"jungletree_jungletree_leaves.png"},
  83. paramtype = "light",
  84. waving = 1,
  85. groups = jungletree.leaves_groups,
  86. drop = jungletree.get_leafdrop_table(SAPLING_CHANCE, "jungletree:jungletree_sapling", "jungletree:jungletree_leaves"),
  87. sounds = default.node_sound_leaves_defaults(),
  88. movement_speed_multiplier = default.SLOW_SPEED,
  89. on_construct = enhanced_leafdecay.make_leaf_constructor({}),
  90. on_timer = enhanced_leafdecay.make_leaf_nodetimer({tree="jungletree:jungletree_tree"}),
  91. })
  92. minetest.register_node("jungletree:jungletree_wood", {
  93. description = "Jungletree Planks",
  94. paramtype2 = "facedir",
  95. place_param2 = 0,
  96. tiles = {"jungletree_jungletree_wood.png"},
  97. groups = jungletree.get_wood_groups({wood_dark = 1}),
  98. sounds = default.node_sound_wood_defaults(),
  99. })
  100. minetest.register_node("jungletree:jungletree_sapling", {
  101. description = "White Jungletree Sapling\n\nWill not grow in deep caves.",
  102. drawtype = "plantlike",
  103. tiles = {"jungletree_jungletree_sapling.png"},
  104. paramtype = "light",
  105. sunlight_propagates = true,
  106. walkable = false,
  107. selection_box = jungletree.sapling_selection_box,
  108. groups = jungletree.sapling_groups,
  109. sounds = default.node_sound_leaves_defaults(),
  110. movement_speed_multiplier = default.SLOW_SPEED_PLANTS,
  111. on_timer = function(pos, elapsed)
  112. if mtflower.can_grow(pos) then
  113. if mtflower.try_grow(pos, "jungletree:jungletree_tree", "jungletree:jungletree_leaves", "glowstone:minerals", "glowstone:minerals") then
  114. return
  115. end
  116. end
  117. if not jungletree.can_grow(pos) then
  118. minetest.get_node_timer(pos):start(math_random(SAPLING_TIME_MIN, SAPLING_TIME_MAX))
  119. return
  120. end
  121. minetest.set_node(pos, {name='air'}) -- Remove sapling first.
  122. local path = jungletree.modpath .. "/schematics/jungletree_jungletree.mts"
  123. local schempos = vector.add(pos, SCHEMATIC_RELP)
  124. local subtract = math_random(0, math_random(0, 2))
  125. schempos.y = schempos.y - subtract
  126. minetest.place_schematic(schempos, path, "0", nil, false)
  127. trunkgen.generate_bole(pos, "jungletree:jungletree_tree")
  128. trunkgen.generate_jungletree_branches(
  129. pos,
  130. "jungletree:jungletree_tree",
  131. "jungletree:jungletree_leaves",
  132. 4, 7-subtract)
  133. snowscatter.dump_snowdust_on_tree(pos, SCHEMATIC_MINP, SCHEMATIC_MAXP)
  134. ambiance.spawn_sound_beacon_inside_area("soundbeacon:trees", pos, SCHEMATIC_MINP, SCHEMATIC_MAXP, 40, 3)
  135. end,
  136. on_construct = function(pos)
  137. minetest.get_node_timer(pos):start(math_random(SAPLING_TIME_MIN, SAPLING_TIME_MAX))
  138. end,
  139. on_place = function(itemstack, placer, pointed_thing)
  140. itemstack = default.sapling_on_place(itemstack, placer, pointed_thing,
  141. "jungletree:jungletree_sapling", SCHEMATIC_MINP, SCHEMATIC_MAXP, 4)
  142. return itemstack
  143. end,
  144. })
  145. minetest.register_craft({
  146. output = 'jungletree:jungletree_wood 4',
  147. recipe = {
  148. {'jungletree:jungletree_tree'},
  149. }
  150. })
  151. minetest.register_craft({
  152. output = "default:fence_junglewood 4",
  153. recipe = {
  154. {"jungletree:jungletree_wood", 'group:stick', "jungletree:jungletree_wood"},
  155. {"jungletree:jungletree_wood", 'group:stick', "jungletree:jungletree_wood"},
  156. }
  157. })
  158. minetest.register_craft({
  159. output = "doors:gate_junglewood_closed",
  160. recipe = {
  161. {"techcrafts:hinge_wood", "jungletree:jungletree_wood", "group:stick"},
  162. {"group:stick", "jungletree:jungletree_wood", "group:stick"}
  163. }
  164. })
  165. stairs.register_stair_and_slab(
  166. "white_jungletree",
  167. "jungletree:jungletree_wood",
  168. jungletree.stair_groups,
  169. {"jungletree_jungletree_wood.png"},
  170. "Jungletree Plank",
  171. default.node_sound_wood_defaults()
  172. )
  173. local find_surface = function(xz, b, t)
  174. for j=t, b, -1 do
  175. local pos = {x=xz.x, y=j, z=xz.z}
  176. local n = minetest.get_node(pos).name
  177. if snow.is_snow(n) then
  178. local pb = {x=pos.x, y=pos.y-1, z=pos.z}
  179. local nb = minetest.get_node(pb).name
  180. if nb == "default:stone" then
  181. return pos, pb -- Position, position below.
  182. else
  183. break
  184. end
  185. elseif n == "default:stone" then
  186. break
  187. end
  188. end
  189. end
  190. local chose_sapling = function(pr, pos)
  191. local name = "jungletree:jungletree_sapling"
  192. minetest.set_node(pos, {name=name})
  193. end
  194. jungletree.generate_flowers = function(minp, maxp, seed)
  195. if maxp.y < -50 or minp.y > 300 then
  196. return
  197. end
  198. local pr = PseudoRandom(seed + 7192)
  199. local count = pr:next(1, 4)
  200. if count == 1 then
  201. local xz = {x=pr:next(minp.x, maxp.x), z=pr:next(minp.z, maxp.z)}
  202. local pos, posb = find_surface(xz, minp.y, maxp.y)
  203. -- Highlands only.
  204. if pos then
  205. if pos.y < 10 then return end
  206. chose_sapling(pr, pos)
  207. minetest.set_node(posb, {name="default:mossycobble"})
  208. end
  209. end
  210. end
  211. minetest.register_on_generated(function(minp, maxp, seed)
  212. jungletree.generate_flowers(minp, maxp, seed) end)