977aac7db9d49aca1523dad0407b9c4bd8686996
[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, chunk_size, 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 inverse_chance = math.floor(chunk_size*chunk_size*chunk_size / ore_per_chunk)
42         --print("generate_ore num_chunks: "..dump(num_chunks))
43         for i=1,num_chunks do
44                 local y0 = pr:next(y_min, y_max-chunk_size+1)
45                 if y0 >= height_min and y0 <= height_max then
46                         local x0 = pr:next(minp.x, maxp.x-chunk_size+1)
47                         local z0 = pr:next(minp.z, maxp.z-chunk_size+1)
48                         local p0 = {x=x0, y=y0, z=z0}
49                         for x1=0,chunk_size-1 do
50                         for y1=0,chunk_size-1 do
51                         for z1=0,chunk_size-1 do
52                                 if pr:next(1,inverse_chance) == 1 then
53                                         local x2 = x0+x1
54                                         local y2 = y0+y1
55                                         local z2 = z0+z1
56                                         local p2 = {x=x2, y=y2, z=z2}
57                                         if minetest.env:get_node(p2).name == wherein then
58                                                 minetest.env:set_node(p2, {name=name})
59                                         end
60                                 end
61                         end
62                         end
63                         end
64                 end
65         end
66         --print("generate_ore done")
67 end
68
69 function default.make_papyrus(pos, size)
70         for y=0,size-1 do
71                 local p = {x=pos.x, y=pos.y+y, z=pos.z}
72                 local nn = minetest.env:get_node(p).name
73                 if minetest.registered_nodes[nn] and
74                         minetest.registered_nodes[nn].buildable_to then
75                         minetest.env:set_node(p, {name="default:papyrus"})
76                 else
77                         return
78                 end
79         end
80 end
81
82 function default.make_cactus(pos, size)
83         for y=0,size-1 do
84                 local p = {x=pos.x, y=pos.y+y, z=pos.z}
85                 local nn = minetest.env:get_node(p).name
86                 if minetest.registered_nodes[nn] and
87                         minetest.registered_nodes[nn].buildable_to then
88                         minetest.env:set_node(p, {name="default:cactus"})
89                 else
90                         return
91                 end
92         end
93 end
94
95 -- facedir: 0/1/2/3 (head node facedir value)
96 -- length: length of rainbow tail
97 function default.make_nyancat(pos, facedir, length)
98         local tailvec = {x=0, y=0, z=0}
99         if facedir == 0 then
100                 tailvec.z = 1
101         elseif facedir == 1 then
102                 tailvec.x = 1
103         elseif facedir == 2 then
104                 tailvec.z = -1
105         elseif facedir == 3 then
106                 tailvec.x = -1
107         else
108                 print("default.make_nyancat(): Invalid facedir: "+dump(facedir))
109                 facedir = 0
110                 tailvec.z = 1
111         end
112         local p = {x=pos.x, y=pos.y, z=pos.z}
113         minetest.env:set_node(p, {name="default:nyancat", param2=facedir})
114         for i=1,length do
115                 p.x = p.x + tailvec.x
116                 p.z = p.z + tailvec.z
117                 minetest.env:set_node(p, {name="default:nyancat_rainbow"})
118         end
119 end
120
121 function generate_nyancats(seed, minp, maxp)
122         local height_min = -31000
123         local height_max = -32
124         if maxp.y < height_min or minp.y > height_max then
125                 return
126         end
127         local y_min = math.max(minp.y, height_min)
128         local y_max = math.min(maxp.y, height_max)
129         local volume = (maxp.x-minp.x+1)*(y_max-y_min+1)*(maxp.z-minp.z+1)
130         local pr = PseudoRandom(seed + 9324342)
131         local max_num_nyancats = math.floor(volume / (16*16*16))
132         for i=1,max_num_nyancats do
133                 if pr:next(0, 1000) == 0 then
134                         local x0 = pr:next(minp.x, maxp.x)
135                         local y0 = pr:next(minp.y, maxp.y)
136                         local z0 = pr:next(minp.z, maxp.z)
137                         local p0 = {x=x0, y=y0, z=z0}
138                         default.make_nyancat(p0, pr:next(0,3), pr:next(3,15))
139                 end
140         end
141 end
142
143 minetest.register_on_generated(function(minp, maxp, seed)
144         -- Generate regular ores
145         generate_ore("default:stone_with_coal", "default:stone", minp, maxp, seed+0, 1/8/8/8,    3, 8, -31000,  64)
146         generate_ore("default:stone_with_iron", "default:stone", minp, maxp, seed+1, 1/12/12/12, 2, 3,    -15,   2)
147         generate_ore("default:stone_with_iron", "default:stone", minp, maxp, seed+2, 1/9/9/9,    3, 5,    -63, -16)
148         generate_ore("default:stone_with_iron", "default:stone", minp, maxp, seed+3, 1/7/7/7,    3, 5, -31000, -64)
149         
150         generate_ore("default:stone_with_mese", "default:stone", minp, maxp, seed+4, 1/16/16/16, 2, 3,   -127,  -64)
151         generate_ore("default:stone_with_mese", "default:stone", minp, maxp, seed+5, 1/9/9/9,    3, 5, -31000, -128)
152         generate_ore("default:mese",            "default:stone", minp, maxp, seed+8, 1/16/16/16, 2, 3, -31000,-1024)
153         
154         generate_ore("default:stone_with_coal", "default:stone", minp, maxp, seed+7, 1/24/24/24, 6,27, -31000,  0)
155         generate_ore("default:stone_with_iron", "default:stone", minp, maxp, seed+6, 1/24/24/24, 6,27, -31000, -64)
156
157         if maxp.y >= 2 and minp.y <= 0 then
158                 -- Generate clay
159                 -- Assume X and Z lengths are equal
160                 local divlen = 4
161                 local divs = (maxp.x-minp.x)/divlen+1;
162                 for divx=0+1,divs-1-1 do
163                 for divz=0+1,divs-1-1 do
164                         local cx = minp.x + math.floor((divx+0.5)*divlen)
165                         local cz = minp.z + math.floor((divz+0.5)*divlen)
166                         if minetest.env:get_node({x=cx,y=1,z=cz}).name == "default:water_source" and
167                                         minetest.env:get_node({x=cx,y=0,z=cz}).name == "default:sand" then
168                                 local is_shallow = true
169                                 local num_water_around = 0
170                                 if minetest.env:get_node({x=cx-divlen*2,y=1,z=cz+0}).name == "default:water_source" then
171                                         num_water_around = num_water_around + 1 end
172                                 if minetest.env:get_node({x=cx+divlen*2,y=1,z=cz+0}).name == "default:water_source" then
173                                         num_water_around = num_water_around + 1 end
174                                 if minetest.env:get_node({x=cx+0,y=1,z=cz-divlen*2}).name == "default:water_source" then
175                                         num_water_around = num_water_around + 1 end
176                                 if minetest.env:get_node({x=cx+0,y=1,z=cz+divlen*2}).name == "default:water_source" then
177                                         num_water_around = num_water_around + 1 end
178                                 if num_water_around >= 2 then
179                                         is_shallow = false
180                                 end     
181                                 if is_shallow then
182                                         for x1=-divlen,divlen do
183                                         for z1=-divlen,divlen do
184                                                 if minetest.env:get_node({x=cx+x1,y=0,z=cz+z1}).name == "default:sand" then
185                                                         minetest.env:set_node({x=cx+x1,y=0,z=cz+z1}, {name="default:clay"})
186                                                 end
187                                         end
188                                         end
189                                 end
190                         end
191                 end
192                 end
193                 -- Generate papyrus
194                 local perlin1 = minetest.env:get_perlin(354, 3, 0.7, 100)
195                 -- Assume X and Z lengths are equal
196                 local divlen = 8
197                 local divs = (maxp.x-minp.x)/divlen+1;
198                 for divx=0,divs-1 do
199                 for divz=0,divs-1 do
200                         local x0 = minp.x + math.floor((divx+0)*divlen)
201                         local z0 = minp.z + math.floor((divz+0)*divlen)
202                         local x1 = minp.x + math.floor((divx+1)*divlen)
203                         local z1 = minp.z + math.floor((divz+1)*divlen)
204                         -- Determine papyrus amount from perlin noise
205                         local papyrus_amount = math.floor(perlin1:get2d({x=x0, y=z0}) * 45 - 20)
206                         -- Find random positions for papyrus based on this random
207                         local pr = PseudoRandom(seed+1)
208                         for i=0,papyrus_amount do
209                                 local x = pr:next(x0, x1)
210                                 local z = pr:next(z0, z1)
211                                 if minetest.env:get_node({x=x,y=1,z=z}).name == "default:dirt_with_grass" and
212                                                 minetest.env:find_node_near({x=x,y=1,z=z}, 1, "default:water_source") then
213                                         default.make_papyrus({x=x,y=2,z=z}, pr:next(2, 4))
214                                 end
215                         end
216                 end
217                 end
218                 -- Generate cactuses
219                 local perlin1 = minetest.env:get_perlin(230, 3, 0.6, 100)
220                 -- Assume X and Z lengths are equal
221                 local divlen = 16
222                 local divs = (maxp.x-minp.x)/divlen+1;
223                 for divx=0,divs-1 do
224                 for divz=0,divs-1 do
225                         local x0 = minp.x + math.floor((divx+0)*divlen)
226                         local z0 = minp.z + math.floor((divz+0)*divlen)
227                         local x1 = minp.x + math.floor((divx+1)*divlen)
228                         local z1 = minp.z + math.floor((divz+1)*divlen)
229                         -- Determine cactus amount from perlin noise
230                         local cactus_amount = math.floor(perlin1:get2d({x=x0, y=z0}) * 6 - 3)
231                         -- Find random positions for cactus based on this random
232                         local pr = PseudoRandom(seed+1)
233                         for i=0,cactus_amount do
234                                 local x = pr:next(x0, x1)
235                                 local z = pr:next(z0, z1)
236                                 -- Find ground level (0...15)
237                                 local ground_y = nil
238                                 for y=30,0,-1 do
239                                         if minetest.env:get_node({x=x,y=y,z=z}).name ~= "air" then
240                                                 ground_y = y
241                                                 break
242                                         end
243                                 end
244                                 -- If desert sand, make cactus
245                                 if ground_y and minetest.env:get_node({x=x,y=ground_y,z=z}).name == "default:desert_sand" then
246                                         default.make_cactus({x=x,y=ground_y+1,z=z}, pr:next(3, 4))
247                                 end
248                         end
249                 end
250                 end
251                 -- Generate dry shrubs
252                 local perlin1 = minetest.env:get_perlin(329, 3, 0.6, 100)
253                 -- Assume X and Z lengths are equal
254                 local divlen = 16
255                 local divs = (maxp.x-minp.x)/divlen+1;
256                 for divx=0,divs-1 do
257                 for divz=0,divs-1 do
258                         local x0 = minp.x + math.floor((divx+0)*divlen)
259                         local z0 = minp.z + math.floor((divz+0)*divlen)
260                         local x1 = minp.x + math.floor((divx+1)*divlen)
261                         local z1 = minp.z + math.floor((divz+1)*divlen)
262                         -- Determine dry shrubs amount from perlin noise
263                         local shrub_amount = math.floor(perlin1:get2d({x=x0, y=z0}) * 5 + 0)
264                         -- Find random positions for dry shrubs based on this random
265                         local pr = PseudoRandom(seed+1)
266                         for i=0,shrub_amount do
267                                 local x = pr:next(x0, x1)
268                                 local z = pr:next(z0, z1)
269                                 -- Find ground level (0...15)
270                                 local ground_y = nil
271                                 for y=30,0,-1 do
272                                         if minetest.env:get_node({x=x,y=y,z=z}).name ~= "air" then
273                                                 ground_y = y
274                                                 break
275                                         end
276                                 end
277                                 -- If desert sand, make dry shrub
278                                 if ground_y and minetest.env:get_node({x=x,y=ground_y,z=z}).name == "default:desert_sand" then
279                                         local p = {x=x,y=ground_y+1,z=z}
280                                         local nn = minetest.env:get_node(p).name
281                                         if minetest.registered_nodes[nn] and
282                                                 minetest.registered_nodes[nn].buildable_to then
283                                                 minetest.env:set_node(p, {name="default:dry_shrub"})
284                                         end
285                                 end
286                         end
287                 end
288                 end
289         end
290
291         -- Generate nyan cats
292         generate_nyancats(seed, minp, maxp)
293 end)
294