Advertisement
DragRacer31

Terrain Generator Demo

Sep 20th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.96 KB | None | 0 0
  1. _G.ChunkSize = 16
  2. _G.RenderDist = 1
  3. _G.BlockSize = 3
  4. _G.MaxTerrainHeight = 64
  5. _G.OptimizedHeight = true
  6. _G.Chunks = {}
  7. _G.NoiseScale = 30
  8. _G.Amplitude = 20
  9. _G.Octaves = 1
  10. _G.Frequency = 1
  11. _G.Gain = 1
  12. _G.CoGain = 1
  13. _G.Lacunarity = 2
  14. _G.CoLacunarity = 1
  15. _G.MaxDensity = 20
  16. _G.Seed = math.random()
  17. math.randomseed(_G.Seed % tick())
  18. _G.Seed = math.random()
  19.  
  20. local f = nil
  21.  
  22. if _G.RenderDist < 1 then
  23.     _G.RenderDist = 1
  24. end
  25.  
  26. function CheckChunk(x, z)
  27.    
  28.     if _G.Chunks[x] == nil then
  29.         _G.Chunks[x] = {}
  30.     end
  31.    
  32.     return _G.Chunks[x][z]
  33. end
  34.  
  35. function RoundTo(a, b)
  36.     return a - (a % b)
  37. end
  38.  
  39. function Noise(x, y, z)
  40.     local Xnoise = 0
  41.     local Ynoise = 0
  42.     local Znoise = 0
  43.    
  44.     local Amp = _G.Amplitude
  45.     local Fre = _G.Frequency
  46.    
  47.     Ynoise = math.noise(Fre * (x / _G.NoiseScale), Fre * (z / _G.NoiseScale), _G.Seed) * Amp
  48.     for i = 0, _G.Octaves do
  49.         Xnoise = Xnoise + math.noise(y / _G.NoiseScale, z / _G.NoiseScale, _G.Seed) * Amp
  50.                    
  51.         Znoise = Znoise + math.noise(x / _G.NoiseScale, y / _G.NoiseScale, _G.Seed) * Amp
  52.    
  53.         Amp = Amp * _G.Gain
  54.         Amp = Amp / _G.Cogain
  55.         Fre = Fre * _G.Lacunarity
  56.         Fre = Fre / _G.CoLacunarity
  57.     end
  58.     return Xnoise + Ynoise + Znoise - y
  59. end
  60.  
  61. function MakeChunk(x, z)
  62.     local Thread = coroutine.create(function()
  63.         for xi = -_G.ChunkSize / _G.BlockSize, _G.ChunkSize / _G.BlockSize do
  64.             for zi = -_G.ChunkSize / _G.BlockSize, _G.ChunkSize / _G.BlockSize do
  65.                
  66.                 if _G.OptimizedHeight == false then
  67.                
  68.                     for yi = -_G.MaxTerrainHeight, _G.MaxTerrainHeight do
  69.                        
  70.                         if Noise(xi, yi, zi) < _G.MaxDensity then
  71.                            
  72.                         end
  73.                        
  74.                     end
  75.                    
  76.                 elseif _G.OptimizedHeight == true then
  77.                    
  78.                     for yi = -_G.MaxTerrainHeight, _G.MaxTerrainHeight do
  79.                        
  80.                         if Noise(xi, yi, zi) < _G.MaxDensity then
  81.                            
  82.                         end
  83.                        
  84.                     end
  85.                    
  86.                 end
  87.             end
  88.         end
  89.     end)
  90.    
  91.     coroutine.resume(Thread)
  92. end
  93.  
  94. function CheckPos(x, z)
  95.     local Thread = coroutine.create(function()
  96.         local ChunkX = RoundTo(x, _G.ChunkSize) / _G.ChunkSize
  97.         local ChunkZ = RoundTo(z, _G.ChunkSize) / _G.ChunkSize
  98.        
  99.         if not CheckChunk(ChunkX, ChunkZ) then
  100.             for xi = -_G.RenderDist, _G.RenderDist do
  101.                 for zi = -_G.RenderDist, _G.RenderDist do
  102.                     local ChunkX = RoundTo(x, _G.ChunkSize) / _G.ChunkSize + xi
  103.                     local ChunkZ = RoundTo(z, _G.ChunkSize) / _G.ChunkSize + zi
  104.                    
  105.                    
  106.                     if not CheckChunk(ChunkX, ChunkZ) then
  107.  
  108.                         _G.Chunks[ChunkX][ChunkZ] = true
  109.                     end
  110.  
  111.                     if f == nil then
  112.                         f = ChunkX.." : "..ChunkZ
  113.                     end
  114.                            
  115.                     if f == ChunkX.." : "..ChunkZ then
  116.                         print("Same!")
  117.                     end
  118.                 end
  119.             end
  120.         end
  121.     end)
  122.    
  123.     coroutine.resume(Thread)
  124. end
  125.  
  126. spawn(function()
  127.     while true do
  128.         wait(1)
  129.         --print("hh")
  130.         for i, v in pairs(game:GetService("Players"):GetChildren()) do
  131.             local C = nil
  132.             local HRP = nil
  133.  
  134.             pcall(function()
  135.                 if v.Character then
  136.                     C = v.Character
  137.                 else
  138.                     C = nil
  139.                 end
  140.             end)
  141.  
  142.             if C ~= nil then
  143.                 pcall(function()
  144.                     if C:FindFirstChild("HumanoidRootPart") then
  145.                         HRP = C:FindFirstChild("HumanoidRootPart")
  146.  
  147.                     else
  148.                         HRP = nil
  149.                     end
  150.                 end)
  151.             end
  152.  
  153.             if HRP ~= nil then
  154.                 CheckPos(HRP.Position.X, HRP.Position.Z)
  155.             end
  156.         end
  157.     end
  158. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement