Advertisement
MajourScripters

Automatic Terrain Generation[Local]

Mar 19th, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.34 KB | None | 0 0
  1.  
  2.  
  3. local baseHeight        = 29            -- The main height factor for the terrain.
  4. local chunkScale        = 12                -- The grid scale for terrain generation. Should be kept relatively low if used in real-time.
  5. local renderDist        = 180/4             -- The length/width of chunks in voxels that should be around the player at all times
  6. local xScale            = 95/4              -- How much we should strech the X scale of the generation noise
  7. local zScale            = 95/4              -- How much we should strech the Z scale of the generation noise
  8. local generationSeed    = math.random()     -- Seed for determining the main height map of the terrain.
  9.  
  10. ------------------------------------------------------------------------------------------------------------------------------------------------
  11.  
  12. local chunks = {}
  13.  
  14. function chunkExists(chunkX,chunkZ)
  15.     if not chunks[chunkX] then
  16.         chunks[chunkX] = {}
  17.     end
  18.     return chunks[chunkX][chunkZ]
  19. end
  20.  
  21. function mountLayer(x,heightY,z,material)
  22.     local begY = -baseHeight
  23.     local endY = heightY
  24.     workspace.Terrain:FillBlock(CFrame.new(x*4+2, (begY+endY)*4/2, z*4+2), Vector3.new(4, (endY-begY)*4, 4), material) 
  25. end
  26.  
  27. function makeChunk(chunkX,chunkZ)
  28.     local rootPos = Vector3.new(chunkX*chunkScale,0,chunkZ*chunkScale)
  29.     chunks[chunkX][chunkZ] = true -- Acknowledge the chunk's existance.
  30.     for x = 0,chunkScale-1 do
  31.         for z = 0,chunkScale-1 do
  32.             local cx = (chunkX*chunkScale) + x
  33.             local cz = (chunkZ*chunkScale) + z
  34.             local noise = math.noise(generationSeed,cx/xScale,cz/zScale)
  35.             local cy = noise*baseHeight
  36.             local Z = math.random(0,20)
  37.             if Z == 1 then
  38.             Choice = Enum.Material.Grass
  39.             elseif Z == 2 then
  40.             Choice = Enum.Material.Grass
  41.             elseif Z == 3 then
  42.             Choice = Enum.Material.Grass
  43.             elseif Z == 4 then
  44.             Choice = Enum.Material.Grass
  45.             elseif Z == 5 then
  46.             Choice = Enum.Material.Grass
  47.             elseif Z == 6 then
  48.             Choice = Enum.Material.Grass
  49.             elseif Z == 7 then
  50.             Choice = Enum.Material.Grass
  51.             elseif Z == 8 then
  52.             Choice = Enum.Material.Rock
  53.             elseif Z == 9 then
  54.             Choice = Enum.Material.Ground
  55.             elseif Z == 10 then
  56.             Choice = Enum.Material.Grass
  57.             elseif Z == 11 then
  58.             Choice = Enum.Material.Rock
  59.             elseif Z == 12 then
  60.             Choice = Enum.Material.Ground
  61.             elseif Z == 13 then
  62.             local Lemon = "Potato"
  63.             elseif Z == 14 then
  64.             Choice = Enum.Material.Grass
  65.             elseif Z == 15 then
  66.             Choice = Enum.Material.Grass
  67.             elseif Z == 16 then
  68.             Choice = Enum.Material.Grass
  69.             elseif Z == 17 then
  70.             Choice = Enum.Material.Grass
  71.             elseif Z == 18 then
  72.             Choice = Enum.Material.Ground
  73.             elseif Z == 19 then
  74.             Choice = Enum.Material.Grass
  75.             elseif Z == 20 then
  76.             Choice = Enum.Material.Rock
  77.             end
  78.             mountLayer(cx,cy,cz,Choice)
  79.             mountLayer(cx,cy-2,cz,Choice)--Makes sure u cant fall and die
  80.         end
  81.     end
  82. end
  83.  
  84. function checkSurroundings(location)
  85.     local chunkX,chunkZ = math.floor(location.X/4/chunkScale),math.floor(location.Z/4/chunkScale)
  86.     local range = math.max(1,renderDist/chunkScale)
  87.     for x = -range,range do
  88.         for z = -range,range do
  89.             local cx,cz = chunkX + x,chunkZ + z
  90.             if not chunkExists(cx,cz) then
  91.                 makeChunk(cx,cz)
  92.             end
  93.         end
  94.     end
  95. end
  96.  
  97. while true do
  98.     for _,player in pairs(game.Players:GetPlayers()) do
  99.         if player.Character then
  100.             local torso = player.Character:FindFirstChild("Torso")
  101.             if torso then
  102.                 checkSurroundings(torso.Position)
  103.             end
  104.         end
  105.     end
  106.     wait(1)
  107. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement