trees.lua 16 KB

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