Guest User

Quarry

a guest
Jan 12th, 2013
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.38 KB | None | 0 0
  1. --Variables for tracking position
  2. --will be used in files later
  3. --for recovery from server reboot
  4. --initialised to 0 to indicate no movement
  5.  
  6. recover = 0
  7. fwdPos = 0
  8. columnPos = 0
  9. depthPos = 0
  10. orientationPos = 0
  11. fwdTarget = 0
  12. columnTarget = 0
  13. depthTarget = 0
  14. isRefuelling = 0
  15. isUnloading = 0
  16.  
  17. --function to update all variables
  18. --for recovery program in file
  19. --check for enderchests
  20.  
  21. function updateFile()
  22.     fs.open("quarryData/","w")
  23.     file.writeLine(recover)
  24.     file.writeLine(fwdPos)
  25.     file.writeLine(columnPos)
  26.     file.writeLine(depthPos)    
  27.     file.writeLine(orientationPos)
  28.     file.writeLine(fwdTarget)
  29.     file.writeLine(columnTarget)
  30.     file.writeLine(depthTarget)
  31.     file.writeLine(isRefuelling)
  32.     file.writeline(isUnloading)
  33.     file.close()
  34. end
  35.  
  36.  
  37. function refuel()    
  38.     --check inventory for fuel
  39.     local maxFuel = 200
  40.    
  41.     --cycles through each slot refuelling
  42.     --using everything in that slot
  43.     --until full
  44.     for i=3, 16 do
  45.         if turtle.getFuelLevel() < maxFuel then
  46.             turtle.select(i)
  47.             turtle.refuel()
  48.         end
  49.     end
  50.    
  51.     --if not enough, use enderchest
  52.     if turtle.getFuelLevel() < maxFuel then
  53.         --select enderchest, place above
  54.         while turtle.getItemCount(2) ~= 0 do    
  55.             sleep(1)
  56.             turtle.select(2)
  57.             turtle.placeUp()
  58.         end
  59.        
  60.         isRefuelling = 1 --enderchest out
  61.         updateFile()
  62.        
  63.         --keep grabbing fuel until full
  64.         while turtle.getFuelLevel() < maxFuel do
  65.             if turtle.suckUp() == false then
  66.                 break --if no fuel available
  67.                       --then stop refueling
  68.             end
  69.             turtle.refuel()
  70.         end
  71.        
  72.         --replace enderchest
  73.         turtle.digUp()
  74.         isRefuelling = 0
  75.         updateFile()
  76.     end
  77.    
  78.     --reselect slot 1
  79.     turtle.select(1)
  80. end
  81.  
  82.  
  83. function unload()
  84.     --places enderchest above
  85.     while turtle.getItemCount(1) ~= 0 do
  86.         sleep(1)
  87.         turtle.select(1)
  88.         turtle.placeUp()
  89.     end
  90.     isUnloading = 1
  91.     updateFile()
  92.    
  93.     --dumps all items in chest
  94.     for i=3, 16 do
  95.         turtle.select(i)
  96.         turtle.dropUp()
  97.     end
  98.    
  99.     --reselects slot 1
  100.     turtle.select(1)
  101.     turtle.digUp() --gets enderchest back
  102.     isUnloading = 0
  103.     updateFile()
  104. end
  105.  
  106. function goFwd(dist)
  107.     local minFuel = 10
  108.  
  109.     for i=1, dist do
  110.         --Refuel if needed
  111.         if turtle.getFuelLevel() < minFuel then
  112.             refuel()
  113.         end
  114.        
  115.         --move + dig anything infront
  116.         while not turtle.forward() do
  117.             if turtle.getItemCount(16) > 0 then
  118.                 unload()
  119.             end
  120.             turtle.dig()
  121.         end
  122.        
  123.         --update File
  124.         --if moving up a column
  125.         if orientationPos == 0 then
  126.             fwdPos = fwdPos + 1
  127.             updateFile()
  128.         end
  129.        
  130.         --else if moving along a column
  131.         if orientationPos == 1 then
  132.             columnPos = columnPos + 1
  133.             fwdPos = 0
  134.             updateFile()
  135.         end
  136.     end
  137. end
  138.  
  139.  
  140. function digRect(N, M)
  141.     --digs an N across M up square
  142.     --dig square except last row
  143.     orientationPos = 0
  144.     updateFile()
  145.     if N > 1 then
  146.         for i=1, (N-1) do
  147.             --on odd rows
  148.             if i % 2 == 1 then
  149.                 goFwd((M-1))
  150.                 turtle.turnRight()
  151.                 orientationPos = 1
  152.                 updateFile()
  153.            
  154.                 goFwd(1)
  155.                 turtle.turnRight()
  156.                 orientationPos = 0
  157.                 updateFile()
  158.        
  159.             else --on even rows
  160.                 goFwd((M-1))
  161.                 turtle.turnLeft()
  162.                 orientationPos = 1
  163.                 updateFile()
  164.            
  165.                 goFwd(1)
  166.                 turtle.turnLeft()
  167.                 orientationPos = 0
  168.                 updateFile()        
  169.             end    
  170.         end
  171.     end
  172.     --finish last row without the dig turn
  173.     --turn so in bottom left of square
  174.     goFwd((M-1))
  175.     turtle.turnRight()
  176.     orientationPos = 1
  177.     updateFile()
  178.    
  179.     if (N%2) == 1 then
  180.         turtle.turnRight()
  181.     end
  182.     orientationPos = 2
  183.     updateFile()
  184. end
  185.  
  186. function goDown(N)
  187.     local minFuel = 10
  188.    
  189.     for i=1, N do
  190.         --refuel if needed
  191.         if (turtle.getFuelLevel() < minFuel) then
  192.             refuel()
  193.         end
  194.        
  195.         --checks for inv space
  196.         if (turtle.getItemCount(16) > 0) then
  197.             unload()
  198.         end
  199.    
  200.         --then goes down
  201.         turtle.digDown()
  202.         turtle.down()
  203.         depthPos = depthPos - 1
  204.         updateFile()
  205.     end
  206. end
  207.  
  208. function goUp(N)
  209.     --not used in file so far
  210.     local minFuel = 10
  211.    
  212.     for i=1, N do
  213.         --checks fuel
  214.         if turtle.getFuelLevel() < minFuel then
  215.             refuel()
  216.         end
  217.        
  218.         --checks for space
  219.         if turtle.getItemCount(16) > 0 then
  220.             unload()
  221.         end
  222.        
  223.         while not turtle.up() do
  224.             turtle.digUp()
  225.         end
  226.     end
  227. end
  228.  
  229. function quarry(depth, sizeAccr, sizeFwd)
  230.     local temp = 0
  231.     for i=1, depth do
  232.        
  233.         --dig layer
  234.         goDown(1)
  235.         digRect(sizeAccr, sizeFwd)
  236.        
  237.         --if went across even num rows
  238.         --flip dimensions for next go
  239.         if (sizeAccr % 2 == 0) then
  240.             temp = sizeAccr
  241.             sizeAccr = sizeFwd
  242.             sizeFwd = temp
  243.            
  244.             --update file to show this
  245.             temp = fwdTarget
  246.             fwdTarget = columnTarget
  247.             columnTarget = temp
  248.             updateFile()
  249.         end
  250.         orientationPos = 3
  251.     end
  252. end
  253.  
  254. function recover()
  255.     --uses global variables
  256.     --taken from file
  257.    
  258.     --if it is mid refuel
  259.     if isRefuelling == 1 then
  260.         turtle.select(2)
  261.         turtle.refuel()
  262.         turtle.digUp()
  263.         isRefuelling = 0
  264.         updateFile()
  265.     end
  266.    
  267.     --if it is mid unload
  268.     if isUnloading == 1 then
  269.         for i=3, 16 do
  270.             turtle.select(i)
  271.             turtle.dropUp()
  272.         end
  273.         isUnloading = 0
  274.         updateFile()
  275.     end
  276.    
  277.    
  278.    
  279.    
  280.      
  281.     --OTHERWISE--
  282.     --if we are midway through a
  283.     --forward move, then get to its end
  284.     if ((fwdPos + 1) < fwdTarget) and
  285.        (fwdPos > 0) then
  286.         local tempDist = fwdTarget - (fwdPos + 1)
  287.         goFwd(tempDist)
  288.     end
  289.    
  290.     --if we are at the end of a column
  291.     --that isnt the last column
  292.     --get to the start of the next one
  293.    
  294.     if ((fwdPos + 1) == fwdTarget) and
  295.        ((columnPos + 1) < columnTarget) then
  296.         if orientationPos == 0 then
  297.             if (columnPos % 2 == 1) then
  298.                 turtle.turnRight()
  299.             else
  300.                 turtle.turnLeft()    
  301.             end
  302.             orientationPos = 1
  303.             updateFile()
  304.         end
  305.        
  306.         goFwd(1)
  307.         if (columnPos % 2 == 0) then
  308.             turtle.turnRight()
  309.         else
  310.             turtle.turnLeft()
  311.         end
  312.         orientationPos = 0
  313.         updateFile()
  314.                
  315.     end
  316.    
  317.     --if we are at the start of a column
  318.     --but facing the wrong way
  319.     --rotate the right way
  320.     if (fwdPos == 0) and
  321.        (orientationPos == 1) then
  322.         if columnPos % 2 == 0 then
  323.             turtle.turnRight()                        
  324.         else
  325.             turtle.turnLeft()
  326.         end
  327.         orientationPos = 0
  328.         updateFile()
  329.     end
  330.    
  331.     --if at start of column and
  332.     --facing the right way
  333.     --dig a rectangle to the end
  334.     if (fwdPos == 0) and
  335.        (orientationPos == 0) then
  336.         local tempCols = columnTarget - columnPos
  337.         digRect(tempCols, fwdTarget)
  338.     end
  339.    
  340.     --if in corner and orientation != 2 or 3
  341.     if ((columnPos + 1) == columnTarget)
  342.        and ((fwdPos + 1) == fwdTarget)
  343.        and (orientationPos < 2) then
  344.        
  345.         if orientationPos == 0 then
  346.             turtle.turnRight()
  347.             orientationPos = 1
  348.             updateFile()
  349.         end
  350.        
  351.         if (columnTarget % 2 == 0) then
  352.             turtle.turnRight()
  353.         end
  354.        
  355.         orientationPos = 2
  356.         updateFile()
  357.        
  358.         local tempDepth = depthTarget - depthPos
  359.         if (columnTarget % 2 == 0) then
  360.             local tempMem = fwdTarget
  361.             fwdtarget = columnTarget
  362.             columnTarget = tempMem
  363.         end
  364.         orientationPos = 3
  365.         updateFile()
  366.         quarry(tempDpeth, columnTarget, fwdTarget)
  367.         return 0
  368.     end
  369.    
  370.     --if square is done, but row/col flip isnt  
  371.     if orientationPos == 2 then
  372.         local tempDepth = depthTarget - depthPos
  373.         if (columnTarget % 2 == 0) then
  374.             quarry(tempDepth, fwdTarget, columnTarget)
  375.         else
  376.             quarry(tempDepth, columnTarget, fwdTarget)
  377.         end
  378.         return 0
  379.     end
  380.    
  381.     --if the entire square is done
  382.     if orientationPos == 3 then
  383.         local tempDepth = depthTarget - depthPos
  384.         quarry(tempDepth, columnTarget, fwdTarget)
  385.         return 0
  386.     end    
  387. end
  388.  
  389. --ACTUAL PROGRAM--
  390. --recover if needed
  391.  
  392. --get data from file
  393. file = fs.open("quarryData/","r")
  394. local fileData = {}
  395. local line = file.readLine()
  396. repeat
  397.     table.insert(fileData,line)
  398.     line = file.readLine()
  399. until line == nil
  400. file.close()
  401.  
  402. --update global variables with file data
  403. recover = fileData[1]
  404. fwdPos = fileData[2]
  405. columnPos = fileData[3]
  406. depthPos = fileData[4]
  407. orientationPos = fileData[5]
  408. fwdTarget = fileData[6]
  409. columnTarget = fileData[7]
  410. depthTarget = fileData[8]
  411. isRefuelling = fileData[9]
  412. isUnloading = fileData[10]
  413.  
  414. if recover == 1 then
  415.     print("recovering from saved location")
  416.     recoverProg()
  417.     recover = 0
  418.     updateFile()
  419. end
  420.  
  421.  
  422. term.clear()
  423. term.setCursorPos(1, 1)
  424. print("Place item chest in slot 1")
  425. print("and fuel chest in slot 2")
  426. print("Then enter how far you would like to")
  427. print("dig down")
  428. local depth = read()
  429. local depthNum = tonumber(depth)
  430.  
  431. print("Enter how wide and long the square")
  432. print("to dig should be")
  433. local sizeAccr = read()
  434. local sizeAccrNum = tonumber(sizeAccr)
  435.  
  436. local sizeFwd = read()
  437. local sizeFwdNum = tonumber(sizeFwd)
  438.  
  439. columnTarget = sizeAccrNum
  440. fwdTarget = sizeFwdNum
  441. depthTarget = depthNum
  442. recover = 1
  443. updateFile()
  444.  
  445. quarry(depthNum, sizeAccrNum, sizeFwdNum)
  446. recover = 0
Advertisement
Add Comment
Please, Sign In to add comment