bbdd86719be54be1d440a90ccc4ac1b0b524f60b
[oweals/minetest_game.git] / mods / default / mapgen.lua
1 -- mods/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_jungletree", "default:jungletree")
12 minetest.register_alias("mapgen_jungleleaves", "default:jungleleaves")
13 minetest.register_alias("mapgen_apple", "default:apple")
14 minetest.register_alias("mapgen_water_source", "default:water_source")
15 minetest.register_alias("mapgen_dirt", "default:dirt")
16 minetest.register_alias("mapgen_sand", "default:sand")
17 minetest.register_alias("mapgen_gravel", "default:gravel")
18 minetest.register_alias("mapgen_clay", "default:clay")
19 minetest.register_alias("mapgen_lava_source", "default:lava_source")
20 minetest.register_alias("mapgen_cobble", "default:cobble")
21 minetest.register_alias("mapgen_mossycobble", "default:mossycobble")
22 minetest.register_alias("mapgen_dirt_with_grass", "default:dirt_with_grass")
23 minetest.register_alias("mapgen_junglegrass", "default:junglegrass")
24 minetest.register_alias("mapgen_stone_with_coal", "default:stone_with_coal")
25 minetest.register_alias("mapgen_stone_with_iron", "default:stone_with_iron")
26 minetest.register_alias("mapgen_mese", "default:mese")
27 minetest.register_alias("mapgen_desert_sand", "default:desert_sand")
28 minetest.register_alias("mapgen_desert_stone", "default:desert_stone")
29
30 --
31 -- Ore generation
32 --
33
34 minetest.register_ore({
35         ore_type       = "scatter",
36         ore            = "default:stone_with_coal",
37         wherein        = "default:stone",
38         clust_scarcity = 8*8*8,
39         clust_num_ores = 8,
40         clust_size     = 3,
41         height_min     = -31000,
42         height_max     = 64,
43 })
44
45 minetest.register_ore({
46         ore_type       = "scatter",
47         ore            = "default:stone_with_coal",
48         wherein        = "default:stone",
49         clust_scarcity = 24*24*24,
50         clust_num_ores = 27,
51         clust_size     = 6,
52         height_min     = -31000,
53         height_max     = 0,
54         flags          = "absheight",
55 })
56
57 minetest.register_ore({
58         ore_type       = "scatter",
59         ore            = "default:stone_with_iron",
60         wherein        = "default:stone",
61         clust_scarcity = 12*12*12,
62         clust_num_ores = 3,
63         clust_size     = 2,
64         height_min     = -15,
65         height_max     = 2,
66 })
67
68 minetest.register_ore({
69         ore_type       = "scatter",
70         ore            = "default:stone_with_iron",
71         wherein        = "default:stone",
72         clust_scarcity = 9*9*9,
73         clust_num_ores = 5,
74         clust_size     = 3,
75         height_min     = -63,
76         height_max     = -16,
77 })
78
79 minetest.register_ore({
80         ore_type       = "scatter",
81         ore            = "default:stone_with_iron",
82         wherein        = "default:stone",
83         clust_scarcity = 7*7*7,
84         clust_num_ores = 5,
85         clust_size     = 3,
86         height_min     = -31000,
87         height_max     = -64,
88         flags          = "absheight",
89 })
90
91 minetest.register_ore({
92         ore_type       = "scatter",
93         ore            = "default:stone_with_iron",
94         wherein        = "default:stone",
95         clust_scarcity = 24*24*24,
96         clust_num_ores = 27,
97         clust_size     = 6,
98         height_min     = -31000,
99         height_max     = -64,
100         flags          = "absheight",
101 })
102
103 minetest.register_ore({
104         ore_type       = "scatter",
105         ore            = "default:stone_with_mese",
106         wherein        = "default:stone",
107         clust_scarcity = 18*18*18,
108         clust_num_ores = 3,
109         clust_size     = 2,
110         height_min     = -255,
111         height_max     = -64,
112         flags          = "absheight",
113 })
114
115 minetest.register_ore({
116         ore_type       = "scatter",
117         ore            = "default:stone_with_mese",
118         wherein        = "default:stone",
119         clust_scarcity = 14*14*14,
120         clust_num_ores = 5,
121         clust_size     = 3,
122         height_min     = -31000,
123         height_max     = -256,
124         flags          = "absheight",
125 })
126
127 minetest.register_ore({
128         ore_type       = "scatter",
129         ore            = "default:mese",
130         wherein        = "default:stone",
131         clust_scarcity = 36*36*36,
132         clust_num_ores = 3,
133         clust_size     = 2,
134         height_min     = -31000,
135         height_max     = -1024,
136         flags          = "absheight",
137 })
138
139 minetest.register_ore({
140         ore_type       = "scatter",
141         ore            = "default:stone_with_gold",
142         wherein        = "default:stone",
143         clust_scarcity = 15*15*15,
144         clust_num_ores = 3,
145         clust_size     = 2,
146         height_min     = -255,
147         height_max     = -64,
148         flags          = "absheight",
149 })
150
151 minetest.register_ore({
152         ore_type       = "scatter",
153         ore            = "default:stone_with_gold",
154         wherein        = "default:stone",
155         clust_scarcity = 13*13*13,
156         clust_num_ores = 5,
157         clust_size     = 3,
158         height_min     = -31000,
159         height_max     = -256,
160         flags          = "absheight",
161 })
162
163 minetest.register_ore({
164         ore_type       = "scatter",
165         ore            = "default:stone_with_diamond",
166         wherein        = "default:stone",
167         clust_scarcity = 17*17*17,
168         clust_num_ores = 4,
169         clust_size     = 3,
170         height_min     = -255,
171         height_max     = -128,
172         flags          = "absheight",
173 })
174
175 minetest.register_ore({
176         ore_type       = "scatter",
177         ore            = "default:stone_with_diamond",
178         wherein        = "default:stone",
179         clust_scarcity = 15*15*15,
180         clust_num_ores = 4,
181         clust_size     = 3,
182         height_min     = -31000,
183         height_max     = -256,
184         flags          = "absheight",
185 })
186
187 minetest.register_ore({
188         ore_type       = "scatter",
189         ore            = "default:stone_with_copper",
190         wherein        = "default:stone",
191         clust_scarcity = 12*12*12,
192         clust_num_ores = 4,
193         clust_size     = 3,
194         height_min     = -63,
195         height_max     = -16,
196 })
197
198 minetest.register_ore({
199         ore_type       = "scatter",
200         ore            = "default:stone_with_copper",
201         wherein        = "default:stone",
202         clust_scarcity = 9*9*9,
203         clust_num_ores = 5,
204         clust_size     = 3,
205         height_min     = -31000,
206         height_max     = -64,
207         flags          = "absheight",
208 })
209
210 if minetest.setting_get("mg_name") == "indev" then
211         -- Floatlands and high mountains springs
212         minetest.register_ore({
213                 ore_type       = "scatter",
214                 ore            = "default:water_source",
215                 ore_param2     = 128,
216                 wherein        = "default:stone",
217                 clust_scarcity = 40*40*40,
218                 clust_num_ores = 8,
219                 clust_size     = 3,
220                 height_min     = 100,
221                 height_max     = 31000,
222         })
223
224         minetest.register_ore({
225                 ore_type       = "scatter",
226                 ore            = "default:lava_source",
227                 ore_param2     = 128,
228                 wherein        = "default:stone",
229                 clust_scarcity = 50*50*50,
230                 clust_num_ores = 5,
231                 clust_size     = 2,
232                 height_min     = 10000,
233                 height_max     = 31000,
234         })
235
236         minetest.register_ore({
237                 ore_type       = "scatter",
238                 ore            = "default:sand",
239                 wherein        = "default:stone",
240                 clust_scarcity = 20*20*20,
241                 clust_num_ores = 5*5*3,
242                 clust_size     = 5,
243                 height_min     = 500,
244                 height_max     = 31000,
245         })
246
247         -- Underground springs
248         minetest.register_ore({
249                 ore_type       = "scatter",
250                 ore            = "default:water_source",
251                 ore_param2     = 128,
252                 wherein        = "default:stone",
253                 clust_scarcity = 25*25*25,
254                 clust_num_ores = 8,
255                 clust_size     = 3,
256                 height_min     = -10000,
257                 height_max     = -10,
258         })
259
260         minetest.register_ore({
261                 ore_type       = "scatter",
262                 ore            = "default:lava_source",
263                 ore_param2     = 128,
264                 wherein        = "default:stone",
265                 clust_scarcity = 35*35*35,
266                 clust_num_ores = 5,
267                 clust_size     = 2,
268                 height_min     = -31000,
269                 height_max     = -100,
270         })
271 end
272
273 function default.generate_ore(name, wherein, minp, maxp, seed, chunks_per_volume, chunk_size, ore_per_chunk, height_min, height_max)
274         minetest.log('action', "WARNING: default.generate_ore is deprecated")
275
276         if maxp.y < height_min or minp.y > height_max then
277                 return
278         end
279         local y_min = math.max(minp.y, height_min)
280         local y_max = math.min(maxp.y, height_max)
281         if chunk_size >= y_max - y_min + 1 then
282                 return
283         end
284         local volume = (maxp.x-minp.x+1)*(y_max-y_min+1)*(maxp.z-minp.z+1)
285         local pr = PseudoRandom(seed)
286         local num_chunks = math.floor(chunks_per_volume * volume)
287         local inverse_chance = math.floor(chunk_size*chunk_size*chunk_size / ore_per_chunk)
288         --print("generate_ore num_chunks: "..dump(num_chunks))
289         for i=1,num_chunks do
290                 local y0 = pr:next(y_min, y_max-chunk_size+1)
291                 if y0 >= height_min and y0 <= height_max then
292                         local x0 = pr:next(minp.x, maxp.x-chunk_size+1)
293                         local z0 = pr:next(minp.z, maxp.z-chunk_size+1)
294                         local p0 = {x=x0, y=y0, z=z0}
295                         for x1=0,chunk_size-1 do
296                         for y1=0,chunk_size-1 do
297                         for z1=0,chunk_size-1 do
298                                 if pr:next(1,inverse_chance) == 1 then
299                                         local x2 = x0+x1
300                                         local y2 = y0+y1
301                                         local z2 = z0+z1
302                                         local p2 = {x=x2, y=y2, z=z2}
303                                         if minetest.get_node(p2).name == wherein then
304                                                 minetest.set_node(p2, {name=name})
305                                         end
306                                 end
307                         end
308                         end
309                         end
310                 end
311         end
312         --print("generate_ore done")
313 end
314
315 function default.make_papyrus(pos, size)
316         for y=0,size-1 do
317                 local p = {x=pos.x, y=pos.y+y, z=pos.z}
318                 local nn = minetest.get_node(p).name
319                 if minetest.registered_nodes[nn] and
320                         minetest.registered_nodes[nn].buildable_to then
321                         minetest.set_node(p, {name="default:papyrus"})
322                 else
323                         return
324                 end
325         end
326 end
327
328 function default.make_cactus(pos, size)
329         for y=0,size-1 do
330                 local p = {x=pos.x, y=pos.y+y, z=pos.z}
331                 local nn = minetest.get_node(p).name
332                 if minetest.registered_nodes[nn] and
333                         minetest.registered_nodes[nn].buildable_to then
334                         minetest.set_node(p, {name="default:cactus"})
335                 else
336                         return
337                 end
338         end
339 end
340
341 -- facedir: 0/1/2/3 (head node facedir value)
342 -- length: length of rainbow tail
343 function default.make_nyancat(pos, facedir, length)
344         local tailvec = {x=0, y=0, z=0}
345         if facedir == 0 then
346                 tailvec.z = 1
347         elseif facedir == 1 then
348                 tailvec.x = 1
349         elseif facedir == 2 then
350                 tailvec.z = -1
351         elseif facedir == 3 then
352                 tailvec.x = -1
353         else
354                 print("default.make_nyancat(): Invalid facedir: "+dump(facedir))
355                 facedir = 0
356                 tailvec.z = 1
357         end
358         local p = {x=pos.x, y=pos.y, z=pos.z}
359         minetest.set_node(p, {name="default:nyancat", param2=facedir})
360         for i=1,length do
361                 p.x = p.x + tailvec.x
362                 p.z = p.z + tailvec.z
363                 minetest.set_node(p, {name="default:nyancat_rainbow"})
364         end
365 end
366
367 function generate_nyancats(seed, minp, maxp)
368         local height_min = -31000
369         local height_max = -32
370         if maxp.y < height_min or minp.y > height_max then
371                 return
372         end
373         local y_min = math.max(minp.y, height_min)
374         local y_max = math.min(maxp.y, height_max)
375         local volume = (maxp.x-minp.x+1)*(y_max-y_min+1)*(maxp.z-minp.z+1)
376         local pr = PseudoRandom(seed + 9324342)
377         local max_num_nyancats = math.floor(volume / (16*16*16))
378         for i=1,max_num_nyancats do
379                 if pr:next(0, 1000) == 0 then
380                         local x0 = pr:next(minp.x, maxp.x)
381                         local y0 = pr:next(minp.y, maxp.y)
382                         local z0 = pr:next(minp.z, maxp.z)
383                         local p0 = {x=x0, y=y0, z=z0}
384                         default.make_nyancat(p0, pr:next(0,3), pr:next(3,15))
385                 end
386         end
387 end
388
389 minetest.register_on_generated(function(minp, maxp, seed)
390         if maxp.y >= 2 and minp.y <= 0 then
391                 -- Generate clay
392                 -- Assume X and Z lengths are equal
393                 local divlen = 4
394                 local divs = (maxp.x-minp.x)/divlen+1;
395                 for divx=0+1,divs-1-1 do
396                 for divz=0+1,divs-1-1 do
397                         local cx = minp.x + math.floor((divx+0.5)*divlen)
398                         local cz = minp.z + math.floor((divz+0.5)*divlen)
399                         if minetest.get_node({x=cx,y=1,z=cz}).name == "default:water_source" and
400                                         minetest.get_node({x=cx,y=0,z=cz}).name == "default:sand" then
401                                 local is_shallow = true
402                                 local num_water_around = 0
403                                 if minetest.get_node({x=cx-divlen*2,y=1,z=cz+0}).name == "default:water_source" then
404                                         num_water_around = num_water_around + 1 end
405                                 if minetest.get_node({x=cx+divlen*2,y=1,z=cz+0}).name == "default:water_source" then
406                                         num_water_around = num_water_around + 1 end
407                                 if minetest.get_node({x=cx+0,y=1,z=cz-divlen*2}).name == "default:water_source" then
408                                         num_water_around = num_water_around + 1 end
409                                 if minetest.get_node({x=cx+0,y=1,z=cz+divlen*2}).name == "default:water_source" then
410                                         num_water_around = num_water_around + 1 end
411                                 if num_water_around >= 2 then
412                                         is_shallow = false
413                                 end     
414                                 if is_shallow then
415                                         for x1=-divlen,divlen do
416                                         for z1=-divlen,divlen do
417                                                 if minetest.get_node({x=cx+x1,y=0,z=cz+z1}).name == "default:sand" then
418                                                         minetest.set_node({x=cx+x1,y=0,z=cz+z1}, {name="default:clay"})
419                                                 end
420                                         end
421                                         end
422                                 end
423                         end
424                 end
425                 end
426                 -- Generate papyrus
427                 local perlin1 = minetest.get_perlin(354, 3, 0.7, 100)
428                 -- Assume X and Z lengths are equal
429                 local divlen = 8
430                 local divs = (maxp.x-minp.x)/divlen+1;
431                 for divx=0,divs-1 do
432                 for divz=0,divs-1 do
433                         local x0 = minp.x + math.floor((divx+0)*divlen)
434                         local z0 = minp.z + math.floor((divz+0)*divlen)
435                         local x1 = minp.x + math.floor((divx+1)*divlen)
436                         local z1 = minp.z + math.floor((divz+1)*divlen)
437                         -- Determine papyrus amount from perlin noise
438                         local papyrus_amount = math.floor(perlin1:get2d({x=x0, y=z0}) * 45 - 20)
439                         -- Find random positions for papyrus based on this random
440                         local pr = PseudoRandom(seed+1)
441                         for i=0,papyrus_amount do
442                                 local x = pr:next(x0, x1)
443                                 local z = pr:next(z0, z1)
444                                 if minetest.get_node({x=x,y=1,z=z}).name == "default:dirt_with_grass" and
445                                                 minetest.find_node_near({x=x,y=1,z=z}, 1, "default:water_source") then
446                                         default.make_papyrus({x=x,y=2,z=z}, pr:next(2, 4))
447                                 end
448                         end
449                 end
450                 end
451                 -- Generate cactuses
452                 local perlin1 = minetest.get_perlin(230, 3, 0.6, 100)
453                 -- Assume X and Z lengths are equal
454                 local divlen = 16
455                 local divs = (maxp.x-minp.x)/divlen+1;
456                 for divx=0,divs-1 do
457                 for divz=0,divs-1 do
458                         local x0 = minp.x + math.floor((divx+0)*divlen)
459                         local z0 = minp.z + math.floor((divz+0)*divlen)
460                         local x1 = minp.x + math.floor((divx+1)*divlen)
461                         local z1 = minp.z + math.floor((divz+1)*divlen)
462                         -- Determine cactus amount from perlin noise
463                         local cactus_amount = math.floor(perlin1:get2d({x=x0, y=z0}) * 6 - 3)
464                         -- Find random positions for cactus based on this random
465                         local pr = PseudoRandom(seed+1)
466                         for i=0,cactus_amount do
467                                 local x = pr:next(x0, x1)
468                                 local z = pr:next(z0, z1)
469                                 -- Find ground level (0...15)
470                                 local ground_y = nil
471                                 for y=30,0,-1 do
472                                         if minetest.get_node({x=x,y=y,z=z}).name ~= "air" then
473                                                 ground_y = y
474                                                 break
475                                         end
476                                 end
477                                 -- If desert sand, make cactus
478                                 if ground_y and minetest.get_node({x=x,y=ground_y,z=z}).name == "default:desert_sand" then
479                                         default.make_cactus({x=x,y=ground_y+1,z=z}, pr:next(3, 4))
480                                 end
481                         end
482                 end
483                 end
484                 -- Generate grass
485                 local perlin1 = minetest.get_perlin(329, 3, 0.6, 100)
486                 -- Assume X and Z lengths are equal
487                 local divlen = 16
488                 local divs = (maxp.x-minp.x)/divlen+1;
489                 for divx=0,divs-1 do
490                 for divz=0,divs-1 do
491                         local x0 = minp.x + math.floor((divx+0)*divlen)
492                         local z0 = minp.z + math.floor((divz+0)*divlen)
493                         local x1 = minp.x + math.floor((divx+1)*divlen)
494                         local z1 = minp.z + math.floor((divz+1)*divlen)
495                         -- Determine grass amount from perlin noise
496                         local grass_amount = math.floor(perlin1:get2d({x=x0, y=z0}) ^ 3 * 9)
497                         -- Find random positions for grass based on this random
498                         local pr = PseudoRandom(seed+1)
499                         for i=0,grass_amount do
500                                 local x = pr:next(x0, x1)
501                                 local z = pr:next(z0, z1)
502                                 -- Find ground level (0...15)
503                                 local ground_y = nil
504                                 for y=30,0,-1 do
505                                         if minetest.get_node({x=x,y=y,z=z}).name ~= "air" then
506                                                 ground_y = y
507                                                 break
508                                         end
509                                 end
510                                 
511                                 if ground_y then
512                                         local p = {x=x,y=ground_y+1,z=z}
513                                         local nn = minetest.get_node(p).name
514                                         -- Check if the node can be replaced
515                                         if minetest.registered_nodes[nn] and
516                                                 minetest.registered_nodes[nn].buildable_to then
517                                                 nn = minetest.get_node({x=x,y=ground_y,z=z}).name
518                                                 -- If desert sand, add dry shrub
519                                                 if nn == "default:desert_sand" then
520                                                         minetest.set_node(p,{name="default:dry_shrub"})
521                                                         
522                                                 -- If dirt with grass, add grass
523                                                 elseif nn == "default:dirt_with_grass" then
524                                                         minetest.set_node(p,{name="default:grass_"..pr:next(1, 5)})
525                                                 end
526                                         end
527                                 end
528                                 
529                         end
530                 end
531                 end
532         end
533
534         -- Generate nyan cats
535         generate_nyancats(seed, minp, maxp)
536 end)
537