Advertisement
Gavin_Sharp

removeWater

Apr 23rd, 2019
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.25 KB | None | 0 0
  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.     local relative = (point - region.CFrame.p) / region.Size
  9.     return -0.5 <= relative.X and relative.X <= 0.5
  10.        and -0.5 <= relative.Y and relative.Y <= 0.5
  11.        and -0.5 <= relative.Z and relative.Z <= 0.5
  12. end
  13.  
  14. local function round(num)
  15.     return math.floor(num + .5)
  16. end
  17.  
  18. local function getAlignedPosition(pos)
  19.     local x = round(pos.X)
  20.     x = x - x%4 + 2
  21.     local y = round(pos.Y)
  22.     y = y - y%4 + 2
  23.     local z = round(pos.Z)
  24.     z = z - z%4 + 2
  25.    
  26.     return Vector3.new(x,y,z)
  27. end
  28.  
  29. local function comma_value(n)
  30.     local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
  31.     return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
  32. end
  33.  
  34. local function removeWater(voxelPos, worldSize)
  35.     local functionStart = tick()
  36.     voxelPos = getAlignedPosition(voxelPos)
  37.     local material = nil
  38.     local occupancy = nil
  39.    
  40.     local boundaryRegion = Region3.new(voxelPos-worldSize/2, voxelPos+worldSize/2)
  41.     boundaryRegion:ExpandToGrid(4)
  42.     local minPoint = boundaryRegion.CFrame.p-boundaryRegion.Size/2
  43.     print("Starting")
  44.     local partsProcessed = 0
  45.    
  46.     local regions = {}
  47.    
  48.     local start = getAlignedPosition(minPoint)
  49.     local xOffset = 0
  50.     local yOffset = 0
  51.     local zOffset = 0
  52.     local done = false
  53.     repeat
  54.         local startPos = start+Vector3.new(xOffset,yOffset,zOffset)
  55.         local size = boundaryRegion.CFrame.p+boundaryRegion.Size/2-startPos
  56.         local x,y,z = size.X, size.Y, size.Z
  57.         if x > processBlockSize then
  58.             x = processBlockSize
  59.         end
  60.         if y > processBlockSize then
  61.             y = processBlockSize
  62.         end
  63.         if z > processBlockSize then
  64.             z = processBlockSize
  65.         end
  66.         size = Vector3.new(x,y,z)
  67.         local region = Region3.new(startPos, startPos+size)
  68.         region = region:ExpandToGrid(4)
  69.         table.insert(regions, region)
  70.         zOffset = zOffset + processBlockSize
  71.         if zOffset >= boundaryRegion.Size.Z then
  72.             zOffset = 0
  73.             yOffset = yOffset + processBlockSize
  74.         end
  75.         if yOffset >= boundaryRegion.Size.Y then
  76.             yOffset = 0
  77.             xOffset = xOffset + processBlockSize
  78.         end
  79.         if xOffset >= boundaryRegion.Size.X then
  80.             done = true
  81.         end
  82.     until done
  83.    
  84.     print("Writing to ", #regions, " regions!")
  85.     print("Removing water...")
  86.     local totalRemoved = 0
  87.     local totalProcessed = 0
  88.     local totalVolume = worldSize.X*worldSize.Y*worldSize.Z
  89.     local changed,materials,occupancy,size,matsX,occX,matsY,occY,p
  90.     for index, region in pairs(regions) do
  91.         changed = 0
  92.         materials, occupancy = Terrain:ReadVoxels(region, 4)
  93.         size = materials.Size
  94.         for x = 1, size.X do
  95.             matsX = materials[x]
  96.             occX = occupancy[x]
  97.             for y = 1, size.Y do
  98.                 matsY = matsX[y]
  99.                 occY = occX[y]
  100.                 for z = 1, size.Z do
  101.                     totalProcessed = totalProcessed + 1
  102.                     if matsY[z] == WATER then
  103.                         matsY[z] = AIR
  104.                         occY[z] = 0
  105.                         changed = changed + 1
  106.                     end
  107.                 end
  108.             end
  109.         end
  110.        
  111.         if changed > 0 then
  112.             Terrain:WriteVoxels(region, 4, materials, occupancy)
  113.         end
  114.         if index%4 == 0 then
  115.             print((floor(index/#regions*1000+0.5)/10).."% complete!")
  116.             wait(0)
  117.         end
  118.         totalRemoved = totalRemoved + changed
  119.     end
  120.    
  121. end
  122.  
  123. removeWater(Vector3.new(0,0,0), Vector3.new(3000,2000,3000))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement