trees.lua 16 KB

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