Thunder7102

SuperMiner

May 25th, 2014
717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.47 KB | None | 0 0
  1. --This program will allow the turtle to automine,
  2. --chunkload himself, put all materials in an ender-chest when
  3. --he's done, and return back to the destination.
  4. --A return-to-surface autostart failsafe may be added
  5. --later.
  6.  
  7. --Should I display the debug renderer?
  8. local useDebugRenderer = true
  9.  
  10. --Flags
  11. isEnderChest = false
  12. isLoaded = false
  13. spareCobble = false
  14.  
  15. --My own custom method of saving time
  16. os.loadAPI("move")
  17. local args = {...}
  18.  
  19. --GLOBAL VARIABLES
  20. local version = "0.0.1"
  21. local status = "Starting up..."
  22.  
  23. --Current Position
  24. local homeX = 0
  25. local homeY = 0
  26. local homeZ = 0
  27. local homeDir = "north"
  28.  
  29. --Loop Resume Position
  30. local loopX = 0
  31. local loopY = 0
  32. local loopZ = 0
  33. local loopDir = 0
  34.  
  35. --ChunkLoaded boudaries
  36. local ChunkStartX = homeX-16
  37. local ChunkStartZ = homeZ-16
  38. local ChunkEndX = homeX+16
  39. local ChunkEndZ = homeZ+16
  40. local ceilingY = 0
  41.  
  42. --FUNCTIONS
  43.  
  44. --MAIN FUNCTIONS
  45.  
  46. function init(...)
  47.     --Starts up and sets up the turtle and puts him in position to begin minePattern
  48.     print("This program assumes an enderchest is in Slot 1 and a chunkloader is in slot 2.")
  49.     print("Press any key to continue...")
  50.     read()
  51.     turtle.select(2)
  52.     if turtle.detectUp() then
  53.         move.digUp()
  54.     end
  55.     turtle.placeUp()
  56.     --Establishing where Mr. Turtle needs to return to when done
  57.     homeX, homeY, homeZ, homeDir = move.getCoords()
  58.     ChunkStartX = homeX-16
  59.     ChunkStartZ = homeZ-16
  60.     ChunkEndX = homeX+16
  61.     ChunkEndZ = homeZ+16
  62.     move.fDown(5)
  63.     ceiling = getPos("y")
  64.  
  65.     --Variables finished, moving into position
  66.     print("Moving into position to begin mining operation...")
  67.     move.goto(ChunkStartX, ceiling-1, ChunkEndZ, "north")
  68.     minePattern()
  69.     move.goto(homeX, homeY, homeZ, homeDir)
  70.     turtle.select(3)
  71.     turtle.placeDown()
  72.     turtle.select(1)
  73.    
  74.     --Chunkloader above, can't clear inv here, gotta move.
  75.     move.fForward()
  76.     clearInv("all")
  77.     move.turnLeft()
  78.     move.turnLeft()
  79.     move.fForward()
  80.     move.turnLeft()
  81.     move.turnLeft()
  82.  
  83.     turtle.select(2)
  84.     turtle.digUp()
  85.     turtle.select(1)
  86. end
  87.  
  88. function minePattern()
  89.     --Here is the loop which makes the turtle eat the entire 32x32 grid of the area.
  90.     for i=1,16 do
  91.         print("Mining progress: Cycle "..i.."/16")
  92.         for i=1, 16 do
  93.             --Building the 'ceiling'
  94.             move.digUp()
  95.             turtle.select(3)
  96.             turtle.placeUp()
  97.             turtle.select(1)
  98.             --Going down
  99.             mineDownLoop()
  100.             move.goto(getPos("x"), ceiling-1, getPos("z")-2, "north")
  101.         end
  102.         --Moving over to the next column and facing down
  103.         move.goto(getPos("x")+1, ceiling-1, getPos("z"), "south")
  104.         for i=1, 16 do
  105.             --Building the 'ceiling'
  106.             move.digUp()
  107.             turtle.select(3)
  108.             turtle.placeUp()
  109.             turtle.select(1)
  110.             --Going down
  111.             mineDownLoop()
  112.             move.goto(getPos("x"), ceiling-1, getPos("z")+2, "south")
  113.         end
  114.  
  115.         --And now to move to third row if it's not the last one
  116.         if move.xPos ~= ChunkEndX then
  117.             move.goto(getPos("x")+1,ceiling-1, getPos("z"), "north")
  118.         else
  119.             print("Alright, I'm done, going home!")
  120.         end
  121.     end
  122. end
  123.  
  124. function hasInv()
  125.     if turtle.getItemCount(16) > 0 then
  126.         sortInv()
  127.     end
  128. end
  129.  
  130. function dungeonCheck(side)
  131.     if turtle.detectDown() and not turtle.digDown() then
  132.         return "This is crap, not a chest."
  133.     else
  134.         if side == "down" then
  135.             hasInv()
  136.             while turtle.suckDown() do
  137.             hasInv()
  138.             end
  139.         elseif side == "forward" then
  140.             hasInv()
  141.             while turtle.suck() do
  142.                 hasInv()
  143.             end
  144.         elseif side == "up" then
  145.             hasInv()
  146.             while turtle.suckUp() do
  147.                 hasInv()
  148.             end
  149.         else
  150.             print("You are passing a carpy argument into dungeonCheck()")
  151.         end
  152.     end
  153. end
  154.  
  155. function mineDownLoop()
  156.     local bedrock = false
  157.     while not bedrock do
  158.         if not turtle.digDown() and turtle.detectDown() then
  159.             bedrock = true
  160.         else
  161.             hasInv()
  162.             dungeonCheck("down")
  163.             turtle.digDown()
  164.             hasInv()
  165.             dungeonCheck("forward")
  166.             turtle.dig()
  167.             move.fDown()
  168.         end
  169.     end
  170. end
  171.  
  172. --UTILITY FUNCTIONS
  173. function compressInv()
  174.     for i = 1, 16 do
  175.         --Step through each inv slot
  176.         for k = 16, i, -1 do
  177.             turtle.select(k)
  178.             if turtle.compareTo(i) or turtle.getItemCount(i) == 0 then
  179.                 turtle.transferTo(i)
  180.             end
  181.         end
  182.     end
  183.  
  184.     turtle.select(1)
  185. end
  186.  
  187. function sortInv()
  188.     --Kills cobble with a fiery passion immediately
  189.     if not spareCobble then
  190.         move.turnLeft()
  191.         move.turnLeft()
  192.  
  193.         countCrapItem1 = 0
  194.         countCrapItem2 = 0
  195.         for i=4, 16 do
  196.             turtle.select(i)
  197.             if turtle.compareTo(2) then
  198.                 countCrapItem1 = countCrapItem1 + turtle.getItemCount(i)
  199.             end
  200.  
  201.             if turtle.compareTo(3) then
  202.                 countCrapItem2 = countCrapItem2 + turtle.getItemCount(i)
  203.             end
  204.         end
  205.  
  206.         if countCrapItem1 > 191 then
  207.             --Kill crap slot1 except one placeholder here if in new version
  208.             for i=4, 16 do
  209.                 turtle.select(i)
  210.                 if turtle.compareTo(2) then
  211.                     turtle.select(i)
  212.                     turtle.dropUp()
  213.                 end
  214.             end
  215.         end
  216.  
  217.         if countCrapItem2 > 191 then
  218.             --Kill crap slot1 except one placeholder here if in new version
  219.             for i=4, 16 do
  220.                 turtle.select(i)
  221.                 if turtle.compareTo(3) then
  222.                     turtle.select(i)
  223.                     turtle.dropUp()
  224.                 end
  225.             end
  226.         end
  227.  
  228.         move.turnLeft()
  229.         move.turnLeft()
  230.     end
  231.        
  232.     --Checks all slots for fuel and eats them if he needs it
  233.     for i=2, 16 do
  234.         turtle.select(i)
  235.         while turtle.refuel(1)  and turtle.getFuelLevel() < 100000 and turtle.getItemCount(i) > 0 do
  236.             turtle.refuel(1)
  237.         end
  238.     end
  239.  
  240.     --Here, we are going to compress items onto others if possible
  241.     compressInv()
  242.  
  243.     --If his inventory 16 still has crap in it, that means it's too damn full. Here, we will now kill all items into a chest.
  244.     clearInv("partial")
  245. end
  246.  
  247. function clearInv(mode)
  248.    
  249.     if isEnderChest then
  250.         move.fUp()
  251.         turtle.select(1)
  252.         turtle.placeDown()
  253.         if mode == "all" then
  254.             for i=2, 16 do
  255.                 turtle.select(i)
  256.                 turtle.dropDown()
  257.             end
  258.         elseif mode == "partial" then
  259.             for i=4, 16 do
  260.                 turtle.select(i)
  261.                 turtle.dropDown()
  262.             end
  263.         else
  264.             print("Error: Invalid input was passed into clearInv!")
  265.         end
  266.  
  267.         turtle.select(1)
  268.         turtle.digDown()
  269.         move.fDown()
  270.     else
  271.         --GotoChestHere
  272.     end
  273. end
  274.  
  275. function canMineForward()
  276.     --Checks ChunkLoad boundaries to ensure what he's about to check
  277.     --is even chunkloaded (can't mine in non-loaded chunks)
  278.     local x, y, z, dir = move.getCoords()
  279.     if dir == "north" then
  280.         if  z > ChunkStartZ then
  281.             return true
  282.         else
  283.             return false
  284.         end
  285.     elseif dir == "south" then
  286.         if  z < ChunkEndZ then
  287.             return true
  288.         else
  289.             return false
  290.         end
  291.     elseif dir == "east" then
  292.         if  x > ChunkStartX then
  293.             return true
  294.         else
  295.             return false
  296.         end
  297.     elseif dir == "west" then
  298.         if  x < ChunkEndX then
  299.             return true
  300.         else
  301.             return false
  302.         end
  303.     else
  304.  
  305.     end    
  306. end
  307.  
  308. function getPos(coordType)
  309.     --If you only need one coordinate letter of the turtle
  310.     local x, y, z, dir = move.getCoords()
  311.     if coordType == "x" then
  312.         return x
  313.     elseif coordType == "y" then
  314.         return y
  315.     elseif coordType == "z" then
  316.         return z
  317.     elseif coordType == "dir" then
  318.         return dir
  319.     else
  320.         print("Improper parameter passed into getPos!")
  321.     end
  322. end
  323.  
  324. function renderDebug()
  325.     --Will display important variables on-screen if set to true in code
  326.     if useDebugRenderer==true then
  327.         term.clear()
  328.         term.setCursorPos(1,1)
  329.         print("Debug Output Code v"..version)
  330.         print("========================")
  331.         print("X: "..x)
  332.         print("Y: "..y)
  333.         print("Z: "..z)
  334.         print("Direction: "..getDir())
  335.         print("========================")
  336.         print("Status: "..status)
  337.         print("Fuel: "..turtle.getFuelLevel())
  338.         print("========================")
  339.     end
  340. end
  341.  
  342.  
  343. --END OF FUNCTIONS
  344.  
  345. --EXECUTION CODE
  346. init()
Advertisement
Add Comment
Please, Sign In to add comment