Advertisement
Delros12

terraingen

Aug 1st, 2019 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. function round(x)
  2. if x%2 ~= 0.5 then
  3. return math.floor(x+0.5)
  4. end
  5. return x-0.5
  6. end
  7. function grid(n, grid)
  8. return math.floor(n/grid + 0.5)*grid
  9. end
  10. function putTexture(part, frontF, backF, topF, bottomF, leftF, rightF, className)
  11. local faces = {"Front", "Back", "Top", "Bottom", "Left", "Right"}
  12. for i,f in pairs(faces) do
  13. local decal = Instance.new(className)
  14. decal.StudsPerTileU = 2.6
  15. decal.StudsPerTileV = 2.6
  16. if i == 1 then decal.Texture = frontF decal.Name = f end
  17. if i == 2 then decal.Texture = backF or frontF decal.Name = f end
  18. if i == 3 then decal.Texture = topF or frontF decal.Name = f end
  19. if i == 4 then decal.Texture = bottomF or frontF decal.Name = f end
  20. if i == 5 then decal.Texture = leftF or frontF decal.Name = f end
  21. if i == 6 then decal.Texture = rightF or frontF decal.Name = f end
  22. decal.Face = f
  23. decal.Parent = part
  24. end
  25. return part
  26. end
  27. function QuickCreateInstance(className, properties)
  28. local inst = Instance.new(className)
  29. for propertyName,propertyValue in pairs(properties or {}) do
  30. inst[propertyName] = propertyValue
  31. end
  32. return inst
  33. end
  34.  
  35. local collectionService = game:GetService("CollectionService")
  36. local posToGen = Vector3.new(10000, 10000, 10000)
  37. posToGen = Vector3.new(grid(posToGen.X, 2.6), grid(posToGen.Y, 2.6), grid(posToGen.Z, 2.6))
  38.  
  39. local x1 = 500
  40. local y1 = 1
  41. local z1 = 500
  42.  
  43. local seed = math.random()*100
  44.  
  45. local terrainBlocks = workspace:FindFirstChild("TerrainBlocks") or Instance.new("Model", workspace)
  46. terrainBlocks.Name = "TerrainBlocks"
  47.  
  48. local oldBlockBehind = nil
  49. for x = 1, z1 do
  50. local line = Instance.new("Model", terrainBlocks)
  51. line.Name = "Line " .. x
  52. for z = 1, x1 do
  53. for y = 1, y1 do
  54. local blockType = "grass"--math.noise(x/150, seed*16, z/150) > 0.4 and "stone" or "grass"
  55.  
  56. local height = math.sqrt(13 + (math.noise(x/160, seed, z/160)*10 + math.noise(x/500, seed*5, z/500)*7))*3
  57.  
  58. local pos = Vector3.new(x * 2.6, (round((height*26) / 2.6) * 2.6) - (y * 2.6), z * 2.6) + posToGen - Vector3.new(x1*2.6/2, 0, z1*2.6/2)
  59.  
  60. local blockBehind = line:FindFirstChild("B " .. grid(pos.X, 2.6)/2.6 .. "," .. grid(pos.Y, 2.6)/2.6 .. "," .. grid(pos.Z - 2.6, 2.6)/2.6)
  61. if not blockBehind or (blockBehind and collectionService:HasTag(blockBehind, blockType) == false) then
  62. if oldBlockBehind then
  63. local oldLine = terrainBlocks:FindFirstChild("Line " .. x - 1)
  64. if oldLine then
  65. local sideBlock = oldLine:FindFirstChild("B " .. grid(oldBlockBehind.CFrame.p.X - 2.6, 2.6)/2.6 .. "," .. grid(oldBlockBehind.CFrame.p.Y, 2.6)/2.6 .. "," .. grid(oldBlockBehind.CFrame.p.Z + oldBlockBehind.Size.Z/2 - 0.5, 2.6)/2.6)
  66. if sideBlock and sideBlock.Size.Z == oldBlockBehind.Size.Z then
  67. sideBlock:Destroy()
  68. oldBlockBehind.Size = sideBlock.Size + Vector3.new(2.6, 0, 0)
  69. oldBlockBehind.CFrame = CFrame.new(sideBlock.CFrame.p.X + 1.3, oldBlockBehind.CFrame.p.Y, oldBlockBehind.CFrame.p.Z)
  70. -- debug
  71. --for _,c in pairs(oldBlockBehind:GetChildren()) do
  72. -- if c:IsA("Texture") then c.Color3 = Color3.new(0, 0, 1) end
  73. --end
  74. end
  75. end
  76. end
  77.  
  78. --local chunkPos = Vector3.new(grid(pos.X/2.6, 16), grid(pos.Y/2.6, 16), grid(pos.Z/2.6, 16))
  79.  
  80. --local chunk = chunks:FindFirstChild(chunkPos.X .. "," .. chunkPos.Y .. "," .. chunkPos.Z) or Instance.new("Model", chunks)
  81. --chunk.Name = chunkPos.X .. "," .. chunkPos.Y .. "," .. chunkPos.Z
  82.  
  83. local block = Instance.new("Part")
  84. block.Material = "Fabric"
  85. block.Anchored = true
  86. block.Size = Vector3.new(2.6, 2.6, 2.6)
  87. block.CFrame = CFrame.new(grid(pos.X, 2.6), grid(pos.Y, 2.6), grid(pos.Z, 2.6))
  88. block.Name = "B " .. grid(pos.X, 2.6)/2.6 .. "," .. grid(pos.Y, 2.6)/2.6 .. "," .. grid(pos.Z, 2.6)/2.6
  89. --if y == 1 then
  90. if blockType == "grass" then
  91. -- if y == 1 then
  92. putTexture(block, "rbxassetid://3132023393", "rbxassetid://3132023393",
  93. "rbxassetid://3131912045", "rbxassetid://3103457697",
  94. "rbxassetid://3132023393", "rbxassetid://3132023393", "Texture")
  95. -- elseif y >= 2 then
  96. -- putTexture(block, "rbxassetid://3103457697", "rbxassetid://3103457697",
  97. -- "rbxassetid://3103457697", "rbxassetid://3103457697",
  98. -- "rbxassetid://3103457697", "rbxassetid://3103457697", "Texture")
  99. -- end
  100. --elseif y == 2 then
  101. else
  102. putTexture(block, "rbxassetid://3131912893", "rbxassetid://3131912893",
  103. "rbxassetid://3131912893", "rbxassetid://3131912893",
  104. "rbxassetid://3131912893", "rbxassetid://3131912893", "Texture")
  105. end
  106. -- elseif y >= 3 then
  107. -- putTexture(block, "rbxassetid://75880927", "rbxassetid://75880927",
  108. -- "rbxassetid://75880927", "rbxassetid://75880927",
  109. -- "rbxassetid://75880927", "rbxassetid://75880927", "Texture")
  110. --end
  111. block.Parent = line
  112. collectionService:AddTag(block, blockType)
  113. else
  114. if blockBehind:IsA("ObjectValue") then blockBehind = blockBehind.Value end
  115. if collectionService:HasTag(blockBehind, blockType) then
  116. blockBehind.Size = blockBehind.Size + Vector3.new(0, 0, 2.6)
  117. blockBehind.CFrame = CFrame.new(blockBehind.CFrame.p + Vector3.new(0, 0, 2.6/2))
  118. blockBehind.Name = "B " .. grid(pos.X, 2.6)/2.6 .. "," .. grid(pos.Y, 2.6)/2.6 .. "," .. grid(pos.Z, 2.6)/2.6
  119. --QuickCreateInstance("ObjectValue", {Name = "B " .. grid(pos.X, 2.6)/2.6 .. "," .. grid(pos.Y, 2.6)/2.6 .. "," .. grid(pos.Z - 2.6, 2.6)/2.6, Value = blockBehind, Parent = blockBehind})
  120. oldBlockBehind = blockBehind
  121. end
  122. end
  123. end
  124. end
  125. wait()
  126. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement