SHARE
TWEET

Untitled

East_Arctica Sep 22nd, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local Terrain = workspace.Terrain
  2. local processBlockSize = 640
  3. local WATER = Enum.Material.Water
  4. local AIR = Enum.Material.Air
  5. local floor = math.floor
  6.  
  7. function isInRegion3(region, point)
  8.     --function by buildthomas
  9.     --https://devforum.roblox.com/t/see-if-vector3-is-in-region3/118457/7
  10.     local relative = (point - region.CFrame.p) / region.Size
  11.     return -0.5 <= relative.X and relative.X <= 0.5
  12.        and -0.5 <= relative.Y and relative.Y <= 0.5
  13.        and -0.5 <= relative.Z and relative.Z <= 0.5
  14. end
  15.  
  16. local function round(num)
  17.     return math.floor(num + .5)
  18. end
  19.  
  20. local function getAlignedPosition(pos)
  21.     --Function from https://wiki.roblox.com/index.php?title=Smooth_terrain#Flood_Fill
  22.     local x = round(pos.X)
  23.     x = x - x%4 + 2
  24.     local y = round(pos.Y)
  25.     y = y - y%4 + 2
  26.     local z = round(pos.Z)
  27.     z = z - z%4 + 2
  28.    
  29.     return Vector3.new(x,y,z)
  30. end
  31.  
  32. local function comma_value(n) --http://lua-users.org/wiki/FormattingNumbers
  33.     local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
  34.     return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
  35. end
  36.  
  37. local function removeWater(voxelPos, worldSize)
  38.     local functionStart = tick()
  39.     voxelPos = getAlignedPosition(voxelPos)
  40.     local material = nil
  41.     local occupancy = nil
  42.    
  43.     local boundaryRegion = Region3.new(voxelPos-worldSize/2, voxelPos+worldSize/2)
  44.     boundaryRegion:ExpandToGrid(4)
  45.     local minPoint = boundaryRegion.CFrame.p-boundaryRegion.Size/2
  46.     print("Starting")
  47.     local partsProcessed = 0
  48.    
  49.     local regions = {} --Stores all of the regions to read/write to
  50.    
  51.     local start = getAlignedPosition(minPoint)
  52.     local xOffset = 0
  53.     local yOffset = 0
  54.     local zOffset = 0
  55.     local done = false
  56.     repeat
  57.         local startPos = start+Vector3.new(xOffset,yOffset,zOffset)
  58.         local size = boundaryRegion.CFrame.p+boundaryRegion.Size/2-startPos
  59.         local x,y,z = size.X, size.Y, size.Z
  60.         if x > processBlockSize then
  61.             x = processBlockSize
  62.         end
  63.         if y > processBlockSize then
  64.             y = processBlockSize
  65.         end
  66.         if z > processBlockSize then
  67.             z = processBlockSize
  68.         end
  69.         size = Vector3.new(x,y,z)
  70.         local region = Region3.new(startPos, startPos+size)
  71.         region = region:ExpandToGrid(4)
  72.         table.insert(regions, region)
  73.         zOffset = zOffset + processBlockSize
  74.         if zOffset >= boundaryRegion.Size.Z then
  75.             zOffset = 0
  76.             yOffset = yOffset + processBlockSize
  77.         end
  78.         if yOffset >= boundaryRegion.Size.Y then
  79.             yOffset = 0
  80.             xOffset = xOffset + processBlockSize
  81.         end
  82.         if xOffset >= boundaryRegion.Size.X then
  83.             done = true
  84.         end
  85.     until done
  86.    
  87.     print("Writing to ", #regions, " regions!")
  88.     print("Removing water...")
  89.     local totalRemoved = 0
  90.     local totalProcessed = 0
  91.     local totalVolume = worldSize.X*worldSize.Y*worldSize.Z
  92.     local changed,materials,occupancy,size,matsX,occX,matsY,occY,p
  93.     for index, region in pairs(regions) do
  94.         --[[
  95.         --Uncomment this if you want parts to display where each chunk is
  96.         p = Instance.new("Part")
  97.         p.Anchored = true
  98.         p.CanCollide = false
  99.         p.Transparency = 0.9
  100.         --p.BrickColor = BrickColor.new(index%255)
  101.         p.Parent = workspace
  102.         p.Size = region.Size
  103.         p.CFrame = region.CFrame
  104.         ]]
  105.         changed = 0
  106.         materials, occupancy = Terrain:ReadVoxels(region, 4)
  107.         size = materials.Size
  108.         for x = 1, size.X do
  109.             matsX = materials[x] --Creating variables to reduce amount of searching
  110.             occX = occupancy[x]
  111.             for y = 1, size.Y do
  112.                 matsY = matsX[y]
  113.                 occY = occX[y]
  114.                 for z = 1, size.Z do
  115.                     totalProcessed = totalProcessed + 1
  116.                     if matsY[z] == WATER then
  117.                         matsY[z] = AIR
  118.                         occY[z] = 0
  119.                         changed = changed + 1
  120.                     end
  121.                 end
  122.             end
  123.             --[[
  124.             --Use this and comment out the wait below if you want constant progress updates
  125.             if x%10 == 0 then
  126.                 print((floor((index-1+x/size.X)/#regions*1000+0.5)/10).."% complete!")
  127.                 wait(0)
  128.             end
  129.             ]]
  130.         end
  131.        
  132.         if changed > 0 then --No need to write if there is no changes
  133.             Terrain:WriteVoxels(region, 4, materials, occupancy)
  134.         end
  135.         --Comment the following block out if you use the above wait ability.
  136.         if index%4 == 0 then
  137.             print((floor(index/#regions*1000+0.5)/10).."% complete!")
  138.             wait(0)
  139.         end
  140.         totalRemoved = totalRemoved + changed
  141.     end
  142.    
  143.     print("Total time elapsed: ", (tick()-functionStart), " seconds!")
  144.     print("Total water blocks removed: ", totalRemoved)
  145.     print("Total cells processed: ", comma_value(totalProcessed))
  146.     print("Region/Chunks: ", #regions)
  147.     print("Total Volume: ", totalVolume, "cubic studs")
  148.     print("Done processing")
  149. end
  150.  
  151. --Use this function as pass it the origin the removing should take place, and the size
  152. --of the area to check for water.
  153. --removeWater(origin, size)
  154. removeWater(Vector3.new(0,0,0), Vector3.new(3000,2000,3000))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top