derekiscool423

terrain

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