Advertisement
leftler

Turtle Flatten

Mar 4th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.75 KB | None | 0 0
  1. -- flatten
  2. -- Will clear all the blocks in the square area and all the blocks above it, up to the first row of 5 air block it encounters
  3. -- Arguments <Forward> <Right> [blocks to check]
  4.  
  5.  
  6. --Config settings
  7. local maxAirRun = 5
  8.  
  9. local tArgs = { ... }
  10.  
  11. if #tArgs < 2 then
  12.   print("Usage: <Forward> <Right> [blocks to check]")
  13.   return
  14. end
  15.  
  16.  
  17. --program temp variables
  18. local maxX = tonumber(tArgs[1])
  19. local maxZ = tonumber(tArgs[2])
  20.  
  21. if(#tArgs > 2) then
  22.     maxAirRun = tonumber(tArgs[3])
  23. end
  24.  
  25. --A helper function to try and move forward, it will attempt to dig if it is blocked.
  26. local function moveForward()
  27.   if turtle.forward() == false then
  28.     --Failed to move, see if we can dig our way forward
  29.     while turtle.dig() do
  30.         turtle.drop()
  31.           --Keep digging till we can't dig any more, in case gravel is falling.
  32.         end
  33.         if turtle.forward() == false then
  34.           print("I am either blocked or out of fuel.")
  35.           return false
  36.         end
  37.   end
  38.   return true  
  39. end
  40.  
  41. --A helper function to try and move down, it will attempt to dig if it is blocked.
  42. local function moveDown()
  43.   if turtle.down() == false then
  44.     --Failed to move, see if we can dig our way down
  45.     turtle.digDown()
  46.         if turtle.down() == false then
  47.           print("I am either blocked or out of fuel.")
  48.           return false
  49.         end
  50.   end
  51.   return true  
  52. end
  53.  
  54. --A helper function to try and move down, it will attempt to dig if it is blocked.
  55. local function moveUp()
  56.   if turtle.up() == false then
  57.     --Failed to move, see if we can dig our way down
  58.     while turtle.digUp() do
  59.           --Keep digging till we can't dig any more, in case gravel is falling.
  60.         end
  61.         if turtle.up() == false then
  62.           print("I am either blocked or out of fuel.")
  63.           return false
  64.         end
  65.   end
  66.   return true  
  67. end
  68.  
  69. --Dig the column of dirt up
  70. local function digColumnUp()
  71.     airCount = 0
  72.     moveCount = 0
  73.     --Keep going up till we hit the top
  74.     while airCount < maxAirRun do
  75.         local dug = false
  76.         while(turtle.digUp()) do
  77.             turtle.dropDown()
  78.             dug = true
  79.             airCount = 0
  80.         end
  81.        
  82.         if(dug == false) then
  83.             airCount = airCount + 1
  84.         end
  85.        
  86.         --move up to the next level
  87.         if(moveUp() == false) then
  88.            error("Could not move up, WTF?")
  89.         end
  90.         moveCount = moveCount + 1
  91.     end
  92.    
  93.     for i = 1, moveCount do
  94.         if(moveDown() == false) then
  95.            error("Could not move down, WTF?")
  96.         end
  97.     end
  98. end
  99.  
  100. moveForward()
  101.  
  102. for z = 1, maxZ do
  103.     for x = 1, maxX - 1 do
  104.         --Only run at the start of the 2nd loop
  105.         if(x == 1 and z > 1) then
  106.             if(z % 2 == 0) then
  107.                 turtle.turnRight()
  108.             else
  109.                 turtle.turnLeft()
  110.             end
  111.             digColumnUp()
  112.             moveForward()
  113.             if(z % 2 == 0) then
  114.                 turtle.turnRight()
  115.             else
  116.                 turtle.turnLeft()
  117.             end
  118.         end
  119.         digColumnUp()
  120.         moveForward()
  121.     end
  122. end
  123.  
  124. --dig up the last column
  125. digColumnUp()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement