gen.lua 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. if factory.fertilizerGeneration then
  2. minetest.register_on_generated(function(minp, maxp, seed)
  3. if maxp.y >= 2 and minp.y <=0 then
  4. -- Generate fertilizer
  5. local perlin1 = minetest.get_perlin(576, 3, 0.6, 100)
  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*divlen)
  11. local z0 = minp.z + math.floor(divz*divlen)
  12. local x1 = minp.x + math.floor((divx+1)*divlen)
  13. local z1 = minp.z + math.floor((divz+1)*divlen)
  14. local grass_amount = math.floor(perlin1:get2d({x=x0, y=z0}) ^ 2)
  15. local pr = PseudoRandom(seed+249)
  16. for i=0,grass_amount do
  17. local x = pr:next(x0, x1)
  18. local z = pr:next(z0, z1)
  19. local ground_y = nil
  20. for y=30,0,-1 do
  21. if minetest.get_node({x=x,y=y,z=z}).name ~= "air" then
  22. ground_y = y
  23. break
  24. end
  25. end
  26. if ground_y then
  27. local p = {x=x, y=ground_y+1, z=z}
  28. local nn = minetest.get_node(p).name
  29. if minetest.registered_nodes[nn] and
  30. minetest.registered_nodes[nn].buildable_to then
  31. nn = minetest.get_node({x=x,y=ground_y,z=z}).name
  32. if nn == "default:dirt_with_grass" then
  33. minetest.set_node(p, {name="factory:sapling_fertilizer"})
  34. end
  35. end
  36. end
  37. end
  38. end
  39. end
  40. end
  41. end)
  42. end