Jameelo

Better Simple Quarry Code

Nov 4th, 2023 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.84 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.     TODO:
  7.     - Move setup into a single function, maybe even allow values to be passed in before runtime as an option?
  8.     - Make code support rectangular paths
  9.     - Change the way it calculates a full inventory, as currently it slows down mining a lot
  10. ]]
  11.  
  12. os.loadAPI("commonUtils")
  13.  
  14. RETURNCOND = 0
  15. DEPTH = 0
  16. WIDTH = 0
  17. UPWARDS = false
  18. ECPRESENT = false
  19.  
  20. function setDimensions()
  21.     print("Enter quarry depth: ")
  22.     DEPTH = tonumber(read())
  23.     print("Enter quarry width: ")
  24.     WIDTH = tonumber(read())
  25.     if WIDTH%2 == 0 then
  26.         EVENWIDTH = true
  27.     end
  28.     -- Shall I go up , or down?
  29.     print("Shall I go up, or down?")
  30.     local directionChoice = string.lower(tostring((read())))
  31.     local acceptedResponses = {"up","down",
  32.                                 "u","d"}
  33.  
  34.     for index,value in pairs(acceptedResponses) do -- find out what they said
  35.         if directionChoice == value then
  36.             if index%2==1 then
  37.                 UPWARDS = true
  38.                 return
  39.             else
  40.                 UPWARDS = false
  41.                 return
  42.             end
  43.         end
  44.     end
  45. end
  46.  
  47. function setReturnCond()
  48.     print("Shall I return to the original height?")
  49.     local returnResponse = string.lower(tostring(read()))
  50.     local acceptedConditions = {"yes","no",
  51.                                 "true","false",
  52.                                 "1","0",
  53.                                 "y","n",
  54.                                 "yeah","nah"}
  55.     for k,v in pairs(acceptedConditions) do
  56.         if returnResponse == v then
  57.             if k%2==1 then
  58.                 RETURNCOND = 1
  59.                 return
  60.             end
  61.         end
  62.     end
  63. end
  64.  
  65. local function calculateFuelExpenditure() -- Calculate how much fuel will be taken from the quarry volume
  66.     local distance = DEPTH*WIDTH*WIDTH
  67.  
  68.     if RETURNCOND == true then
  69.         distance = distance + DEPTH
  70.     end
  71.  
  72.     if turtle.getFuelLevel() > distance then
  73.         return true
  74.     else
  75.         return false
  76.     end
  77. end
  78.  
  79. function minesquare(layer)
  80.  
  81.     local reverse,right
  82.  
  83.     if layer%2 == 0 and not EVENWIDTH then
  84.         -- if the width is odd and the current layer count is even
  85.         reverse = true
  86.     end
  87.  
  88.     for n = 1,WIDTH,1 do -- Theoretically should be easy to make rectuangular
  89.         -- The first block in a line is mined differently, as the turtle needs to move into said ine.
  90.         if n == 1 then -- if its the first iteration, then the turtle needs to move down a layer
  91.             if UPWARDS then
  92.                 turtle.digUp()
  93.                 turtle.up()
  94.             else
  95.                 turtle.digDown()
  96.                 turtle.down()
  97.             end
  98.         end
  99.         commonUtils.digForward(WIDTH-1) -- mine out the rest of the line
  100.         if n < WIDTH then -- if the turtle hasn't finished this layer yet
  101.             -- There was a huge bug with odd width quarries, so I'm trying out a boolean "am I turning right or left" value
  102.             if reverse then
  103.                 right = false
  104.             else
  105.                 right = true
  106.             end
  107.             if n%2 == 0 then
  108.                 right = not right
  109.             end
  110.             if right then -- alternate between turning right and left at the end of a line
  111.                 turtle.turnRight()
  112.                 commonUtils.digForward()
  113.                 turtle.turnRight()
  114.             else
  115.                 turtle.turnLeft()
  116.                 commonUtils.digForward()
  117.                 turtle.turnLeft()
  118.             end
  119.             if reverse then
  120.                 right = not right
  121.             end
  122.         else
  123.             if not EVENWIDTH and not reverse then
  124.                 turtle.turnLeft()
  125.             else
  126.                 turtle.turnRight()
  127.             end
  128.         end
  129.     end
  130. end
  131.  
  132. function main()
  133.     term.clear()
  134.     for i = 1,DEPTH,1 do
  135.         minesquare(i)
  136.     end
  137.  
  138.     if RETURNCOND == 1 then
  139.         for _ = 1, DEPTH, 1 do
  140.             if UPWARDS then
  141.                 turtle.down()
  142.             else
  143.                 turtle.up()
  144.             end
  145.         end
  146.     end
  147.     commonUtils.dumpItems()
  148.     write("Execution complete, fuel remaining: ")
  149.     print(turtle.getFuelLevel())
  150. end
  151.  
  152. setDimensions()
  153. setReturnCond()
  154.  
  155. if calculateFuelExpenditure() == true then -- I know about the '== true' but it doesn't work otherwise so shut up
  156.     main()
  157. else
  158.     if commonUtils.refuelChestSafe() == true then
  159.         if calculateFuelExpenditure() == true then
  160.             main()
  161.         else
  162.             print("Refuel attempt insufficient, shutting down...")
  163.         end
  164.     else
  165.         print("Failed refuel attempt, shutting down...")
  166.     end
  167. end
Tags: lua
Advertisement
Add Comment
Please, Sign In to add comment