Jameelo

Simple Quarry Code

Feb 18th, 2023 (edited)
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.37 KB | Gaming | 0 0
  1. --[[
  2.     Quarry code for a turtle
  3.     Digs an WIDTH by WIDTH hole, DEPTH blocks deep; specified by the input
  4.     Calculates fuel efficiency
  5.     Recognises a full inventory & dumps excess into ender chest
  6. ]]
  7.  
  8. print("Enter quarry depth")
  9. DEPTH = string.lower(tostring(read()))
  10.  
  11. print("Enter quarry width")
  12. WIDTH = string.lower(tostring(read()))
  13.  
  14. if WIDTH%2 == 0 then
  15.     isEven = true
  16. end
  17.  
  18. print("Shall I return to the original height?")
  19. returnResponse = string.lower(tostring(read()))
  20.  
  21. acceptedConditions = {"yes","no","true","false","1","0","y","n"}
  22. RETURNCOND = 0
  23.  
  24. for k,v in pairs(acceptedConditions) do
  25.     if returnResponse == v then
  26.         if k%2==1 then
  27.             RETURNCOND = 1
  28.             break
  29.         else
  30.             RETURNCOND = 0
  31.         end
  32.     end
  33. end
  34.  
  35. function dumpItems()
  36.     eSlot = findEChest()
  37.     if eSlot ~= false then
  38.         turtle.refuel()
  39.         turtle.select(eSlot)
  40.         turtle.placeUp()
  41.         emptyInv()
  42.         turtle.digUp()  
  43.     else
  44.         print("No E-Chest detected, skill issue")  
  45.     end
  46. end
  47.  
  48. function emptyInv()
  49.     for n = 1,16,1 do
  50.         turtle.select(n)
  51.         if turtle.getItemCount(n) ~= 0 then
  52.             turtle.dropUp()
  53.         end        
  54.     end
  55. end
  56.  
  57. function findEChest()
  58.     for n = 1,16,1 do
  59.         if turtle.getItemCount(n) ~= 0 then
  60.             if turtle.getItemDetail(n).name == "enderstorage:ender_chest" then
  61.                 return n
  62.             end
  63.         end
  64.     end
  65.     return false
  66. end
  67.  
  68. function existsTable(tableIn, element)
  69.     for _, value in pairs(tableIn) do
  70.       if value == element then
  71.         return true
  72.       end
  73.     end
  74.     return false
  75. end
  76.  
  77. function calculateFuelExpenditure() -- Consider return
  78.     local distance
  79.     if RETURNCOND == true then
  80.         distance = DEPTH*((WIDTH*WIDTH)) + DEPTH
  81.     else
  82.         distance = DEPTH*((WIDTH*WIDTH))
  83.     end
  84.     if turtle.getFuelLevel() > distance then
  85.         return true
  86.     else
  87.         return false
  88.     end
  89. end
  90.  
  91. function minesquare()
  92.     turtle.digDown()
  93.     turtle.down()
  94.     if isEven == true then
  95.         local halfWidth = WIDTH/2
  96.         for count = 1,WIDTH/2,1 do
  97.             digForward(WIDTH-1)
  98.             turtle.turnRight()
  99.             digForward()
  100.             turtle.turnRight()
  101.             digForward(WIDTH-1)
  102.             if count == WIDTH/2 then
  103.                 turtle.turnRight()
  104.                 break
  105.             end
  106.             turtle.turnLeft()
  107.             digForward()
  108.             turtle.turnLeft()
  109.         end
  110.     else
  111.         local halfWidth = math.floor(WIDTH/2)
  112.         for count = 1,halfWidth,1 do
  113.             digForward(WIDTH-1)
  114.             turtle.turnRight()
  115.             digForward()
  116.             turtle.turnRight()
  117.             digForward(WIDTH-1)
  118.             if count == halfWidth then
  119.                 turtle.turnLeft()
  120.                 digForward()
  121.                 turtle.turnLeft()
  122.                 digForward(WIDTH-1)
  123.                 turtle.turnLeft()
  124.                 turtle.turnLeft()
  125.                 break
  126.             end
  127.             turtle.turnLeft()
  128.             digForward()
  129.             turtle.turnLeft()
  130.         end
  131.     end
  132. end
  133.  
  134. function digForward(length)
  135.     if length == nil then
  136.         length = 1 -- default
  137.     end
  138.     for _ = 1,length,1 do
  139.         if everySlotTaken() == true then
  140.             print("Storage full")
  141.             dumpItems()
  142.         end
  143.         turtle.dig()
  144.         turtle.forward()
  145.     end
  146. end
  147.  
  148. function getCoords()
  149.     local coords = vector.new(gps.locate())
  150.     return coords
  151. end
  152.  
  153. function everySlotTaken()
  154.     --Cycle through all the slots and get the inventory size
  155.     local takenSlots = 0
  156.  
  157.     for n = 1,16,1 do
  158.         if turtle.getItemCount(n) > 0 then
  159.             takenSlots = takenSlots + 1
  160.         end
  161.     end
  162.     if takenSlots == 16 then
  163.         return true
  164.     else
  165.         return false
  166.     end
  167. end
  168.  
  169. function main()
  170.     for count = 1,DEPTH,1 do
  171.         minesquare()
  172.     end
  173.  
  174.     if RETURNCOND == 1 then
  175.         for i = 1, DEPTH, 1 do
  176.             turtle.up()
  177.         end
  178.     end
  179.     dumpItems()
  180. end
  181.  
  182. if calculateFuelExpenditure() then
  183.     main()
  184. else
  185.     if turtle.refuel() then
  186.         if calculateFuelExpenditure() then
  187.             main()
  188.         else
  189.             print("Failed refuel attempt, skill issue ig")
  190.         end
  191.     else
  192.         print("Not enough fuel!")
  193.     end
  194. end
Add Comment
Please, Sign In to add comment