Advertisement
Muhi98

MiningTurtleQuarry

Apr 9th, 2021 (edited)
868
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.47 KB | None | 0 0
  1. --load moveAPI's as neeeded
  2. os.loadAPI("moveAPI")
  3.  
  4.  
  5. --[[TODO
  6. automatic fuel search
  7. wireless integration through API
  8. --]]
  9.  
  10. --[[Features
  11. variable quarry length, depth, width
  12. automatic inventory emptying
  13. automatic refueling (as long as fuel is found)
  14. maintance built into the program, e.g. fuel empty -> return to chest, waits for command
  15. accepts any chest
  16. --]]
  17.  
  18.  
  19. --CONSTANTS
  20. LEFT, RIGHT = 1, 2
  21.  
  22.  
  23. function welcomeScreen()
  24.     local okParameter = false
  25.     local x, y, z, face
  26.  
  27.     while not okParameter do
  28.         --clear terminal TODO
  29.    
  30.         print("Welcome to Quarry 2.0")
  31.         print("I will stand in the left-down corner")
  32.         term.write("Length: ")
  33.         x = tonumber(read())
  34.    
  35.         print()
  36.    
  37.         term.write("Width: ")
  38.         z = tonumber(read())
  39.    
  40.         print()
  41.    
  42.         term.write("Depth(optional): ")
  43.         y = read()
  44.         if y == "" then y = 255 else y = tonumber(y) end  --check for optional
  45.    
  46.         term.write("Direction(optional) N/S/W/E: ")
  47.         face = read()
  48.         if face == "" then face = nil else face = moveAPI.stringToCompass(face) end
  49.         if face == -1 then face = nil end
  50.    
  51.         --check if parameters are okay
  52.         if x > 0 and z > 0 then
  53.             okParameter = true
  54.         end
  55.    
  56.         print("-----------------")
  57.     end
  58.  
  59.  
  60.     return x, y, z, face
  61. end
  62.  
  63. function printSettings(x, y, z)
  64.     y = y ~= 255 and y or "Bedrock" --Bedrock or Real-Number
  65.    
  66.     --term.write Settings
  67.     print("Settings: ")
  68.     print("Length: " .. x)
  69.     print("Width : " .. z)
  70.     print("Depth : " .. y)
  71. end
  72.  
  73. function checkForChest()
  74.     print("-----------------")
  75.     print("Ensuring that chest is behind me...")
  76.     moveAPI.turnLeft(2)
  77.     repeat
  78.         os.sleep(0.5)
  79.         _, data = turtle.inspect()
  80.         data = data or ""
  81.     until data.name ~= nil and string.find(data.name, "chest")
  82.     print("Chest found!")
  83.     moveAPI.turnLeft(2)
  84.     print("-----------------")
  85. end
  86.  
  87. function waitForMuhi(why)
  88.     why = why or "NIL"
  89.  
  90.     print("-----------------")
  91.     print("Error: " .. why)
  92.     print("Decide: continue(c)/refuel(r)")
  93.     term.write("/end(en)/empty(em): ")
  94.     local str = read()
  95.     print("-----------------")
  96.  
  97.     if      str == "c"   then return
  98.     elseif  str == "r"     then checkFuel(true)
  99.     elseif  str == "en"        then returnToChest() ; emptyInv() ; os.reboot()
  100.     elseif  str == "em"      then returnToChest() ; emptyInv()
  101.     else    print("Wrong command") ; waitForMuhi()
  102.     end
  103. end
  104.  
  105. function checkInv()
  106.     for i=1, 16 do
  107.         turtle.select(i)
  108.         if turtle.getItemDetail() == nil then turtle.select(1) ; return false
  109.         end
  110.     end
  111.  
  112.     -- arriving here means inv is full
  113.     turtle.select(1)
  114.     return true
  115. end
  116.  
  117. function checkFuel(force)
  118.     force = force or false
  119.  
  120.     --idea: turtle should always be able to drive home (chest)
  121.     --      if turtle-inv does not contain fuel, return home
  122.     --      check the chest for fuel (how?),
  123.     --      if it does not contain fuel, throw error
  124.  
  125.     local minLevel =
  126.     math.abs(limit.x) +
  127.     math.abs(limit.y) +
  128.     math.abs(limit.z);
  129.  
  130.  
  131.     if turtle.getFuelLevel() < minLevel or force then
  132.         for i=1, 16 do
  133.             turtle.select(i)
  134.             if turtle.refuel(0) then
  135.                 turtle.refuel(turtle.getItemCount(i))
  136.             end
  137.         end
  138.         turtle.select(1)
  139.     end
  140.  
  141.     --check if refueled
  142.     if turtle.getFuelLevel() < minLevel then
  143.         print("-----------------")
  144.         print("Fuel is too low to continue")
  145.         print("Needed: " .. minLevel .. " / Is: " .. turtle.getFuelLevel())
  146.         --return to chest check for coal
  147.         local x, y, z = moveAPI.getXYZ()
  148.         local face = moveAPI.getFace()
  149.  
  150.         returnToChest()
  151.         emptyInv()
  152.         waitForMuhi("fuel")
  153.         returnToMine(x, y, z, face)
  154.     end
  155. end
  156.  
  157. function readyUpLayer()
  158.     moveAPI.turnRight(2)
  159.     moveAPI.down()
  160. end
  161.  
  162. function readyUpRow(turnSide)
  163.     if turnSide == RIGHT then
  164.         moveAPI.turnRight()
  165.         moveAPI.forward()
  166.         moveAPI.turnRight()
  167.     else
  168.         moveAPI.turnLeft()
  169.         moveAPI.forward()
  170.         moveAPI.turnLeft()
  171.     end
  172. end
  173.  
  174. function digRow()
  175.     --for i=2, limit.x do
  176.     --    moveAPI.forward()
  177.     --end
  178.     moveAPI.forward(limit.x - 1)
  179. end
  180.  
  181. function returnToChest()
  182.     print("Returning to chest")
  183.  
  184.     while moveAPI.getY() ~= 0 do
  185.         moveAPI.up()
  186.     end
  187.  
  188.     moveAPI.turnTo(moveAPI.west())
  189.     while moveAPI.getZ() ~= 0 do
  190.         moveAPI.forward()
  191.     end
  192.  
  193.     moveAPI.turnTo(moveAPI.south())
  194.     while moveAPI.getX() ~= 0 do
  195.         moveAPI.forward()
  196.     end  
  197. end
  198.  
  199. function returnToMine(x, y, z, face)
  200.     print("Returning to mine")
  201.  
  202.     moveAPI.turnTo(moveAPI.north())
  203.     while moveAPI.getX() ~= x do
  204.         moveAPI.forward()
  205.     end
  206.  
  207.     moveAPI.turnTo(moveAPI.east())
  208.     while moveAPI.getZ() ~= z do
  209.         moveAPI.forward()
  210.     end
  211.  
  212.     while moveAPI.getY() ~= y do
  213.         moveAPI.down()
  214.     end
  215.  
  216.     moveAPI.turnTo(face)
  217. end
  218.  
  219. function emptyInv()
  220.     for i=1, 16 do
  221.         turtle.select(i)
  222.         turtle.drop()
  223.     end
  224.  
  225.     turtle.select(1)
  226. end
  227.  
  228. function main()
  229.  
  230.     --Start of Script
  231.     x, y, z, face = welcomeScreen()
  232.     printSettings(x, y, z)
  233.     moveAPI.initLocation(face)
  234.     checkForChest()
  235.  
  236.     limit = {x=x, y=y, z=z} --Record
  237.     turnSide = RIGHT --determines first rotation
  238.  
  239.  
  240.     --[[
  241.         First go through X
  242.         turn right, forward once
  243.         turn right
  244.         repeat (but change turn side each time)
  245.     --]]
  246.     repeat  
  247.         for j=1, limit.z do
  248.             checkFuel()
  249.             digRow()
  250.             if j ~= limit.z then readyUpRow(turnSide) end
  251.  
  252.             --manditory checks
  253.             if checkInv() then
  254.                 local x, y, z = moveAPI.getXYZ() --save current pos
  255.                 local face = moveAPI.getFace() --save face
  256.                 returnToChest()
  257.                 emptyInv()
  258.                 returnToMine(x, y, z, face)
  259.             end
  260.  
  261.             turnSide = (turnSide ~= 2) and (turnSide+1) or 1 --change turn
  262.         end
  263.  
  264.         readyUpLayer()
  265.         turnSide = (turnSide ~= 2) and (turnSide+1) or 1 --change turn
  266.     until -limit.y == moveAPI.getY()
  267.  
  268.  
  269.     --Go back to chest
  270.     returnToChest()
  271.     emptyInv()
  272.     moveAPI.turnLeft(2)
  273.     turtle.select(1)
  274.  
  275.     print("-----------------")
  276.     print("Done :)")
  277.  
  278. end
  279.  
  280. limit = {}
  281.  
  282. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement