LynXS_

Untitled

Aug 16th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. -- Constants
  2. local CHUNK_SIZE = 32
  3. local WORLD_SIZE = 8
  4. local BLOCK_SIZE = 2
  5. local HEIGHT_SCALE = 10 -- Controls the height of the terrain
  6. local NOISE_SCALE = 0.05 -- Adjust for more or less noise detail
  7. local NOISE_OCTAVES = 3 -- Controls the complexity of the noise
  8. local START_POSITION = Vector3.new(0, 50, 0) -- Starting position for the terrain generation
  9.  
  10. -- Function to generate terrain using Perlin noise
  11. local function generateTerrain()
  12. for chunkX = 0, WORLD_SIZE - 1 do
  13. for chunkY = 0, WORLD_SIZE - 1 do
  14. local startX = chunkX * CHUNK_SIZE
  15. local startY = chunkY * CHUNK_SIZE
  16.  
  17. for x = 0, CHUNK_SIZE - 1 do
  18. for y = 0, CHUNK_SIZE - 1 do
  19. local worldX = startX + x
  20. local worldY = startY + y
  21.  
  22. -- Generate height using Perlin noise
  23. local noiseValue = 0
  24. local amplitude = 1
  25. local frequency = NOISE_SCALE
  26.  
  27. -- Combine octaves for more complex terrain
  28. for octave = 1, NOISE_OCTAVES do
  29. noiseValue = noiseValue + amplitude * math.noise(worldX * frequency, worldY * frequency)
  30. amplitude = amplitude * 0.5
  31. frequency = frequency * 2
  32. end
  33.  
  34. -- Normalize noise to [0, 1] and scale
  35. noiseValue = (noiseValue + 1) / 2
  36. local height = noiseValue * HEIGHT_SCALE
  37.  
  38. -- Create blocks based on calculated height
  39. for h = 0, height - 1 do
  40. local block = Instance.new("Part")
  41. block.Size = Vector3.new(BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)
  42. block.Position = START_POSITION + Vector3.new(worldX * BLOCK_SIZE, h * BLOCK_SIZE, worldY * BLOCK_SIZE)
  43. block.Anchored = true
  44. block.BrickColor = BrickColor.new("Light green") -- Set to Light Green
  45. block.Material = Enum.Material.Mud
  46. block.Parent = workspace
  47. end
  48.  
  49. -- Create grass block at the top
  50. local grassBlock = Instance.new("Part")
  51. grassBlock.Size = Vector3.new(BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)
  52. grassBlock.Position = START_POSITION + Vector3.new(worldX * BLOCK_SIZE, height * BLOCK_SIZE, worldY * BLOCK_SIZE)
  53. grassBlock.Anchored = true
  54. grassBlock.BrickColor = BrickColor.Green() -- Change to match your grass block color
  55. grassBlock.Material = Enum.Material.Grass
  56. grassBlock.Parent = workspace
  57. end
  58. end
  59. end
  60. end
  61. end
  62.  
  63. -- Generate the entire world
  64. generateTerrain()
  65.  
Advertisement
Add Comment
Please, Sign In to add comment