trees.lua 16 KB

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