growth.lua 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. -- The growing ABM
  2. function biome_lib.check_surface(name, nodes)
  3. if not nodes then return true end
  4. if type(nodes) == "string" then return nodes == name end
  5. if nodes.set and nodes[name] then
  6. return true
  7. else
  8. for _, n in ipairs(nodes) do
  9. if name == n then return true end
  10. end
  11. end
  12. return false
  13. end
  14. function biome_lib.update_plant(opts)
  15. local options = opts
  16. options.height_limit = options.height_limit or 5
  17. options.ground_nodes = options.ground_nodes or biome_lib.default_ground_nodes
  18. options.grow_nodes = options.grow_nodes or biome_lib.default_grow_nodes
  19. options.seed_diff = options.seed_diff or 0
  20. local n
  21. if type(options.grow_plant) == "table" then
  22. n = "multi: "..options.grow_plant[1]..", ..."
  23. else
  24. n = options.grow_plant
  25. end
  26. options.label = options.label or "biome_lib.update_plant(): "..n
  27. if options.grow_delay*biome_lib.time_scale >= 1 then
  28. options.interval = options.grow_delay*biome_lib.time_scale
  29. else
  30. options.interval = 1
  31. end
  32. minetest.register_abm({
  33. nodenames = { options.grow_plant },
  34. interval = options.interval,
  35. chance = options.grow_chance,
  36. label = options.label,
  37. action = function(pos, node, active_object_count, active_object_count_wider)
  38. local p_top = {x=pos.x, y=pos.y+1, z=pos.z}
  39. local p_bot = {x=pos.x, y=pos.y-1, z=pos.z}
  40. local n_top = minetest.get_node(p_top)
  41. local n_bot = minetest.get_node(p_bot)
  42. local root_node = minetest.get_node({x=pos.x, y=pos.y-options.height_limit, z=pos.z})
  43. local walldir = nil
  44. if options.need_wall and options.verticals_list then
  45. walldir = biome_lib.find_adjacent_wall(p_top, options.verticals_list, options.choose_random_wall)
  46. end
  47. if biome_lib.default_grow_through_nodes[n_top.name]
  48. and (not options.need_wall or (options.need_wall and walldir)) then
  49. if options.grow_vertically and walldir then
  50. if biome_lib.search_downward(pos, options.height_limit, options.ground_nodes) then
  51. minetest.swap_node(p_top, { name = options.grow_plant, param2 = walldir})
  52. end
  53. elseif biome_lib.check_surface(n_bot.name, options.grow_nodes) then
  54. if not options.grow_result and not options.grow_function then
  55. minetest.swap_node(pos, biome_lib.air)
  56. else
  57. biome_lib.replace_plant(pos, options.grow_result, options.grow_function, options.facedir, options.seed_diff)
  58. end
  59. end
  60. end
  61. end
  62. })
  63. end