Disable treespawning on mapgen
[minetest_hades.git] / mods / flowers / mapgen.lua
blob7148f6e547e2e69269592542e0e749520c65dab4
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:get2d({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
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 nn == "default:dirt_with_grass" then
38 local flower_choice = pr:next(1, 6)
39 local flower
40 if flower_choice == 1 then
41 flower = "flowers:tulip"
42 elseif flower_choice == 2 then
43 flower = "flowers:rose"
44 elseif flower_choice == 3 then
45 flower = "flowers:dandelion_yellow"
46 elseif flower_choice == 4 then
47 flower = "flowers:dandelion_white"
48 elseif flower_choice == 5 then
49 flower = "flowers:geranium"
50 elseif flower_choice == 6 then
51 flower = "flowers:viola"
52 end
53 minetest.set_node(p, {name=flower})
54 end
55 end
56 end
58 end
59 end
60 end
61 end
62 end)