trees.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 < 13 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(300)
  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:blueberry_bush_sapling" then
  75. minetest.log("action", "A blueberry bush sapling grows into a bush at "..
  76. minetest.pos_to_string(pos))
  77. default.grow_blueberry_bush(pos)
  78. elseif node.name == "default:acacia_bush_sapling" then
  79. minetest.log("action", "An acacia bush sapling grows into a bush at "..
  80. minetest.pos_to_string(pos))
  81. default.grow_acacia_bush(pos)
  82. elseif node.name == "default:pine_bush_sapling" then
  83. minetest.log("action", "A pine bush sapling grows into a bush at "..
  84. minetest.pos_to_string(pos))
  85. default.grow_pine_bush(pos)
  86. elseif node.name == "default:emergent_jungle_sapling" then
  87. minetest.log("action", "An emergent jungle sapling grows into a tree at "..
  88. minetest.pos_to_string(pos))
  89. default.grow_new_emergent_jungle_tree(pos)
  90. end
  91. end
  92. minetest.register_lbm({
  93. name = "default:convert_saplings_to_node_timer",
  94. nodenames = {"default:sapling", "default:junglesapling",
  95. "default:pine_sapling", "default:acacia_sapling",
  96. "default:aspen_sapling"},
  97. action = function(pos)
  98. minetest.get_node_timer(pos):start(math.random(300, 1500))
  99. end
  100. })
  101. --
  102. -- Tree generation
  103. --
  104. -- Apple tree and jungle tree trunk and leaves function
  105. local function add_trunk_and_leaves(data, a, pos, tree_cid, leaves_cid,
  106. height, size, iters, is_apple_tree)
  107. local x, y, z = pos.x, pos.y, pos.z
  108. local c_air = minetest.get_content_id("air")
  109. local c_ignore = minetest.get_content_id("ignore")
  110. local c_apple = minetest.get_content_id("default:apple")
  111. -- Trunk
  112. data[a:index(x, y, z)] = tree_cid -- Force-place lowest trunk node to replace sapling
  113. for yy = y + 1, y + height - 1 do
  114. local vi = a:index(x, yy, z)
  115. local node_id = data[vi]
  116. if node_id == c_air or node_id == c_ignore or node_id == leaves_cid then
  117. data[vi] = tree_cid
  118. end
  119. end
  120. -- Force leaves near the trunk
  121. for z_dist = -1, 1 do
  122. for y_dist = -size, 1 do
  123. local vi = a:index(x - 1, y + height + y_dist, z + z_dist)
  124. for x_dist = -1, 1 do
  125. if data[vi] == c_air or data[vi] == c_ignore then
  126. if is_apple_tree and random(1, 8) == 1 then
  127. data[vi] = c_apple
  128. else
  129. data[vi] = leaves_cid
  130. end
  131. end
  132. vi = vi + 1
  133. end
  134. end
  135. end
  136. -- Randomly add leaves in 2x2x2 clusters.
  137. for i = 1, iters do
  138. local clust_x = x + random(-size, size - 1)
  139. local clust_y = y + height + random(-size, 0)
  140. local clust_z = z + random(-size, size - 1)
  141. for xi = 0, 1 do
  142. for yi = 0, 1 do
  143. for zi = 0, 1 do
  144. local vi = a:index(clust_x + xi, clust_y + yi, clust_z + zi)
  145. if data[vi] == c_air or data[vi] == c_ignore then
  146. if is_apple_tree and random(1, 8) == 1 then
  147. data[vi] = c_apple
  148. else
  149. data[vi] = leaves_cid
  150. end
  151. end
  152. end
  153. end
  154. end
  155. end
  156. end
  157. -- Apple tree
  158. function default.grow_tree(pos, is_apple_tree, bad)
  159. --[[
  160. NOTE: Tree-placing code is currently duplicated in the engine
  161. and in games that have saplings; both are deprecated but not
  162. replaced yet
  163. --]]
  164. if bad then
  165. error("Deprecated use of default.grow_tree")
  166. end
  167. local x, y, z = pos.x, pos.y, pos.z
  168. local height = random(4, 5)
  169. local c_tree = minetest.get_content_id("default:tree")
  170. local c_leaves = minetest.get_content_id("default:leaves")
  171. local vm = minetest.get_voxel_manip()
  172. local minp, maxp = vm:read_from_map(
  173. {x = x - 2, y = y, z = z - 2},
  174. {x = x + 2, y = y + height + 1, z = z + 2}
  175. )
  176. local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
  177. local data = vm:get_data()
  178. add_trunk_and_leaves(data, a, pos, c_tree, c_leaves, height, 2, 8, is_apple_tree)
  179. vm:set_data(data)
  180. vm:write_to_map()
  181. vm:update_map()
  182. end
  183. -- Jungle tree
  184. function default.grow_jungle_tree(pos, bad)
  185. --[[
  186. NOTE: Jungletree-placing code is currently duplicated in the engine
  187. and in games that have saplings; both are deprecated but not
  188. replaced yet
  189. --]]
  190. if bad then
  191. error("Deprecated use of default.grow_jungle_tree")
  192. end
  193. local x, y, z = pos.x, pos.y, pos.z
  194. local height = random(8, 12)
  195. local c_air = minetest.get_content_id("air")
  196. local c_ignore = minetest.get_content_id("ignore")
  197. local c_jungletree = minetest.get_content_id("default:jungletree")
  198. local c_jungleleaves = minetest.get_content_id("default:jungleleaves")
  199. local vm = minetest.get_voxel_manip()
  200. local minp, maxp = vm:read_from_map(
  201. {x = x - 3, y = y - 1, z = z - 3},
  202. {x = x + 3, y = y + height + 1, z = z + 3}
  203. )
  204. local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
  205. local data = vm:get_data()
  206. add_trunk_and_leaves(data, a, pos, c_jungletree, c_jungleleaves,
  207. height, 3, 30, false)
  208. -- Roots
  209. for z_dist = -1, 1 do
  210. local vi_1 = a:index(x - 1, y - 1, z + z_dist)
  211. local vi_2 = a:index(x - 1, y, z + z_dist)
  212. for x_dist = -1, 1 do
  213. if random(1, 3) >= 2 then
  214. if data[vi_1] == c_air or data[vi_1] == c_ignore then
  215. data[vi_1] = c_jungletree
  216. elseif data[vi_2] == c_air or data[vi_2] == c_ignore then
  217. data[vi_2] = c_jungletree
  218. end
  219. end
  220. vi_1 = vi_1 + 1
  221. vi_2 = vi_2 + 1
  222. end
  223. end
  224. vm:set_data(data)
  225. vm:write_to_map()
  226. vm:update_map()
  227. end
  228. -- Pine tree from mg mapgen mod, design by sfan5, pointy top added by paramat
  229. local function add_pine_needles(data, vi, c_air, c_ignore, c_snow, c_pine_needles)
  230. local node_id = data[vi]
  231. if node_id == c_air or node_id == c_ignore or node_id == c_snow then
  232. data[vi] = c_pine_needles
  233. end
  234. end
  235. local function add_snow(data, vi, c_air, c_ignore, c_snow)
  236. local node_id = data[vi]
  237. if node_id == c_air or node_id == c_ignore then
  238. data[vi] = c_snow
  239. end
  240. end
  241. function default.grow_pine_tree(pos, snow)
  242. local x, y, z = pos.x, pos.y, pos.z
  243. local maxy = y + random(9, 13) -- Trunk top
  244. local c_air = minetest.get_content_id("air")
  245. local c_ignore = minetest.get_content_id("ignore")
  246. local c_pine_tree = minetest.get_content_id("default:pine_tree")
  247. local c_pine_needles = minetest.get_content_id("default:pine_needles")
  248. local c_snow = minetest.get_content_id("default:snow")
  249. local vm = minetest.get_voxel_manip()
  250. local minp, maxp = vm:read_from_map(
  251. {x = x - 3, y = y, z = z - 3},
  252. {x = x + 3, y = maxy + 3, z = z + 3}
  253. )
  254. local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
  255. local data = vm:get_data()
  256. -- Upper branches layer
  257. local dev = 3
  258. for yy = maxy - 1, maxy + 1 do
  259. for zz = z - dev, z + dev do
  260. local vi = a:index(x - dev, yy, zz)
  261. local via = a:index(x - dev, yy + 1, zz)
  262. for xx = x - dev, x + dev do
  263. if random() < 0.95 - dev * 0.05 then
  264. add_pine_needles(data, vi, c_air, c_ignore, c_snow,
  265. c_pine_needles)
  266. if snow then
  267. add_snow(data, via, c_air, c_ignore, c_snow)
  268. end
  269. end
  270. vi = vi + 1
  271. via = via + 1
  272. end
  273. end
  274. dev = dev - 1
  275. end
  276. -- Centre top nodes
  277. add_pine_needles(data, a:index(x, maxy + 1, z), c_air, c_ignore, c_snow,
  278. c_pine_needles)
  279. add_pine_needles(data, a:index(x, maxy + 2, z), c_air, c_ignore, c_snow,
  280. c_pine_needles) -- Paramat added a pointy top node
  281. if snow then
  282. add_snow(data, a:index(x, maxy + 3, z), c_air, c_ignore, c_snow)
  283. end
  284. -- Lower branches layer
  285. local my = 0
  286. for i = 1, 20 do -- Random 2x2 squares of needles
  287. local xi = x + random(-3, 2)
  288. local yy = maxy + random(-6, -5)
  289. local zi = z + random(-3, 2)
  290. if yy > my then
  291. my = yy
  292. end
  293. for zz = zi, zi+1 do
  294. local vi = a:index(xi, yy, zz)
  295. local via = a:index(xi, yy + 1, zz)
  296. for xx = xi, xi + 1 do
  297. add_pine_needles(data, vi, c_air, c_ignore, c_snow,
  298. c_pine_needles)
  299. if snow then
  300. add_snow(data, via, c_air, c_ignore, c_snow)
  301. end
  302. vi = vi + 1
  303. via = via + 1
  304. end
  305. end
  306. end
  307. dev = 2
  308. for yy = my + 1, my + 2 do
  309. for zz = z - dev, z + dev do
  310. local vi = a:index(x - dev, yy, zz)
  311. local via = a:index(x - dev, yy + 1, zz)
  312. for xx = x - dev, x + dev do
  313. if random() < 0.95 - dev * 0.05 then
  314. add_pine_needles(data, vi, c_air, c_ignore, c_snow,
  315. c_pine_needles)
  316. if snow then
  317. add_snow(data, via, c_air, c_ignore, c_snow)
  318. end
  319. end
  320. vi = vi + 1
  321. via = via + 1
  322. end
  323. end
  324. dev = dev - 1
  325. end
  326. -- Trunk
  327. -- Force-place lowest trunk node to replace sapling
  328. data[a:index(x, y, z)] = c_pine_tree
  329. for yy = y + 1, maxy do
  330. local vi = a:index(x, yy, z)
  331. local node_id = data[vi]
  332. if node_id == c_air or node_id == c_ignore or
  333. node_id == c_pine_needles or node_id == c_snow then
  334. data[vi] = c_pine_tree
  335. end
  336. end
  337. vm:set_data(data)
  338. vm:write_to_map()
  339. vm:update_map()
  340. end
  341. -- New apple tree
  342. function default.grow_new_apple_tree(pos)
  343. local path = minetest.get_modpath("default") ..
  344. "/schematics/apple_tree_from_sapling.mts"
  345. minetest.place_schematic({x = pos.x - 3, y = pos.y - 1, z = pos.z - 3},
  346. path, "random", nil, false)
  347. end
  348. -- New jungle tree
  349. function default.grow_new_jungle_tree(pos)
  350. local path = minetest.get_modpath("default") ..
  351. "/schematics/jungle_tree_from_sapling.mts"
  352. minetest.place_schematic({x = pos.x - 2, y = pos.y - 1, z = pos.z - 2},
  353. path, "random", nil, false)
  354. end
  355. -- New emergent jungle tree
  356. function default.grow_new_emergent_jungle_tree(pos)
  357. local path = minetest.get_modpath("default") ..
  358. "/schematics/emergent_jungle_tree_from_sapling.mts"
  359. minetest.place_schematic({x = pos.x - 3, y = pos.y - 5, z = pos.z - 3},
  360. path, "random", nil, false)
  361. end
  362. -- New pine tree
  363. function default.grow_new_pine_tree(pos)
  364. local path
  365. if math.random() > 0.5 then
  366. path = minetest.get_modpath("default") ..
  367. "/schematics/pine_tree_from_sapling.mts"
  368. else
  369. path = minetest.get_modpath("default") ..
  370. "/schematics/small_pine_tree_from_sapling.mts"
  371. end
  372. minetest.place_schematic({x = pos.x - 2, y = pos.y - 1, z = pos.z - 2},
  373. path, "0", nil, false)
  374. end
  375. -- New snowy pine tree
  376. function default.grow_new_snowy_pine_tree(pos)
  377. local path
  378. if math.random() > 0.5 then
  379. path = minetest.get_modpath("default") ..
  380. "/schematics/snowy_pine_tree_from_sapling.mts"
  381. else
  382. path = minetest.get_modpath("default") ..
  383. "/schematics/snowy_small_pine_tree_from_sapling.mts"
  384. end
  385. minetest.place_schematic({x = pos.x - 2, y = pos.y - 1, z = pos.z - 2},
  386. path, "random", nil, false)
  387. end
  388. -- New acacia tree
  389. function default.grow_new_acacia_tree(pos)
  390. local path = minetest.get_modpath("default") ..
  391. "/schematics/acacia_tree_from_sapling.mts"
  392. minetest.place_schematic({x = pos.x - 4, y = pos.y - 1, z = pos.z - 4},
  393. path, "random", nil, false)
  394. end
  395. -- New aspen tree
  396. function default.grow_new_aspen_tree(pos)
  397. local path = minetest.get_modpath("default") ..
  398. "/schematics/aspen_tree_from_sapling.mts"
  399. minetest.place_schematic({x = pos.x - 2, y = pos.y - 1, z = pos.z - 2},
  400. path, "0", nil, false)
  401. end
  402. -- Bushes do not need 'from sapling' schematic variants because
  403. -- only the stem node is force-placed in the schematic.
  404. -- Bush
  405. function default.grow_bush(pos)
  406. local path = minetest.get_modpath("default") ..
  407. "/schematics/bush.mts"
  408. minetest.place_schematic({x = pos.x - 1, y = pos.y - 1, z = pos.z - 1},
  409. path, "0", nil, false)
  410. end
  411. -- Blueberry bush
  412. function default.grow_blueberry_bush(pos)
  413. local path = minetest.get_modpath("default") ..
  414. "/schematics/blueberry_bush.mts"
  415. minetest.place_schematic({x = pos.x - 1, y = pos.y, z = pos.z - 1},
  416. path, "0", nil, false)
  417. end
  418. -- Acacia bush
  419. function default.grow_acacia_bush(pos)
  420. local path = minetest.get_modpath("default") ..
  421. "/schematics/acacia_bush.mts"
  422. minetest.place_schematic({x = pos.x - 1, y = pos.y - 1, z = pos.z - 1},
  423. path, "0", nil, false)
  424. end
  425. -- Pine bush
  426. function default.grow_pine_bush(pos)
  427. local path = minetest.get_modpath("default") ..
  428. "/schematics/pine_bush.mts"
  429. minetest.place_schematic({x = pos.x - 1, y = pos.y - 1, z = pos.z - 1},
  430. path, "0", nil, false)
  431. end
  432. -- Large cactus
  433. function default.grow_large_cactus(pos)
  434. local path = minetest.get_modpath("default") ..
  435. "/schematics/large_cactus.mts"
  436. minetest.place_schematic({x = pos.x - 2, y = pos.y - 1, z = pos.z - 2},
  437. path, "random", nil, false)
  438. end
  439. --
  440. -- Sapling 'on place' function to check protection of node and resulting tree volume
  441. --
  442. function default.sapling_on_place(itemstack, placer, pointed_thing,
  443. sapling_name, minp_relative, maxp_relative, interval)
  444. -- Position of sapling
  445. local pos = pointed_thing.under
  446. local node = minetest.get_node_or_nil(pos)
  447. local pdef = node and minetest.registered_nodes[node.name]
  448. if pdef and pdef.on_rightclick and
  449. not (placer and placer:is_player() and
  450. placer:get_player_control().sneak) then
  451. return pdef.on_rightclick(pos, node, placer, itemstack, pointed_thing)
  452. end
  453. if not pdef or not pdef.buildable_to then
  454. pos = pointed_thing.above
  455. node = minetest.get_node_or_nil(pos)
  456. pdef = node and minetest.registered_nodes[node.name]
  457. if not pdef or not pdef.buildable_to then
  458. return itemstack
  459. end
  460. end
  461. local player_name = placer and placer:get_player_name() or ""
  462. -- Check sapling position for protection
  463. if minetest.is_protected(pos, player_name) then
  464. minetest.record_protection_violation(pos, player_name)
  465. return itemstack
  466. end
  467. -- Check tree volume for protection
  468. if minetest.is_area_protected(
  469. vector.add(pos, minp_relative),
  470. vector.add(pos, maxp_relative),
  471. player_name,
  472. interval) then
  473. minetest.record_protection_violation(pos, player_name)
  474. -- Print extra information to explain
  475. minetest.chat_send_player(player_name,
  476. itemstack:get_definition().description .. " will intersect protection " ..
  477. "on growth")
  478. return itemstack
  479. end
  480. minetest.log("action", player_name .. " places node "
  481. .. sapling_name .. " at " .. minetest.pos_to_string(pos))
  482. local take_item = not (creative and creative.is_enabled_for
  483. and creative.is_enabled_for(player_name))
  484. local newnode = {name = sapling_name}
  485. local ndef = minetest.registered_nodes[sapling_name]
  486. minetest.set_node(pos, newnode)
  487. -- Run callback
  488. if ndef and ndef.after_place_node then
  489. -- Deepcopy place_to and pointed_thing because callback can modify it
  490. if ndef.after_place_node(table.copy(pos), placer,
  491. itemstack, table.copy(pointed_thing)) then
  492. take_item = false
  493. end
  494. end
  495. -- Run script hook
  496. for _, callback in ipairs(minetest.registered_on_placenodes) do
  497. -- Deepcopy pos, node and pointed_thing because callback can modify them
  498. if callback(table.copy(pos), table.copy(newnode),
  499. placer, table.copy(node or {}),
  500. itemstack, table.copy(pointed_thing)) then
  501. take_item = false
  502. end
  503. end
  504. if take_item then
  505. itemstack:take_item()
  506. end
  507. return itemstack
  508. end