c6e7d24e09e1d424bae8a5018e6184f2f8f17ef9
[oweals/minetest_game.git] / mods / default / mapgen.lua
1 -- minetest/default/mapgen.lua
2
3 --
4 -- Aliases for map generator outputs
5 --
6
7 minetest.register_alias("mapgen_air", "air")
8 minetest.register_alias("mapgen_stone", "default:stone")
9 minetest.register_alias("mapgen_tree", "default:tree")
10 minetest.register_alias("mapgen_leaves", "default:leaves")
11 minetest.register_alias("mapgen_apple", "default:apple")
12 minetest.register_alias("mapgen_water_source", "default:water_source")
13 minetest.register_alias("mapgen_dirt", "default:dirt")
14 minetest.register_alias("mapgen_sand", "default:sand")
15 minetest.register_alias("mapgen_gravel", "default:gravel")
16 minetest.register_alias("mapgen_clay", "default:clay")
17 minetest.register_alias("mapgen_lava_source", "default:lava_source")
18 minetest.register_alias("mapgen_cobble", "default:cobble")
19 minetest.register_alias("mapgen_mossycobble", "default:mossycobble")
20 minetest.register_alias("mapgen_dirt_with_grass", "default:dirt_with_grass")
21 minetest.register_alias("mapgen_junglegrass", "default:junglegrass")
22 minetest.register_alias("mapgen_stone_with_coal", "default:stone_with_coal")
23 minetest.register_alias("mapgen_stone_with_iron", "default:stone_with_iron")
24 minetest.register_alias("mapgen_mese", "default:mese")
25
26 --
27 -- Ore generation
28 --
29
30 local function generate_ore(name, wherein, minp, maxp, seed, chunks_per_volume, ore_per_chunk, height_min, height_max)
31         if maxp.y < height_min or minp.y > height_max then
32                 return
33         end
34         local y_min = math.max(minp.y, height_min)
35         local y_max = math.min(maxp.y, height_max)
36         local volume = (maxp.x-minp.x+1)*(y_max-y_min+1)*(maxp.z-minp.z+1)
37         local pr = PseudoRandom(seed)
38         local num_chunks = math.floor(chunks_per_volume * volume)
39         local chunk_size = 3
40         if ore_per_chunk <= 4 then
41                 chunk_size = 2
42         end
43         local inverse_chance = math.floor(chunk_size*chunk_size*chunk_size / ore_per_chunk)
44         --print("generate_ore num_chunks: "..dump(num_chunks))
45         for i=1,num_chunks do
46                 local y0 = pr:next(y_min, y_max-chunk_size+1)
47                 if y0 >= height_min and y0 <= height_max then
48                         local x0 = pr:next(minp.x, maxp.x-chunk_size+1)
49                         local z0 = pr:next(minp.z, maxp.z-chunk_size+1)
50                         local p0 = {x=x0, y=y0, z=z0}
51                         for x1=0,chunk_size-1 do
52                         for y1=0,chunk_size-1 do
53                         for z1=0,chunk_size-1 do
54                                 if pr:next(1,inverse_chance) == 1 then
55                                         local x2 = x0+x1
56                                         local y2 = y0+y1
57                                         local z2 = z0+z1
58                                         local p2 = {x=x2, y=y2, z=z2}
59                                         if minetest.env:get_node(p2).name == wherein then
60                                                 minetest.env:set_node(p2, {name=name})
61                                         end
62                                 end
63                         end
64                         end
65                         end
66                 end
67         end
68         --print("generate_ore done")
69 end
70
71 local function make_papyrus(pos, size)
72         for y=0,size-1 do
73                 local p = {x=pos.x, y=pos.y+y, z=pos.z}
74                 minetest.env:set_node(p, {name="default:papyrus"})
75         end
76 end
77
78 minetest.register_on_generated(function(minp, maxp, seed)
79         generate_ore("default:stone_with_coal", "default:stone", minp, maxp, seed,   1/8/8/8,    5, -31000,  64)
80         generate_ore("default:stone_with_iron", "default:stone", minp, maxp, seed+1, 1/16/16/16, 5,   -5,   7)
81         generate_ore("default:stone_with_iron", "default:stone", minp, maxp, seed+2, 1/12/12/12, 5, -16,   -5)
82         generate_ore("default:stone_with_iron", "default:stone", minp, maxp, seed+3, 1/9/9/9,    5, -31000, -17)
83         if maxp.y >= 2 and minp.y <= 0 then
84                 -- Generate clay
85                 -- Assume X and Z lengths are equal
86                 local divlen = 4
87                 local divs = (maxp.x-minp.x)/divlen+1;
88                 for divx=0+1,divs-1-1 do
89                 for divz=0+1,divs-1-1 do
90                         local cx = minp.x + math.floor((divx+0.5)*divlen)
91                         local cz = minp.z + math.floor((divz+0.5)*divlen)
92                         if minetest.env:get_node({x=cx,y=1,z=cz}).name == "default:water_source" and
93                                         minetest.env:get_node({x=cx,y=0,z=cz}).name == "default:sand" then
94                                 local is_shallow = true
95                                 local num_water_around = 0
96                                 if minetest.env:get_node({x=cx-divlen*2,y=1,z=cz+0}).name == "default:water_source" then
97                                         num_water_around = num_water_around + 1 end
98                                 if minetest.env:get_node({x=cx+divlen*2,y=1,z=cz+0}).name == "default:water_source" then
99                                         num_water_around = num_water_around + 1 end
100                                 if minetest.env:get_node({x=cx+0,y=1,z=cz-divlen*2}).name == "default:water_source" then
101                                         num_water_around = num_water_around + 1 end
102                                 if minetest.env:get_node({x=cx+0,y=1,z=cz+divlen*2}).name == "default:water_source" then
103                                         num_water_around = num_water_around + 1 end
104                                 if num_water_around >= 2 then
105                                         is_shallow = false
106                                 end     
107                                 if is_shallow then
108                                         for x1=-divlen,divlen do
109                                         for z1=-divlen,divlen do
110                                                 if minetest.env:get_node({x=cx+x1,y=0,z=cz+z1}).name == "default:sand" then
111                                                         minetest.env:set_node({x=cx+x1,y=0,z=cz+z1}, {name="default:clay"})
112                                                 end
113                                         end
114                                         end
115                                 end
116                         end
117                 end
118                 end
119                 -- Generate papyrus
120                 local perlin1 = minetest.env:get_perlin(354, 3, 0.7, 100)
121                 -- Assume X and Z lengths are equal
122                 local divlen = 8
123                 local divs = (maxp.x-minp.x)/divlen+1;
124                 for divx=0,divs-1 do
125                 for divz=0,divs-1 do
126                         local x0 = minp.x + math.floor((divx+0)*divlen)
127                         local z0 = minp.z + math.floor((divz+0)*divlen)
128                         local x1 = minp.x + math.floor((divx+1)*divlen)
129                         local z1 = minp.z + math.floor((divz+1)*divlen)
130                         -- Determine papyrus amount from perlin noise
131                         local papyrus_amount = math.floor(perlin1:get2d({x=x0, y=z0}) * 45 - 20)
132                         -- Find random positions for papyrus based on this random
133                         local pr = PseudoRandom(seed+1)
134                         for i=0,papyrus_amount do
135                                 local x = pr:next(x0, x1)
136                                 local z = pr:next(z0, z1)
137                                 if minetest.env:get_node({x=x,y=1,z=z}).name == "default:dirt_with_grass" and
138                                                 minetest.env:find_node_near({x=x,y=1,z=z}, 1, "default:water_source") then
139                                         make_papyrus({x=x,y=2,z=z}, pr:next(2, 4))
140                                 end
141                         end
142                 end
143                 end
144         end
145 end)
146