Advertisement
Guest User

TurtleQuary

a guest
Feb 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.32 KB | None | 0 0
  1.  
  2.  
  3. --Enums for direction
  4. UP = 0
  5. DOWN = 1
  6. FORWARD = 2
  7.  
  8. --Ignored block IDs
  9. IGNORE_BLOCKS = {
  10.     "STONE_ID", "GRAVEL_ID", "DIRT_ID",
  11.     "MARBLE_ID"
  12. }
  13.  
  14. -- Helper function to compare
  15. function compareDirection(direction)
  16.     if direction == UP then
  17.         return turtle.compareUp()
  18.     elseif direction == DOWN then
  19.         return turtle.compareDown()
  20.     else
  21.         return turtle.compare()
  22.     end
  23. end
  24.  
  25. -- Helper function to inspect
  26. function inspectDirection(direction)
  27.     if direction == UP then
  28.         return turtle.inspectUp()
  29.     elseif direction == DOWN then
  30.         return turtle.inspectDown()
  31.     else
  32.         return turtle.inspect()
  33.     end
  34. end
  35.  
  36. -- Helper function to detect
  37. function detectDirection(direction)
  38.     if direction == UP then
  39.         return turtle.detectUp()
  40.     elseif direction == DOWN then
  41.         return turtle.detectDown()
  42.     else
  43.         return turtle.detect()
  44.     end
  45. end
  46.  
  47. --Helper function to dig
  48. function digDirection(direction)
  49.     if direciton == UP then
  50.         return turtle.digUp()
  51.     elseif direction == DOWN then
  52.         return turtle.digDown()
  53.     else
  54.         return turtle.dig()
  55.     end
  56. end
  57.  
  58. --Helper function to move
  59. function moveDirection(direction)
  60.     if direction == UP then
  61.         return turtle.up()
  62.     elseif direction == DOWN then
  63.         return turtle.down()
  64.     else
  65.         return turtle.forward()
  66.     end
  67. end
  68.  
  69. --Helper function to undo move
  70. function undoMoveDirection(direction)
  71.     if direction == UP then
  72.         return turtle.down()
  73.     elseif direction == DOWN then
  74.         return turtle.up()
  75.     else
  76.         return turtle.back()
  77.     end
  78. end
  79.  
  80.  
  81. -- Helper function to ensure I have enough fuel
  82. function checkSufficentFuel(requiredReturnFuel)
  83.     -- We do +1 here because moving takes a fuel away and adds a fuel to required Return fuel
  84.     return turtle.getFuelLevel() > requiredReturnFuel + 1
  85. end
  86.  
  87. --Checks the inventory to see if there is room to mine the block in direction
  88. function checkInventorySpaceAvailable(direction)
  89.     local curSlot = turtle.getSelectSlot()
  90.     local hasRoom = false
  91.     for i = 1, 16 do
  92.         turtle.select(i)
  93.         local sameBlock = turtleWrappers.compareDirection(direction)
  94.         if sameBlock then
  95.             if turtle.getItemCount() < 64 then
  96.                 hasRoom = true
  97.                 break
  98.             end
  99.         end
  100.     end
  101.     turtle.select(curSlot);
  102.     return hasRoom
  103. end
  104.  
  105. --Helper function to dig in given direction; does not move
  106. function advancedDig(direction)
  107.     -- TODO: handle gravel fall in here somehow
  108.     --Check if i need to dig
  109.     local needToDig = false
  110.     needToDig = turtleWrappers.compareDirection(direction)
  111.  
  112.     if not needToDig then
  113.         return true
  114.     end
  115.  
  116.     --If I need to dig - make sure I can dig
  117.     local hasRoom = checkInventorySpaceAvailable(direction)
  118.     if hasRoom then
  119.         return turtleWrappers.digDirection(direction)
  120.     else
  121.         return false
  122.     end
  123. end
  124.  
  125. --Checks to see if we should dig in direction for ores
  126. function checkForOreInDirection(direction)
  127.     --Note: We may want to ignore air block when checking because we could run into problems with caves
  128.     --Its not really a problem just a waste of fuel
  129.     --If we do ignore air spaces, it will be beneficial to remove the `advancedDig(UP)` from digBranch as the top row will never be checked for ores
  130.     if not turtleWrappers.detectDirection(direction) then
  131.         return false
  132.     end
  133.  
  134.     local data = turtleWrappers.inspectDirection(direction)
  135.     for k, v in pairs(IGNORE_BLOCKS) do
  136.         if data.name == v then
  137.             return false
  138.         end
  139.     end
  140.  
  141.     return true
  142. end
  143.  
  144. -- Recursive function to dig out ores that Have been found
  145. function digOreInDirection(direction, requiredReturnFuel)
  146.     --Make sure I have enough fuel to get home
  147.     if not checkSufficentFuel(requiredReturnFuel) then
  148.         return
  149.     end
  150.  
  151.     --Ok, I "know" there is ore in direction (could be an empty space) lets dig
  152.     local success = advancedDig(direction)
  153.     if not success then
  154.         return
  155.     end
  156.  
  157.     turtleWrappers.moveDirection(direction)
  158.    
  159.     digOresAllAround(requiredReturnFuel + 1)
  160.  
  161.     turtleWrappers.undoMoveDirection(direction)
  162.  
  163. end
  164.  
  165. --Helper function to check around and dig out ores
  166. function digOresAllAround(requiredReturnFuel)
  167. --Check left    
  168.     turtle.turnLeft()
  169.     if checkForOreInDirection(FORWARD) then
  170.         digOreInDirection(FORWARD, requiredReturnFuel)
  171.     end
  172.     turtle.turnRight()
  173.  
  174. -- check right
  175.     turtle.turnRight()
  176.     if checkForOreInDirection(FORWARD) then
  177.         digOreInDirection(FORWARD, requiredReturnFuel)
  178.     end
  179.     turtle.turnLeft()
  180.  
  181. -- check up
  182.     if checkForOreInDirection(UP) then
  183.         digOreInDirection(UP, requiredReturnFuel)
  184.     end
  185.  
  186.  -- check down
  187.     if checkForOreInDirection(DOWN) then
  188.         digOreInDirection(DOWN, requiredReturnFuel)
  189.     end
  190.  
  191.  -- check forward
  192.     if checkForOreInDirection(FORWARD) then
  193.         digOreInDirection(FORWARD, requiredReturnFuel)
  194.     end
  195. end
  196.  
  197. function digOresTopRow(requiredReturnFuel)
  198.     turtleWrappers.moveDirection(UP)
  199.  
  200.     digOresAllAround(requiredReturnFuel + 1)
  201.  
  202.     turtleWrappers.undoMoveDirection(UP)
  203. end
  204.  
  205. --This assumes the branch is supposed to be dug in the forward direction with turtle sitting at bottom of tunnel
  206. function digBranch(branchLength, requiredReturnFuel)
  207.     --Make sure I have enough fuel to get home
  208.     if not checkSufficentFuel(requiredReturnFuel) then
  209.         return
  210.     end
  211.     --Base Case
  212.     if branchLength <= 0 then
  213.         return
  214.     end
  215.  
  216.     --Make sure I'm capable of digging
  217.     local success = true
  218.     success = success and advancedDig(FORWARD)
  219.     success = success and advancedDig(UP)
  220.  
  221.     --Cool I dig - lets go forward
  222.     if success then
  223.         turtleWrappers.moveDirection(FORWARD)
  224.         digBranch(branchLength - 1, requiredReturnFuel + 1)
  225.  
  226.         --I have completely dug forward - lets move backwards and check for ores
  227.         digOresAllAround(requiredReturnFuel + 1)
  228.  
  229.         digOresTopRow(requiredReturnFuel + 1)
  230.  
  231.         --Lets unwind
  232.         undoMoveDirection(FORWARD)
  233.  
  234.     end
  235.  
  236. end
  237.  
  238. --======================================
  239. -- Above here completely digs out a branch with inventory check, ore check and fuel check
  240. --======================================
  241.  
  242. function digStripMine(width, height, length, branchSpacing, branchLength, requiredReturnFuel)
  243.     --home fuel check
  244.     if not checkSufficentFuel(requiredReturnFuel) then
  245.         return
  246.     end
  247.  
  248.     -- Base case
  249.     if length <= 0 then
  250.         return
  251.     end
  252.  
  253.     --Do I need to dig some branches?
  254.     if lenght % branchSpacing == 0 then
  255.         --Left branch
  256.         turtle.turnLeft()
  257.         digBranch(branchLength, requiredReturnFuel)
  258.         turtle.turnRight()
  259.  
  260.         --Right branch
  261.         turtle.turnRight()
  262.         digBranch(branchLength, requiredReturnFuel)
  263.         turtle.turnLeft()
  264.     end
  265.  
  266.  
  267.  
  268.     --Dig out mine shaft
  269.     local success = advancedDig(FORWARD)
  270.     if not success then
  271.         return
  272.     end
  273.  
  274.     turtleWrappers.moveDirection(FORWARD)
  275.  
  276.     digTunnelSliceLeft(width, height, requiredReturnFuel + 1)
  277.  
  278.     digStripMine(width, height, length - 1, branchSpacing, branchLength, requiredReturnFuel + 1)
  279.  
  280.     turtleWrappers.undoMoveDirection(FORWARD)
  281. end
  282.  
  283. function digTunnelSliceLeft(width, height, requiredReturnFuel)
  284.  
  285.     digTunnelColumn(height, requiredReturnFuel)
  286.  
  287.     if not checkSufficentFuel(requiredReturnFuel) then
  288.         return
  289.     end
  290.  
  291.     turtle.turnLeft()
  292.     local success = advancedDig(FORWARD)
  293.     if not success then
  294.         return
  295.     end
  296.    
  297.     turtleWrappers.moveDirection(FORWARD)
  298.     digSideSlice(width - 1, height, requiredReturnFuel + 1)
  299.  
  300.     turtleWrappers.undoMoveDirection(FORWARD)
  301.     turtleWrappers.turnRight()
  302.  
  303. end
  304.  
  305. function digTunnelSliceRight(width, height, requiredReturnFuel)
  306.    
  307.     digTunnelColumn(height, requiredReturnFuel)
  308.  
  309.     if not checkSufficentFuel(requiredReturnFuel) then
  310.         return
  311.     end
  312.  
  313.     turtle.turnRight()
  314.     local success = advancedDig(FORWARD)
  315.     if not success then
  316.         return
  317.     end
  318.  
  319.     turtleWrappers.moveDirection(FORWARD)
  320.     digSideSlice(width - 1, height, requiredReturnFuel + 1)
  321.  
  322.     turtleWrappers.undoMoveDirection(FORWARD)
  323.     turtleWrappers.turnLeft()
  324. end
  325.  
  326. function digSideSlice(width, height, requiredReturnFuel)
  327.    
  328.     digTunnelColumn(height, requiredReturnFuel)
  329.  
  330.     if not checkSufficentFuel(requiredReturnFuel) then
  331.         return
  332.     end
  333.  
  334.     if width <= 0 then
  335.         return
  336.     end
  337.  
  338.     local success = advancedDig(FORWARD)
  339.     if not success then
  340.         return
  341.     end
  342.  
  343.     turtleWrappers.moveDirection(FORWARD)
  344.     digSideSlice(width - 1, height, requiredReturnFuel + 1)
  345.     turtleWrappers.undoMoveDirection(FORWARD)
  346.  
  347. end
  348.  
  349. function digTunnelColumn(height, requiredReturnFuel)
  350.  
  351.     if not checkSufficentFuel(requiredReturnFuel) then
  352.         return
  353.     end
  354.  
  355.     -- <= 1 here because the first block is already dug out
  356.     if height <= 1 then
  357.         return
  358.     end
  359.  
  360.     local success = advancedDig(UP)
  361.     if not success then
  362.         return
  363.     end
  364.  
  365.     turtleWrappers.moveDirection(UP)
  366.    
  367.     digTunnelColumn(height - 1, requiredReturnFuel + 1 )
  368.  
  369.     turtleWrappers.undoMoveDirection(UP)
  370. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement