Guest User

quarry

a guest
Nov 27th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 30.23 KB | None | 0 0
  1. --fuel enderchest slot 15
  2. --item enderchest slot 16
  3.  
  4. --global variables for recovery file
  5.   --initialise all to 0
  6. data = {}
  7. --if program should recover from file
  8. data["shouldRecover"] = 0
  9. --length of the quarry area
  10. data["length"] = 0
  11. --width of the quarry area
  12. data["width"] = 0
  13. --remaining distance forward
  14.   --until current move is complete
  15. data["moveDistLeft"] = 0
  16. --current column being mined in rect
  17. data["currCol"] = 0
  18. --current direction of mining
  19.   -- -1 == left; 1 = right
  20. data["currDir"] = 0
  21. --current depth remaining of quarry
  22. data["currDepth"] = 0
  23. --state (column, corner or layerChange)
  24. data["state"] = 0
  25. --number of turns required before
  26.   --we can dig straight again
  27. data["numTurnsReq"] = 0
  28. --direction last enderchest was placed
  29. data["lastEChestDir"] = 0
  30. data["currLayer"] = 0
  31.  
  32. --writes a file with the recover data
  33. --file name is recoverFile
  34. function updateFile()
  35.   --open the recovery file
  36.     --in 'overwrite' mode
  37.   file = fs.open("recoverFile/","w")
  38.     --write all recovery info to file
  39.     file.writeLine(data["shouldRecover"])
  40.     file.writeLine(data["length"])
  41.     file.writeLine(data["width"])
  42.     file.writeLine(data["moveDistLeft"])
  43.     file.writeLine(data["currCol"])
  44.     file.writeLine(data["currDir"])
  45.     file.writeLine(data["currDepth"])
  46.     file.writeLine(data["state"])
  47.     file.writeLine(data["numTurnsReq"])
  48.     file.writeLine(data["lastEChestDir"])
  49.     file.writeLine(data["currLayer"])
  50.   --close the file now we're done with it    
  51.   file.close()  
  52. end
  53.  
  54. function loadFile()
  55.   --open the file in read mode
  56.   file = fs.open("recoverFile/","r")
  57.     --load data into temporary table
  58.     local fileData = {}
  59.     local line = file.readLine()
  60.     repeat
  61.       table.insert(fileData,line)
  62.       line = file.readLine()
  63.     until line == nil  
  64.   --close the file now we're done with it
  65.   file.close()
  66.  
  67.   --put the data in the proper table
  68.     --after converting it to numbers
  69.   data["shouldRecover"] = tonumber(fileData[1])
  70.   data["length"] = tonumber(fileData[2])
  71.   data["width"] = tonumber(fileData[3])
  72.   data["moveDistLeft"] = tonumber(fileData[4])
  73.   data["currCol"] = tonumber(fileData[5])
  74.   data["currDir"] = tonumber(fileData[6])
  75.   data["currDepth"] = tonumber(fileData[7])
  76.   data["state"] = tonumber(fileData[8])
  77.   data["numTurnsReq"] = tonumber(fileData[9])
  78.   data["lastEChestDir"] = tonumber(fileData[10])
  79.   data["currLayer"] = tonumber(fileData[11])
  80. end
  81.  
  82. --determines if EChest in 'slot' is out
  83. function isUsingEChest(slot)
  84.   --if there is nothing in EChest slot
  85.   if turtle.getItemCount(slot) == 0 then
  86.     --the chest is out
  87.     return true
  88.   else --something is in EChest slot
  89.     --chest is not out
  90.     return false  
  91.   end
  92. end
  93.  
  94. --returns true if inventory is full
  95. function checkInventory()
  96.   if turtle.getItemCount(14) == 0 then
  97.     return false
  98.   else
  99.     return true
  100.   end
  101. end
  102.  
  103. --returns true if
  104.   --there is a block in 'direction'
  105. function detectDirection(direction)
  106.   --down
  107.   if direction == -1 then
  108.     return turtle.detectDown()
  109.   --fwd  
  110.   elseif direction == 0 then
  111.     return turtle.detect()
  112.   --up
  113.   else --if direction == 1 then
  114.     return turtle.detectUp()
  115.   end
  116. end
  117.  
  118. --performs turtle.suck() in direction
  119.   --and returns whether the
  120.   --operation was successful
  121. function suckDirection(direction)
  122.   --down
  123.   if direction == -1 then
  124.     return turtle.suckDown()
  125.   --fwd
  126.   elseif direction == 0 then
  127.     return turtle.suck()
  128.   --up
  129.   else --direction == 1
  130.     return turtle.suckUp()
  131.   end
  132. end
  133.  
  134. --digs once in 'direction'
  135. --does not make any checks
  136. function digDirection(direction)
  137.   if direction == -1 then
  138.     return turtle.digDown()
  139.   elseif direction == 0 then
  140.     return turtle.dig()
  141.   else --direction == 1 then
  142.     return turtle.digUp()
  143.   end
  144. end
  145.  
  146. --turns the turtle in 'direction'
  147. function turnDirection(direction)
  148.   if direction < 0 then
  149.     return turtle.turnLeft()
  150.   else
  151.     return turtle.turnRight()
  152.   end
  153. end
  154.  
  155. --returns name of block
  156. --in direction 'direction'
  157.   -- -1 = Down
  158.   -- 0  = Fwd
  159.   -- 1  = Up
  160. function getBlockName(direction)
  161.   --get data from relevant direction
  162.   local success, data
  163.   --if block below
  164.   if direction < 0 then
  165.     success, data = turtle.inspectDown()
  166.   --if block in front
  167.   elseif direction == 0 then
  168.     success, data = turtle.inspect()
  169.   --else: block above
  170.   else
  171.     success, data = turtle.inspectUp()
  172.   end
  173.  
  174.     --if there is a block above the turtle
  175.   if success then
  176.     --*debug code*
  177. --    print("Block Name: ", data.name)
  178. --    print("Block Data: ", data.metadata)
  179.     --return the block's name
  180.     return data.name
  181.   --if there is no block above the turtle
  182.   else
  183.     --return 'air' above the turtle
  184.     return "air"
  185.   end
  186. end
  187.  
  188. --returns a value based on
  189. --whether the turtle
  190. --should mine it or not
  191. --used to test for enderchest placement
  192.   --0 = no problem to mine
  193.   --1 = preferred not to mine
  194.   --2 = dangerous to mine
  195.       --but will move given time
  196.   --3 = impossible to mine
  197.       --or should never be mined
  198. function getMineability(blockName)
  199.   --if the block is not computercraft
  200.     --may need to handle warded blocks
  201.   if string.find(blockName, "ComputerCraft") == nil then
  202.     --*debug code*
  203. --    print("Not a computercraft block")  
  204.     --*/debug code*
  205.    
  206.     --create a mineability table
  207.     local mLevel = {}
  208.     mLevel["minecraft:bedrock"] = 3
  209.     mLevel["minecraft:chest"] = 1
  210.     mLevel["EnderStorage:enderChest"] = 3
  211.     mLevel["Thaumcraft:blockWarded"] = 3
  212.    
  213.     --get mining level from table
  214.     local level = mLevel[blockName]
  215.    
  216.     --if the block isnt in the table
  217.     if level == nil then
  218.       --*debug code*
  219. --      print("Block not in table")
  220. --      print("Mining level = 0")
  221.       --*/debug code*
  222.       --return a mining level of 0
  223.       return 0
  224.     --if the block is in the table
  225.     else
  226.       --*debug code*
  227. --      print("Block name found in table")
  228. --      print("Mining level = ", level)
  229.       --*/debug code
  230.       --return mining level from table
  231.       return level
  232.     end
  233.    
  234.   --if the block is computercraft
  235.   else
  236.     return 2
  237.   end
  238. end
  239.  
  240. --returns the mining level in direction dir
  241. function getMLevel(dir)
  242.   local level = getMineability(getBlockName(dir))
  243.   return level
  244. end
  245.  
  246. --returns the mining level
  247. --of all directions specified
  248. function getMLevels(down, fwd, up)
  249.   local upLevel
  250.   local fwdLevel
  251.   local downLevel
  252.  
  253.   --repeat the detection process until
  254.     --Enderchest location is found  
  255.   repeat
  256.     --calculate mining level of downward block
  257.     --if down is an option
  258.     if down then
  259.       --downLevel = mining level(down)
  260.       downLevel = getMineability(getBlockName(-1))
  261.     --if down is not an option
  262.     else
  263.       --downlevel = impossible
  264.       downLevel = 3
  265.     end
  266.  
  267.     --calculate mining level of fwd block
  268.     if fwd then
  269.       fwdLevel = getMineability(getBlockName(0))
  270.     else
  271.       fwdLevel = 3
  272.     end
  273.  
  274.     if up then
  275.       upLevel = getMineability(getBlockName(1))
  276.     else
  277.       upLevel = 3
  278.     end
  279.    
  280.     --if nowhere to place enderchest
  281.     if (9 == downLevel + fwdLevel + upLevel) then
  282.       print("Unable to place Enderchest")
  283.       print("Waiting 30s for assistance")
  284.       print("Before retrying placement")
  285.       sleep(30)
  286.     end
  287.   until (9 > downLevel + fwdLevel + upLevel)
  288.     --return all calculated mining levels
  289.     return downLevel, fwdLevel, upLevel
  290. end
  291.  
  292. --returns the direction an enderchest
  293. --should be placed
  294. --and the mining level of that direction
  295. function getEChestDir(down, fwd, up)
  296.   --get the levels of all specified directions
  297.   local downL, fwdL, upL = getMLevels(down, fwd, up)
  298.  
  299.   --find the lowest level
  300.   local minLevel = math.min(downL, fwdL, upL)
  301.  
  302.   --cycle through to return the lowest
  303.   if upL == minLevel then
  304.     return 1, minLevel
  305.   elseif downL == minLevel then
  306.     return -1, minLevel
  307.   else --fwdL = minLevel
  308.     return 0, minLevel
  309.   end
  310. end
  311.  
  312. --places the correct enderchest
  313. --in the specified direction
  314. --handles waiting for computercraft
  315.   --blocks to move out of the way
  316. --does not handle
  317.   --being surrounded by unbreakable blocks
  318. function placeEChest(slot, direction, mLevel)
  319.   --if the best direction is
  320.   --a computercraft block
  321.   if mLevel == 2 then
  322.   --wait for it to move
  323.     while 2 == getMLevel(direction) do
  324.       print("Computercraft block in way of chest")
  325.       print("Waiting 10s for it to move")
  326.       sleep(10)
  327.     end
  328.   end
  329.  
  330.   --clear a space for the chest
  331.   if direction == -1 then
  332.     while turtle.digDown() do
  333.       sleep(1)
  334.     end
  335.   elseif direction == 0 then
  336.     while turtle.dig() do
  337.       sleep(1)
  338.     end
  339.   else --direction == 1
  340.     while turtle.digUp() do
  341.       sleep(1)
  342.     end
  343.   end
  344.  
  345.   --place the chest
  346.   turtle.select(slot)
  347.   if direction == -1 then
  348.     while not turtle.placeDown() do
  349.       print("Failed to place enderchest")
  350.       print("Trying again in 1s")
  351.       sleep(1)
  352.     end
  353.     --update the file
  354.     data["lastEChestDir"] = -1
  355.   elseif direction == 0 then
  356.     while not turtle.place() do
  357.       print("Failed to place enderchest")
  358.       print("Trying again in 1s")
  359.       sleep(1)
  360.     end
  361.     --update the file
  362.     data["lastEChestDir"] = 0
  363.   else --direction == 1 then
  364.     while not turtle.placeUp() do
  365.       print("Failed to place enderchest")
  366.       print("Trying again in 1s")
  367.       sleep(1)
  368.     end
  369.     --update the data
  370.     data["lastEChestDir"] = 1
  371.     --and update the file
  372.     updateFile()
  373.   end
  374.   --reselect slot 1
  375.   turtle.select(1)
  376. end
  377.  
  378. function retrieveEChest(slot, direction)
  379.   turtle.select(slot)
  380.   --if chest is below
  381.   if direction == -1 then
  382.     --retrieve it
  383.     turtle.digDown()
  384.   elseif direction == 0 then
  385.     turtle.dig()
  386.   else --direction == 1 then
  387.     turtle.digUp()
  388.   end
  389.   --reselect slot 1
  390.   turtle.select(1)  
  391. end
  392.  
  393. --dumps every item in the inventory
  394. --into the enderchest in slot 16
  395. function unload()
  396.   --find where to place the enderchest
  397.   local direction, level = getEChestDir(true, true, true)
  398.   --place the enderchest in direction
  399.   placeEChest(16, direction, level)
  400.  
  401.   --unload everything into the enderchest
  402.   --for each slot in the turtle
  403.   for i = 1, 14 do
  404.     --select the slot
  405.     turtle.select(i)
  406.    
  407.     --while there's items in the slot
  408.     while turtle.getItemCount() > 0 do
  409.       --if enderchest is down
  410.       if direction == -1 then
  411.         --try to drop the items into it
  412.         --if that fails
  413.         if not turtle.dropDown() then
  414.           print("Item drop failed")
  415.           print("Waiting 5s for chest to clear")
  416.           sleep(5)
  417.         end
  418.       elseif direction == 0 then
  419.         if not turtle.drop() then
  420.           print("Item drop failed")
  421.           print("Waiting 5s for chest to clear")
  422.           sleep(5)
  423.         end    
  424.       else --if direction == 1 then
  425.         if not turtle.dropUp() then
  426.           print("Item drop failed")
  427.           print("Waiting 5s for chest to clear")
  428.           sleep(5)
  429.         end
  430.       end
  431.     end
  432.   end
  433.   --select the enderchest's slot
  434.   turtle.select(16)
  435.   --grab the enderchest
  436.   digDirection(direction)
  437.   --reselect slot 1
  438.   turtle.select(1)
  439. end
  440.  
  441.  
  442. --returns the amount of fuel which
  443. --would neccessitate turtle refuelling
  444. function getMinFuel()
  445.   --if for some reason the configs are
  446.   --set to a limit below 5
  447.   --min fuel = fuel limit/2
  448.   return math.min(5, (turtle.getFuelLimit()/2))
  449. end
  450.  
  451. --returns amount of fuel needed
  452. --before turtle can stop refuelling
  453. function getMaxFuel()
  454.   --only refuels up to 1,000 if limit
  455.   --if limit is above that
  456.   --if limit is below, fuels to limit
  457.   return math.min(1000, turtle.getFuelLimit())
  458. end
  459.  
  460. --returns true if turtle needs to refuel  
  461. function shouldRefuel()
  462.   if turtle.getFuelLevel() < getMinFuel() then
  463.     return true
  464.   else
  465.     return false
  466.   end
  467. end
  468.  
  469. function continueRefuel()
  470.   if turtle.getFuelLevel() < getMaxFuel() then
  471.     return true
  472.   else --at or above max fuel
  473.     return false
  474.   end
  475. end
  476.  
  477. --*NOT CODED YET*
  478. --returns true if an item is a valid fuel
  479. function isValidFuel()
  480.   return true  
  481. end
  482.  
  483. --uses whatever is in the inventory
  484. --to refuel the turtle
  485. function refuelFromInventory()
  486.   --for each inventory slot
  487.   for i = 1, 14 do
  488.     --select the slot
  489.     turtle.select(i)
  490.     --*debug code*
  491.     --print("refuelling from slot ", i)
  492.     --*/debug code*
  493.     --if the slot contains valid fuel
  494.     if turtle.refuel(0) then
  495.       --while there are items in the slot
  496.       if isValidFuel(i) then
  497.         while turtle.getItemCount() > 0 do
  498.           --if the turtle is below max fuel then
  499.           if continueRefuel() then
  500.             turtle.refuel(1)
  501.             --*debug code*
  502.             --print("Fuel level: ", turtle.getFuelLevel())
  503.             --*/debug code*
  504.           --if we have enough fuel
  505.           else
  506.             --*debug code*
  507.             print("Full of fuel")
  508.             --*/debug code*
  509.             --select slot 1
  510.             turtle.select(1)
  511.             --stop refuelling
  512.             return true
  513.           end        
  514.         end      
  515.       end
  516.     end
  517.   end
  518.   --reselect slot 1
  519.   turtle.select(1)
  520. end
  521.  
  522. --refuels the turtle from an enderchest
  523. --in direction 'direction'
  524. --requires a clear inventory
  525. function refuelFromEChest(direction)
  526.   local success
  527.   turtle.select(1)
  528.   --while we still need to refuel
  529.   while continueRefuel() do
  530.     --suck one fuel item
  531.       --from direction of enderchest      
  532.     if direction == -1 then
  533.       success = turtle.suckDown(1)
  534.     elseif direction == 0 then
  535.       success = turtle.suck(1)
  536.     else --direction == 1 then
  537.       success = turtle.suckUp(1)
  538.     end
  539.    
  540.     --if item was sucked from chest
  541.     if success then
  542.       --refuel with it
  543.       turtle.refuel()
  544.     else --failed to suck fuel item
  545.       print("No Fuel in enderchest")
  546.       print("Waiting 30s for chest to be refilled")
  547.       --wait for it to be refilled
  548.       sleep(30)
  549.     end
  550.   end  
  551. end  
  552.  
  553. function refuel()
  554.   --if the turtle should refuel
  555.   if shouldRefuel() then
  556.     refuelFromInventory()
  557.     --if still need to refuel
  558.     if continueRefuel() then
  559.       --unload
  560.       unload()
  561.       --find where to place enderchest
  562.       local direction, level = getEChestDir(true, true, true)
  563.       --place enderchest
  564.       placeEChest(15, direction, level)
  565.       --refuel from enderchest
  566.       refuelFromEChest(direction)
  567.       --select slot enderchestshould be in
  568.       turtle.select(15)
  569.       --grab the enderchest
  570.       digDirection(direction)
  571.       --reselect slot 1
  572.       turtle.select(1)
  573.     end  
  574.   end
  575. end
  576.  
  577. --empties a chest in 'direction'
  578. function emptyChest(direction)
  579.   --while chest exists in direction
  580.   while getMLevel(direction) == 1 do  
  581.     --if inventory full
  582.     if checkInventory() then
  583.       --empty inventory
  584.       unload()
  585.     else --inventory empty
  586.       --if chest empty
  587.       --or removed by unloading
  588.       if suckDirection(direction) == false then
  589.         digDirection(direction)
  590.         return true
  591.       end
  592.     end
  593.   end
  594. end
  595.  
  596.  
  597. --either guarantees a dig in 'direction'
  598. --or returns false if
  599.   --asked to dig an unmineable block
  600. --NEED TO DEAL WITH LIQUIDS
  601. function safeDig(direction)
  602.   --while there is something
  603.     --(while not air in direction)
  604.   --in the specified direction
  605.   local level
  606.   while detectDirection(direction) do
  607.     --if the inventory is full
  608.     if checkInventory() then
  609.       --empty the inventory
  610.       unload()
  611.     end
  612.    
  613.     --get mining level of block
  614.     level = getMLevel(direction)
  615.    
  616.     --if block is unbreakable
  617.     if level == 3 then
  618.       print("Error: Block is unbreakable")
  619.       --return false to indicate this
  620.       return false
  621.     end
  622.    
  623.     --if block is from computercraft
  624.     while level == 2 do
  625.       print("Computercraft block detected")
  626.       print("Waiting 5s for it to move")
  627.       --wait for it to move
  628.       sleep(5)
  629.       --retest
  630.       level = getMLevel(direction)
  631.     end
  632.    
  633.     --if a chest is detected
  634.     if level == 1 then
  635.       emptyChest(direction)
  636.     end
  637.    
  638.     if level == 0 then
  639.       digDirection(direction)
  640.     end
  641.     --wait for blocks with gravity
  642.     sleep(1)  
  643.   end
  644.   return true  
  645. end
  646.  
  647. --a single function to 'safe dig'
  648.   --in any and all directions
  649. function safeDigMulti(down, fwd, up)
  650.   local d = false
  651.   local f = false
  652.   local u = false
  653.   --if digging down is desired
  654.   if down then
  655.     --safeDig down
  656.     --set d to result of safeDig
  657.     d = safeDig(-1)
  658.   end
  659.  
  660.   if fwd then
  661.     f = safeDig(0)
  662.   end
  663.  
  664.   if up then
  665.     u = safeDig(1)
  666.   end
  667.  
  668.   return d, f, u
  669. end
  670.  
  671. --attempts to move in 'direction'
  672. --returns result of the move function
  673. function moveDirection(direction)
  674.   if direction == -1 then
  675.     return turtle.down()
  676.   elseif direction == 0 then
  677.     return turtle.forward()
  678.   else --if direction == 1 then
  679.     return turtle.up()
  680.   end
  681. end
  682.  
  683. --guarantees that the turtle will
  684. --move in the specified direction
  685. --simply waits if unable to
  686. function safeMove(direction)
  687.   --check fuel
  688.   refuel() --this has built in check
  689.   --if turtle doesnt move in direction
  690.   while not moveDirection(direction) do
  691.     --attempt to dig in that direction
  692.     --if it fails
  693.     while not safeDig(direction) do
  694.       --try again later
  695.       print("Error: Unable to break block")
  696.       print("and therefore cannot move.")
  697.       print("Waiting 30s for assistance")
  698.       sleep(30)
  699.     end
  700.   end
  701.   --turtle has moved now update variable
  702.   data["moveDistLeft"] = data["moveDistLeft"] - 1
  703.   --and update the file.
  704.   updateFile()  
  705.   return true
  706. end
  707.  
  708. --digs a column
  709.   --of length length
  710.   --in directions stated
  711.   --may dig fwd anyway for turtle moves
  712. function digColumn(length, down, fwd, up)
  713.   --update the state variable to 'column'
  714.   data["state"] = 0
  715.   --update the remaining movement variable
  716.   data["moveDistLeft"] = length
  717.   --and update the file
  718.   updateFile()
  719.   --for all but the end of the column
  720.   while length > 1 do
  721.     --dig in all specified directions
  722.     safeDigMulti(down, fwd, up)
  723.     --then move forward 1
  724.     safeMove(0)
  725.    
  726.     --update the 'length dug' variable
  727.     length = length - 1
  728.     --update the movement variable
  729.     data["moveDistLeft"] = length
  730.     --and update the file
  731.     updateFile()
  732.   end
  733.   --now dig the last bit of the column
  734.     --ie. every direction specified
  735.       --except forwards
  736.   safeDigMulti(down, false, up)
  737.   --update the file to say
  738.     --we are at the end of the column
  739.   data["moveDistLeft"] = 0
  740.   updateFile()
  741. end
  742.  
  743. --digs a corner, for linking two columns
  744. --left = -1
  745. --right = 1
  746. function digCorner(direction)
  747.   --update state variable to 'corner'
  748.   data["state"] = 1
  749.   --update orientation var to '1 turn'
  750.   data["numTurnsReq"] = 1
  751.   --update dist remain var to '1'
  752.   data["moveDistLeft"] = 1
  753.   --update the file
  754.   updateFile()
  755.   --turn in direction specified
  756.   if direction < 0 then
  757.     turtle.turnLeft()
  758.     data["numTurnsReq"] = 0
  759.     updateFile()
  760.   else
  761.     turtle.turnRight()
  762.     data["numTurnsReq"] = 0
  763.     updateFile()
  764.   end
  765.   --safeMove will dig whatever's needed
  766.   --move forwards 1
  767.   safeMove(0) --this updates file automatically
  768.   --indicate we need to turn again
  769.   data["numTurnsReq"] = 1
  770.   updateFile()
  771.   --turn in specified direction
  772.   if direction < 0 then
  773.     turtle.turnLeft()
  774.     data["numTurnsReq"] = 0
  775.     updateFile()
  776.   else
  777.     turtle.turnRight()
  778.     data["numTurnsReq"] = 0
  779.     updateFile()
  780.   end  
  781. end
  782.  
  783. --digs a rectangle
  784.   --of specified length
  785.   --of specified width
  786.     --in direction dir (l/r = -1, 1)
  787.   --with specified layers
  788.   --does not turn around at the end!
  789. function digRect(length, width, dir, down, fwd, up)
  790.   --create a counter for the width
  791.   local w = width
  792.   --update the 'currCol' variable
  793.   data["currCol"] = w
  794.   --and the dist remain variable
  795.   data["moveDistLeft"] = length
  796.   --and the state to column
  797.   data["state"] = 0
  798.   --update file
  799.   updateFile()
  800.  
  801.   --while we are not on the last column
  802.   while w > 1 do
  803.     --mine a column
  804.     digColumn(length, down, fwd, up)
  805.     --mine a corner in correct direction
  806.     --on the even columns
  807.     if ((data["width"] - w)%2) == 1 then
  808.       --turn in opposite direction of dir
  809.       digCorner(-dir)
  810.     --on the odd columns
  811.     else --w%2 == 1 then
  812.       --turn in same direction as dir
  813.       digCorner(dir)
  814.     end
  815.    
  816.     --finished digging the corner
  817.     --update state, moveDist, currCol
  818.     data["state"] = 0
  819.     data["moveDistLeft"] = length
  820.     data["currCol"] = w - 1
  821.     updateFile()
  822.    
  823.     --update w to show column was mined
  824.     w = w - 1
  825.   end
  826.  
  827.   --then mine the last column
  828.     --it deals with file changes
  829.   digColumn(length, down, fwd, up)
  830. end
  831.  
  832. --moves the turtle to the next layer
  833.   --which is 'depth' blocks down
  834. function nextLayer(depth)
  835.   --update state to depth change
  836.   data["state"] = 2
  837.   --update movement left
  838.   data["moveDistLeft"] = depth
  839.   --update orientation
  840.   data["numTurnsReq"] = 2
  841.   --update file
  842.   updateFile()
  843.  
  844.   --turn 180 for next layer
  845.   turtle.turnLeft()
  846.   --update file
  847.   data["numTurnsReq"] = 1
  848.   updateFile()
  849.  
  850.   turtle.turnLeft()
  851.   --update file
  852.   data["numTurnsReq"] = 0
  853.   updateFile()
  854.   --move down specified amount
  855.   while depth > 0 do
  856.     safeMove(-1)
  857.     data["currDepth"] = data["currDepth"] - 1
  858.     updateFile()
  859.     depth = depth - 1
  860.   end
  861.   --update the direction
  862.   if (data["width"] % 2) == 0 then
  863.     data["currDir"] = 0 - data["currDir"]
  864.   end
  865.   data["currLayer"] = data["currLayer"] + 1
  866.   updateFile()
  867. end
  868.  
  869. --digs a length by width hole
  870.   --with a depth of depth
  871.   --in direction dir
  872. function digHole(length, width, dir, depth)
  873.   --update the file as best we can
  874.   data["shouldRecover"] = 1
  875.   data["length"] = length
  876.   data["width"] = width
  877.   data["moveDistLeft"] = length
  878.   data["currCol"] = 1
  879.   data["currDir"] = dir
  880.   data["currDepth"] = depth
  881.   data["state"] = 0
  882.   data["numTurnsReq"] = 0
  883.   data["lastEChestDir"] = 0
  884.   data["currLayer"] = 1 --not really needed
  885.   updateFile()
  886.      
  887.        
  888.   local layer = 1
  889.   local depthRem = depth
  890.   --while we are not near the bottom
  891.   while depthRem > 3 do
  892.     --first dig the CORRECT rect
  893.     --nextLayer() deals with dir
  894.     digRect(length, width, data["currDir"], true, true, true)
  895.    
  896.     --move to the next layer
  897.     nextLayer(3)
  898.     depthRem = depthRem - 3
  899.     layer = layer + 1
  900.   end
  901.  
  902.   --if there's three layers to bedrock
  903.   if depthRem == 3 then
  904.     --same as before, but go down 2
  905.     digRect(length, width, data["currDir"], true, true, true)
  906.     --digRect done, now go down 2
  907.     nextLayer(2)
  908.     depthRem = depthRem - 2
  909.     layer = layer + 1
  910.     --should move on to the
  911.     --depthRem == 1 if statement
  912.   end
  913.  
  914.   --if depth remaining == 2
  915.   if depthRem == 2 then
  916.     --same dig rect as before
  917.     --but no more going down
  918.     digRect(length, width, data["currDir"], true, true, true)
  919.     data["shouldRecover"] = 0
  920.     updateFile()
  921.     --all done!
  922.     return true    
  923.   end
  924.   --if depth remaining == 1
  925.   if depthRem == 1 then
  926.     --same as 2 except
  927.       --do not dig down on digRect
  928.     digRect(length, width, data["currDir"], false, true, true)
  929.     --all done!
  930.     data["shouldRecover"] = 0
  931.     updateFile()
  932.     return true
  933.   end
  934. end
  935.  
  936. --gets the direction of the next rect
  937. --only used in recovery function
  938. function getRectDir()
  939. --if the width is even
  940.   if (data["width"] % 2) == 0 then
  941.     --if the layer is even
  942.     if (data["currLayer"] % 2) == 0 then
  943.       return (0 - data["currDir"])
  944.     else
  945.       return data["currentDir"]
  946.     end
  947.   else
  948.     return data["currDir"]
  949.   end
  950. end
  951.  
  952. --gets the direction of the current turn
  953. --mostly used for recover function
  954. function getTurnDir()
  955.   --if even column
  956.   if (data["width"] - data["currCol"]) % 2 == 1 then
  957.     return (0 - data["currDir"])
  958.   else --odd column
  959.     return data["currDir"]
  960.   end
  961. end
  962.  
  963. --if the turtle was mid way through
  964.   --digging a hole and stopped
  965.   --for whatever reason, this function
  966.   --will carry on from where it stopped
  967. function recover()
  968.   --load all the variables
  969.   loadFile()
  970.   --if we shouldnt recover, return false
  971.   if data["shouldRecover"] == 0 then
  972.     return false
  973.    
  974.   --we should recover
  975.   else
  976.     --if we are refuelling
  977.     if isUsingEChest(15) == true then
  978.       --recover the enderchest by:
  979.       --select inventory slot
  980.       turtle.select(15)
  981.       --dig in direction of EChest
  982.       digDirection(data["lastEChestDir"])
  983.       --reselect slot 1
  984.       turtle.select(1)
  985.       --restart refuelling if we need to
  986.       refuel()
  987.     end
  988.    
  989.     --if we are unloading
  990.     if isUsingEChest(16) == true then
  991.       --dig the enderchest
  992.       turtle.select(16)
  993.       digDirection(data["lastEChestDir"])
  994.       --then restart unloading
  995.       unload()
  996.     end
  997.    
  998.     --if we are on a column
  999.     if data["state"] == 0 then
  1000.       --finish the column
  1001.       --if we are at min depth
  1002.       if data["currDepth"] == 1 then
  1003.         --mine only above and in front
  1004.         digColumn(data["moveDistLeft"], false, true, true)
  1005.       else --any other depth
  1006.         digColumn(data["moveDistLeft"], true, true, true)
  1007.       end
  1008.      
  1009.       --set up the next stage
  1010.       --if this was the last column
  1011.       if data["currCol"] == 1 then
  1012.         --if we are not near the end
  1013.         if data["currDepth"] > 3 then
  1014.           --set up vars as if
  1015.           --newLayer(3) was called
  1016.           data["state"] = 2
  1017.           data["moveDistLeft"] = 3
  1018.           data["numTurnsReq"] = 2
  1019.           updateFile()
  1020.         --if we are on layer 3
  1021.         elseif data["currDepth"] == 3 then
  1022.           --newlayer(2)
  1023.           data["state"] = 2
  1024.           data["moveDistLeft"] = 2
  1025.           data["numTurnsReq"] = 2
  1026.           updateFile()
  1027.         --if we are on layer 1 or 2
  1028.         else
  1029.           --finished digging!
  1030.           data["shouldRecover"] = 0
  1031.           updateFile()
  1032.           return true
  1033.         end
  1034.       --if this is not the last column
  1035.       else
  1036.         --update Vars to say
  1037.         --we are starting a corner
  1038.         data["state"] = 1
  1039.         data["numTurnsReq"] = 1
  1040.         data["moveDistLeft"] = 1
  1041.       end
  1042.     end
  1043.    
  1044.     --if we are on a corner
  1045.     if data["state"] == 1 then
  1046.       --if we have not moved forwards yet
  1047.       if data["moveDistLeft"] == 1 then
  1048.         --if we have not turned
  1049.         if data["numTurnsReq"] == 1 then
  1050.           --turn
  1051.           turnDirection(getTurnDir())
  1052.           --update the file
  1053.           data["numTurnsReq"] = 0
  1054.           updateFile()
  1055.         end
  1056.         --if we have turned
  1057.         if data["numTurnsReq"] == 0 then
  1058.           safeMove(0) --partially deals with file
  1059.           data["numTurnsReq"] = 1
  1060.           updateFile()
  1061.         end
  1062.       end
  1063.       --if we have moved forwards already
  1064.       if data["moveDistLeft"] == 0 then
  1065.         --if we have not turned yet
  1066.         if data["numTurnsReq"] == 1 then
  1067.           turnDirection(getTurnDir())
  1068.           --update File
  1069.           data["numTurnsReq"] = 0
  1070.           updateFile()
  1071.         end
  1072.         --if we have turned already
  1073.         if data["numTurnsReq"] == 0 then
  1074.           --we are on the next column
  1075.           data["moveDistleft"] = data["length"]
  1076.           data["currCol"] = data["currCol"] - 1
  1077.           data["state"] = 0
  1078.           updateFile()
  1079.           --set up the var's to dig rect
  1080.         end
  1081.       end
  1082.     end
  1083.    
  1084.     --if we are moving layers
  1085.     if data["state"] == 2 then
  1086.       --complete turns (left always?)
  1087.       if data["numTurnsReq"] == 2 then
  1088.         turtle.turnLeft()
  1089.         data["numTurnsReq"] = 1
  1090.         updateFile()
  1091.       end
  1092.       if data["numTurnsReq"] == 1 then
  1093.         turtle.turnLeft()
  1094.           data["numTurnsReq"] = 0
  1095.         updateFile()
  1096.       end
  1097.       --complete downward movement
  1098.       while data["moveDistLeft"] > 0 do
  1099.         safeMove(-1) --deals with file stuff
  1100.         data["currDepth"] = data["currDepth"] - 1
  1101.         updateFile()
  1102.       end
  1103.       --update file for new layer
  1104.       data["currLayer"] = data["currLayer"] + 1
  1105.       data["currCol"] = data["width"]
  1106.       data["moveDistLeft"] = data["length"]
  1107.       if (data["width"] % 2) == 0 then
  1108.         data["currDir"] = (0 - data["currDir"])
  1109.       end
  1110.       data["state"] = 0
  1111.       updateFile()
  1112.     end
  1113.    
  1114.     --we are now definitly at the
  1115.     --beginning of a column
  1116.    
  1117.     --digRect (remaining width)
  1118.     --if we arent at the very bottom
  1119.     if data["currDepth"] ~= 1 then
  1120.       --finish the current layer
  1121.       --by doing
  1122.       digRect(data["length"], data["currCol"], getTurnDir(), true, true, true)
  1123.     --if we are on the bottom layer
  1124.     else
  1125.       digRect(data["length"], data["currCol"], getTurnDir(), false, true, true)
  1126.     end
  1127.    
  1128.     --if this was the last rect to dig
  1129.     if data["currDepth"] < 3 then
  1130.       data["shouldRecover"] = 0
  1131.       updateFile()
  1132.       return true
  1133.     --if this is the last but one
  1134.     --rect to dig
  1135.     elseif data["currDepth"] == 3 then
  1136.       --move to next layer
  1137.       nextLayer(2)      
  1138.       digRect(data["length"], data["width"], data["currDir"], false, true, true)
  1139.       --done!
  1140.       data["shouldRecover"] = 0
  1141.       updateFile()
  1142.    
  1143.     --there are many more rect's to dig
  1144.     else
  1145.       --move to next layer
  1146.       nextLayer(3)
  1147.       --continue with dig hole
  1148.       digHole(data["length"], data["width"], data["currDir"], data["currDepth"])
  1149.       data["shouldRecover"] = 0
  1150.       updateFile()
  1151.     end    
  1152.   end  
  1153. end
  1154.  
  1155.  
  1156. --actual turtle program
  1157.  
  1158. --checks if it should recover
  1159.   --and if it should, it does so
  1160. recover()
  1161.  
  1162. --explanation text
  1163. print("Turtle has completed previous tasks")
  1164. print("and is ready to mine a new hole")
  1165. print("")
  1166. --get length
  1167. print("Please enter the forward distance")
  1168. print("of the hole you want to dig;")
  1169.  
  1170. local inputLength = tonumber(io.read())
  1171. --get width
  1172. print("Now please enter the width of the")
  1173. print("hole you would like to dig")
  1174.  
  1175. local inputWidth = tonumber(io.read())
  1176.  
  1177. --get direction
  1178. print("Please choose which direction you")
  1179. print("would like the turtle to dig in")
  1180. print("Enter -1 for left")
  1181. print("Enter 1 for right")
  1182.  
  1183. local inputDir = tonumber(io.read())
  1184.  
  1185. --get depth
  1186. print("Finally, please enter the y level")
  1187. print("of the turtle")
  1188.  
  1189. local inputDepth = tonumber(io.read())
  1190.  
  1191. --apply all values
  1192. print("Turtle will now start digging")
  1193. sleep(1)
  1194. digHole(inputLength, inputWidth, inputDir, inputDepth)
Advertisement
Add Comment
Please, Sign In to add comment