e6e08c79406a0ea323f034865b0e84db15c13059
[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 minetest.register_alias("mapgen_desert_sand", "default:desert_sand")
26 minetest.register_alias("mapgen_desert_stone", "default:desert_stone")
27
28 --
29 -- Ore generation
30 --
31
32 local function generate_ore(name, wherein, minp, maxp, seed, chunks_per_volume, ore_per_chunk, height_min, height_max)
33         if maxp.y < height_min or minp.y > height_max then
34                 return
35         end
36         local y_min = math.max(minp.y, height_min)
37         local y_max = math.min(maxp.y, height_max)
38         local volume = (maxp.x-minp.x+1)*(y_max-y_min+1)*(maxp.z-minp.z+1)
39         local pr = PseudoRandom(seed)
40         local num_chunks = math.floor(chunks_per_volume * volume)
41         local chunk_size = 3
42         if ore_per_chunk <= 4 then
43                 chunk_size = 2
44         end
45         local inverse_chance = math.floor(chunk_size*chunk_size*chunk_size / ore_per_chunk)
46         --print("generate_ore num_chunks: "..dump(num_chunks))
47         for i=1,num_chunks do
48                 local y0 = pr:next(y_min, y_max-chunk_size+1)
49                 if y0 >= height_min and y0 <= height_max then
50                         local x0 = pr:next(minp.x, maxp.x-chunk_size+1)
51                         local z0 = pr:next(minp.z, maxp.z-chunk_size+1)
52                         local p0 = {x=x0, y=y0, z=z0}
53                         for x1=0,chunk_size-1 do
54                         for y1=0,chunk_size-1 do
55                         for z1=0,chunk_size-1 do
56                                 if pr:next(1,inverse_chance) == 1 then
57                                         local x2 = x0+x1
58                                         local y2 = y0+y1
59                                         local z2 = z0+z1
60                                         local p2 = {x=x2, y=y2, z=z2}
61                                         if minetest.env:get_node(p2).name == wherein then
62                                                 minetest.env:set_node(p2, {name=name})
63                                         end
64                                 end
65                         end
66                         end
67                         end
68                 end
69         end
70         --print("generate_ore done")
71 end
72
73 function default.make_papyrus(pos, size)
74         for y=0,size-1 do
75                 local p = {x=pos.x, y=pos.y+y, z=pos.z}
76                 minetest.env:set_node(p, {name="default:papyrus"})
77         end
78 end
79
80 function default.make_cactus(pos, size)
81         for y=0,size-1 do
82                 local p = {x=pos.x, y=pos.y+y, z=pos.z}
83                 minetest.env:set_node(p, {name="default:cactus"})
84         end
85 end
86
87 minetest.register_on_generated(function(minp, maxp, seed)
88         generate_ore("default:stone_with_coal", "default:stone", minp, maxp, seed,   1/8/8/8,    5, -31000,  64)
89         generate_ore("default:stone_with_iron", "default:stone", minp, maxp, seed+1, 1/16/16/16, 5,   -5,   7)
90         generate_ore("default:stone_with_iron", "default:stone", minp, maxp, seed+2, 1/12/12/12, 5, -16,   -5)
91         generate_ore("default:stone_with_iron", "default:stone", minp, maxp, seed+3, 1/9/9/9,    5, -31000, -17)
92         if maxp.y >= 2 and minp.y <= 0 then
93                 -- Generate clay
94                 -- Assume X and Z lengths are equal
95                 local divlen = 4
96                 local divs = (maxp.x-minp.x)/divlen+1;
97                 for divx=0+1,divs-1-1 do
98                 for divz=0+1,divs-1-1 do
99                         local cx = minp.x + math.floor((divx+0.5)*divlen)
100                         local cz = minp.z + math.floor((divz+0.5)*divlen)
101                         if minetest.env:get_node({x=cx,y=1,z=cz}).name == "default:water_source" and
102                                         minetest.env:get_node({x=cx,y=0,z=cz}).name == "default:sand" then
103                                 local is_shallow = true
104                                 local num_water_around = 0
105                                 if minetest.env:get_node({x=cx-divlen*2,y=1,z=cz+0}).name == "default:water_source" then
106                                         num_water_around = num_water_around + 1 end
107                                 if minetest.env:get_node({x=cx+divlen*2,y=1,z=cz+0}).name == "default:water_source" then
108                                         num_water_around = num_water_around + 1 end
109                                 if minetest.env:get_node({x=cx+0,y=1,z=cz-divlen*2}).name == "default:water_source" then
110                                         num_water_around = num_water_around + 1 end
111                                 if minetest.env:get_node({x=cx+0,y=1,z=cz+divlen*2}).name == "default:water_source" then
112                                         num_water_around = num_water_around + 1 end
113                                 if num_water_around >= 2 then
114                                         is_shallow = false
115                                 end     
116                                 if is_shallow then
117                                         for x1=-divlen,divlen do
118                                         for z1=-divlen,divlen do
119                                                 if minetest.env:get_node({x=cx+x1,y=0,z=cz+z1}).name == "default:sand" then
120                                                         minetest.env:set_node({x=cx+x1,y=0,z=cz+z1}, {name="default:clay"})
121                                                 end
122                                         end
123                                         end
124                                 end
125                         end
126                 end
127                 end
128                 -- Generate papyrus
129                 local perlin1 = minetest.env:get_perlin(354, 3, 0.7, 100)
130                 -- Assume X and Z lengths are equal
131                 local divlen = 8
132                 local divs = (maxp.x-minp.x)/divlen+1;
133                 for divx=0,divs-1 do
134                 for divz=0,divs-1 do
135                         local x0 = minp.x + math.floor((divx+0)*divlen)
136                         local z0 = minp.z + math.floor((divz+0)*divlen)
137                         local x1 = minp.x + math.floor((divx+1)*divlen)
138                         local z1 = minp.z + math.floor((divz+1)*divlen)
139                         -- Determine papyrus amount from perlin noise
140                         local papyrus_amount = math.floor(perlin1:get2d({x=x0, y=z0}) * 45 - 20)
141                         -- Find random positions for papyrus based on this random
142                         local pr = PseudoRandom(seed+1)
143                         for i=0,papyrus_amount do
144                                 local x = pr:next(x0, x1)
145                                 local z = pr:next(z0, z1)
146                                 if minetest.env:get_node({x=x,y=1,z=z}).name == "default:dirt_with_grass" and
147                                                 minetest.env:find_node_near({x=x,y=1,z=z}, 1, "default:water_source") then
148                                         default.make_papyrus({x=x,y=2,z=z}, pr:next(2, 4))
149                                 end
150                         end
151                 end
152                 end
153                 -- Generate cactuses
154                 local perlin1 = minetest.env:get_perlin(230, 3, 0.7, 100)
155                 -- Assume X and Z lengths are equal
156                 local divlen = 8
157                 local divs = (maxp.x-minp.x)/divlen+1;
158                 for divx=0,divs-1 do
159                 for divz=0,divs-1 do
160                         local x0 = minp.x + math.floor((divx+0)*divlen)
161                         local z0 = minp.z + math.floor((divz+0)*divlen)
162                         local x1 = minp.x + math.floor((divx+1)*divlen)
163                         local z1 = minp.z + math.floor((divz+1)*divlen)
164                         -- Determine cactus amount from perlin noise
165                         local cactus_amount = math.floor(perlin1:get2d({x=x0, y=z0}) * 6 - 5)
166                         -- Find random positions for cactus based on this random
167                         local pr = PseudoRandom(seed+1)
168                         for i=0,cactus_amount do
169                                 local x = pr:next(x0, x1)
170                                 local z = pr:next(z0, z1)
171                                 -- Find ground level (0...15)
172                                 local ground_y = nil
173                                 for y=30,0,-1 do
174                                         if minetest.env:get_node({x=x,y=y,z=z}).name ~= "air" then
175                                                 ground_y = y
176                                                 break
177                                         end
178                                 end
179                                 -- If desert sand, make cactus
180                                 if ground_y and minetest.env:get_node({x=x,y=ground_y,z=z}).name == "default:desert_sand" then
181                                         default.make_cactus({x=x,y=ground_y+1,z=z}, pr:next(3, 4))
182                                 end
183                         end
184                 end
185                 end
186         end
187 end)
188