mapgen.lua 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. --Map Generation Stuff
  2. minetest.register_on_generated(function(minp, maxp, seed)
  3. if maxp.y >= 2 and minp.y <= 0 then
  4. -- Generate pebbles
  5. local perlin1 = minetest.get_perlin(329, 3, 0.6, 100)
  6. -- Assume X and Z lengths are equal
  7. local divlen = 16
  8. local divs = (maxp.x-minp.x)/divlen+1;
  9. for divx=0,divs-1 do
  10. for divz=0,divs-1 do
  11. local x0 = minp.x + math.floor((divx+0)*divlen)
  12. local z0 = minp.z + math.floor((divz+0)*divlen)
  13. local x1 = minp.x + math.floor((divx+1)*divlen)
  14. local z1 = minp.z + math.floor((divz+1)*divlen)
  15. -- Determine pebble amount from perlin noise
  16. local pebble_amount = math.floor(perlin1:get2d({x=x0, y=z0}) ^ 2 * 2)
  17. -- Find random positions for pebbles based on this random
  18. local pr = PseudoRandom(seed+1)
  19. for i=0,pebble_amount do
  20. local x = pr:next(x0, x1)
  21. local z = pr:next(z0, z1)
  22. -- Find ground level (0...15)
  23. local ground_y = nil
  24. for y=30,0,-1 do
  25. if minetest.get_node({x=x,y=y,z=z}).name ~= "air" then
  26. ground_y = y
  27. break
  28. end
  29. end
  30. if ground_y then
  31. local p = {x=x,y=ground_y+1,z=z}
  32. local nn = minetest.get_node(p).name
  33. -- Check if the node can be replaced
  34. if minetest.registered_nodes[nn] and
  35. minetest.registered_nodes[nn].buildable_to then
  36. nn = minetest.get_node({x=x,y=ground_y,z=z}).name
  37. -- If desert sand, add dry shrub
  38. if nn == "default:dirt_with_grass" then
  39. minetest.swap_node(p,{name="cavestuff:pebble_"..pr:next(1,2), param2=math.random(0,3)})
  40. elseif nn == "default:desert_sand" then
  41. minetest.swap_node(p,{name="cavestuff:desert_pebble_"..pr:next(1,2), param2=math.random(0,3)})
  42. end
  43. end
  44. end
  45. end
  46. end
  47. end
  48. end
  49. end)