new apple image.
[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         generate_ore("default:mese",            "default:stone", minp, maxp, seed+4, 1/16/16/16, 2, 3,   -127, -64)
150         generate_ore("default:mese",            "default:stone", minp, maxp, seed+5, 1/9/9/9,    3, 5, -31000,-128)
151         
152         generate_ore("default:stone_with_coal", "default:stone", minp, maxp, seed+7, 1/24/24/24, 6,27, -31000,  0)
153         generate_ore("default:stone_with_iron", "default:stone", minp, maxp, seed+6, 1/24/24/24, 6,27, -31000, -64)
154
155         if maxp.y >= 2 and minp.y <= 0 then
156                 -- Generate clay
157                 -- Assume X and Z lengths are equal
158                 local divlen = 4
159                 local divs = (maxp.x-minp.x)/divlen+1;
160                 for divx=0+1,divs-1-1 do
161                 for divz=0+1,divs-1-1 do
162                         local cx = minp.x + math.floor((divx+0.5)*divlen)
163                         local cz = minp.z + math.floor((divz+0.5)*divlen)
164                         if minetest.env:get_node({x=cx,y=1,z=cz}).name == "default:water_source" and
165                                         minetest.env:get_node({x=cx,y=0,z=cz}).name == "default:sand" then
166                                 local is_shallow = true
167                                 local num_water_around = 0
168                                 if minetest.env:get_node({x=cx-divlen*2,y=1,z=cz+0}).name == "default:water_source" then
169                                         num_water_around = num_water_around + 1 end
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+0,y=1,z=cz-divlen*2}).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 num_water_around >= 2 then
177                                         is_shallow = false
178                                 end     
179                                 if is_shallow then
180                                         for x1=-divlen,divlen do
181                                         for z1=-divlen,divlen do
182                                                 if minetest.env:get_node({x=cx+x1,y=0,z=cz+z1}).name == "default:sand" then
183                                                         minetest.env:set_node({x=cx+x1,y=0,z=cz+z1}, {name="default:clay"})
184                                                 end
185                                         end
186                                         end
187                                 end
188                         end
189                 end
190                 end
191                 -- Generate papyrus
192                 local perlin1 = minetest.env:get_perlin(354, 3, 0.7, 100)
193                 -- Assume X and Z lengths are equal
194                 local divlen = 8
195                 local divs = (maxp.x-minp.x)/divlen+1;
196                 for divx=0,divs-1 do
197                 for divz=0,divs-1 do
198                         local x0 = minp.x + math.floor((divx+0)*divlen)
199                         local z0 = minp.z + math.floor((divz+0)*divlen)
200                         local x1 = minp.x + math.floor((divx+1)*divlen)
201                         local z1 = minp.z + math.floor((divz+1)*divlen)
202                         -- Determine papyrus amount from perlin noise
203                         local papyrus_amount = math.floor(perlin1:get2d({x=x0, y=z0}) * 45 - 20)
204                         -- Find random positions for papyrus based on this random
205                         local pr = PseudoRandom(seed+1)
206                         for i=0,papyrus_amount do
207                                 local x = pr:next(x0, x1)
208                                 local z = pr:next(z0, z1)
209                                 if minetest.env:get_node({x=x,y=1,z=z}).name == "default:dirt_with_grass" and
210                                                 minetest.env:find_node_near({x=x,y=1,z=z}, 1, "default:water_source") then
211                                         default.make_papyrus({x=x,y=2,z=z}, pr:next(2, 4))
212                                 end
213                         end
214                 end
215                 end
216                 -- Generate cactuses
217                 local perlin1 = minetest.env:get_perlin(230, 3, 0.6, 100)
218                 -- Assume X and Z lengths are equal
219                 local divlen = 16
220                 local divs = (maxp.x-minp.x)/divlen+1;
221                 for divx=0,divs-1 do
222                 for divz=0,divs-1 do
223                         local x0 = minp.x + math.floor((divx+0)*divlen)
224                         local z0 = minp.z + math.floor((divz+0)*divlen)
225                         local x1 = minp.x + math.floor((divx+1)*divlen)
226                         local z1 = minp.z + math.floor((divz+1)*divlen)
227                         -- Determine cactus amount from perlin noise
228                         local cactus_amount = math.floor(perlin1:get2d({x=x0, y=z0}) * 6 - 3)
229                         -- Find random positions for cactus based on this random
230                         local pr = PseudoRandom(seed+1)
231                         for i=0,cactus_amount do
232                                 local x = pr:next(x0, x1)
233                                 local z = pr:next(z0, z1)
234                                 -- Find ground level (0...15)
235                                 local ground_y = nil
236                                 for y=30,0,-1 do
237                                         if minetest.env:get_node({x=x,y=y,z=z}).name ~= "air" then
238                                                 ground_y = y
239                                                 break
240                                         end
241                                 end
242                                 -- If desert sand, make cactus
243                                 if ground_y and minetest.env:get_node({x=x,y=ground_y,z=z}).name == "default:desert_sand" then
244                                         default.make_cactus({x=x,y=ground_y+1,z=z}, pr:next(3, 4))
245                                 end
246                         end
247                 end
248                 end
249                 -- Generate dry shrubs
250                 local perlin1 = minetest.env:get_perlin(329, 3, 0.6, 100)
251                 -- Assume X and Z lengths are equal
252                 local divlen = 16
253                 local divs = (maxp.x-minp.x)/divlen+1;
254                 for divx=0,divs-1 do
255                 for divz=0,divs-1 do
256                         local x0 = minp.x + math.floor((divx+0)*divlen)
257                         local z0 = minp.z + math.floor((divz+0)*divlen)
258                         local x1 = minp.x + math.floor((divx+1)*divlen)
259                         local z1 = minp.z + math.floor((divz+1)*divlen)
260                         -- Determine dry shrubs amount from perlin noise
261                         local shrub_amount = math.floor(perlin1:get2d({x=x0, y=z0}) * 5 + 0)
262                         -- Find random positions for dry shrubs based on this random
263                         local pr = PseudoRandom(seed+1)
264                         for i=0,shrub_amount do
265                                 local x = pr:next(x0, x1)
266                                 local z = pr:next(z0, z1)
267                                 -- Find ground level (0...15)
268                                 local ground_y = nil
269                                 for y=30,0,-1 do
270                                         if minetest.env:get_node({x=x,y=y,z=z}).name ~= "air" then
271                                                 ground_y = y
272                                                 break
273                                         end
274                                 end
275                                 -- If desert sand, make dry shrub
276                                 if ground_y and minetest.env:get_node({x=x,y=ground_y,z=z}).name == "default:desert_sand" then
277                                         local p = {x=x,y=ground_y+1,z=z}
278                                         local nn = minetest.env:get_node(p).name
279                                         if minetest.registered_nodes[nn] and
280                                                 minetest.registered_nodes[nn].buildable_to then
281                                                 minetest.env:set_node(p, {name="default:dry_shrub"})
282                                         end
283                                 end
284                         end
285                 end
286                 end
287         end
288
289         -- Generate nyan cats
290         generate_nyancats(seed, minp, maxp)
291 end)
292