growth.lua 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. local time_scale = ...
  2. -- The growing ABM
  3. function biome_lib.check_surface(name, nodes)
  4. if not nodes then return true end
  5. if type(nodes) == "string" then return nodes == name end
  6. if nodes.set and nodes[name] then
  7. return true
  8. else
  9. for _, n in ipairs(nodes) do
  10. if name == n then return true end
  11. end
  12. end
  13. return false
  14. end
  15. function biome_lib:grow_plants(opts)
  16. local options = opts
  17. options.height_limit = options.height_limit or 5
  18. options.ground_nodes = options.ground_nodes or { "default:dirt_with_grass" }
  19. options.grow_nodes = options.grow_nodes or { "default:dirt_with_grass" }
  20. options.seed_diff = options.seed_diff or 0
  21. local n
  22. if type(options.grow_plant) == "table" then
  23. n = "multi: "..options.grow_plant[1]..", ..."
  24. else
  25. n = options.grow_plant
  26. end
  27. options.label = options.label or "biome_lib grow_plants(): "..n
  28. if options.grow_delay*time_scale >= 1 then
  29. options.interval = options.grow_delay*time_scale
  30. else
  31. options.interval = 1
  32. end
  33. minetest.register_abm({
  34. nodenames = { options.grow_plant },
  35. interval = options.interval,
  36. chance = options.grow_chance,
  37. label = options.label,
  38. action = function(pos, node, active_object_count, active_object_count_wider)
  39. local p_top = {x=pos.x, y=pos.y+1, z=pos.z}
  40. local p_bot = {x=pos.x, y=pos.y-1, z=pos.z}
  41. local n_top = minetest.get_node(p_top)
  42. local n_bot = minetest.get_node(p_bot)
  43. local root_node = minetest.get_node({x=pos.x, y=pos.y-options.height_limit, z=pos.z})
  44. local walldir = nil
  45. if options.need_wall and options.verticals_list then
  46. walldir = biome_lib:find_adjacent_wall(p_top, options.verticals_list, options.choose_random_wall)
  47. end
  48. if (n_top.name == "air" or n_top.name == "default:snow")
  49. and (not options.need_wall or (options.need_wall and walldir)) then
  50. if options.grow_vertically and walldir then
  51. if biome_lib:search_downward(pos, options.height_limit, options.ground_nodes) then
  52. minetest.swap_node(p_top, { name = options.grow_plant, param2 = walldir})
  53. end
  54. elseif biome_lib.check_surface(n_bot.name, options.grow_nodes) then
  55. if not options.grow_result and not options.grow_function then
  56. minetest.swap_node(pos, biome_lib.air)
  57. else
  58. biome_lib:replace_object(pos, options.grow_result, options.grow_function, options.facedir, options.seed_diff)
  59. end
  60. end
  61. end
  62. end
  63. })
  64. end
  65. -- spawn_tree() on generate is routed through here so that other mods can hook
  66. -- into it.
  67. function biome_lib:generate_tree(pos, nodes_or_function_or_model)
  68. minetest.spawn_tree(pos, nodes_or_function_or_model)
  69. end
  70. -- and this one's for the call used in the growing code
  71. function biome_lib:grow_tree(pos, nodes_or_function_or_model)
  72. minetest.spawn_tree(pos, nodes_or_function_or_model)
  73. end