mapgen.lua 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. minetest.register_on_generated(function(minp, maxp, seed)
  2. if maxp.y >= 2 and minp.y <= 0 then
  3. -- Generate flowers
  4. local perlin1 = minetest.get_perlin(436, 3, 0.6, 100)
  5. -- Assume X and Z lengths are equal
  6. local divlen = 16
  7. local divs = (maxp.x-minp.x)/divlen+1;
  8. for divx=0,divs-1 do
  9. for divz=0,divs-1 do
  10. local x0 = minp.x + math.floor((divx+0)*divlen)
  11. local z0 = minp.z + math.floor((divz+0)*divlen)
  12. local x1 = minp.x + math.floor((divx+1)*divlen)
  13. local z1 = minp.z + math.floor((divz+1)*divlen)
  14. -- Determine flowers amount from perlin noise
  15. local grass_amount = math.floor(perlin1:get_2d({x=x0, y=z0}) ^ 3 * 9)
  16. -- Find random positions for flowers based on this random
  17. local pr = PseudoRandom(seed+456)
  18. for i=0,grass_amount do
  19. local x = pr:next(x0, x1)
  20. local z = pr:next(z0, z1)
  21. -- Find ground level (0...15)
  22. local ground_y = nil
  23. for y=30,0,-1 do
  24. if minetest.get_node({x=x,y=y,z=z}).name ~= "air" then
  25. ground_y = y
  26. break
  27. end
  28. end
  29. if ground_y then
  30. local p = {x=x,y=ground_y+1,z=z}
  31. local nn = minetest.get_node(p).name
  32. -- Check if the node can be replaced
  33. if minetest.registered_nodes[nn] and
  34. minetest.registered_nodes[nn].buildable_to then
  35. nn = minetest.get_node({x=x,y=ground_y,z=z}).name
  36. if nn == "default:dirt_with_grass" then
  37. local flower_choice = pr:next(1, 6)
  38. local flower
  39. if flower_choice == 1 then
  40. flower = "flowers:tulip"
  41. elseif flower_choice == 2 then
  42. flower = "flowers:rose"
  43. elseif flower_choice == 3 then
  44. flower = "flowers:dandelion_yellow"
  45. elseif flower_choice == 4 then
  46. flower = "flowers:dandelion_white"
  47. elseif flower_choice == 5 then
  48. flower = "flowers:geranium"
  49. elseif flower_choice == 6 then
  50. flower = "flowers:viola"
  51. end
  52. minetest.set_node(p, {name=flower})
  53. end
  54. end
  55. end
  56. end
  57. end
  58. end
  59. end
  60. end)