XelaLord

GPSMine

Sep 21st, 2021 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.57 KB | None | 0 0
  1. -- Utility functions --
  2. function containsValue (tab, val)
  3.     for index, value in ipairs(tab) do
  4.         if value == val then
  5.             return true
  6.         end
  7.     end
  8.  
  9.     return false
  10. end
  11.  
  12. function sign(x)
  13.     if x > 0 then
  14.         return 1
  15.     elseif x < 0 then
  16.         return -1
  17.     else
  18.         return 1
  19.     end
  20. end
  21.  
  22. -- Safe Movement Methods --
  23. function forward()
  24.     while t.detect() do t.dig() end
  25.     attemptForward()
  26. end
  27.  
  28. function attemptForward()
  29.     if w == 0 then
  30.         z = z + 1
  31.     elseif w == 1 then
  32.         x = x - 1
  33.     elseif w == 2 then
  34.         z = z - 1
  35.     elseif w == 3 then
  36.         x = x + 1
  37.     end
  38.     while not t.forward() do t.attack() end
  39. end
  40.  
  41. function up()
  42.     while t.detectUp() do t.digUp() end
  43.     y = y + 1
  44.     while not t.up() do t.attackUp() end
  45. end
  46.  
  47. function down()
  48.     while t.detectDown() do t.digDown() end
  49.     y = y - 1
  50.     while not t.down() do t.attackDown() end
  51. end
  52.  
  53. function turnLeft()
  54.     w = (w-1) % 4
  55.    
  56.     if handedness == 0 then
  57.         while not t.turnLeft() do end
  58.     else
  59.         while not t.turnRight() do end
  60.     end
  61. end
  62.  
  63. function turnRight()
  64.     w = (w+1) % 4
  65.    
  66.     if handedness == 0 then
  67.         while not t.turnRight() do end
  68.     else
  69.         while not t.turnLeft() do end
  70.     end
  71. end
  72.  
  73. function selfOrient()
  74.     a, c, b = gps.locate()
  75.     forward()
  76.     x, y, z = gps.locate()
  77.    
  78.     a = x - a
  79.     b = z - b
  80.     print(a,b)
  81.     if b == 1 then
  82.         w = 0
  83.     elseif a == 1 then
  84.         w = 1
  85.     elseif b == -1 then
  86.         w = 2
  87.     else
  88.         w = 3  
  89.     end
  90.    
  91.     t.back()
  92.     x, y, z = gps.locate()
  93.     print(string.format("Location:%s, %s, %s, %s", x, y, z, w))
  94. end
  95.  
  96. function moveToY(set)
  97.     while y < set do
  98.         up()
  99.     end
  100.     while y > set do
  101.         down()
  102.     end
  103. end
  104.  
  105. -- Mining Operation Methods --
  106.  
  107. function dir() -- Mine Relative Coordinate System
  108.     return (w - homeDir) % 4
  109. end
  110.  
  111. function verPos() -- Mine Relative Coordinate System
  112.     if homeDir == 0 then
  113.         return z - homeZ
  114.     elseif homeDir == 1 then
  115.         return x - homeX
  116.     elseif homeDir == 2 then
  117.         return homeZ - z
  118.     elseif homeDir == 3 then
  119.         return homeX - x
  120.     end
  121. end
  122.  
  123. function latPos() -- Mine Relative Coordinate System
  124.     if homeDir == 0 then
  125.         return homeX - x
  126.     elseif homeDir == 1 then
  127.         return z - homeZ
  128.     elseif homeDir == 2 then
  129.         return x - homeX
  130.     elseif homeDir == 3 then
  131.         return homeZ - z
  132.     end
  133. end
  134.  
  135. function returnToCorner()
  136.     while dir() ~= 3 do
  137.         turnLeft()
  138.     end
  139.    
  140.     while latPos() > 0 do
  141.         forward()
  142.     end
  143.     turnLeft()
  144.  
  145.     while verPos() > 0 do
  146.         forward()
  147.     end
  148.     turnLeft()
  149.     turnLeft()
  150. end
  151.  
  152. function resupply()
  153.     returnToCorner()
  154.     returnY = y
  155.     moveToY(homeY)
  156.     emptyAndRefuel()
  157.     moveToY(returnY)
  158. end
  159.  
  160. function emptyAndRefuel()
  161.     while dir() ~= 2 do
  162.         turnLeft()
  163.     end
  164.  
  165.     for i = 1,16 do
  166.         t.select(i)
  167.         if t.getItemCount() > 0 then
  168.             if not t.drop() then
  169.                 up()
  170.                 success, data = t.inspect()
  171.                 while data.name ~= "minecraft:chest" do
  172.                     success, data = t.inspect()
  173.                 end
  174.                 t.drop()
  175.             end
  176.         end
  177.     end
  178.     print("Inventory emptied")
  179.    
  180.     turnRight()
  181.     t.select(1)
  182.     while t.getFuelLevel() < 15000 and (t.suck() or (t.getFuelLevel() < 600)) do
  183.         t.refuel()
  184.         t.drop()
  185.     end
  186.     print("Refueled")
  187.    
  188.     turnRight()
  189. end
  190.  
  191. function checkInventory()
  192.     if (t.getItemCount(16) > 0) or (t.getFuelLevel() < (homeY + 2*width + 50)) then
  193.         print("Fuel low or Inventory full, returning to base")
  194.         local returnLat = latPos()
  195.         local returnVer = verPos()
  196.         local returnDir = w
  197.        
  198.         returnToCorner()
  199.         resupply()
  200.         print("Resuming")
  201.        
  202.         while verPos() < returnVer do
  203.             forward()
  204.         end
  205.         turnRight()
  206.         while latPos() < returnLat do
  207.             forward()
  208.         end
  209.        
  210.         while dir() ~= returnDir do
  211.         turnRight()
  212.         end
  213.     end
  214. end
  215.  
  216. function step()
  217.     checkInventory()
  218.     forward()
  219.    
  220.     success, data = t.inspectUp()
  221.     if not containsValue(unwantedBlocks, data.name) or (mineMode == 1) then
  222.         while t.detectUp() do
  223.             t.digUp()
  224.             t.attackUp()
  225.         end
  226.     end
  227.    
  228.     success, data = t.inspectDown()
  229.     if not containsValue(unwantedBlocks, data.name) or  (mineMode == 1) then
  230.         while t.detectDown() do
  231.             t.digDown()
  232.             t.attackDown()
  233.         end
  234.     end
  235.     print(latPos(), verPos(), dir())
  236. end
  237.  
  238. function layer(size)
  239.     print("Digging layer")
  240.     print(latPos())
  241.     print(verPos())
  242.     print(dir())
  243.    
  244.     -- Find and return to last location in layer --
  245.     while (not t.detect()) and verPos() < size-1 do
  246.         attemptForward()
  247.     end
  248.    
  249.     if verPos() == size-1 then
  250.         turnRight()
  251.         while (not t.detect()) and latPos() < size-1 do
  252.             attemptForward()
  253.         end
  254.        
  255.         if latPos() == size-1 then
  256.             returnToCorner()
  257.             return true
  258.         else
  259.         turnRight()
  260.             while verPos() > 0 do
  261.                 step()
  262.             end
  263.         end    
  264.     else
  265.         while verPos() < size-1 do
  266.             step()
  267.         end
  268.     end
  269.    
  270.     -- Dig Layer--
  271.     for i = latPos(),size-2 do
  272.         if dir() == 0 then
  273.             turnRight()
  274.             step()
  275.             turnRight()
  276.         else
  277.             turnLeft()
  278.             step()
  279.             turnLeft()
  280.         end
  281.        
  282.         for j = 1,size-1 do
  283.             step()
  284.         end
  285.     end
  286.    
  287.     returnToCorner()
  288. end
  289.  
  290. -- Save File Methods --
  291. function updateSave()
  292.     s = string.format(":%s:%s:%s:%s:%s:%s:%s:%s:%s:%s", homeX, homeY, homeZ, homeDir, startHeight, stopHeight, width, currentLayer, tostring(mineMode), tostring(handedness))
  293.     print(s)
  294.     saveFile.writeLine(s)
  295.     saveFile.flush()
  296. end
  297.  
  298. function recoverSaveFile()
  299.     local f = fs.open("quarry_status", "r")
  300.    
  301.     local latestLine = f.readLine()
  302.     local lastLine = latestLine
  303.     while latestLine ~= nil do
  304.         lastLine = latestLine
  305.         latestLine = f.readLine()
  306.     end
  307.     f.close()
  308.    
  309.     local val = {}
  310.     for gmat in string.gmatch(lastLine, ':%d+') do
  311.         i = string.find(gmat, ':')
  312.         table.insert(val, tonumber(string.sub(gmat, i + 1)))
  313.     end
  314.    
  315.     homeX, homeY, homeZ, homeDir, startHeight, stopHeight, width, currentLayer, mineMode, handedness = val[1], val[2], val[3], val[4], val[5], val[6], val[7], val[8], val[9], val[10]
  316.     selfOrient()
  317.    
  318.     print("Save recoverd")
  319.    
  320.     print(string.format("Home:%s, %s, %s, %s", homeX, homeY, homeZ, homeDir))
  321.     print(string.format("Location:%s, %s, %s, %s", x, y, z, w))
  322.     print("Start: " .. startHeight)
  323.     print("End: " .. stopHeight)
  324.     print("Width: " .. width)
  325.     print("Mine Mode: " .. mineMode)
  326.    
  327.     saveFile = fs.open("quarry_status", "w")
  328.     updateSave()
  329. end
  330.  
  331.  
  332. -- Main --
  333. t = turtle
  334. unwantedBlocks = {"minecraft:dirt", "minecraft:grass" , "minecraft:stone", "minecraft:cobblestone", "minecraft:sand", "minecraft:sandstone", "minecraft:gravel", "minecraft:mossy_cobblestone", "minecraft:bedrock", "minecraft:netherrack", "minecraft:soul_sand"}
  335.  
  336.  
  337. if fs.exists("startup") then
  338.     fs.delete("startup")
  339. end
  340. fs.copy(shell.getRunningProgram(), "startup")
  341.  
  342. -- Recover from reboot with SaveFile --
  343. if not fs.exists("quarry_status") then 
  344.     print("Starting Height (Recommended: 5): ")
  345.     startHeight = tonumber(io.read())
  346.     currentLayer = startHeight
  347.    
  348.     print("Stopping Height (Recommended: 30): ")
  349.     stopHeight = tonumber(io.read())
  350.  
  351.     print("Quarry width:")
  352.     width = tonumber(io.read())
  353.    
  354.     print("Input t if all blocks should be mined")
  355.     mineMode = (io.read() == "t") and 1 or 0
  356.    
  357.     --print("Handedness (Default: R): ")
  358.     handedness = 0 --(io.read() == "l") and 1 or 0
  359.        
  360.     x, y, z, w, homeDir = 0, 0, 0, 0, 0
  361.     emptyAndRefuel()
  362.     selfOrient()
  363.     homeX, homeY, homeZ, homeDir = x, y, z, w
  364.    
  365.     saveFile = fs.open("quarry_status", "w")
  366.     updateSave()
  367.    
  368.     print("Starting quarry")
  369.     moveToY(startHeight)
  370. else
  371.     recoverSaveFile()
  372.    
  373.     returnToCorner()
  374.     print("Return to layer at: " .. currentLayer)
  375.     moveToY(currentLayer)
  376. end
  377.  
  378. -- Main Loop --
  379. for i = y, stopHeight, sign(stopHeight-startHeight)*3 do
  380.     currentLayer = i
  381.     moveToY(currentLayer)
  382.     updateSave()
  383.     layer(width)
  384.  
  385.     saveFile.close()
  386.     saveFile = fs.open("quarry_status", "w")
  387. end
  388.  
  389. returnToCorner()
  390. moveToY(homeY)
  391. resupply()
  392.  
  393. fs.delete("quarry_status")
  394. fs.delete("startup")
  395. print("Quarry completed!")
Add Comment
Please, Sign In to add comment