1 -- minetest/default/mapgen.lua
4 -- Aliases for map generator outputs
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")
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
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))
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
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})
66 --print("generate_ore done")
69 function default.make_papyrus(pos, size)
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"})
82 function default.make_cactus(pos, size)
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"})
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}
101 elseif facedir == 1 then
103 elseif facedir == 2 then
105 elseif facedir == 3 then
108 print("default.make_nyancat(): Invalid facedir: "+dump(facedir))
112 local p = {x=pos.x, y=pos.y, z=pos.z}
113 minetest.env:set_node(p, {name="default:nyancat", param2=facedir})
115 p.x = p.x + tailvec.x
116 p.z = p.z + tailvec.z
117 minetest.env:set_node(p, {name="default:nyancat_rainbow"})
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
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))
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)
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)
155 if maxp.y >= 2 and minp.y <= 0 then
157 -- Assume X and Z lengths are equal
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
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"})
192 local perlin1 = minetest.env:get_perlin(354, 3, 0.7, 100)
193 -- Assume X and Z lengths are equal
195 local divs = (maxp.x-minp.x)/divlen+1;
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))
217 local perlin1 = minetest.env:get_perlin(230, 3, 0.6, 100)
218 -- Assume X and Z lengths are equal
220 local divs = (maxp.x-minp.x)/divlen+1;
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)
237 if minetest.env:get_node({x=x,y=y,z=z}).name ~= "air" then
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))
249 -- Generate dry shrubs
250 local perlin1 = minetest.env:get_perlin(329, 3, 0.6, 100)
251 -- Assume X and Z lengths are equal
253 local divs = (maxp.x-minp.x)/divlen+1;
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)
270 if minetest.env:get_node({x=x,y=y,z=z}).name ~= "air" then
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"})
289 -- Generate nyan cats
290 generate_nyancats(seed, minp, maxp)