Advertisement
k_goos

ClearOut

Dec 11th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.02 KB | None | 0 0
  1. local xPos, yPos, zPos = 0, 0, 0 --location zPos (bigger then zero is below startpoint and smaller then zero is above startpoint)
  2. local xSave, ySave, zSave, dirSave = 0, 0, 0, 0
  3. local direction = 0 --direction in for ways 0 is start position
  4. local al = true --alternate
  5. local al2 = true --alternate2
  6.  
  7. local firsttime = true
  8. local length, width, height = 0, 0, 0
  9.  
  10. local args={...}
  11.  
  12. function main()
  13.     if #args ~= 3 then
  14.         print("ClearOut<height><width><length>")
  15.     else   
  16.         length = tonumber(args[1])
  17.         width = tonumber(args[2])
  18.         height = tonumber(args[3])
  19.        
  20.         for z = 0, height - 1 do
  21.             for x = 0, width - 1 do
  22.                 for y = 0, length - 2 do
  23.                     if firsttime then
  24.                         y = y + 1
  25.                         firsttime = false
  26.                     end
  27.                     Forward()          
  28.                 end
  29.                 if x < length - 1 then
  30.                     if al then
  31.                         RightUTurn(true)
  32.                         al = false
  33.                     else
  34.                         LeftUTurn(true)
  35.                         al = true
  36.                     end
  37.                 end
  38.             end
  39.             if height > 0 then
  40.                 Down()
  41.             else
  42.                 Up()
  43.             end
  44.             RightUTurn(false)
  45.            
  46.             if al2 then
  47.                 if direction == 0 then
  48.                     al = false
  49.                 else
  50.                     al = true
  51.                 end
  52.                 al2 = false
  53.             else
  54.                 if direction == 0 then
  55.                     al = true
  56.                 else
  57.                     al = false
  58.                 end
  59.                 al2 = true
  60.             end
  61.         end
  62.     end
  63. end
  64.  
  65. function CheckFuel()
  66.     local fuel = turtle.getFuelLevel()
  67.     local level = xPos + yPos + math.abs(zPos) + 2
  68.    
  69.     if fuel > level then
  70.         return true;
  71.     else
  72.         print("Taking new fuel from slot 1")
  73.         if turtle.getItemCount(1) > 0 then
  74.             turtle.select(1)
  75.             if turtle.refuel(5) == false then
  76.                 print("No fuel in slot")
  77.                 SaveLocation()
  78.                 print("Going back to chest")
  79.                 Goto(0,0,0)
  80.                 if CheckChestFuel() == false then
  81.                     RightUTurn(false)
  82.                     error("No fuel in chest")
  83.                 end
  84.                 CheckFuel()
  85.                 RestoreLocation()
  86.             end
  87.         end
  88.     end
  89. end
  90.  
  91. function Forward()
  92.     CheckFuel()
  93.     while turtle.forward() == false do
  94.         CheckInventory(0)
  95.         turtle.select(2)
  96.         turtle.dig()
  97.     end
  98.    
  99.     if direction == 0 then
  100.         yPos = yPos + 1
  101.     elseif direction == 2 then
  102.         yPos = yPos - 1
  103.     elseif direction == 1 then
  104.         xPos = xPos + 1
  105.     else
  106.         xPos = xPos - 1
  107.     end
  108. end
  109.  
  110. function Down()
  111.     CheckFuel()
  112.     while turtle.down() == false do
  113.         CheckInventory(2)
  114.         turtle.select(2)
  115.         turtle.digDown()
  116.     end
  117.     zPos = zPos + 1
  118. end
  119.  
  120. function Up()
  121.     CheckFuel()
  122.     while turtle.up() == false do
  123.         CheckInventory(1)
  124.         turtle.select(2)
  125.         turtle.digUp()
  126.     end
  127.     zPos = zPos - 1
  128. end
  129.  
  130. function Left(forward)
  131.     turtle.turnLeft()
  132.    
  133.     SubstractDirection()
  134.    
  135.     if forward then
  136.         Forward()
  137.     end
  138. end
  139.  
  140. function Right(forward)
  141.     turtle.turnRight()
  142.    
  143.     AddDirection()
  144.    
  145.     if forward then
  146.         Forward()
  147.     end
  148. end
  149.  
  150. function AddDirection()
  151.     direction = direction + 1
  152.    
  153.     if direction > 3 then
  154.         direction = 0
  155.     end
  156. end
  157.  
  158. function SubstractDirection()
  159.     direction = direction - 1
  160.    
  161.     if direction < 0 then
  162.         direction = 3
  163.     end
  164. end
  165.  
  166. function RightUTurn(forward)
  167.     Right(forward)
  168.     Right(false)
  169. end
  170.  
  171. function LeftUTurn(forward)
  172.     Left(forward)
  173.     Left(false)
  174. end
  175.  
  176. function Goto(xLoc, yLoc, zLoc)
  177.     while zPos ~= zLoc do
  178.         if zLoc > zPos then
  179.             Up()
  180.         else
  181.             Down()
  182.         end
  183.     end
  184.    
  185.     while direction ~= 3 do
  186.         if direction == 0 then
  187.             Left(false)
  188.         else
  189.             Right(false)
  190.         end
  191.     end
  192.        
  193.     while xPos > xLoc do
  194.         Forward()
  195.     end
  196.    
  197.     Left()
  198.    
  199.     while yPos > yLoc do
  200.         Forward()
  201.     end
  202. end
  203.  
  204. function ChangeDirection(dir)
  205.     while direction ~= dir do
  206.         Right(false)
  207.     end
  208. end
  209.  
  210. function SaveLocation()
  211.     xSave, ySave, zSave, dirSave = xPos, yPos, zPos, direction
  212. end
  213.  
  214. function RestoreLocation()
  215.     Goto(xSave, ySave, zSave)
  216.     ChangeDirection(dirSave)
  217. end
  218.  
  219. function CheckInventory(check)
  220.     if turtle.getItemCount(16) > 0 then
  221.         if BlockFit(check) == false then
  222.             print("Inventory is full")
  223.             SaveLocation()
  224.             print("Returning to chest...")
  225.             Goto(0, 0, 0)
  226.        
  227.             print("Empty inventory")
  228.             EmptyInventory()
  229.            
  230.             print("Restore location")
  231.             RestoreLocation()
  232.         end
  233.     end
  234. end
  235.  
  236. function BlockFit(check)
  237.     for i = 1, 16 do
  238.         if turtle.getItemSpace(i) > 0 then
  239.             turtle.select(i)
  240.             if check == 0 then
  241.                 if turtle.compare() then
  242.                     return true
  243.                 end
  244.             elseif check == 1 then
  245.                 if turtle.compareUp() then
  246.                     return true
  247.                 end
  248.             else
  249.                 if turtle.compareDown() then
  250.                     return true
  251.                 end
  252.             end
  253.         end
  254.     end
  255.     return false
  256. end
  257.  
  258. function EmptyInventory()
  259.     for i = 2, 16 do
  260.         print("Emptying slot " .. i)
  261.         if turtle.getItemCount(i) ~= 0 then
  262.             turtle.select(i)
  263.            
  264.             if turtle.drop() == false then
  265.                 RightUTurn(false)
  266.                 error("Chest is full")
  267.             end
  268.         end
  269.     end
  270. end
  271.  
  272. function GetEmptySlot()
  273.     for i = 2, 16 do
  274.         if turtle.getItemCount(i) == 0 then
  275.             return i
  276.         end
  277.     end
  278.     return 0
  279. end
  280.  
  281. function CheckChestFuel()
  282.     local slot = GetEmptySlot()
  283.     if slot == 0 then
  284.         EmptyInventory()
  285.     else
  286.         turtle.select(slot)
  287.         for i = 0, 10 do
  288.             if turtle.suck() == false then
  289.                 RightUTurn(false)
  290.                 error("No fuel found")
  291.             end
  292.             if turtle.drop() == false then
  293.                 RightUTurn(false)
  294.                 error("Chest is full")
  295.             end
  296.         end
  297.         CheckFuel()
  298.     end
  299. end
  300.  
  301. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement