Guest User

Untitled

a guest
Mar 10th, 2013
50
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  26. --
  27. -- Ore generation
  28. --
  29.  
  30. local function generate_ore(name, wherein, minp, maxp, seed, chunks_per_volume, ore_per_chunk, height_min, height_max, param2)
  31.     if maxp.y < height_min or minp.y > height_max then
  32.         return
  33.     end
  34.     local y_min = math.max(minp.y, height_min)
  35.     local y_max = math.min(maxp.y, height_max)
  36.     local volume = (maxp.x-minp.x+1)*(y_max-y_min+1)*(maxp.z-minp.z+1)
  37.     local pr = PseudoRandom(seed)
  38.     local num_chunks = math.floor(chunks_per_volume * volume)
  39.     local chunk_size = 3
  40.     if ore_per_chunk <= 4 then
  41.         chunk_size = 2
  42.     end
  43.     local inverse_chance = math.floor(chunk_size*chunk_size*chunk_size / ore_per_chunk)
  44.     --print("generate_ore num_chunks: "..dump(num_chunks))
  45.     for i=1,num_chunks do
  46.         local y0 = pr:next(y_min, y_max-chunk_size+1)
  47.         if y0 >= height_min and y0 <= height_max then
  48.             local x0 = pr:next(minp.x, maxp.x-chunk_size+1)
  49.             local z0 = pr:next(minp.z, maxp.z-chunk_size+1)
  50.             local p0 = {x=x0, y=y0, z=z0}
  51.             for x1=0,chunk_size-1 do
  52.             for y1=0,chunk_size-1 do
  53.             for z1=0,chunk_size-1 do
  54.                 if pr:next(1,inverse_chance) == 1 then
  55.                     local x2 = x0+x1
  56.                     local y2 = y0+y1
  57.                     local z2 = z0+z1
  58.                     local p2 = {x=x2, y=y2, z=z2}
  59.                     if minetest.env:get_node(p2).name == wherein then
  60.                         minetest.env:set_node(p2, {name=name, param2=param2})
  61.                     end
  62.                 end
  63.             end
  64.             end
  65.             end
  66.         end
  67.     end
  68.     --print("generate_ore done")
  69. end
  70.  
  71. minetest.register_on_generated(function(minp, maxp, seed)
  72.     generate_ore("default:stone_with_coal", "default:stone", minp, maxp, seed,   1/8/8/8,    5, -31000,  64)
  73.     generate_ore("default:stone_with_iron", "default:stone", minp, maxp, seed+1, 1/16/16/16, 5,   -5,   7)
  74.     generate_ore("default:stone_with_iron", "default:stone", minp, maxp, seed+2, 1/12/12/12, 5, -16,   -5)
  75.     generate_ore("default:stone_with_iron", "default:stone", minp, maxp, seed+3, 1/9/9/9,    5, -31000, -17)
  76.     -- Generate clay
  77.     if maxp.y >= 2 and minp.y <= 0 then
  78.         -- Assume X and Z lengths are equal
  79.         local divlen = 4
  80.         local divs = (maxp.x-minp.x)/divlen+1;
  81.         for divx=0+1,divs-1-1 do
  82.         for divz=0+1,divs-1-1 do
  83.             local cx = minp.x + math.floor((divx+0.5)*divlen)
  84.             local cz = minp.z + math.floor((divz+0.5)*divlen)
  85.             if minetest.env:get_node({x=cx,y=1,z=cz}).name == "default:water_source" and
  86.                     minetest.env:get_node({x=cx,y=0,z=cz}).name == "default:sand" then
  87.                 local is_shallow = true
  88.                 local num_water_around = 0
  89.                 if minetest.env:get_node({x=cx-divlen*2,y=1,z=cz+0}).name == "default:water_source" then
  90.                     num_water_around = num_water_around + 1 end
  91.                 if minetest.env:get_node({x=cx+divlen*2,y=1,z=cz+0}).name == "default:water_source" then
  92.                     num_water_around = num_water_around + 1 end
  93.                 if minetest.env:get_node({x=cx+0,y=1,z=cz-divlen*2}).name == "default:water_source" then
  94.                     num_water_around = num_water_around + 1 end
  95.                 if minetest.env:get_node({x=cx+0,y=1,z=cz+divlen*2}).name == "default:water_source" then
  96.                     num_water_around = num_water_around + 1 end
  97.                 if num_water_around >= 2 then
  98.                     is_shallow = false
  99.                 end
  100.                 if is_shallow then
  101.                     for x1=-divlen,divlen do
  102.                     for z1=-divlen,divlen do
  103.                         if minetest.env:get_node({x=cx+x1,y=0,z=cz+z1}).name == "default:sand" then
  104.                             minetest.env:set_node({x=cx+x1,y=0,z=cz+z1}, {name="default:clay"})
  105.                         end
  106.                     end
  107.                     end
  108.                 end
  109.             end
  110.         end
  111.         end
  112.     end
  113.     if minetest.setting_get("liquid_finite") then
  114.         generate_ore("default:water_source", "default:stone", minp, maxp, seed+42, 1/24/24/24, 4,  -100,   -10,  128)
  115.         generate_ore("default:water_source", "default:stone", minp, maxp, seed+42, 1/28/28/28, 3,  -10000, -101, 128)
  116.         generate_ore("default:lava_source",  "default:stone", minp, maxp, seed+43, 1/38/38/38, 2,  -500,   -100, 128)
  117.         generate_ore("default:lava_source",  "default:stone", minp, maxp, seed+43, 1/30/30/30, 4,  -31000, -501, 128)
  118.     end
  119. end)
RAW Paste Data