Advertisement
Guest User

Untitled

a guest
Oct 28th, 2012
3,553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.62 KB | None | 0 0
  1. --~ Before usage:
  2. --~ Make sure you have at least one piece of fuel in the first slot of the turtle.
  3. --~ Preferably, try to refuel it a bit before starting the program, so that it won't have problem.
  4.  
  5. --~ Usage: there are 3 ways of launching the program:
  6. --~ "mine"      will prompt the size
  7. --~ "mine 16 32"    will make a quarry of 16 width and 32 long
  8. --~ "mine 16 32 10"     will make the quarry start by going 10 times down, skipping the first layers
  9.  
  10. --~ How to place your base (side view) :
  11. --~ _C
  12. --~ C_T>
  13. --~
  14. --~ With C being chests, _ air and T the turtle, facing the > direction
  15. --~ The chest at the top is the one that has to contain fuel,
  16. --~ of the same type as the one put in the first slot of the turtle,
  17. --~ so that the turtle will be able to refuel automatically.
  18. --~ Charcoal is advised, being a renewable and reliable source of fuel,
  19. --~ but you are free to use any fuel as long as it stays the same kind
  20. --~ and is a valid fuel for turtles.
  21.  
  22. --~ As for the size (top view) :
  23. --~ CTxxxxxxxxxxxxxxx
  24. --~ _xxxxxxxxxxxxxxxx
  25. --~ _xxxxxxxxxxxxxxxx
  26. --~ _xxxxxxxxxxxxxxxx
  27. --~
  28. --~ this is the quarry for inputing "mine 4 16" (see above)
  29.  
  30. --~ The program is trying to log basic events, as via the function "log(text, alsoPrint)".
  31. --~ If you do not wish for the log, comment (or delete) the log function and uncomment the second one.
  32.  
  33. --~ NOTE: it is a basic program, doesn't take much into account, and certainly not being shut down.
  34. --~ As such, if the chuck unload, you will have to break the turtle and bring it back to the origin point before restarting it.
  35.  
  36. local x = 0
  37. local y = 0
  38. local h = 0
  39. local dir = 0
  40. local workX, workY, workH, workDir
  41. local row = 0
  42. local stop = false
  43. local maxX, maxY
  44. local args = {...}
  45.  
  46. local function log(text, alsoPrint)
  47.     logName = "MiningLog.txt"
  48.     local logFile = io.open(logName, "a")
  49.     if logFile == nil then
  50.         print("Error! ")
  51.     else
  52.         logFile:write(text.."\n")
  53.         logFile:close()
  54.     end
  55.     if alsoPrint then
  56.         print(text)
  57.     end
  58. end
  59.  
  60. --~ local function log(text, alsoPrint)
  61. --~     if alsoPrint then
  62. --~         print(text)
  63. --~     end
  64. --~ end
  65.  
  66. local function clearLog()
  67.     logName = "MiningLog.txt"
  68.     if fs.exists(logName) then
  69.         fs.delete(logName)
  70.     end
  71. end
  72.  
  73. local function dig()
  74.     local i = 0
  75.     if turtle.detectUp() then
  76.         turtle.digUp()
  77.     end
  78.     if turtle.detect() then
  79.         turtle.dig()
  80.     end
  81.     if turtle.detectDown() then
  82.         turtle.digDown()
  83.     end
  84. end
  85.  
  86. local function turn(direction)
  87.     if direction == "left" then
  88.         turtle.turnLeft()
  89.         dir = (dir-1)%4
  90.     elseif direction == "right" then
  91.         turtle.turnRight()
  92.         dir = (dir+1)%4
  93.     end
  94.    
  95.     log("TURN "..direction.." x="..x.."  y="..y.." h="..h.." dir="..dir)
  96. end
  97.  
  98. local function forward()
  99.     local j=0
  100.     while not turtle.forward() do
  101.         if turtle.detect() then
  102.             turtle.dig()
  103.             j = j+1
  104.             if j == 40 then
  105.                 stop = true
  106.                 return false
  107.             end
  108.         else
  109.             turtle.attack()
  110.         end
  111.     end
  112.     if dir == 0 then
  113.         x = x+1
  114.     elseif dir == 2 then
  115.         x = x-1
  116.     elseif dir == 1 then
  117.         y = y+1
  118.     elseif dir == 3 then
  119.         y = y-1
  120.     end
  121.     return true
  122. end
  123.  
  124. local function up()
  125.     local j=0
  126.     while not turtle.up() do
  127.         if turtle.detectUp() then
  128.             turtle.digUp()
  129.             j = j+1
  130.             if j == 40 then
  131.                 stop = true
  132.                 return false
  133.             end
  134.         else
  135.             turtle.attackUp()
  136.         end
  137.     end
  138.     h = h-1
  139.     return true
  140. end
  141.  
  142. local function down()
  143.     local j=0
  144.     while not turtle.down() do
  145.         if turtle.detectDown() then
  146.             turtle.digDown()
  147.             j = j+1
  148.             if j == 40 then
  149.                 stop = true
  150.                 return false
  151.             end
  152.         else
  153.             turtle.attackDown()
  154.         end
  155.     end
  156.     h = h+1
  157.     return true
  158. end
  159.  
  160. local function setDir(newDir)
  161.     while not (newDir == dir) do
  162.         turn("right")
  163.     end
  164. end
  165.  
  166. local function move()
  167.     local locMin, locMax, locDirA, locDirB, directionA, directionB
  168.    
  169.    
  170.     if row%2 == 0 then
  171.         locMin = 0
  172.         locMax = maxY
  173.         directionA = "right"
  174.         directionB = "left"
  175.         locDir = 1
  176.     else
  177.         locMin = maxY
  178.         locMax = 0
  179.         directionA = "left"
  180.         directionB = "right"
  181.         locDir = 3
  182.     end
  183.    
  184.     --Stopping the mining operation
  185.     if x == locMax and y == 0 and turtle.detectDown() then
  186.         stop = true
  187.     --Going down to the next layer
  188.     elseif x == 0 and y == locMax then
  189.        
  190.         for i=1,3 do
  191.             local j=0
  192.             down()
  193.             if stop then
  194.                 break
  195.             end
  196.         end
  197.         if not stop then
  198.             row = row +1
  199.         end
  200.         log("DOWN  x="..x.."  y="..y.." h="..h.." dir="..dir)
  201.     --Turning at end of a line
  202.     elseif (x == maxX-1 and dir == 0) or (x == maxX and dir == locDir)then
  203.         forward()
  204.         turn(directionA)
  205.     --Turning at the other end of the line
  206.     elseif ((x == 1 and dir == 2) or (x==0 and dir == locDir)) and y ~= locMin and y ~= locMax  then
  207.         forward()
  208.         turn(directionB)
  209.     elseif x==1 and y==locMax then
  210.         forward()
  211.         turn("right")
  212.         turn("right")
  213.     --In other cases, just go forward once
  214.     else
  215.         forward()
  216.     end
  217. end
  218.  
  219. local function returnHome()
  220.     local curDir = dir
  221.     local fuelCount
  222.     log("Returning home: x="..x.."  y="..y.."  h="..h)
  223.    
  224.    
  225.     for i=1,h do
  226.         up()
  227.     end
  228.    
  229.     setDir(3)
  230.     for i=1, y do
  231.         forward()
  232.     end
  233.    
  234.     setDir(2)
  235.     for i=1, x do
  236.         forward()
  237.     end
  238. end
  239.  
  240. local function manageInventory()
  241.     setDir(2)
  242.     forward()
  243.    
  244.     local rowFuelNeed = maxX*maxY+h
  245.     if turtle.getFuelLevel() < rowFuelNeed then
  246.         turtle.select(1)
  247.         turtle.suckUp()
  248.         fuelCount = turtle.getItemCount(1) -1
  249.         turtle.refuel(fuelCount)
  250.         log("Refuelling...")
  251.         if turtle.getFuelLevel() < rowFuelNeed then
  252.             log("missing fuel")
  253.             stop = true
  254.         end
  255.     end
  256.     log("Dropping items")
  257.     for i=2, 16 do
  258.         turtle.select(i)
  259.         turtle.drop()
  260.     end
  261.    
  262.     turtle.select(1)
  263.     setDir(0)
  264.    
  265.     forward()
  266. end
  267.  
  268. local function returnPosition()
  269.  
  270.     log("Returning to work from : x="..x.."  y="..y.."  h="..h.." dir:"..dir)
  271.     log("Returning to work to   : x="..workX.."  y="..workY.."  h="..workH.." dir:"..workDir)
  272.     setDir(0)
  273.     for i=x, workX-1 do
  274.         forward()
  275.     end
  276.    
  277.     setDir(1)
  278.     for i=y, workY-1 do
  279.         forward()
  280.     end
  281.    
  282.    
  283.     for i=h, workH-1 do
  284.         down()
  285.     end
  286.     setDir(workDir)
  287. end
  288.  
  289. local function inventoryCheck()
  290.     local fuelLeft = turtle.getFuelLevel() -(x+y+h+1)
  291.     if turtle.getItemCount(16) >0 or fuelLeft < 10 then
  292.         workX = x
  293.         workY = y
  294.         workH = h
  295.         workDir = dir
  296.         returnHome()
  297.         manageInventory()
  298.         returnPosition()
  299.         if(x == workX and y == workY and h == workH and dir == workDir) then
  300.             log("Sucessfully returned at x="..x.."  y="..y.."  h="..h.."  dir="..dir)
  301.         else
  302.             log("For some reason, couldn't properly return, aborting...")
  303.             stop = true
  304.         end
  305.     end
  306. end
  307.  
  308. local function digLoop()
  309.     while not stop do
  310.         dig()
  311.         inventoryCheck()
  312.         move()
  313.         if stop then
  314.             returnHome()
  315.             manageInventory()
  316.             log("Your turtle has returned and stopped")
  317.         end
  318.     end
  319. end
  320.  
  321. clearLog()
  322. x=0
  323. y=0
  324. h=0
  325. dir=0
  326. if args[1] == nil then
  327.     print("Size?")
  328.     write("Width: ")
  329.     maxY = read()
  330.     maxY = maxY -1
  331.     if maxY%2 == 0 then
  332.         log("Width not a multiple of 2, adding 1", true)
  333.         maxY =maxY+ 1
  334.     end
  335.     write("Length: ")
  336.     maxX = read()
  337.     maxX = maxX -1
  338.     if maxX%2 == 0 then
  339.         log("Length not a multiple of 2, adding 1", true)
  340.         maxY =maxX+ 1
  341.     end
  342. else
  343.     local var1, var2
  344.     if args[2] == nil then
  345.         var1 = args[1]-1
  346.         if var1 == 0 then
  347.             log("Not a multiple of 2, adding 1", true)
  348.             var1 =var1+ 1
  349.         end
  350.         log(var1)
  351.         maxX = var1
  352.         maxY = var1
  353.     else       
  354.         maxY = args[1]-1
  355.         if maxY%2 == 0 then
  356.             log("Width not a multiple of 2, adding 1", true)
  357.             maxY =maxY+ 1
  358.         end
  359.        
  360.         maxX = args[2]-1
  361.         if maxX%2 == 0 then
  362.             log("Length not a multiple of 2, adding 1", true)
  363.             maxX =maxX+ 1
  364.         end
  365.        
  366.         if args[3] ~= nill then
  367.             workX = 0
  368.             workY = 0
  369.             workH = args[3]
  370.             workDir = 0
  371.             returnPosition()
  372.         end
  373.     end
  374. end
  375. log("Starting mining operation with dimensions "..(maxY+1).."x"..(maxX+1), true)
  376.  
  377. digLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement