mapgen.lua 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. if not minetest.global_exists("basictrees") then basictrees = {} end
  2. local find_surface = function(xz, b, t)
  3. for j=t, b, -1 do
  4. local pos = {x=xz.x, y=j, z=xz.z}
  5. local n = minetest.get_node(pos).name
  6. if snow.is_snow(n) then
  7. local pb = {x=pos.x, y=pos.y-1, z=pos.z}
  8. local nb = minetest.get_node(pb).name
  9. if nb == "default:stone" then
  10. return pos, pb -- Position, position below.
  11. else
  12. break
  13. end
  14. elseif n == "default:stone" then
  15. break
  16. end
  17. end
  18. end
  19. local sapling_list = {
  20. 'basictrees:acacia_sapling',
  21. 'basictrees:aspen_sapling',
  22. 'basictrees:jungletree_sapling',
  23. 'basictrees:pine_sapling',
  24. 'basictrees:tree_sapling',
  25. }
  26. local chose_sapling = function(pr, pos)
  27. local which = pr:next(1, #sapling_list)
  28. local name = sapling_list[which]
  29. minetest.set_node(pos, {name=name})
  30. end
  31. basictrees.generate_flowers = function(minp, maxp, seed)
  32. if maxp.y < -50 or minp.y > 300 then
  33. return
  34. end
  35. local pr = PseudoRandom(seed + 18923)
  36. local count = pr:next(1, 4)
  37. if count == 1 then
  38. local xz = {x=pr:next(minp.x, maxp.x), z=pr:next(minp.z, maxp.z)}
  39. local pos, posb = find_surface(xz, minp.y, maxp.y)
  40. -- Highlands only.
  41. if pos then
  42. if pos.y < 10 then return end
  43. chose_sapling(pr, pos)
  44. minetest.set_node(posb, {name="default:mossycobble"})
  45. end
  46. end
  47. end
  48. -- Register mapgen once only. Function is reloadable.
  49. if not basictrees.mapgen_registered then
  50. minetest.register_on_generated(function(minp, maxp, seed)
  51. basictrees.generate_flowers(minp, maxp, seed) end)
  52. basictrees.mapgen_registered = true
  53. end