SHARE
TWEET

CCMiningTurtle

hamwich3 Mar 20th, 2019 (edited) 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.  
  3. Mining program for CCTurtles
  4. Saves states and starts where it left off in case of chunk unloading/logging off/game crash
  5.  
  6. Creates a 1x2 shaft and mines in layers from bottom up. Checks up and down for ores. Makes a level every 3 vertical blocks, as shown below:
  7.  
  8. __________||||__________ Surface
  9. ##########||||##########
  10. ##########||||##########
  11. ==========||||========== topLayer
  12. ##########||||##########
  13. ##########||||##########
  14. ==========||||========== level 3 and so on
  15. ##########||||##########
  16. ##########||||##########
  17. ==========||||========== level 2
  18. ##########||||##########
  19. ##########||||##########
  20. ==========||||========== bottomLayer/level 1
  21. ##########||||########## bedrock layer
  22.  
  23. Mines level by spiraling out from the center until reaching the specified width. Usually a multiple of 16. 48 is good (3 x 3 chunks)
  24.  
  25. Descends on the right and ascends on the left to avoid collisions when using multiple turtles.
  26.  
  27. Collisions with other turtles will not cause a problem, they will wait.
  28.  
  29. Can auto refuel at surface if fueling turtle is set up.
  30.  
  31. TODO:
  32.     -remove all instances of log() instead of just having it do nothing. (Long logs were crashing the program)
  33.     -automatically find peripherals and act accordingly instead of assuming they're in the correct place
  34.     -assess surroundings on startup: chest, refueling, disk drive etc.
  35.     -implement disk drive to help coordinate multiple turtles
  36.     -clear instructions/options on startup
  37.     -installer
  38. ]]--
  39.  
  40. --{x, y, z, f} store coords/facing of mine. pathIndices help calculate the path if the turtle is shutoff while mining.
  41. mine = {x, y, z, f, width, level, bottomLayer, topLayer, pathIndex, totalBlocksInPath, surfacePathIndex = 1, returnPathIndex = 1}
  42. status = {mining, surfacing, returning, paused, pauseatsurface}
  43.  
  44. --create new mineshaft
  45. newMine = false
  46.  
  47. distbetweenlevels = 3
  48.  
  49. debug = false
  50.  
  51. tFacing =
  52. {
  53. south = 0,
  54. west = 1,
  55. north = 2,
  56. east = 3
  57. }
  58.  
  59. table.insert(turtle, x)
  60. table.insert(turtle, y)
  61. table.insert(turtle, z)
  62. table.insert(turtle, f)
  63.  
  64.  --ignore these blocks while mining, drop while purging
  65. local tWaste = {
  66.   'minecraft:cobblestone',
  67.   'minecraft:sandstone',
  68.   'minecraft:stone',
  69.   'minecraft:torch',
  70.   'minecraft:dirt',
  71.   'minecraft:grass',
  72.   'minecraft:gravel',
  73.   'minecraft:sand',
  74.   'minecraft:hardened_clay',
  75.   'minecraft:planks',
  76.   'minecraft:fence',
  77.   'minecraft:nether_brick',
  78.   'minecraft:nether_brick_fence',
  79.   'minecraft:nether_brick_stairs',
  80.   'minecraft:netherrack',
  81.   'minecraft:soul_sand',
  82.   'chisel:basalt',
  83.   'chisel:basalt2'
  84. }
  85.  
  86. math.round = function(num)
  87.     local decimalval = num - math.floor(num)
  88.     if decimalval >= .5 then
  89.         return math.ceil(num)
  90.     else
  91.         return math.floor(num)
  92.     end
  93. end
  94.  
  95. math.pow = function(base, exp)
  96.     return base^exp
  97. end
  98.  
  99. function saveMineData()
  100.     if debug then print("saveMineData") end
  101.     local fx = fs.open("minedata", "w")
  102.     fx.write(textutils.serialize(mine))
  103.     fx.close()
  104. end
  105.  
  106. function getMineData()
  107.     if debug then print("getMineData") end
  108.     if fs.exists("minedata") then
  109.         local h = fs.open("minedata", "r")
  110.         if not h then return false end
  111.         mine = textutils.unserialize(h.readAll())
  112.         h.close()
  113.         return true
  114.     else return false
  115.     end
  116. end
  117.  
  118. function saveStatus()
  119.     if debug then print("saveStatus") end
  120.     local data = textutils.serialize(status)
  121.     local f = fs.open("status", "w")
  122.     f.write(data)
  123.     f.close()
  124. end
  125.  
  126. function getStatus()
  127.     if debug then print("getStatus") end
  128.     local f = fs.open("status", "r")
  129.     if not f then return false end
  130.     local data = f.readAll()
  131.     status = textutils.unserialize(data)
  132.     return true
  133. end
  134.  
  135. function getPosData()
  136.     if debug then print("getPosData") end
  137.     if fs.exists("positiondata") then
  138.         local h = fs.open("positiondata", "r")
  139.         local data = textutils.unserialize(h.readAll())
  140.         h.close()
  141.         turtle.x = data.x
  142.         turtle.y = data.y
  143.         turtle.z = data.z
  144.         turtle.f = data.f
  145.         return true
  146.     else return false
  147.     end
  148. end
  149.  
  150. function savePosData()
  151.     if debug then print("savePosData") end
  152.     local h = fs.open("positiondata", "w")
  153.     local data = {x = turtle.x, y = turtle.y, z = turtle.z, f = turtle.f}
  154.     h.write(textutils.serialize(data))
  155.     h.close()
  156. end
  157.  
  158. local function updatePos(m)
  159.     if debug then print("updatePos") end
  160.     if turtle.f == tFacing.north then
  161.         if m == "f" then
  162.             turtle.z = turtle.z - 1
  163.         elseif m == "b" then
  164.             turtle.z = turtle.z + 1
  165.         end
  166.     elseif turtle.f == tFacing.south then
  167.         if m == "f" then
  168.             turtle.z = turtle.z + 1
  169.         elseif m == "b" then
  170.             turtle.z = turtle.z - 1
  171.         end
  172.     elseif turtle.f == tFacing.east then
  173.         if m == "f" then
  174.             turtle.x = turtle.x + 1
  175.         elseif m == "b" then
  176.             turtle.x = turtle.x - 1
  177.         end
  178.     elseif turtle.f == tFacing.west then
  179.         if m == "f" then
  180.             turtle.x = turtle.x - 1
  181.         elseif m == "b" then
  182.             turtle.x = turtle.x + 1
  183.         end
  184.     else
  185.         error("f")
  186.     end
  187.     if m == "u" then turtle.y = turtle.y + 1 end
  188.     if m == "d" then turtle.y = turtle.y - 1 end
  189.     savePosData()
  190. end
  191.  
  192. function face(direction)
  193.     if debug then print("face") end
  194.     local n = turtle.f - direction
  195.     if n == -1 or n == 3 then
  196.         while direction ~= turtle.f do turnRight() end
  197.     else
  198.         while direction ~= turtle.f do turnLeft() end
  199.     end
  200. end
  201.  
  202. function turnLeft()
  203.     if debug then print("turnLeft") end
  204.     if turtle.turnLeft() then
  205.         turtle.f = turtle.f - 1
  206.         if turtle.f == -1 then turtle.f = 3 end
  207.         savePosData()
  208.     end
  209. end
  210.  
  211. function turnRight()
  212.     if debug then print("turnRight") end
  213.     if turtle.turnRight() then
  214.         turtle.f = turtle.f + 1
  215.         if turtle.f == 4 then turtle.f = 0 end
  216.         savePosData()
  217.     end
  218. end
  219.  
  220. function forward()
  221.     if debug then print("forward") end
  222.     if turtle.forward() then
  223.         updatePos("f")
  224.     else
  225.         if turtle.getFuelLevel() == 0 then
  226.             refuel()
  227.             forward()
  228.         elseif turtle.detect() then
  229.             dig()
  230.             forward()
  231.         else
  232.             for a = 0, 4 do turtle.attack() os.sleep(.25) end
  233.             forward()
  234.         end
  235.     end
  236. end
  237.  
  238. function back()
  239.     if debug then print("back") end
  240.     if turtle.back() then
  241.         updatePos("b")
  242.     else
  243.         if turtle.getFuelLevel() == 0 then
  244.             refuel()
  245.             back()
  246.         else
  247.             while turtle.back() == false do os.sleep(1) end
  248.         end
  249.     end
  250. end
  251.  
  252. function up()
  253.     if debug then print("up") end
  254.     if turtle.up() then
  255.         updatePos("u")
  256.     else
  257.         if turtle.getFuelLevel() == 0 then
  258.             refuel()
  259.             up()
  260.         elseif turtle.detectUp() then
  261.             digUp()
  262.             up()
  263.         else
  264.             for a = 0, 4 do turtle.attackUp() os.sleep(.25) end
  265.             up()
  266.         end
  267.     end
  268.  
  269. end
  270.  
  271. function down()
  272.     if debug then print("down") end
  273.     if turtle.down() then
  274.         updatePos("d")
  275.     else
  276.         if turtle.getFuelLevel() == 0 then
  277.             refuel()
  278.             down()
  279.         elseif turtle.detectDown() then
  280.             digDown()
  281.             down()
  282.         else
  283.             for a = 0, 4 do turtle.attackDown() os.sleep(.25) end
  284.             down()
  285.         end
  286.     end
  287.  
  288. end
  289.  
  290. function findInInventory(itemname)
  291.     if debug then print("find") end
  292.     if type(itemname) ~= "string" then error("itemname must be string") end
  293.     local numSlots = 16
  294.     local i
  295.     for i = 1, numSlots do
  296.         local itemdata = turtle.getItemDetail(i)
  297.         if itemdata and itemdata.name == itemname then return true,i end
  298.     end
  299.     return false
  300. end
  301.  
  302. function placeTorch()
  303.     if debug then print("placeTorch") end
  304.     local torchFound, torchSlot = findInInventory("minecraft:torch")
  305.     if not torchFound then return end
  306.     local placeHolder = turtle.getSelectedSlot()
  307.     digDown()
  308.     turtle.select(torchSlot)
  309.     turtle.placeDown()
  310.     turtle.select(placeHolder)
  311. end
  312.  
  313. function checkCollectLava()
  314.     local bucketFound, bucketSlot = findInInventory("minecraft:bucket")
  315.     if not bucketFound then return end
  316.     local success, data = turtle.inspect()
  317.     if success and (data.name == "minecraft:flowing_lava" or data.name == "minecraft:lava") then
  318.         if data.metadata == 0 then
  319.             local placeHolder = turtle.getSelectedSlot()
  320.             turtle.select(bucketSlot)
  321.             turtle.refuel()
  322.             turtle.place()
  323.             turtle.refuel()
  324.             turtle.select(placeHolder)
  325.         end
  326.     end
  327.     local success, updata = turtle.inspectUp()
  328.     if success and (updata.name == "minecraft:flowing_lava" or updata.name == "minecraft:lava") then
  329.         if updata.metadata == 0 then
  330.             local placeHolder = turtle.getSelectedSlot()
  331.             turtle.select(bucketSlot)
  332.             turtle.refuel()
  333.             turtle.placeUp()
  334.             turtle.refuel()
  335.             turtle.select(placeHolder)
  336.         end
  337.     end
  338.     local success, downdata = turtle.inspectDown()
  339.     if success and (downdata.name == "minecraft:flowing_lava" or downdata.name == "minecraft:lava") then
  340.         if downdata.metadata == 0 then
  341.             local placeHolder = turtle.getSelectedSlot()
  342.             turtle.select(bucketSlot)
  343.             turtle.refuel()
  344.             turtle.placeDown()
  345.             turtle.refuel()
  346.             turtle.select(placeHolder)
  347.         end
  348.     end
  349. end
  350.  
  351. function dig()
  352.     if debug then print("dig") end
  353.     while turtle.detect() do
  354.         local success, block = turtle.inspect()
  355.         local comp = block.name and string.match(block.name, "comput")
  356.         local chest = block.name and string.match(block.name, "chest")
  357.         local bedrock = block.name and string.match(block.name, "bedrock")
  358.         if comp ~= nil or chest ~= nil or berock ~= nil then
  359.             os.sleep(5)
  360.         elseif success and (block.name ~= nil) then
  361.             turtle.dig()
  362.         end
  363.     end
  364.     listItemsFound()
  365.     checkCollectLava()
  366. end
  367.  
  368. function digUp()
  369.     if debug then print("digUp") end
  370.     while turtle.detectUp() do
  371.         local success, block = turtle.inspectUp()
  372.         local comp = block.name and string.match(block.name, "comput")
  373.         local chest = block.name and string.match(block.name, "chest")
  374.         local bedrock = block.name and string.match(block.name, "bedrock")
  375.         if comp ~= nil or chest ~= nil or berock ~= nil then
  376.             os.sleep(5)
  377.         elseif success and (block.name ~= nil) then
  378.             turtle.digUp()
  379.         end
  380.     end
  381.     listItemsFound()
  382.     checkCollectLava()
  383. end
  384.  
  385. function digDown()
  386.     if debug then print("digDown") end
  387.     while turtle.detectDown() do
  388.         local success, block = turtle.inspectDown()
  389.         local comp = block.name and string.match(block.name, "comput")
  390.         local chest = block.name and string.match(block.name, "chest")
  391.         local bedrock = block.name and string.match(block.name, "bedrock")
  392.         if comp ~= nil or chest ~= nil or berock ~= nil then
  393.             os.sleep(5)
  394.         elseif success and (block.name ~= nil) then
  395.             turtle.digDown()
  396.         end
  397.     end
  398.     listItemsFound()
  399.     checkCollectLava()
  400. end
  401.  
  402. function log(data)
  403.     --lazy way of no longer logging data
  404.     --do nothing
  405. end
  406.  
  407. function refuel()
  408.     if debug then print("refuel") end
  409.     for n = 1, 16 do
  410.         if turtle.getFuelLevel() > (turtle.getFuelLimit() * .9) then
  411.             return
  412.         end
  413.         turtle.select(n)
  414.         local item = turtle.getItemDetail()
  415.         if item ~= nil and item.name ~= "minecraft:torch" then
  416.             turtle.refuel()
  417.         end
  418.     end
  419.     if turtle.getFuelLevel() == 0 then
  420.         log("Out of fuel, waiting for refuel...")
  421.         while turtle.getFuelLevel() == 0 do
  422.             for n = 1, 16 do
  423.                 turtle.select(n)
  424.                 local item = turtle.getItemDetail()
  425.                 if item ~= nil and item.name ~= "minecraft:torch" then
  426.                     turtle.refuel()
  427.                 end
  428.                 if turtle.getFuelLevel() > (turtle.getFuelLimit() * .9) then
  429.                     return
  430.                 end
  431.             end
  432.             os.sleep(5)
  433.         end
  434.     end
  435. end
  436.  
  437. function getNextTileCoordinates()
  438.     local tileNum = mine.pathIndex
  439.     local intRoot = math.floor(math.sqrt(tileNum))
  440.     local x = (math.round(intRoot/2)*math.pow(-1,intRoot+1))+(math.pow(-1,intRoot+1)*(((intRoot*(intRoot+1))-tileNum)-math.abs((intRoot*(intRoot+1))-tileNum))/2)
  441.     local z = (math.round(intRoot/2)*math.pow(-1,intRoot))+(math.pow(-1,intRoot+1)*(((intRoot*(intRoot+1))-tileNum)+math.abs((intRoot*(intRoot+1))-tileNum))/2)
  442.     return mine.x + x, mine.z + z
  443. end
  444.  
  445. function mineNextBlock()
  446.     local nextBlock = {x, z}
  447.     nextBlock.x, nextBlock.z = getNextTileCoordinates()
  448.     if nextBlock.x > turtle.x then
  449.         face(tFacing.east)
  450.         dig()
  451.         checkUpDown()
  452.         forward()
  453.     elseif nextBlock.x < turtle.x then
  454.         face(tFacing.west)
  455.         dig()
  456.         checkUpDown()
  457.         forward()
  458.     elseif nextBlock.z > turtle.z then
  459.         face(tFacing.south)
  460.         dig()
  461.         checkUpDown()
  462.         forward()
  463.     elseif nextBlock.z < turtle.z then
  464.         face(tFacing.north)
  465.         dig()
  466.         checkUpDown()
  467.         forward()
  468.     end
  469.     if (nextBlock.x == turtle.x) and (nextBlock.z == turtle.z)then
  470.         mine.pathIndex = mine.pathIndex + 1
  471.         saveMineData()
  472.     end
  473. end
  474.  
  475. function checkUpDown()
  476.     listItemsFound()
  477.     local success, block = turtle.inspectUp()
  478.     local digup, digdown = true, true
  479.     if success then
  480.         for i, blockname in pairs(tWaste) do
  481.             if string.find(block.name, blockname) then digup = false end
  482.         end
  483.         if digup then digUp() end
  484.     end
  485.     success, block = turtle.inspectDown()
  486.     if success then
  487.         for i, blockname in pairs(tWaste) do
  488.             if string.find(block.name, blockname) then digdown = false end
  489.         end
  490.         if digdown then digDown() end
  491.     end
  492. end
  493.  
  494. function calculateSurfacePath()
  495.     local tSurfacePath = {}
  496.     local tReverseReturnPath = {}
  497.  
  498.     -- Surf shaft is immediatly left of return shaft
  499.     local surfaceShaft = {x, z}
  500.  
  501.     -- simulated position during surface
  502.     local simPosSurf = {x = turtle.x, y = turtle.y, z = turtle.z, f = turtle.f}
  503.     -- simulated position during return
  504.     local simPosRetn = {x = turtle.x, y = turtle.y, z = turtle.z, f = turtle.f}
  505.  
  506.     -- starts and ends on same block
  507.     table.insert(tSurfacePath, {x = simPosSurf.x, y = simPosSurf.y, z = simPosSurf.z})
  508.     table.insert(tReverseReturnPath, {x = simPosRetn.x, y = simPosRetn.y, z = simPosRetn.z})
  509.  
  510.     if mine.f == tFacing.north then
  511.         surfaceShaft.x = mine.x - 1
  512.         surfaceShaft.z = mine.z
  513.     elseif mine.f == tFacing.south then
  514.         surfaceShaft.x = mine.x + 1
  515.         surfaceShaft.z = mine.z
  516.     elseif mine.f == tFacing.east then
  517.         surfaceShaft.x = mine.x
  518.         surfaceShaft.z = mine.z - 1
  519.     elseif mine.f == tFacing.west then
  520.         surfaceShaft.x = mine.x
  521.         surfaceShaft.z = mine.z + 1
  522.     end
  523.  
  524.     --x Relative Distance Surface and Return
  525.     --z Relative Distance Surface and Return
  526.     local xRelDistSurf = turtle.x - surfaceShaft.x
  527.     local zRelDistSurf = turtle.z - surfaceShaft.z
  528.     local xRelDistRetn = turtle.x - mine.x
  529.     local ZRelDistRetn = turtle.z - mine.z
  530.  
  531.     -- Surf Path
  532.     --both positive
  533.     if xRelDistSurf >= 0 and zRelDistSurf >= 0 then
  534.         while simPosSurf.x > surfaceShaft.x do
  535.             simPosSurf.x = simPosSurf.x - 1
  536.             table.insert(tSurfacePath, {x = simPosSurf.x, y = simPosSurf.y, z = simPosSurf.z})
  537.         end
  538.         while simPosSurf.z > surfaceShaft.z do
  539.             simPosSurf.z = simPosSurf.z - 1
  540.             table.insert(tSurfacePath, {x = simPosSurf.x, y = simPosSurf.y, z = simPosSurf.z})
  541.         end
  542.         --both negative
  543.     elseif xRelDistSurf < 0 and zRelDistSurf < 0 then
  544.         while simPosSurf.x < surfaceShaft.x do
  545.             simPosSurf.x = simPosSurf.x + 1
  546.             table.insert(tSurfacePath, {x = simPosSurf.x, y = simPosSurf.y, z = simPosSurf.z})
  547.         end
  548.         while simPosSurf.z < surfaceShaft.z do
  549.             simPosSurf.z = simPosSurf.z + 1
  550.             table.insert(tSurfacePath, {x = simPosSurf.x, y = simPosSurf.y, z = simPosSurf.z})
  551.         end
  552.         --x negative and z positive
  553.     elseif xRelDistSurf < 0 and zRelDistSurf >= 0 then
  554.         while simPosSurf.z > surfaceShaft.z do
  555.             simPosSurf.z = simPosSurf.z - 1
  556.             table.insert(tSurfacePath, {x = simPosSurf.x, y = simPosSurf.y, z = simPosSurf.z})
  557.         end
  558.         while simPosSurf.x < surfaceShaft.x do
  559.             simPosSurf.x = simPosSurf.x + 1
  560.             table.insert(tSurfacePath, {x = simPosSurf.x, y = simPosSurf.y, z = simPosSurf.z})
  561.         end
  562.         --x positive and z negative
  563.     elseif xRelDistSurf >= 0 and zRelDistSurf < 0 then
  564.         while simPosSurf.z < surfaceShaft.z do
  565.             simPosSurf.z = simPosSurf.z + 1
  566.             table.insert(tSurfacePath, {x = simPosSurf.x, y = simPosSurf.y, z = simPosSurf.z})
  567.         end
  568.         while simPosSurf.x > surfaceShaft.x do
  569.             simPosSurf.x = simPosSurf.x - 1
  570.             table.insert(tSurfacePath, {x = simPosSurf.x, y = simPosSurf.y, z = simPosSurf.z})
  571.         end
  572.     end
  573.  
  574.     -- Returning Path
  575.     --both positive
  576.     if xRelDistRetn >= 0 and ZRelDistRetn >= 0 then
  577.         while simPosRetn.x > mine.x do
  578.             simPosRetn.x = simPosRetn.x - 1
  579.             table.insert(tReverseReturnPath, {x = simPosRetn.x, y = simPosRetn.y, z = simPosRetn.z})
  580.         end
  581.         while simPosRetn.z > mine.z do
  582.             simPosRetn.z = simPosRetn.z - 1
  583.             table.insert(tReverseReturnPath, {x = simPosRetn.x, y = simPosRetn.y, z = simPosRetn.z})
  584.         end
  585.         --both negative
  586.     elseif xRelDistRetn < 0 and ZRelDistRetn < 0 then
  587.         while simPosRetn.x < mine.x do
  588.             simPosRetn.x = simPosRetn.x + 1
  589.             table.insert(tReverseReturnPath, {x = simPosRetn.x, y = simPosRetn.y, z = simPosRetn.z})
  590.         end
  591.         while simPosRetn.z < mine.z do
  592.             simPosRetn.z = simPosRetn.z + 1
  593.             table.insert(tReverseReturnPath, {x = simPosRetn.x, y = simPosRetn.y, z = simPosRetn.z})
  594.         end
  595.         --x negative and z positive
  596.     elseif xRelDistRetn < 0 and ZRelDistRetn >= 0 then
  597.         while simPosRetn.z > mine.z do
  598.             simPosRetn.z = simPosRetn.z - 1
  599.             table.insert(tReverseReturnPath, {x = simPosRetn.x, y = simPosRetn.y, z = simPosRetn.z})
  600.         end
  601.         while simPosRetn.x < mine.x do
  602.             simPosRetn.x = simPosRetn.x + 1
  603.             table.insert(tReverseReturnPath, {x = simPosRetn.x, y = simPosRetn.y, z = simPosRetn.z})
  604.         end
  605.         --x positive and z negative
  606.     elseif xRelDistRetn >= 0 and ZRelDistRetn < 0 then
  607.         while simPosRetn.z < mine.z do
  608.             simPosRetn.z = simPosRetn.z + 1
  609.             table.insert(tReverseReturnPath, {x = simPosRetn.x, y = simPosRetn.y, z = simPosRetn.z})
  610.         end
  611.         while simPosRetn.x > mine.x do
  612.             simPosRetn.x = simPosRetn.x - 1
  613.             table.insert(tReverseReturnPath, {x = simPosRetn.x, y = simPosRetn.y, z = simPosRetn.z})
  614.         end
  615.     end
  616.  
  617.     while simPosSurf.y < mine.y do
  618.         simPosSurf.y = simPosSurf.y + 1
  619.         simPosRetn.y = simPosRetn.y + 1
  620.         table.insert(tSurfacePath, {x = simPosSurf.x, y = simPosSurf.y, z = simPosSurf.z})
  621.         table.insert(tReverseReturnPath, {x = simPosRetn.x, y = simPosRetn.y, z = simPosRetn.z})
  622.     end
  623.     table.insert(tSurfacePath, {x = mine.x, y = mine.y, z = mine.z})
  624.  
  625.     local tReturnPath = reverseTable(tReverseReturnPath)
  626.  
  627.     local sp = fs.open("pathdata/surfacePathdata", "w")
  628.     local data = textutils.serialize(tSurfacePath)
  629.     sp.write(data)
  630.     sp.close()
  631.  
  632.     local rp = fs.open("pathdata/returnPathdata", "w")
  633.     data = textutils.serialize(tReturnPath)
  634.     rp.write(data)
  635.     rp.close()
  636. end
  637.  
  638. function followSurfacePath()
  639.     if debug then print("followSurfacePath") end
  640.     local hPath = fs.open("pathdata/surfacePathdata", "r")
  641.     local data = hPath.readAll()
  642.     assert(data)
  643.     local surfaceComplete = false
  644.     hPath.close()
  645.  
  646.     local tPathData = textutils.unserialize(data)
  647.     if not tPathData then
  648.         calculateSurfacePath()
  649.         return surfaceComplete
  650.     end
  651.     if mine.surfacePathIndex <= #tPathData then
  652.  
  653.         log("spi "..mine.surfacePathIndex.." "..#tPathData)
  654.  
  655.         local tLoc = tPathData[mine.surfacePathIndex]
  656.         if tLoc.x > turtle.x then
  657.             face(tFacing.east)
  658.             forward()
  659.         elseif tLoc.x < turtle.x then
  660.             face(tFacing.west)
  661.             forward()
  662.         elseif tLoc.z > turtle.z then
  663.             face(tFacing.south)
  664.             forward()
  665.         elseif tLoc.z < turtle.z then
  666.             face(tFacing.north)
  667.             forward()
  668.         elseif tLoc.y > turtle.y then
  669.             up()
  670.         elseif tLoc.y < turtle.y then
  671.             down()
  672.         end
  673.         if (tLoc.x == turtle.x) and (tLoc.z == turtle.z) and (tLoc.y == turtle.y) then
  674.             mine.surfacePathIndex = mine.surfacePathIndex + 1
  675.             saveMineData()
  676.         end
  677.     else surfaceComplete = true
  678.     end
  679.     return surfaceComplete
  680. end
  681.  
  682. function followReturnPath()
  683.     if debug then print("followReturnPath") end
  684.     local hPath = fs.open("pathdata/returnPathdata", "r")
  685.     local data = hPath.readAll()
  686.     hPath.close()
  687.     local returnComplete = false
  688.  
  689.     local tPathData = textutils.unserialize(data)
  690.     if mine.returnPathIndex <= #tPathData then
  691.         log("rpi "..mine.returnPathIndex.." "..#tPathData)
  692.         local tLoc = tPathData[mine.returnPathIndex]
  693.         if tLoc.x > turtle.x then
  694.             face(tFacing.east)
  695.             forward()
  696.         elseif tLoc.x < turtle.x then
  697.             face(tFacing.west)
  698.             forward()
  699.         elseif tLoc.z > turtle.z then
  700.             face(tFacing.south)
  701.             forward()
  702.         elseif tLoc.z < turtle.z then
  703.             face(tFacing.north)
  704.             forward()
  705.         elseif tLoc.y > turtle.y then
  706.             up()
  707.         elseif tLoc.y < turtle.y then
  708.             down()
  709.         end
  710.         if (tLoc.x == turtle.x) and (tLoc.z == turtle.z) and (tLoc.y == turtle.y) then
  711.             mine.returnPathIndex = mine.returnPathIndex + 1
  712.             saveMineData()
  713.         end
  714.     else returnComplete = true
  715.     end
  716.     return returnComplete
  717. end
  718.  
  719. function reverseTable(t)
  720.     local reversedTable = {}
  721.     local itemCount = #t
  722.     for k, v in ipairs(t) do
  723.         reversedTable[itemCount + 1 - k] = v
  724.     end
  725.     return reversedTable
  726. end
  727.  
  728. function returnToCenter()
  729.     if debug then print("returnToCenter") end
  730.     local xDist = turtle.x - mine.x
  731.     local zDist = turtle.z - mine.z
  732.     turnLeft()
  733.     while turtle.x ~= mine.x and turtle.z ~= mine.z do
  734.         forward()
  735.     end
  736.     turnLeft()
  737.     while turtle.x ~= mine.x or turtle.z ~= mine.z do
  738.         forward()
  739.     end
  740.     face(mine.f)
  741. end
  742.  
  743. function nextLevelUp()
  744.     for i = 1, distbetweenlevels do
  745.         if turtle.y >= mine.y then break end
  746.         up()
  747.     end
  748.     mine.level = mine.level + 1
  749.     mine.pathIndex = 1
  750.     saveMineData()
  751. end
  752.  
  753. function emptySlots()
  754.     local slots = 0
  755.     for i = 1, 16 do
  756.         local item = turtle.getItemDetail(i)
  757.         if item == nil then
  758.             slots = slots + 1
  759.         end
  760.     end
  761.     return slots
  762. end
  763.  
  764. function purgeInventory()
  765.     if debug then print("purgeInventory") end
  766.     local success, trash = findInInventory("cyclicmagic:trash")
  767.     if success then
  768.         turtle.select(trash)
  769.         dig()
  770.         turtle.place()
  771.     end
  772.     for i = 1, 16 do
  773.         local keep = true
  774.         turtle.select(i)
  775.         local item = turtle.getItemDetail()
  776.         if item ~= nil then
  777.             listItemsFound(item)
  778.             for k, blockname in pairs(tWaste) do
  779.                 if string.find(item.name, blockname) then keep = false end
  780.             end
  781.             if item.name == "minecraft:bucket" then keep = true end
  782.             if item.name == "minecraft:torch" then keep = true end
  783.             if item.name == "computercraft:advanced_modem" then keep = true end
  784.             if item.name == "cyclicmagic:trash" then keep = true end
  785.         end
  786.         if keep == false then turtle.drop() end
  787.     end
  788.     dig()
  789. end
  790.  
  791. function listItemsFound(passeditem)
  792.     local success, item = false, nil
  793.     if passeditem then
  794.         success, item = true, passeditem
  795.     else
  796.         success, item = turtle.inspect()
  797.     end
  798.     local logdata = true
  799.     if success then
  800.         local h = fs.open("foundlog", "r")
  801.         local data = h.readAll()
  802.         h.close()
  803.         local datatable = textutils.unserialize(data) or {}
  804.         for k, v in pairs(datatable) do
  805.             if v == item.name then
  806.                 logdata = false
  807.             end
  808.         end
  809.         if logdata then
  810.             table.insert(datatable, item.name)
  811.             local f = fs.open("foundlog", "w")
  812.             f.write(textutils.serialize(datatable))
  813.             f.close()
  814.         end
  815.     end
  816.     listItemsFoundDown()
  817.     listItemsFoundUp()
  818. end
  819.  
  820. function listItemsFoundDown()
  821.     local success, item = turtle.inspectDown()
  822.     local logdata = true
  823.     if success then
  824.         local h = fs.open("foundlog", "r")
  825.         local data = h.readAll()
  826.         h.close()
  827.         local datatable = textutils.unserialize(data) or {}
  828.         for k, v in pairs(datatable) do
  829.             if v == item.name then
  830.                 logdata = false
  831.             end
  832.         end
  833.         if logdata then
  834.             table.insert(datatable, item.name)
  835.             local f = fs.open("foundlog", "w")
  836.             f.write(textutils.serialize(datatable))
  837.             f.close()
  838.         end
  839.     end
  840. end
  841.  
  842. function listItemsFoundUp()
  843.     local success, item = turtle.inspectUp()
  844.     local logdata = true
  845.     if success then
  846.         local h = fs.open("foundlog", "r")
  847.         local data = h.readAll()
  848.         h.close()
  849.         local datatable = textutils.unserialize(data) or {}
  850.         for k, v in pairs(datatable) do
  851.             if v == item.name then
  852.                 logdata = false
  853.             end
  854.         end
  855.         if logdata then
  856.             table.insert(datatable, item.name)
  857.             local f = fs.open("foundlog", "w")
  858.             f.write(textutils.serialize(datatable))
  859.             f.close()
  860.         end
  861.     end
  862. end
  863.  
  864. function deposit()
  865.     if debug then print("deposit") end
  866.     local success, boxdata = turtle.inspect()
  867.     if not success or string.find("chest", string.lower(boxdata.name), 1, true) then
  868.         os.sleep(5)
  869.         deposit()
  870.     end
  871.     if success and string.find("chest", string.lower(boxdata.name), 1, true) then
  872.         deposit()
  873.     end
  874.     for s = 1, 16 do
  875.         turtle.select(s)
  876.         local itemdata = turtle.getItemDetail()
  877.         if itemdata and itemdata.name ~= "minecraft:bucket"
  878.             and itemdata.name ~= "minecraft:torch"
  879.             and itemdata.name ~= "computercraft:advanced_modem"
  880.             and itemdata.name ~= "cyclicmagic:trash"
  881.             then
  882.             local success = turtle.drop()
  883.             if not success then
  884.                 while not success do
  885.                     os.sleep(1)
  886.                     success = turtle.drop()
  887.                 end
  888.             end
  889.         end
  890.     end
  891. end
  892.  
  893. function mineLoop()
  894.     while true do
  895.         if debug then print("mineloop") end
  896.         if status.mining == true and not status.paused then
  897.             if not status.surfacing and not status.returning then
  898.                 if mine.pathIndex < mine.totalBlocksInPath then
  899.                     mineNextBlock()
  900.                     if (turtle.x % 5) == 0 and (turtle.z % 5) == 0 then
  901.                         placeTorch()
  902.                     end
  903.                 else
  904.                     returnToCenter()
  905.                     nextLevelUp()
  906.    
  907.                     --END MINING
  908.                     local currentLayer = mine.bottomLayer + (3 * (mine.level - 1))
  909.                     if currentLayer >= mine.topLayer then
  910.                         calculateSurfacePath()
  911.                         status.surfacing = true
  912.                         status.pauseatsurface = true
  913.                         saveStatus()
  914.                         mine.surfacePathIndex = 1
  915.                         mine.returnPathIndex = 1
  916.                         saveMineData()
  917.                     end
  918.                 end
  919.             end
  920.             if emptySlots() == 0 and not status.surfacing and not status.returning then
  921.                 purgeInventory()
  922.                 if emptySlots() <= 1 and not status.surfacing and not status.returning then
  923.                     calculateSurfacePath()
  924.                     status.surfacing = true
  925.                     saveStatus()
  926.                     mine.surfacePathIndex = 1
  927.                     mine.returnPathIndex = 1
  928.                     saveMineData()
  929.                 end
  930.             end
  931.             if status.surfacing then
  932.                 status.surfacing = not followSurfacePath()
  933.                 status.returning = not status.surfacing
  934.                 saveStatus()
  935.                 if status.returning then
  936.                     face(mine.f)
  937.                     deposit()
  938.                     refuelAtStation()
  939.                     if status.pauseatsurface then
  940.                         status.paused = true
  941.                         status.returning = false
  942.                         saveStatus()
  943.                     end
  944.                 end
  945.             end
  946.             if status.returning then
  947.                 status.returning = not followReturnPath()
  948.                 saveStatus()
  949.             end
  950.         end
  951.         if status.paused then
  952.             if status.mining == true then
  953.                 up()
  954.                 status.mining = false
  955.                 saveStatus()
  956.             end
  957.             if turtle.detectDown() then up() end
  958.         end
  959.        
  960.     end
  961. end
  962.  
  963. function startNewMine(widthEntered, depthEntered, topLayerEntered)
  964.     if debug then print("startNewMine") end
  965.     if status.mining == false then
  966.         mine.totalBlocksInPath = ((tonumber(widthEntered)/2)^2)*4
  967.         mine.bottomLayer = tonumber(depthEntered)
  968.         mine.level = 1
  969.         mine.pathIndex = 1
  970.         mine.surfacePathIndex = 1
  971.         mine.returnPathIndex = 1
  972.         mine.width = tonumber(widthEntered)
  973.         mine.topLayer = tonumber(topLayerEntered)
  974.         mine.x = turtle.x
  975.         mine.y = turtle.y
  976.         mine.z = turtle.z
  977.         mine.f = turtle.f
  978.         saveMineData()
  979.         status.mining = true;
  980.         saveStatus()
  981.         if (newMine) then
  982.             log("Initializing Mine at: "..turtle.x..", "..turtle.y..", "..turtle.z..", width="..widthEntered..", bottom="..depthEntered)
  983.             createShaft()
  984.         end
  985.         while turtle.y > mine.bottomLayer do
  986.             down()
  987.         end
  988.         mineLoop()
  989.     end
  990. end
  991.  
  992. function createShaft()
  993.     turnLeft()
  994.     while turtle.y > mine.bottomLayer do
  995.         digDown()
  996.         down()
  997.         dig()
  998.     end
  999.     --turnLeft()
  1000.     --dig()
  1001.     --forward()
  1002.     --turnRight()
  1003.     --dig()
  1004.     local success, slot = findInInventory("minecraft:ladder")
  1005.     while turtle.y < mine.y do
  1006.         digUp()
  1007.         up()
  1008.         dig()
  1009.         if success then
  1010.             turtle.select(slot)
  1011.             if turtle.getItemDetail(slot) and (turtle.getItemDetail(slot)).name ~= "minecraft:ladder" then
  1012.                 success, slot = findInInventory("minecraft:ladder")
  1013.                 if success then
  1014.                     turtle.select(slot)
  1015.                     turtle.place()
  1016.                 end
  1017.             else
  1018.                 turtle.place()
  1019.             end
  1020.         end
  1021.     end
  1022.     turnRight()
  1023.     --forward()
  1024. end
  1025.  
  1026. function enterStartData()
  1027.     local dock = {x, y, z, f}
  1028.     if not locate() then
  1029.         local xin, yin, zin, fin = nil, nil, nil, nil
  1030.         term.write("Enter x: ")
  1031.         xin = read()
  1032.         term.write("Enter y: ")
  1033.         yin = read()
  1034.         term.write("Enter z: ")
  1035.         zin = read()
  1036.         while fin == nil do
  1037.             term.write("Enter facing(n,s,e,w): ")
  1038.             fin = read()
  1039.             fin = string.sub(fin,1,1)
  1040.             if fin == 'n' then
  1041.                 dock.f = tFacing.north
  1042.             elseif fin == "s" then
  1043.                 dock.f = tFacing.south
  1044.             elseif fin == "e" then
  1045.                 dock.f = tFacing.east
  1046.             elseif fin == "w" then
  1047.                 dock.f = tFacing.west
  1048.             else
  1049.                 fin = nil
  1050.             end
  1051.         end
  1052.         dock.x = tonumber(xin)
  1053.         dock.y = tonumber(yin)
  1054.         dock.z = tonumber(zin)
  1055.         turtle.x = dock.x
  1056.         turtle.y = dock.y
  1057.         turtle.z = dock.z
  1058.         turtle.f = dock.f
  1059.     end
  1060.     term.write("Enter width: ")
  1061.     widthEntered = read()
  1062.     term.write("Enter Bottom Layer: ")
  1063.     depthEntered = read()
  1064.     term.write("Enter Top Layer: ")
  1065.     topLayerEntered = read()
  1066.     term.write("Is this a new mine? y/n:  ")
  1067.     newMineEntered = read()
  1068.     if (newMineEntered == "y") then newMine = true end
  1069.     savePosData()
  1070. end
  1071.  
  1072. function Main()
  1073.     local h = fs.open("foundlog", fs.exists("foundlog") and "a" or "w")
  1074.     h.close()
  1075.     if not getStatus() then
  1076.         status.mining = false
  1077.         status.surfacing = false
  1078.         status.returning = false
  1079.         status.paused = false
  1080.         status.pauseatsurface = false
  1081.         saveStatus()
  1082.     end
  1083.     if status.mining == false and status.pauseatsurface == false then
  1084.         os.sleep(5)
  1085.         enterStartData()
  1086.         startNewMine(widthEntered, depthEntered, topLayerEntered)
  1087.     else
  1088.         if debug then print("resume mining") end
  1089.         os.sleep(5)
  1090.         getPosData()
  1091.         getMineData()
  1092.         locate()
  1093.         mineLoop()
  1094.     end
  1095. end
  1096.  
  1097. function refuelAtStation()
  1098.     if debug then print("refuel at station") end
  1099.     local success = findInInventory("minecraft:bucket")
  1100.     if success then
  1101.         turnRight()
  1102.         while turtle.getFuelLevel() < turtle.getFuelLimit() * 0.8 do
  1103.             success, bucket = findInInventory("minecraft:bucket")
  1104.             if success then
  1105.             turtle.select(bucket)
  1106.             turtle.drop()
  1107.             local e = os.pullEvent("turtle_inventory")
  1108.             refuel()
  1109.             end
  1110.             os.sleep(1)
  1111.         end
  1112.         turnLeft()
  1113.     end
  1114. end
  1115.  
  1116. function locate()
  1117.     if debug then print("locate") end
  1118.     local success = false
  1119.     local found, modem = findInInventory("computercraft:advanced_modem")
  1120.     local equiped = peripheral.getType("left") == "modem"
  1121.     if found or equiped then
  1122.         if not equiped then
  1123.             turtle.select(modem)
  1124.             turtle.equipLeft()
  1125.         end
  1126.         os.sleep(1)
  1127.         local x1, y1, z1 = gps.locate(5)
  1128.         if x1 then
  1129.             turtle.f = turtle.f or tFacing.north
  1130.             turtle.x = turtle.x or 0
  1131.             turtle.y = turtle.y or 0
  1132.             turtle.z = turtle.z or 0
  1133.             turtle.equipLeft()
  1134.             turnLeft()
  1135.             turnLeft()
  1136.             forward()
  1137.             turtle.equipLeft()
  1138.             os.sleep(1)
  1139.             local x2, y2, z2 = gps.locate(5)
  1140.             turnLeft()
  1141.             turnLeft()
  1142.             forward()
  1143.             if z2 < z1 then
  1144.                 turtle.f = tFacing.south
  1145.             elseif z2 > z1 then
  1146.                 turtle.f = tFacing.north
  1147.             elseif x2 < x1 then
  1148.                 turtle.f = tFacing.east
  1149.             elseif x2 > x1 then
  1150.                 turtle.f = tFacing.west
  1151.             end
  1152.             turtle.x = x1
  1153.             turtle.y = y1
  1154.             turtle.z = z1
  1155.             success = true
  1156.         end
  1157.     end
  1158.     turtle.equipLeft()
  1159.     return success
  1160. end
  1161.  
  1162. debug = true
  1163. Main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top