Advertisement
Shyvha

Roblox Canyon Generator

Jul 21st, 2017
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.87 KB | None | 0 0
  1. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  2. -- @CloneTrooper1019, 2015 , Shyvha , 2017
  3. -- Infinite Smooth Canyon Generator :3
  4. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  5. -- CONFIGURATION:
  6. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  7.  
  8. baseHeight = 10
  9. -- ^ The main height factor for the terrain.
  10.  
  11. mountainHeight = 12
  12. -- ^ How tall should mountains be relative to the baseHeight of the terrain
  13.  
  14. mountainMaterialWeight = 0.4
  15. -- ^ The chance that a mountain terrain chunk will generate using slate rather than rock
  16. --    Should be a number between 0 and 1
  17. --    0 = only rock, 1 = only slate, 0.5 = half n half
  18. canyonMaterialDensity = 0.1
  19. -- ^ The limestone rarity
  20. -- 0 = No Limestone at all , 1 = Only limestone (NOT RECOMMANDED) , 0.2 = small bumps (Realist)
  21. -- Note that limestone is not very different from sand, but it adds some realism
  22. chunkScale = 4
  23. -- ^ The grid scale for terrain generation.
  24.  
  25. renderDist = 90
  26. -- ^ The length/width of chunks in voxels that should be around the player at all times
  27.  
  28. xScale = 30
  29. -- ^ How much we should strech the X scale of the generation noise
  30.  
  31. zScale = 30
  32. -- ^ How much we should strech the Z scale of the generation noise
  33.  
  34. waterLevel = -0.9
  35. -- ^ Determines if we should generate ponds if the height level goes below this
  36. --    Should be a number between -1 and 1
  37. --    -1 = no water, 0 = all grass levels are filled with water, 1 = The entire map is flooded with water (Except for tall mountains)
  38.  
  39. seed = math.random()
  40. -- ^ Seed for determining the height map of the terrain.
  41. --    By default its random.
  42.  
  43. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  44.  
  45. local chunks = {}
  46.  
  47. function chunkExists(chunkX,chunkZ)
  48.         if not chunks[chunkX] then
  49.                 chunks[chunkX] = {}
  50.         end
  51.         return chunks[chunkX][chunkZ]
  52. end
  53.  
  54. function fillSmoothBlock(x,z,begY,endY,material)
  55.         local location = CFrame.new(x*4+2, (begY+endY)*4/2, z*4+2)
  56.         local fill = Vector3.new(4, (endY-begY)*4, 4)
  57.         workspace.Terrain:FillBlock(location,fill,material)
  58. end
  59.  
  60. function mountLayer(x,heightY,z,material)
  61.         -- Fill in Lakes/Ponds
  62.         local waterFill = baseHeight * waterLevel
  63.         if heightY < waterFill then
  64.                 material = Enum.Material.Sand -- Make the material sand.
  65.                 fillSmoothBlock(x,z,heightY-2,waterFill,Enum.Material.Water) -- Fill some water in.
  66.         end
  67.         -- Fill in the main terrain.
  68.         fillSmoothBlock(x,z,-baseHeight,heightY,material)
  69. end
  70.  
  71. function makeChunk(chunkX,chunkZ)
  72.         local rootPos = Vector3.new(chunkX*chunkScale,0,chunkZ*chunkScale)
  73.         chunks[chunkX][chunkZ] = true -- Acknowledge the chunk's existance.
  74.         for x = 0,chunkScale-1 do
  75.                 for z = 0,chunkScale-1 do
  76.                         local cx = (chunkX*chunkScale) + x
  77.                         local cz = (chunkZ*chunkScale) + z
  78.                         local noise = math.noise(seed,cx/xScale,cz/zScale)
  79.                         local isMountain = (noise > 0)
  80.                         local material,materialScale do
  81.                                 if not isMountain then
  82.                                         material = Enum.Material.Sand
  83.                                         materialScale = 1
  84.                                 else
  85.                                         materialScale = mountainHeight
  86.                                         if math.random() > mountainMaterialWeight then
  87.                                                 material = Enum.Material.Sandstone
  88.                                         else
  89.                                             if math.random() > canyonMaterialDensity then
  90.                                                 material = Enum.Material.Basalt
  91.                                             else
  92.                                                 material = Enum.Material.Rock
  93.                                             end
  94.                                         end
  95.                                 end
  96.                         end
  97.                         local cy = noise*baseHeight*materialScale
  98.                         mountLayer(cx,cy,cz,material)
  99.                 end
  100.         end
  101. end
  102.  
  103. function doAreaFill(location)
  104.         local chunkX,chunkZ = math.floor(location.X/4/chunkScale),math.floor(location.Z/4/chunkScale)
  105.         local range = math.max(1,renderDist/chunkScale)
  106.         for x = -range,range do
  107.                 for z = -range,range do
  108.                         local cx,cz = chunkX + x,chunkZ + z
  109.                         if not chunkExists(cx,cz) then
  110.                                 makeChunk(cx,cz)
  111.                         end
  112.                 end
  113.         end    
  114. end
  115.  
  116. while true do
  117.         for _,v in pairs(game.Players:GetPlayers()) do
  118.                 spawn(function ()
  119.                         local char = v.Character
  120.                         if char then
  121.                                 local torso = char:findFirstChild("Torso")
  122.                                 if torso then
  123.                                         doAreaFill(torso.Position)
  124.                                 end
  125.                         end
  126.                 end)
  127.         end
  128.         wait(1)
  129. end
  130.  
  131. ---------------------------------------------------------------------------------------------------------------------------------------
  132.  
  133. --Thanks for CloneTrooper1019 for the main script
  134. --I scripted the sand to be up there.
  135. --WIP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement