trees.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. local random = math.random
  2. --
  3. -- Grow trees from saplings
  4. --
  5. -- 'can grow' function
  6. function default.can_grow(pos)
  7. local node_under = minetest.get_node_or_nil({x = pos.x, y = pos.y - 1, z = pos.z})
  8. if not node_under then
  9. return false
  10. end
  11. local name_under = node_under.name
  12. local is_soil = minetest.get_item_group(name_under, "soil")
  13. if is_soil == 0 then
  14. return false
  15. end
  16. local light_level = minetest.get_node_light(pos)
  17. if not light_level or light_level < 10 then
  18. return false
  19. end
  20. return true
  21. end
  22. -- 'is snow nearby' function
  23. local function is_snow_nearby(pos)
  24. return minetest.find_node_near(pos, 1, {"group:snowy"})
  25. end
  26. -- Grow sapling
  27. function default.grow_sapling(pos)
  28. if not default.can_grow(pos) then
  29. -- try again 5 min later
  30. minetest.get_node_timer(pos):start(1)
  31. return
  32. end
  33. local mg_name = minetest.get_mapgen_setting("mg_name")
  34. local node = minetest.get_node(pos)
  35. if node.name == "default:sapling" then
  36. minetest.log("action", "A sapling grows into a tree at "..
  37. minetest.pos_to_string(pos))
  38. if mg_name == "v6" then
  39. default.grow_tree(pos, random(1, 4) == 1)
  40. else
  41. default.grow_new_apple_tree(pos)
  42. end
  43. elseif node.name == "default:junglesapling" then
  44. minetest.log("action", "A jungle sapling grows into a tree at "..
  45. minetest.pos_to_string(pos))
  46. if mg_name == "v6" then
  47. default.grow_jungle_tree(pos)
  48. else
  49. default.grow_new_jungle_tree(pos)
  50. end
  51. elseif node.name == "default:pine_sapling" then
  52. minetest.log("action", "A pine sapling grows into a tree at "..
  53. minetest.pos_to_string(pos))
  54. local snow = is_snow_nearby(pos)
  55. if mg_name == "v6" then
  56. default.grow_pine_tree(pos, snow)
  57. elseif snow then
  58. default.grow_new_snowy_pine_tree(pos)
  59. else
  60. default.grow_new_pine_tree(pos)
  61. end
  62. elseif node.name == "default:acacia_sapling" then
  63. minetest.log("action", "An acacia sapling grows into a tree at "..
  64. minetest.pos_to_string(pos))
  65. default.grow_new_acacia_tree(pos)
  66. elseif node.name == "default:aspen_sapling" then
  67. minetest.log("action", "An aspen sapling grows into a tree at "..
  68. minetest.pos_to_string(pos))
  69. default.grow_new_aspen_tree(pos)
  70. elseif node.name == "default:bush_sapling" then
  71. minetest.log("action", "A bush sapling grows into a bush at "..
  72. minetest.pos_to_string(pos))
  73. default.grow_bush(pos)
  74. elseif node.name == "default:acacia_bush_sapling" then
  75. minetest.log("action", "An acacia bush sapling grows into a bush at "..
  76. minetest.pos_to_string(pos))
  77. default.grow_acacia_bush(pos)
  78. end
  79. end
  80. minetest.register_lbm({
  81. name = "default:convert_saplings_to_node_timer",
  82. nodenames = {"default:sapling", "default:junglesapling",
  83. "default:pine_sapling", "default:acacia_sapling",
  84. "default:aspen_sapling"},
  85. action = function(pos)
  86. minetest.get_node_timer(pos):start(math.random(10, 11))
  87. end
  88. })
  89. --
  90. -- Tree generation
  91. --
  92. -- Apple tree and jungle tree trunk and leaves function
  93. local function add_trunk_and_leaves(data, a, pos, tree_cid, leaves_cid,
  94. height, size, iters, is_apple_tree)
  95. local x, y, z = pos.x, pos.y, pos.z
  96. local c_air = minetest.get_content_id("air")
  97. local c_ignore = minetest.get_content_id("ignore")
  98. local c_apple = minetest.get_content_id("default:apple")
  99. -- Trunk
  100. data[a:index(x, y, z)] = tree_cid -- Force-place lowest trunk node to replace sapling
  101. for yy = y + 1, y + height - 1 do
  102. local vi = a:index(x, yy, z)
  103. local node_id = data[vi]
  104. if node_id == c_air or node_id == c_ignore or node_id == leaves_cid then
  105. data[vi] = tree_cid
  106. end
  107. end
  108. -- Force leaves near the trunk
  109. for z_dist = -1, 1 do
  110. for y_dist = -size, 1 do
  111. local vi = a:index(x - 1, y + height + y_dist, z + z_dist)
  112. for x_dist = -1, 1 do
  113. if data[vi] == c_air or data[vi] == c_ignore then
  114. if is_apple_tree and random(1, 8) == 1 then
  115. data[vi] = c_apple
  116. else
  117. data[vi] = leaves_cid
  118. end
  119. end
  120. vi = vi + 1
  121. end
  122. end
  123. end
  124. -- Randomly add leaves in 2x2x2 clusters.
  125. for i = 1, iters do
  126. local clust_x = x + random(-size, size - 1)
  127. local clust_y = y + height + random(-size, 0)
  128. local clust_z = z + random(-size, size - 1)
  129. for xi = 0, 1 do
  130. for yi = 0, 1 do
  131. for zi = 0, 1 do
  132. local vi = a:index(clust_x + xi, clust_y + yi, clust_z + zi)
  133. if data[vi] == c_air or data[vi] == c_ignore then
  134. if is_apple_tree and random(1, 8) == 1 then
  135. data[vi] = c_apple
  136. else
  137. data[vi] = leaves_cid
  138. end
  139. end
  140. end
  141. end
  142. end
  143. end
  144. end
  145. -- Apple tree
  146. function default.grow_tree(pos, is_apple_tree, bad)
  147. --[[
  148. NOTE: Tree-placing code is currently duplicated in the engine
  149. and in games that have saplings; both are deprecated but not
  150. replaced yet
  151. --]]
  152. if bad then
  153. error("Deprecated use of default.grow_tree")
  154. end
  155. local x, y, z = pos.x, pos.y, pos.z
  156. local height = random(4, 5)
  157. local c_tree = minetest.get_content_id("default:tree")
  158. local c_leaves = minetest.get_content_id("default:leaves")
  159. local vm = minetest.get_voxel_manip()
  160. local minp, maxp = vm:read_from_map(
  161. {x = x - 2, y = y, z = z - 2},
  162. {x = x + 2, y = y + height + 1, z = z + 2}
  163. )
  164. local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
  165. local data = vm:get_data()
  166. add_trunk_and_leaves(data, a, pos, c_tree, c_leaves, height, 2, 8, is_apple_tree)
  167. vm:set_data(data)
  168. vm:write_to_map()
  169. vm:update_map()
  170. end
  171. -- Jungle tree
  172. function default.grow_jungle_tree(pos, bad)
  173. --[[
  174. NOTE: Jungletree-placing code is currently duplicated in the engine
  175. and in games that have saplings; both are deprecated but not
  176. replaced yet
  177. --]]
  178. if bad then
  179. error("Deprecated use of default.grow_jungle_tree")
  180. end
  181. local x, y, z = pos.x, pos.y, pos.z
  182. local height = random(8, 12)
  183. local c_air = minetest.get_content_id("air")
  184. local c_ignore = minetest.get_content_id("ignore")
  185. local c_jungletree = minetest.get_content_id("default:jungletree")
  186. local c_jungleleaves = minetest.get_content_id("default:jungleleaves")
  187. local vm = minetest.get_voxel_manip()
  188. local minp, maxp = vm:read_from_map(
  189. {x = x - 3, y = y - 1, z = z - 3},
  190. {x = x + 3, y = y + height + 1, z = z + 3}
  191. )
  192. local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
  193. local data = vm:get_data()
  194. add_trunk_and_leaves(data, a, pos, c_jungletree, c_jungleleaves,
  195. height, 3, 30, false)
  196. -- Roots
  197. for z_dist = -1, 1 do
  198. local vi_1 = a:index(x - 1, y - 1, z + z_dist)
  199. local vi_2 = a:index(x - 1, y, z + z_dist)
  200. for x_dist = -1, 1 do
  201. if random(1, 3) >= 2 then
  202. if data[vi_1] == c_air or data[vi_1] == c_ignore then
  203. data[vi_1] = c_jungletree
  204. elseif data[vi_2] == c_air or data[vi_2] == c_ignore then
  205. data[vi_2] = c_jungletree
  206. end
  207. end
  208. vi_1 = vi_1 + 1
  209. vi_2 = vi_2 + 1
  210. end
  211. end
  212. vm:set_data(data)
  213. vm:write_to_map()
  214. vm:update_map()
  215. end
  216. -- Pine tree from mg mapgen mod, design by sfan5, pointy top added by paramat
  217. local function add_pine_needles(data, vi, c_air, c_ignore, c_snow, c_pine_needles)
  218. local node_id = data[vi]
  219. if node_id == c_air or node_id == c_ignore or node_id == c_snow then
  220. data[vi] = c_pine_needles
  221. end
  222. end
  223. local function add_snow(data, vi, c_air, c_ignore, c_snow)
  224. local node_id = data[vi]
  225. if node_id == c_air or node_id == c_ignore then
  226. data[vi] = c_snow
  227. end
  228. end
  229. function default.grow_pine_tree(pos, snow)
  230. local x, y, z = pos.x, pos.y, pos.z
  231. local maxy = y + random(9, 13) -- Trunk top
  232. local c_air = minetest.get_content_id("air")
  233. local c_ignore = minetest.get_content_id("ignore")
  234. local c_pine_tree = minetest.get_content_id("default:pine_tree")
  235. local c_pine_needles = minetest.get_content_id("default:pine_needles")
  236. local c_snow = minetest.get_content_id("default:snow")
  237. local vm = minetest.get_voxel_manip()
  238. local minp, maxp = vm:read_from_map(
  239. {x = x - 3, y = y, z = z - 3},
  240. {x = x + 3, y = maxy + 3, z = z + 3}
  241. )
  242. local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
  243. local data = vm:get_data()
  244. -- Upper branches layer
  245. local dev = 3
  246. for yy = maxy - 1, maxy + 1 do
  247. for zz = z - dev, z + dev do
  248. local vi = a:index(x - dev, yy, zz)
  249. local via = a:index(x - dev, yy + 1, zz)
  250. for xx = x - dev, x + dev do
  251. if random() < 0.95 - dev * 0.05 then
  252. add_pine_needles(data, vi, c_air, c_ignore, c_snow,
  253. c_pine_needles)
  254. if snow then
  255. add_snow(data, via, c_air, c_ignore, c_snow)
  256. end
  257. end
  258. vi = vi + 1
  259. via = via + 1
  260. end
  261. end
  262. dev = dev - 1
  263. end
  264. -- Centre top nodes
  265. add_pine_needles(data, a:index(x, maxy + 1, z), c_air, c_ignore, c_snow,
  266. c_pine_needles)
  267. add_pine_needles(data, a:index(x, maxy + 2, z), c_air, c_ignore, c_snow,
  268. c_pine_needles) -- Paramat added a pointy top node
  269. if snow then
  270. add_snow(data, a:index(x, maxy + 3, z), c_air, c_ignore, c_snow)
  271. end
  272. -- Lower branches layer
  273. local my = 0
  274. for i = 1, 20 do -- Random 2x2 squares of needles
  275. local xi = x + random(-3, 2)
  276. local yy = maxy + random(-6, -5)
  277. local zi = z + random(-3, 2)
  278. if yy > my then
  279. my = yy
  280. end
  281. for zz = zi, zi+1 do
  282. local vi = a:index(xi, yy, zz)
  283. local via = a:index(xi, yy + 1, zz)
  284. for xx = xi, xi + 1 do
  285. add_pine_needles(data, vi, c_air, c_ignore, c_snow,
  286. c_pine_needles)
  287. if snow then
  288. add_snow(data, via, c_air, c_ignore, c_snow)
  289. end
  290. vi = vi + 1
  291. via = via + 1
  292. end
  293. end
  294. end
  295. dev = 2
  296. for yy = my + 1, my + 2 do
  297. for zz = z - dev, z + dev do
  298. local vi = a:index(x - dev, yy, zz)
  299. local via = a:index(x - dev, yy + 1, zz)
  300. for xx = x - dev, x + dev do
  301. if random() < 0.95 - dev * 0.05 then
  302. add_pine_needles(data, vi, c_air, c_ignore, c_snow,
  303. c_pine_needles)
  304. if snow then
  305. add_snow(data, via, c_air, c_ignore, c_snow)
  306. end
  307. end
  308. vi = vi + 1
  309. via = via + 1
  310. end
  311. end
  312. dev = dev - 1
  313. end
  314. -- Trunk
  315. -- Force-place lowest trunk node to replace sapling
  316. data[a:index(x, y, z)] = c_pine_tree
  317. for yy = y + 1, maxy do
  318. local vi = a:index(x, yy, z)
  319. local node_id = data[vi]
  320. if node_id == c_air or node_id == c_ignore or
  321. node_id == c_pine_needles or node_id == c_snow then
  322. data[vi] = c_pine_tree
  323. end
  324. end
  325. vm:set_data(data)
  326. vm:write_to_map()
  327. vm:update_map()
  328. end
  329. -- New apple tree
  330. function default.grow_new_apple_tree(pos)
  331. local path = minetest.get_modpath("default") ..
  332. "/schematics/apple_tree_from_sapling.mts"
  333. minetest.place_schematic({x = pos.x - 2, y = pos.y - 1, z = pos.z - 2},
  334. path, "random", nil, false)
  335. end
  336. -- New jungle tree
  337. function default.grow_new_jungle_tree(pos)
  338. local path = minetest.get_modpath("default") ..
  339. "/schematics/jungle_tree_from_sapling.mts"
  340. minetest.place_schematic({x = pos.x - 2, y = pos.y - 1, z = pos.z - 2},
  341. path, "random", nil, false)
  342. end
  343. -- New pine tree
  344. function default.grow_new_pine_tree(pos)
  345. local path = minetest.get_modpath("default") ..
  346. "/schematics/pine_tree_from_sapling.mts"
  347. minetest.place_schematic({x = pos.x - 2, y = pos.y - 1, z = pos.z - 2},
  348. path, "0", nil, false)
  349. end
  350. -- New snowy pine tree
  351. function default.grow_new_snowy_pine_tree(pos)
  352. local path = minetest.get_modpath("default") ..
  353. "/schematics/snowy_pine_tree_from_sapling.mts"
  354. minetest.place_schematic({x = pos.x - 2, y = pos.y - 1, z = pos.z - 2},
  355. path, "random", nil, false)
  356. end
  357. -- New acacia tree
  358. function default.grow_new_acacia_tree(pos)
  359. local path = minetest.get_modpath("default") ..
  360. "/schematics/acacia_tree_from_sapling.mts"
  361. minetest.place_schematic({x = pos.x - 4, y = pos.y - 1, z = pos.z - 4},
  362. path, "random", nil, false)
  363. end
  364. -- New aspen tree
  365. function default.grow_new_aspen_tree(pos)
  366. local path = minetest.get_modpath("default") ..
  367. "/schematics/aspen_tree_from_sapling.mts"
  368. minetest.place_schematic({x = pos.x - 2, y = pos.y - 1, z = pos.z - 2},
  369. path, "0", nil, false)
  370. end
  371. -- Bushes do not need 'from sapling' schematic variants because
  372. -- only the stem node is force-placed in the schematic.
  373. -- Bush
  374. function default.grow_bush(pos)
  375. local path = minetest.get_modpath("default") ..
  376. "/schematics/bush.mts"
  377. minetest.place_schematic({x = pos.x - 1, y = pos.y - 1, z = pos.z - 1},
  378. path, "0", nil, false)
  379. end
  380. -- Acacia bush
  381. function default.grow_acacia_bush(pos)
  382. local path = minetest.get_modpath("default") ..
  383. "/schematics/acacia_bush.mts"
  384. minetest.place_schematic({x = pos.x - 1, y = pos.y - 1, z = pos.z - 1},
  385. path, "0", nil, false)
  386. end
  387. --
  388. -- Sapling 'on place' function to check protection of node and resulting tree volume
  389. --
  390. function default.sapling_on_place(itemstack, placer, pointed_thing,
  391. sapling_name, minp_relative, maxp_relative, interval)
  392. -- Position of sapling
  393. local pos = pointed_thing.under
  394. local node = minetest.get_node_or_nil(pos)
  395. local pdef = node and minetest.registered_nodes[node.name]
  396. if pdef and pdef.on_rightclick and not placer:get_player_control().sneak then
  397. return pdef.on_rightclick(pos, node, placer, itemstack, pointed_thing)
  398. end
  399. if not pdef or not pdef.buildable_to then
  400. pos = pointed_thing.above
  401. node = minetest.get_node_or_nil(pos)
  402. pdef = node and minetest.registered_nodes[node.name]
  403. if not pdef or not pdef.buildable_to then
  404. return itemstack
  405. end
  406. end
  407. local player_name = placer:get_player_name()
  408. -- Check sapling position for protection
  409. if minetest.is_protected(pos, player_name) then
  410. minetest.record_protection_violation(pos, player_name)
  411. return itemstack
  412. end
  413. -- Check tree volume for protection
  414. if default.intersects_protection(
  415. vector.add(pos, minp_relative),
  416. vector.add(pos, maxp_relative),
  417. player_name,
  418. interval) then
  419. minetest.record_protection_violation(pos, player_name)
  420. -- Print extra information to explain
  421. minetest.chat_send_player(player_name, "Tree will intersect protection")
  422. return itemstack
  423. end
  424. minetest.log("action", player_name .. " places node "
  425. .. sapling_name .. " at " .. minetest.pos_to_string(pos))
  426. local take_item = not (creative and creative.is_enabled_for
  427. and creative.is_enabled_for(player_name))
  428. local newnode = {name = sapling_name}
  429. local ndef = minetest.registered_nodes[sapling_name]
  430. minetest.set_node(pos, newnode)
  431. -- Run callback
  432. if ndef and ndef.after_place_node then
  433. -- Deepcopy place_to and pointed_thing because callback can modify it
  434. if ndef.after_place_node(table.copy(pos), placer,
  435. itemstack, table.copy(pointed_thing)) then
  436. take_item = false
  437. end
  438. end
  439. -- Run script hook
  440. for _, callback in ipairs(minetest.registered_on_placenodes) do
  441. -- Deepcopy pos, node and pointed_thing because callback can modify them
  442. if callback(table.copy(pos), table.copy(newnode),
  443. placer, table.copy(node or {}),
  444. itemstack, table.copy(pointed_thing)) then
  445. take_item = false
  446. end
  447. end
  448. if take_item then
  449. itemstack:take_item()
  450. end
  451. return itemstack
  452. end
  453. -- Important bloody aliases
  454. minetest.register_alias("trees:tree_conifer", "default:pine_tree")
  455. minetest.register_alias("trees:wood_conifer", "default:pine_wood")
  456. minetest.register_alias("trees:sapling_conifer", "default:pine_sapling")
  457. minetest.register_alias("trees:leaves_conifer", "default:pine_needles")
  458. minetest.register_alias("trees:tree_palm", "default:jungletree")
  459. minetest.register_alias("trees:leaves_palm", "default:jungleleaves")
  460. minetest.register_alias("trees:sapling_palm", "default:junglesapling")
  461. minetest.register_alias("trees:wood_palm", "default:junglewood")
  462. minetest.register_alias("trees:tree_mangrove", "default:aspen_tree")
  463. minetest.register_alias("trees:leaves_mangrove", "default:aspen_leaves")
  464. minetest.register_alias("trees:sapling_mangrove", "default:aspen_sapling")
  465. minetest.register_alias("trees:wood_mangrove", "default:aspen_wood")
  466. minetest.register_alias("trees:leaves_red", "default:jungleleaves")
  467. minetest.register_alias("trees:leaves_yellow", "default:jungleleaves")
  468. minetest.register_alias("trees:leaves_green", "default:jungleleaves")