Advertisement
IMarvinTPA

Minecraft Dig

Jan 11th, 2013
3,785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 32.01 KB | None | 0 0
  1. --pastebin get Bct0mdbv Dig
  2. --pastebin get SpEd82Jp StripLayer
  3. local tArgs = { ... }
  4. local filename = shell.getRunningProgram()
  5.  
  6. if #tArgs < 2 then
  7.     print( "Usage: " .. filename .. " <type> <length> <width> <height> <ender chest (0:1)>" )
  8.     print( "Type: Room, Quarry, Branch, or Tunnel" )
  9.     print( "Slot 1 should be a non-wood floor block" )
  10.     print( "Slot 2 should be a fuel" )
  11.     print( "Slot 3 should be a stack of chests" )
  12.     print( "Slot 4 should be a stack of torches" )
  13.     return
  14. end
  15.  
  16. -- Mine in a quarry pattern until we hit something we can't dig
  17. local digType = tArgs[1]
  18. local inLength = tonumber( tArgs[2] )
  19. local inWidth = tonumber( tArgs[3] )
  20. local tall = tonumber( tArgs[4] )
  21. local useEnderChest = tonumber( tArgs[5] )
  22. local relCurFacing = 0
  23. local movedCnt = 0
  24. local collectMode = 0
  25.  
  26. if tall == nil then
  27.     tall = 3
  28. end
  29.  
  30. if useEnderChest == nil then
  31.     useEnderChest = 0
  32. end
  33.  
  34. if inLength < 1 then
  35.     print( "Room length must be positive" )
  36.     return
  37. end
  38.  
  39. local length = inLength
  40.  
  41. if tall < 1 then
  42.     print( "Tunnel height must be positive" )
  43.     return
  44. end
  45.        
  46. local depth = 0
  47. local collected = 0
  48. local unloaded = 0
  49.  
  50. local faceForward = 0
  51. local faceBack = -2
  52. local faceLeft = -1
  53. local faceRight = 1
  54.  
  55. local relX, relY, relZ = 0, 0, 0
  56. local absX, absY, absZ = 0, 0, 0
  57.  
  58. local North = 2
  59. local South = 0
  60. local East = 3
  61. local West = 1
  62. local realFace = South
  63.  
  64. local activity = ""
  65.  
  66. local planX, planY, planZ = 0, 0, 0
  67.  
  68. local planFace = South
  69.  
  70. local route = {}
  71. local routeLen = 0
  72. local routePos = 1
  73.  
  74. local alreadyUnloading = false
  75. local haveRealGPS = false
  76. local dontSave = false
  77.  
  78. local sOpenedSide = nil
  79. local function openModem()
  80.     local bOpen, sFreeSide = false, nil
  81.     for n,sSide in pairs(rs.getSides()) do 
  82.         if peripheral.getType( sSide ) == "modem" then
  83.             sFreeSide = sSide
  84.             if rednet.isOpen( sSide ) then
  85.                 bOpen = true
  86.                 break
  87.             end
  88.         end
  89.     end
  90.    
  91.     if not bOpen then
  92.         if sFreeSide then
  93.             print( "No modem active. Opening "..sFreeSide.." modem" )
  94.             rednet.open( sFreeSide )
  95.             sOpenedSide = sFreeSide
  96.             return true
  97.         else
  98.             print( "No modem attached" )
  99.             return false
  100.         end
  101.     end
  102.     return true
  103. end
  104.  
  105. function closeModem()
  106.     if sOpenedSide then
  107.         rednet.close( sOpenedSide )
  108.     end
  109. end
  110.  
  111. local function saveState()
  112.     --print("Saving State")
  113.     if dontSave then
  114.         return true
  115.     end
  116.     local sf = fs.open(filename .. ".state", "w")
  117.     sf.writeLine('' .. digType)
  118.     sf.writeLine('' .. collected)
  119.     sf.writeLine('' .. unloaded)
  120.     sf.writeLine('' .. relX)
  121.     sf.writeLine('' .. relY)
  122.     sf.writeLine('' .. relZ)
  123.     sf.writeLine('' .. relCurFacing)
  124.     sf.writeLine('' .. absX)
  125.     sf.writeLine('' .. absY)
  126.     sf.writeLine('' .. absZ)
  127.     sf.writeLine('' .. realFace)
  128.     sf.writeLine('' .. routePos)
  129.     if alreadyUnloading then
  130.         sf.writeLine("unloading")
  131.     else
  132.         sf.writeLine("")
  133.     end
  134.     sf.writeLine('' .. movedCnt)
  135.  
  136.     print("Saving State Values: ")
  137.     print("rel: ", relX, ", ", relY, ", ", relZ)
  138.  
  139.    
  140.     sf.close()
  141.     sleep(0)
  142. end
  143.  
  144.  
  145. local function isFull()
  146.         local bFull = true
  147.         local nTotalItems = 0
  148.         for n=1,15 do
  149.             local nCount = turtle.getItemCount(n)
  150.             if nCount == 0 then
  151.                 bFull = false
  152.             end
  153.             nTotalItems = nTotalItems + nCount
  154.         end
  155.              
  156.         if bFull then
  157.             print( "No empty slots left." )
  158.             return true
  159.         end
  160.         return false
  161. end
  162.  
  163. --local function relTurnFacing(dir)
  164. --  print("used wrong relTurnFacing")
  165. --  return nil
  166. --end
  167.  
  168. local function relTurnLeft()
  169.     turtle.turnLeft()
  170.     relCurFacing = relCurFacing - 1
  171.     while relCurFacing <= -3 do
  172.         relCurFacing = relCurFacing + 4
  173.     end
  174.    
  175.     realFace = realFace - 1
  176.    
  177.     while realFace < South do
  178.         realFace = realFace + 4
  179.     end
  180.    
  181.     saveState()
  182.    
  183. end
  184.  
  185. local function relTurnRight()
  186.     turtle.turnRight()
  187.     relCurFacing = relCurFacing + 1
  188.     while relCurFacing >= 2 do
  189.         relCurFacing = relCurFacing - 4
  190.     end
  191.    
  192.     realFace = realFace + 1
  193.     while realFace > East do
  194.         realFace = realFace - 4
  195.     end
  196.    
  197.     saveState()
  198.    
  199. end
  200.  
  201. local function relTurnFacing(wantDir)
  202.     while wantDir <= -3 do
  203.         wantDir = wantDir + 4
  204.     end
  205.     while wantDir >= 2 do
  206.         wantDir = wantDir - 4
  207.     end
  208.    
  209.     --Turn right once more to face behind the general dirction of travel.
  210.     if relCurFacing == faceRight and wantDir == faceBack then
  211.         relTurnRight()
  212.     end
  213.  
  214.     if relCurFacing == faceBack and wantDir == faceRight then
  215.         relTurnLeft()
  216.     end
  217.    
  218.     --Turn left until we are facing our desired direction
  219.     while relCurFacing > wantDir do
  220.         --print("Left: " .. relCurFacing .. " Want: " .. wantDir)
  221.         relTurnLeft()      
  222.     end
  223.  
  224.     --Turn right until we are facing our desired direction
  225.     while relCurFacing < wantDir do
  226.         --print("Right: " .. relCurFacing .. " Want: " .. wantDir)
  227.         relTurnRight()
  228.     end
  229.    
  230. end
  231.  
  232. local function absTurnFacing(wantDir)
  233.     print("Absolute Turning: Want: ", wantDir, " At: ", realFace )
  234.     while wantDir < South do
  235.         wantDir = wantDir + 4
  236.     end
  237.     while wantDir > East do
  238.         wantDir = wantDir - 4
  239.     end
  240.    
  241.     --Turn right once more to face behind the general dirction of travel.
  242.     if realFace == East and wantDir == South then
  243.         relTurnRight()
  244.     end
  245.  
  246.     if realFace == South and wantDir == East then
  247.         relTurnLeft()
  248.     end
  249.    
  250.     --Turn left until we are facing our desired direction
  251.     while realFace > wantDir do
  252.         print("Left: " .. realFace .. " Want: " .. wantDir)
  253.         relTurnLeft()      
  254.     end
  255.  
  256.     --Turn right until we are facing our desired direction
  257.     while realFace < wantDir do
  258.         print("Right: " .. realFace .. " Want: " .. wantDir)
  259.         relTurnRight()
  260.     end
  261.    
  262. end
  263.  
  264.  
  265. local function simpleDig()
  266.     while turtle.detect() do
  267.         if turtle.dig() then
  268.             sleep(1)
  269.         else
  270.             return false
  271.         end
  272.     end
  273.     return true
  274. end
  275.  
  276. local function simpleDigUp()
  277.     while turtle.detectUp() do
  278.         if turtle.digUp() then
  279.             sleep(1)
  280.         else
  281.             return false
  282.         end
  283.     end
  284.     return true
  285. end
  286.  
  287. local function unload()
  288.     if alreadyUnloading then
  289.         return false
  290.     end
  291.     print( "Unloading items..." )
  292.     alreadyUnloading = true
  293.     local wasFacing = relCurFacing
  294.     relTurnFacing(faceBack)
  295.     turtle.select(3)
  296.     existingChest = false
  297.     if turtle.detect() then
  298.         --There is something in front of us.
  299.         if turtle.suck() then
  300.             --It is a chest!
  301.             existingChest = true
  302.         end
  303.         --Or it was an empty chest and we'll just swap it out with ours.
  304.     end
  305.  
  306.     if not existingChest then
  307.         while not turtle.place() do
  308.             if turtle.detect() then
  309.                 if not simpleDig() then
  310.                     return false
  311.                 end
  312.             elseif turtle.attack() then
  313.                 sleep( 0.5 )
  314.             else
  315.                 sleep( 0.5 )
  316.             end
  317.         end
  318.     end
  319.     --1: Flooring
  320.     --2: Fuel
  321.     --3: Chests
  322.     --4: Torches
  323.     for n=5,16 do
  324.         unloaded = unloaded + turtle.getItemCount(n)
  325.         turtle.select(n)
  326.         if turtle.getItemCount(n) > 0 then
  327.             while not turtle.drop() do
  328.                 print("The target chest is full")
  329.                 sleep(30)
  330.             end            
  331.         end
  332.     end
  333.     if useEnderChest ~= 0 then
  334.         turtle.select(3)   
  335.         turtle.dig()
  336.     end
  337.  
  338.     if turtle.getItemCount(3) == 0 then
  339.         print("Please load more chests")
  340.         while turtle.getItemCount(3) == 0 do
  341.             sleep(10)
  342.         end
  343.     end
  344.     --collected = 0
  345.     turtle.select(1)
  346.     relTurnFacing(wasFacing)
  347.     alreadyUnloading = false
  348. end
  349.  
  350. local function findCompress()
  351.     for n=1,16 do
  352.         --print("Slot " .. n .. " has " .. turtle.getItemCount(n))
  353.         if turtle.getItemCount(n) > 8 then
  354.             --print("Slot " .. n .. " will be compressed.")
  355.             --sleep(0.01)
  356.             return n
  357.         end
  358.     end
  359.     --print("No slot has enough to compress")
  360.     --sleep(0.01)
  361.     return 0
  362. end
  363.  
  364. local function tryCompress()
  365.     local cmpr
  366.    
  367.     print("Restacking")
  368.     turtle.select(1)
  369.     while turtle.suck() do
  370.         --sleep(0)
  371.         cmpr = 0
  372.     end
  373.        
  374.     cmpr = findCompress()
  375.    
  376.     while cmpr > 0 do
  377.         --get rid of what we don't need
  378.         for n=1,16 do
  379.             if n ~= cmpr and turtle.getItemCount(n) > 0 then
  380.                 turtle.select(n)
  381.                 while not turtle.drop() do
  382.                     print("The target chest is full")
  383.                     sleep(30)
  384.                 end            
  385.             end
  386.         end
  387.         print("Should have only the item to compress.")
  388.         turtle.select(cmpr)
  389.         turtle.transferTo(1)
  390.         print("Moved it to the top left")
  391.         while turtle.getItemCount(1) > 8 do
  392.             turtle.select(1)
  393.             turtle.transferTo(2, 1)
  394.             turtle.transferTo(3, 1)
  395.            
  396.             turtle.transferTo(5, 1)
  397.             turtle.transferTo(6, 1)
  398.             turtle.transferTo(7, 1)
  399.  
  400.             turtle.transferTo(9, 1)
  401.             turtle.transferTo(10, 1)
  402.             turtle.transferTo(11, 1)
  403.            
  404.             turtle.select(4)
  405.             if not turtle.craft(1) then
  406.                 print("Failed to craft.")
  407.                 --It does not compress, get it out of the system.
  408.                 turtle.select(1)
  409.                 turtle.dropUp()
  410.             else
  411.                 print("Made one!")
  412.                 --put the result into the chest
  413.                 turtle.drop()          
  414.             end
  415.         end
  416.         --put the leftovers into the chest  (dupe bug work-around, I hope)
  417.         turtle.select(1)
  418.         turtle.drop()          
  419.        
  420.         --Re stack everything to cascade the compression and start a new search.
  421.         print("Restacking")
  422.         turtle.select(1)
  423.         while turtle.suck() do
  424.             cmpr = findCompress()
  425.             if cmpr > 0 then
  426.                 break
  427.             end
  428.             sleep(0)
  429.         end    
  430.         cmpr = findCompress()
  431.     end
  432.  
  433.     print("Dumping.")
  434.     --Put everything into the chest
  435.     for n=1,16,1 do
  436.         if turtle.getItemCount(n) > 0 then
  437.             turtle.select(n)
  438.             while not turtle.drop() do
  439.                 print("The target chest is full")
  440.                 sleep(30)
  441.             end            
  442.         end
  443.     end
  444.     print("Done Compressing")
  445.     return true
  446. end
  447.  
  448. local function compress()
  449.     if useEnderChest ~= 0 then
  450.         return unload()
  451.     end
  452.    
  453.     print( "Compressing items..." )
  454.     local wasFacing = relCurFacing
  455.     relTurnFacing(faceBack)
  456.     turtle.select(3)
  457.     existingChest = false
  458.     if turtle.detect() then
  459.         --There is something in front of us.
  460.         if turtle.suck() then
  461.             --It is a chest!
  462.             existingChest = true
  463.         end
  464.         --Or it was an empty chest and we'll just swap it out with ours.
  465.     end
  466.  
  467.     if not existingChest then
  468.         while not turtle.place() do
  469.             if turtle.detect() then
  470.                 if not simpleDig() then
  471.                     return false
  472.                 end
  473.             elseif turtle.attack() then
  474.                 sleep( 0.5 )
  475.             else
  476.                 sleep( 0.5 )
  477.             end
  478.         end
  479.     end
  480.    
  481.     --again with a chest above.
  482.     existingChest = false
  483.     if turtle.detectUp() then
  484.         --There is something in front of us.
  485.         if turtle.suckUp() then
  486.             --It is a chest!
  487.             existingChest = true
  488.         end
  489.         --Or it was an empty chest and we'll just swap it out with ours.
  490.     end
  491.  
  492.     if not existingChest then
  493.         while not turtle.placeUp() do
  494.             if turtle.detectUp() then
  495.                 if not simpleDigUp() then
  496.                     return false
  497.                 end
  498.             elseif turtle.attackUp() then
  499.                 sleep( 0.5 )
  500.             else
  501.                 sleep( 0.5 )
  502.             end
  503.         end
  504.     end
  505.  
  506.    
  507.     --1: Flooring
  508.     --2: Fuel
  509.     --3: Chests
  510.     --4: Torches
  511.    
  512.     --Unload the stuff we need to keep in proper place.
  513.     for n=1,4 do
  514.         --unloaded = unloaded + turtle.getItemCount(n)
  515.         turtle.select(n)
  516.         if turtle.getItemCount(n) > 0 then
  517.             while not turtle.dropUp() do
  518.                 print("The target chest is full")
  519.                 sleep(30)
  520.             end            
  521.         end
  522.     end
  523.     --
  524.    
  525.     for n=5,16 do
  526.         unloaded = unloaded + turtle.getItemCount(n)
  527.         turtle.select(n)
  528.         if turtle.getItemCount(n) > 0 then
  529.             while not turtle.drop() do
  530.                 print("The target chest is full")
  531.                 sleep(30)
  532.             end            
  533.         end
  534.     end
  535.  
  536.     tryCompress()
  537.  
  538.     --Reload the stuff we need to keep in proper place.
  539.     for n=1,16 do
  540.         turtle.select(n)
  541.         turtle.suckUp()
  542.     end
  543.    
  544.     for n=16,5,-1 do
  545.         turtle.select(n)
  546.         turtle.suck()
  547.     end
  548.    
  549.     --collect the chests
  550.     turtle.select(3)
  551.     simpleDigUp()
  552.     simpleDig()
  553.    
  554.     if turtle.getItemCount(3) == 0 then
  555.         print("Please load more chests")
  556.         while turtle.getItemCount(3) == 0 do
  557.             sleep(10)
  558.         end
  559.     end
  560.     --collected = 0
  561.     turtle.select(1)
  562.     relTurnFacing(wasFacing)
  563. end
  564.  
  565.  
  566. local function collect()
  567.     collected = collected + 1
  568.     if math.fmod(collected, 25) == 0 then
  569.         print( "Mined "..collected.." items." )
  570.     end
  571.    
  572.     if isFull() then
  573.         if collectMode == 0 then
  574.             unload()
  575.         else
  576.             compress()
  577.         end
  578.     end
  579. end
  580.  
  581. local function tryDig()
  582.     --Collect stuff from chests.
  583.     local cnt = false
  584.     while turtle.suck() do
  585.         collect()
  586.     end
  587.    
  588.     while turtle.detect() do
  589.         if turtle.dig() then
  590.             collect()
  591.             cnt = true
  592.             --sleep(0.5)
  593.         else
  594.             return false, cnt
  595.         end
  596.     end
  597.  
  598.     return true, cnt
  599. end
  600.  
  601. local function tryDigUp()
  602.     while turtle.suckUp() do
  603.         collect()
  604.     end
  605.     while turtle.detectUp() do
  606.         if turtle.digUp() then
  607.             collect()
  608.             --sleep(0.5)
  609.         else
  610.                 return false
  611.         end
  612.     end
  613.     return true
  614. end
  615.  
  616. local function tryDigDown()
  617.     while turtle.suckDown() do
  618.         collect()
  619.     end
  620.     while turtle.detectDown() do
  621.         if turtle.digDown() then
  622.             collect()
  623.             --sleep(0.5)
  624.         else
  625.             return false
  626.         end
  627.     end
  628.     return true
  629. end
  630.  
  631.  
  632. local function refuel()
  633.     local fuelLevel = turtle.getFuelLevel()
  634.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  635.         return
  636.     end
  637.    
  638.     local function tryRefuel()
  639.         for n=2,16 do
  640.             if n ~= 3 and n ~= 4 then
  641.                 if turtle.getItemCount(n) > 0 then
  642.                     turtle.select(n)
  643.                     if turtle.refuel(1) then
  644.                         turtle.select(1)
  645.                         return true
  646.                     end
  647.                 end
  648.             end
  649.         end
  650.         turtle.select(1)
  651.         return false
  652.     end
  653.    
  654.     if not tryRefuel() then
  655.         print( "Add more fuel to continue." )
  656.         while not tryRefuel() do
  657.             sleep(1)
  658.         end
  659.         print( "Resuming Tunnel." )
  660.     end
  661. end
  662.  
  663. local function tryUp()
  664.     refuel()
  665.     while not turtle.up() do
  666.         if turtle.detectUp() then
  667.             if not tryDigUp() then
  668.                 return false
  669.             end
  670.         elseif turtle.attackUp() then
  671.             collect()
  672.         else
  673.             sleep( 0.5 )
  674.         end
  675.     end
  676.     relY = relY + 1
  677.     absY = absY + 1
  678.     saveState()
  679.     print("rel: ", relX, ", ", relY, ", ", relZ)
  680.     print("abs: ", absX, ", ", absY, ", ", absZ)
  681.     print("route pos: ", routePos)
  682.     if routePos ~= nil and route[routePos] ~= nil then
  683.         print("route: ", route[routePos].x, ", ", route[routePos].y, ", ", route[routePos].z)
  684.     end
  685.     return true
  686. end
  687.  
  688. local function tryDown()
  689.     refuel()
  690.     while not turtle.down() do
  691.         if turtle.detectDown() then
  692.             if not tryDigDown() then
  693.                 return false
  694.             end
  695.         elseif turtle.attackDown() then
  696.             collect()
  697.         else
  698.             sleep( 0.5 )
  699.         end
  700.     end
  701.     relY = relY - 1
  702.     absY = absY - 1
  703.     saveState()
  704.     print("rel: ", relX, ", ", relY, ", ", relZ)
  705.     print("abs: ", absX, ", ", absY, ", ", absZ)
  706.     print("route pos: ", routePos)
  707.     if routePos ~= nil and route[routePos] ~= nil then
  708.         print("route: ", route[routePos].x, ", ", route[routePos].y, ", ", route[routePos].z)
  709.     end
  710.     return true
  711. end
  712.  
  713. local function absVector(inDir)
  714.  
  715.     if inDir == North then
  716.         return 0, -1
  717.     elseif inDir == South then
  718.         return 0, 1
  719.     elseif inDir == East then
  720.         return 1, 0
  721.     elseif inDir == West then
  722.         return -1, 0
  723.     end
  724.    
  725.     return nil, nil
  726.  
  727. end
  728.  
  729. local function trySimpleForwardNoTorch()
  730.     refuel()
  731.     while not turtle.forward() do
  732.         if turtle.detect() then
  733.             return false
  734.         elseif turtle.attack() then
  735.             collect()
  736.         else
  737.             sleep( 0.5 )
  738.         end
  739.     end
  740.    
  741.     movedCnt = movedCnt + 1
  742.     local dx, dz = absVector(realFace)
  743.     relX = relX + dx
  744.     absX = absX + dx
  745.     relZ = relZ + dz
  746.     absZ = absZ + dz
  747.     saveState()
  748.    
  749.     print("rel: ", relX, ", ", relY, ", ", relZ)
  750.     print("abs: ", absX, ", ", absY, ", ", absZ)
  751.     print("route pos: ", routePos)
  752.     if routePos ~= nil and route[routePos] ~= nil then
  753.         print("route: ", route[routePos].x, ", ", route[routePos].y, ", ", route[routePos].z)
  754.     end
  755.    
  756.     return true
  757.    
  758. end
  759.  
  760. local function tryForwardNoTorch()
  761.     refuel()
  762.     while not turtle.forward() do
  763.         if turtle.detect() then
  764.             if not tryDig() then
  765.                 return false
  766.             end
  767.         elseif turtle.attack() then
  768.             collect()
  769.         else
  770.             sleep( 0.5 )
  771.         end
  772.     end
  773.    
  774.     movedCnt = movedCnt + 1
  775.     local dx, dz = absVector(realFace)
  776.     relX = relX + dx
  777.     absX = absX + dx
  778.     relZ = relZ + dz
  779.     absZ = absZ + dz
  780.     saveState()
  781.    
  782.     print("rel: ", relX, ", ", relY, ", ", relZ)
  783.     print("abs: ", absX, ", ", absY, ", ", absZ)
  784.     print("route pos: ", routePos)
  785.     if routePos ~= nil and route[routePos] ~= nil then
  786.         print("route: ", route[routePos].x, ", ", route[routePos].y, ", ", route[routePos].z)
  787.     end
  788.    
  789.     return true
  790.    
  791. end
  792.  
  793. local function tryForward()
  794.     local result
  795.     result = tryForwardNoTorch()
  796.    
  797.     if result == false then
  798.         return false
  799.     end
  800.    
  801.     if math.fmod(movedCnt, 6) == 0 and turtle.getItemCount(4) then
  802.    
  803.         if turtle.getItemCount(4) == 1 then
  804.             print("Please load more torches")
  805.             while turtle.getItemCount(4) <= 1 do
  806.                 sleep(10)
  807.             end
  808.         end
  809.         relTurnLeft()
  810.         relTurnLeft()
  811.         turtle.select(4)
  812.         turtle.place()
  813.         turtle.select(1)
  814.         relTurnLeft()
  815.         relTurnLeft()
  816.     end
  817.     return true
  818. end
  819.  
  820. local function planAdd(a, px, py, pz)
  821.     routeLen = routeLen + 1
  822.     point = {}
  823.     point.action = a
  824.     point.x = px
  825.     point.y = py
  826.     point.z = pz
  827.     route[routeLen] = point
  828. end
  829.  
  830. local function planForward(planDist)
  831.     if planDist == nil then
  832.         planDist = 1
  833.     end
  834.     if planFace == North then
  835.         planZ = planZ - planDist
  836.     elseif planFace == South then
  837.         planZ = planZ + planDist
  838.     elseif planFace == West then
  839.         planX = planX - planDist
  840.     elseif planFace == East then
  841.         planX = planX + planDist
  842.     end
  843.  
  844.     --planAdd(planX, planY, planZ)
  845. end
  846.  
  847. local function planBack(planDist)
  848.     if planDist == nil then
  849.         planDist = 1
  850.     end
  851.     planForward(-planDist)
  852. end
  853.  
  854. local function planUp(planDist)
  855.     if planDist == nil then
  856.         planDist = 1
  857.     end
  858.     planY = planY + planDist
  859.     --planAdd(planX, planY, planZ)
  860. end
  861.  
  862. local function planDown(planDist)
  863.     if planDist == nil then
  864.         planDist = 1
  865.     end
  866.     planUp(-planDist)
  867. end
  868.  
  869. local function planTurnLeft()
  870.     planFace = planFace - 1
  871.     while planFace < South do
  872.         planFace = planFace + 4
  873.     end
  874. end
  875.  
  876. local function planTurnRight()
  877.     planFace = planFace + 1
  878.     while planFace > East do
  879.         planFace = planFace - 4
  880.     end
  881. end
  882.  
  883.  
  884. local function tryBack()
  885.     refuel()
  886.     while not turtle.back() do
  887.         relTurnRight()
  888.         relTurnRight()
  889.         if turtle.detect() then
  890.             if not tryDig() then
  891.                 return false
  892.             end
  893.         elseif turtle.attack() then
  894.             collect()
  895.         else
  896.             sleep( 0.5 )
  897.         end
  898.         relTurnRight()
  899.         relTurnRight()
  900.     end
  901.    
  902.     movedCnt = movedCnt - 1
  903.     local dx, dz = absVector(realFace)
  904.     relX = relX - dx
  905.     absX = absX - dx
  906.     relZ = relZ - dz
  907.     absZ = absZ - dz
  908.     saveState()
  909.  
  910.     return true
  911. end
  912.  
  913. local function getRealFacing()
  914.     print("Real Facing.")
  915.     openModem()
  916.     dontSave = true
  917.     local startX, startY, startZ = gps.locate(5, true)
  918.     print(startX)
  919.     if startX == nil then
  920.         dontSave = false
  921.         return false
  922.     end
  923.    
  924.     if not tryForward() then
  925.         print("could not go forward during facing test")
  926.         dontSave = false
  927.         return false
  928.     end
  929.    
  930.     absX, absY, absZ = gps.locate(5)
  931.     if absX == nil then
  932.         absX, absY, absZ = 0, 0, 0
  933.         dontSave = false
  934.         return false
  935.     end
  936.     dx = startX - absX
  937.     dz = startZ - absZ
  938.     if dx == -1 then
  939.         realFace = East
  940.     elseif dx == 1 then
  941.         realFace = West
  942.     elseif dz == -1 then
  943.         realFace = South
  944.     elseif dz == 1 then
  945.         realFace = North
  946.     end
  947.    
  948.     print("Real facing: ", realFace)
  949.    
  950.     tryBack()
  951.     relX, relY, relZ = 0, 0, 0
  952.     absX, absY, absZ = startX, startY, startZ
  953.     --sleep(10)
  954.     dontSave = false
  955.     return true
  956. end
  957.  
  958. local function planHallway(hallLength, ltall)
  959.     planForward(hallLength - 1)
  960.     planAdd("t", planX, planY, planZ)
  961. end
  962.  
  963. local function planTube(hallLength, ltall)
  964.     planForward(hallLength - 1)
  965.     planAdd("t1", planX, planY, planZ)
  966. end
  967.  
  968.  
  969. local function hallway(hallLength, ltall)
  970.     relCurFacing = 0
  971.     local leftDig, rightDig, resCollect, resDig
  972.     for n=1,hallLength do
  973.         leftDig, rightDig, preCol = 0, 0, collected
  974.         turtle.placeDown()
  975.         relTurnFacing(faceLeft)
  976.         ResDig, resCollect = tryDig()
  977.         if resCollect then
  978.             leftDig = leftDig + 1
  979.         end
  980.         for m=2, ltall do
  981.             tryDigUp()
  982.             tryUp()
  983.             ResDig, resCollect = tryDig()
  984.             if resCollect then
  985.                 leftDig = leftDig + 1
  986.             end
  987.         end
  988.         relTurnFacing(faceRight)
  989.         ResDig, resCollect = tryDig()
  990.         if resCollect then
  991.             rightDig = rightDig + 1
  992.         end
  993.         for m=2, ltall do
  994.             tryDown()
  995.             ResDig, resCollect = tryDig()
  996.             if resCollect then
  997.                 rightDig = rightDig + 1
  998.             end
  999.         end
  1000.        
  1001.         --Clean up the damn gravel/sand
  1002.         if rightDig > 0 then
  1003.             sleep(0.5)
  1004.             while turtle.detect() do
  1005.                 tryDig()
  1006.                 sleep(2)
  1007.             end
  1008.         end
  1009.        
  1010.         if leftDig > 0 then
  1011.             relTurnFacing(faceLeft)
  1012.             while turtle.detect() do
  1013.                 tryDig()
  1014.                 sleep(2)
  1015.             end
  1016.         end
  1017.        
  1018.         relTurnFacing(faceForward)
  1019.         while ltall > 1 and turtle.detectUp() do
  1020.             tryDigUp()
  1021.             sleep(2)
  1022.         end
  1023.        
  1024.         if n<hallLength then
  1025.                 tryDig()
  1026.                 if not tryForward() then
  1027.                         return false
  1028.                 end
  1029.         else
  1030.                 return true
  1031.         end
  1032.     end
  1033. end
  1034.  
  1035.  
  1036. local function tube(hallLength, ltall)
  1037.     relCurFacing = 0
  1038.     local leftDig, rightDig, resCollect, resDig
  1039.     for n=1,hallLength do
  1040.         leftDig, rightDig, preCol = 0, 0, collected
  1041.         turtle.placeDown()
  1042.         if ltall == 2 then
  1043.             tryDigUp()
  1044.         elseif ltall > 2 then
  1045.             for m=2, ltall do
  1046.                 tryDigUp()
  1047.                 tryUp()
  1048.                 ResDig, resCollect = tryDig()
  1049.                 if resCollect then
  1050.                     leftDig = leftDig + 1
  1051.                 end
  1052.             end
  1053.         end
  1054.         if ltall > 2 then
  1055.             for m=2, ltall do
  1056.                 tryDown()
  1057.                 ResDig, resCollect = tryDig()
  1058.                 if resCollect then
  1059.                     rightDig = rightDig + 1
  1060.                 end
  1061.             end
  1062.         end
  1063.        
  1064.         --Clean up the damn gravel/sand
  1065.        
  1066.         relTurnFacing(faceForward)
  1067.         while ltall > 1 and turtle.detectUp() do
  1068.             tryDigUp()
  1069.             sleep(2)
  1070.         end
  1071.        
  1072.         if n<hallLength then
  1073.                 tryDig()
  1074.                 if not tryForward() then
  1075.                         return false
  1076.                 end
  1077.         else
  1078.                 return true
  1079.         end
  1080.     end
  1081. end
  1082.  
  1083.  
  1084. local function PlanRoom()
  1085.     print( "Planning Room..." )
  1086.  
  1087.     planTurnRight()
  1088.     planTurnRight()
  1089.     planForward(1)
  1090.     planAdd("d1", planX, planY, planZ)
  1091.     planHallway(1, tall)
  1092.     planTurnRight()
  1093.     planTurnRight()
  1094.     planForward(1)
  1095.     planAdd("d1", planX, planY, planZ)
  1096.  
  1097.     local lmod = 0
  1098.     for s=1,4 do
  1099.         print( "Side: "..s )
  1100.         planHallway(length - lmod, tall)
  1101.         planBack()
  1102.         if s == 4 then
  1103.             planBack()
  1104.         end
  1105.         planAdd("d1", planX, planY, planZ)
  1106.         planTurnLeft()
  1107.         --if s < 4 then
  1108.         planForward(2)
  1109.         planAdd("d1", planX, planY, planZ)
  1110.         lmod = 2
  1111.         --end
  1112.         if s == 3 then
  1113.             lmod = 4
  1114.         end
  1115.     end
  1116.  
  1117.     --planTurnRight()
  1118.     --planBack(1)
  1119.     --planAdd("d1", planX, planY, planZ)
  1120.     --planTurnLeft()
  1121.     --planForward()
  1122.     --planAdd("d1", planX, planY, planZ)
  1123.     --                cut + hall error + length mod.
  1124.     length = length - 6 + 1 + 1
  1125.     reduceRun = true
  1126.     while length > 1 do
  1127.         print( "Length: "..length )
  1128.         planHallway(length - 1, tall)
  1129.         if reduceRun then
  1130.             length = length - 3
  1131.             reduceRun = false
  1132.         else
  1133.             length = length - 0
  1134.             reduceRun = true
  1135.         end
  1136.         planBack()
  1137.         planAdd("d1", planX, planY, planZ)
  1138.         planTurnLeft()
  1139.         planForward(2)
  1140.         planAdd("d1", planX, planY, planZ)
  1141.     end
  1142.     print( "Room planning complete." )
  1143. end
  1144.  
  1145. local function planBranchMining()
  1146.     print( "Planning Mine..." )
  1147.     planTube(inLength, 2)
  1148.     planTurnLeft()
  1149.     local turnLeft = true
  1150.    
  1151.     local cnt = inLength / 3
  1152.    
  1153.     for tn = 1, cnt do
  1154.         planTube(inWidth, 2)
  1155.         if turnLeft then
  1156.             planTurnLeft()
  1157.         else
  1158.             planTurnRight()
  1159.         end
  1160.         planTube(4, 2)
  1161.         if turnLeft then
  1162.             planTurnLeft()
  1163.             turnLeft = false
  1164.         else
  1165.             planTurnRight()
  1166.             turnLeft = true
  1167.         end
  1168.     end
  1169.     print( "Mine planning complete." )
  1170.    
  1171. end
  1172.  
  1173. local function savePlan()
  1174.     print("Save Plan")
  1175.     local sf = fs.open(filename .. ".plan", "w")
  1176.     sf.writeLine(routeLen)
  1177.     for rp = 1, routeLen do
  1178.         sf.writeLine(route[rp].action)
  1179.         sf.writeLine(route[rp].x)
  1180.         sf.writeLine(route[rp].y)
  1181.         sf.writeLine(route[rp].z)
  1182.     end
  1183.     sf.close()
  1184. end
  1185.  
  1186. local function readNumber(sf)
  1187.     print("Read Number")
  1188.     local x = tonumber(sf.readLine()) + 0
  1189.     print("Read ", x)
  1190.     return x
  1191. end
  1192.  
  1193. local function loadState()
  1194.     print("Load State")
  1195.     local sf = fs.open(filename .. ".state", "r")
  1196.     digType = sf.readLine()
  1197.     collected = readNumber(sf)
  1198.     unloaded = readNumber(sf)
  1199.     relX = readNumber(sf)
  1200.     relY = readNumber(sf)
  1201.     relZ = readNumber(sf)
  1202.     relCurFacing = readNumber(sf)
  1203.     absX = readNumber(sf)
  1204.     absY = readNumber(sf)
  1205.     absZ = readNumber(sf)
  1206.     realFace = readNumber(sf)
  1207.     routePos = readNumber(sf)
  1208.     alreadyUnloading = (sf.readLine() == "unloading")
  1209.     movedCnt = readNumber(sf)
  1210.    
  1211.     sf.close()
  1212.  
  1213.     --sleep(5)
  1214.    
  1215.     print("State Values: ")
  1216.     print("digType: ", digType)
  1217.     --print("collected: ", collected)
  1218.     --print("unloaded: ", unloaded)
  1219.     --print("absX: ", absX)
  1220.     --print("absY: ", absY)
  1221.     --print("absZ: ", absZ)
  1222.     print("routePos: ", routePos)
  1223.     --print("alreadyUnloading: ", alreadyUnloading)
  1224.     print("movedCnt: ", movedCnt)
  1225.     print("rel: ", relX, ", ", relY, ", ", relZ)
  1226.     --sleep(12)
  1227.  
  1228. end
  1229.  
  1230. local function loadPlan()
  1231.     print("Load Plan")
  1232.     local sf = fs.open(filename .. ".plan", "r")
  1233.     local rl = readNumber(sf)
  1234.     local rp, x, y, z
  1235.     for rp = 1, rl do
  1236.         action = sf.readLine()
  1237.         x = readNumber(sf)
  1238.         y = readNumber(sf)
  1239.         z = readNumber(sf)
  1240.         planAdd(action, x, y, z)
  1241.     end
  1242.     sf.close()
  1243. end
  1244.  
  1245. local function tryFace(targX, targZ)
  1246.     print("Try Face")
  1247.     local dx, dz = targX - relX, targZ - relZ
  1248.     print("X: ", relX, " to ", targX, "  Z: ", relZ, " to ", targZ)
  1249.  
  1250.     if math.abs(dx) > math.abs(dz) then
  1251.         if dx > 0 then
  1252.             absTurnFacing(East)
  1253.             print("Turnig to the East")
  1254.         elseif dx < 0 then
  1255.             absTurnFacing(West)
  1256.             print("Turnig to the West")
  1257.         end
  1258.     else
  1259.         if dz > 0 then
  1260.             absTurnFacing(South)
  1261.             print("Turnig to the South")
  1262.         elseif dz < 0 then
  1263.             absTurnFacing(North)
  1264.             print("Turnig to the North")
  1265.         end
  1266.     end
  1267.     --sleep(5)
  1268.     return true
  1269. end
  1270.  
  1271. local function DoGo(x, y, z)
  1272.     print("Do Go")
  1273.     local dx, dy, dz = x - relX, y - relY, z - relZ
  1274.     local adx, ady, adz, tryRes
  1275.     while dx ~= 0 or dy ~= 0 or dz ~= 0 do
  1276.         print(dx, dy, dz)
  1277.         adx = math.abs(dx)
  1278.         ady = math.abs(dy)
  1279.         adz = math.abs(dz)
  1280.         tryRes = true
  1281.         if dy ~= 0 and (ady <= adx or dx == 0) and (ady <= adz or dz ==0) then
  1282.             if dy > 0 then
  1283.                 tryRes = tryUp()
  1284.             else
  1285.                 tryRes = tryDown()
  1286.             end
  1287.         elseif dx ~= 0 and (adx <= ady or dy == 0) and (adx <= adz or dz ==0) then
  1288.             if dx > 0 then
  1289.                 absTurnFacing(East)
  1290.             else
  1291.                 absTurnFacing(West)
  1292.             end
  1293.             relCurFacing = faceForward
  1294.             tryRes = tryForward()
  1295.         elseif dz ~= 0 and (adz <= ady or dy == 0) and (adz <= adx or dx ==0) then
  1296.             if dz > 0 then
  1297.                 absTurnFacing(South)
  1298.             else
  1299.                 absTurnFacing(North)
  1300.             end
  1301.             relCurFacing = faceForward
  1302.             tryRes = tryForward()
  1303.         end
  1304.         if tryRes == false then
  1305.             print("Unable to go to desired location!")
  1306.             return false
  1307.         end
  1308.         dx, dy, dz = x - relX, y - relY, z - relZ
  1309.     end
  1310.     return true
  1311. end
  1312.  
  1313. local function DoGoAbs(x, y, z)
  1314.     print("Do Go")
  1315.     local dx, dy, dz = x - absX, y - absY, z - absZ
  1316.     local adx, ady, adz, tryRes
  1317.     while dx ~= 0 or dy ~= 0 or dz ~= 0 do
  1318.         print(dx, dy, dz)
  1319.         adx = math.abs(dx)
  1320.         ady = math.abs(dy)
  1321.         adz = math.abs(dz)
  1322.         tryRes = true
  1323.         if dy ~= 0 and ady < adx and ady < adz then
  1324.             if dy > 0 then
  1325.                 tryRes = tryUp()
  1326.             else
  1327.                 tryRes = tryDown()
  1328.             end
  1329.         elseif dx ~= 0 and adx < ady and adx < adz then
  1330.             if dx > 0 then
  1331.                 absTurnFacing(East)
  1332.             else
  1333.                 absTurnFacing(West)
  1334.             end
  1335.             tryRes = tryForward()
  1336.         elseif dz ~= 0 and adz < ady and adz < adx then
  1337.             if dz > 0 then
  1338.                 absTurnFacing(South)
  1339.             else
  1340.                 absTurnFacing(North)
  1341.             end
  1342.             tryRes = tryForward()
  1343.         end
  1344.         if tryRes == false then
  1345.             print("Unable to go to desired location!")
  1346.             return false
  1347.         end
  1348.         dx, dy, dz = x - absX, y - absY, z - absZ
  1349.     end
  1350.     return true
  1351. end
  1352.  
  1353.  
  1354. local function DoPlan(startPos)
  1355.     print("Do Plan")
  1356.     local p
  1357.     for p=startPos,routeLen do
  1358.         routePos = p
  1359.         if route[p].action == "t" then
  1360.             tryFace(route[p].x, route[p].z)
  1361.             local dist = math.abs(route[p].x - relX) + math.abs(route[p].z - relZ)
  1362.             hallway(dist + 1, tall)
  1363.         elseif route[p].action == "t1" then
  1364.             tryFace(route[p].x, route[p].z)
  1365.             local dist = math.abs(route[p].x - relX) + math.abs(route[p].z - relZ)
  1366.             tube(dist + 1, tall)
  1367.         elseif route[p].action == "d1" then
  1368.             DoGo(route[p].x, route[p].y, route[p].z)
  1369.         end
  1370.     end
  1371. end
  1372.  
  1373. local function resume()
  1374.     print("Resume")
  1375.     local curX, curY, curZ, curFace = absX, absY, absZ, realFace
  1376.  
  1377.     loadState()
  1378.    
  1379.     if haveRealGPS then
  1380.         print("Real GPS said ", curX, ", ", curY, ", ", curZ, ", ", curFace)
  1381.         print("State GPS said ", absX, ", ", absY, " , ", absZ, ", ", realFace)
  1382.         print("State Rel said ", relX, ", ", relY, " , ", relZ, ", ", realFace)
  1383.         realFace = curFace
  1384.         --sleep(5)
  1385.     end
  1386.    
  1387.    
  1388.     loadPlan()
  1389.    
  1390.     if alreadyUnloading then
  1391.         alreadyUnloading = false
  1392.         if useEnderChest ~= 0 then
  1393.             turtle.select(3)   
  1394.             turtle.dig()
  1395.             unload()
  1396.         elseif turtle.getItemCount(3) == 0 then
  1397.             print("Please load more chests")
  1398.             while turtle.getItemCount(3) == 0 do
  1399.                 sleep(10)
  1400.             end
  1401.         else
  1402.             unload()
  1403.         end
  1404.     end
  1405.    
  1406.     if routePos > routeLen then
  1407.         return false
  1408.     end
  1409.    
  1410.     if route[routePos].action == "t" and route[routePos].y < relY then
  1411.         while route[routePos].y < relY do
  1412.             if tryDown() == false then
  1413.                 print("could not get back into position")
  1414.                 return false
  1415.             end
  1416.         end
  1417.     end
  1418.    
  1419.     DoPlan(routePos)
  1420.     return true
  1421. end
  1422.  
  1423. local function createStartup()
  1424.     print("Create Startup")
  1425.     if fs.exists("startup") then
  1426.         if fs.exists("startup.original") then
  1427.             fs.delete("startup.original")
  1428.         end
  1429.         fs.copy("startup", "startup.original")
  1430.     end
  1431.    
  1432.     local sf = fs.open("startup", "w")
  1433.     sf.writeLine("shell.run('" .. filename .. "',  'resume', " .. inLength ..  ", " .. inWidth ..  ", " .. tall .. ", " .. useEnderChest .. ")")
  1434.     sf.close()
  1435.    
  1436. end
  1437.  
  1438. local function revertStartup()
  1439.     print("Revert Startup")
  1440.     fs.delete("startup")
  1441.     if fs.exists("startup.original") then      
  1442.         fs.move("startup.original", "startup")
  1443.     end
  1444. end
  1445.  
  1446.  
  1447.  
  1448. local function  startPicky()
  1449.  
  1450.     local d, compressTime
  1451.    
  1452.     compressTime = 0
  1453.     collectMode = 1
  1454.    
  1455.     print("Planed Distance: " .. inLength)
  1456.     for d = 1,inLength do
  1457.         print("Distance: " .. d .. " of " .. inLength)
  1458.         turtle.select(1)
  1459.         if turtle.detect() then
  1460.             --Make sure we have a block to deal with.
  1461.             if turtle.compare() then
  1462.                 turtle.dig()
  1463.                 trySimpleForwardNoTorch()
  1464.             else
  1465.                 relTurnRight()
  1466.                 if turtle.detect() then
  1467.                     if turtle.compare() then
  1468.                         turtle.dig()
  1469.                         trySimpleForwardNoTorch()
  1470.                     else
  1471.                         relTurnLeft()
  1472.                         relTurnLeft()
  1473.                         if turtle.detect() then
  1474.                             if turtle.compare() then
  1475.                                 turtle.dig()
  1476.                                 trySimpleForwardNoTorch()
  1477.                             else
  1478.                                 relTurnLeft()
  1479.                                 tryForwardNoTorch()
  1480.                                 relTurnLeft()
  1481.                             end
  1482.                         end
  1483.                     end
  1484.                 end
  1485.             end
  1486.         else
  1487.             --Nothing in front of us, just go!
  1488.             trySimpleForwardNoTorch()
  1489.         end
  1490.        
  1491.         turtle.select(1)
  1492.         if turtle.compareUp() then
  1493.             turtle.digUp()
  1494.         end
  1495.        
  1496.         compressTime = compressTime + 1
  1497.        
  1498.         if isFull() and compressTime > 8 then
  1499.             compressTime = 0
  1500.             compress()
  1501.         end    
  1502.     end
  1503.    
  1504. end
  1505.  
  1506.  
  1507. local function startPlan()
  1508.     print("Start Plan")
  1509.     saveState()
  1510.     savePlan()
  1511.     createStartup()
  1512.     return DoPlan(1)
  1513. end
  1514.  
  1515. function main()
  1516.     digType = string.lower(digType)
  1517.    
  1518.     haveRealGPS = false --getRealFacing()
  1519.    
  1520.     --if haveRealGPS then
  1521.         planFace = realFace
  1522.     --end
  1523.    
  1524.     if digType == "resume" then
  1525.         resume()
  1526.     elseif digType == "room" or digType == "r" then
  1527.         PlanRoom()
  1528.         startPlan()
  1529.     elseif digType =="quarry" or digType == "q" then
  1530.         planDown(tall)
  1531.         planAdd("d1", planX, planY, planZ)
  1532.         PlanRoom()
  1533.         startPlan()
  1534.     elseif digType =="branch" or digType == "bm" then
  1535.         planBranchMining()
  1536.         startPlan()
  1537.     elseif digType == "tunnel" or digType == "t" then
  1538.         planHallway(length, tall)
  1539.         startPlan()
  1540.     elseif digType == "down" or digType == "d" then
  1541.         planDown(length)
  1542.         planAdd("d1", planX, planY, planZ)
  1543.         startPlan()
  1544.     elseif digType == "up" or digType == "u" then
  1545.         planUp(length)
  1546.         planAdd("d1", planX, planY, planZ)
  1547.         startPlan()
  1548.     elseif digType == "forward" or digType == "f" then
  1549.         planForward(length)
  1550.         planAdd("d1", planX, planY, planZ)
  1551.         startPlan()
  1552.     elseif digType == "back" or digType == "b" then
  1553.         planBack(length)
  1554.         planAdd("d1", planX, planY, planZ)
  1555.         startPlan()
  1556.     elseif digType == "south" then
  1557.         absTurnFacing(South)
  1558.     elseif digType == "north" then
  1559.         absTurnFacing(North)
  1560.     elseif digType == "east" then
  1561.         absTurnFacing(East)
  1562.     elseif digType == "west" then
  1563.         absTurnFacing(West)
  1564.     elseif digType == "picky" or digType == "p" then
  1565.         startPicky()
  1566.     elseif digType == "crafttest" then
  1567.         compress()     
  1568.     end
  1569.    
  1570.     if digType == "back" or digType == "b" then
  1571.         turtle.turnRight()
  1572.         turtle.turnRight()
  1573.     end
  1574.  
  1575.     if useEnderChest ~= 0 then
  1576.         unload()
  1577.     end
  1578.      
  1579.     print( "Digging complete." )
  1580.     print( "Mined "..collected.." items total." )
  1581.    
  1582.     revertStartup()
  1583.    
  1584.     closeModem()
  1585.    
  1586. end
  1587.  
  1588. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement