bigougit

digger

Jan 3rd, 2013
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.50 KB | None | 0 0
  1. --[[
  2. digger v1.1 by bigougit
  3. youtube.com/user/bigougit
  4.  
  5. This program is for turtles only,
  6. ideally mining turtles.
  7.  
  8. To install in-game, make sure
  9. that http = true in your
  10. computercraft config.
  11.  
  12. To install, open your turtle
  13. and type:
  14. pastebin get fxZCZprs digger
  15.  
  16. usage: digger x y z [d]
  17. x = how far forward the turtle will dig
  18. y = how far up or down the turtle will dig
  19. z = how far to the right the turtle will dig
  20. [d] = optional, will make the turtle dig
  21.     down instead of up.
  22.  
  23. The turtle will not start if it doesn't have
  24. enough fuel.
  25.  
  26. to calculate fuel needed:
  27. x*y*z*1.5
  28.  
  29. not the best calculation, but
  30. it works and the turtle usually
  31. doesn't need more fuel while working.
  32.  
  33. report any errors to me on youtube or
  34. twitter.
  35.  
  36. http://youtube.com/user/bigougit
  37. http://twitter.com/bigougit
  38.  
  39. --]]
  40.  
  41.  
  42. --return how many items in [slot]
  43. local function sCount(slot)
  44.   return turtle.getItemCount(slot)
  45. end
  46.  
  47. --move similar items to [slot]
  48. function stack(slot)
  49.  
  50.   local room = turtle.getItemSpace(slot)
  51.   local s = 0
  52.   while room > 0 do
  53.     s = s + 1
  54.     if s >= 16 then
  55.       room = 0
  56.     end
  57.     if s ~= slot then      
  58.       turtle.select(s)
  59.       if turtle.compareTo(slot) then
  60.         if sCount(s) >= room then        
  61.           turtle.transferTo(slot, room)
  62.           room = 0
  63.         else
  64.           local temp = sCount(s)
  65.           turtle.transferTo(slot,temp)
  66.           room = room - temp
  67.         end
  68.       end
  69.     end
  70.   end
  71. end
  72.  
  73. function fuel(slot, need)
  74.   local iCount = sCount(slot)
  75.   --stack(slot)
  76.   local cont = true
  77.   while cont do
  78.     if turtle.getFuelLevel() < need then
  79.       if iCount > 1 then
  80.         turtle.select(slot)
  81.         turtle.refuel(1)
  82.         if iCount == sCount(slot) then
  83.           cont = false
  84.         else
  85.           iCount = sCount(slot)
  86.         end
  87.       else
  88.         cont = false
  89.       end
  90.     else
  91.       return true
  92.     end
  93.   end
  94.   if turtle.getFuelLevel() < need then
  95.     return false
  96.   else
  97.     return true
  98.   end
  99. end
  100.  
  101. function tryForward(n)
  102.   n = n or 1
  103.   for i=1,n do
  104.     while not turtle.forward() do
  105.       turtle.dig()
  106.       turtle.attack()
  107.     end
  108.   end
  109. end
  110.  
  111. function tryUp(n)
  112.   n = n or 1
  113.   for i=1,n do
  114.     while not turtle.up() do
  115.       turtle.digUp()
  116.       turtle.attackUp()
  117.     end
  118.   end
  119. end
  120.  
  121. function tryDown(n)
  122.   n = n or 1
  123.   for i=1,n do
  124.     while not turtle.down() do
  125.       turtle.digDown()
  126.       turtle.attackDown()
  127.     end
  128.   end
  129. end
  130.  
  131. function left(n)
  132.   n = n or 1
  133.   for i=1,n do
  134.     turtle.turnLeft()
  135.   end
  136. end
  137.  
  138. function right(n)
  139.   n = n or 1
  140.   for i=1,n do
  141.     turtle.turnRight()
  142.   end
  143. end
  144.  
  145. function haveRoom()
  146.   local hasRoom = false
  147.   for i=1,16 do
  148.     if sCount(i) == 0 then
  149.       hasRoom = true
  150.     end
  151.   end
  152.   return hasRoom
  153. end  
  154.  
  155. function turnTo(curF,toF)
  156.   while curF ~= toF do
  157.     if curF > 3 then
  158.       turtle.turnRight()
  159.       curF = 0
  160.     elseif curF == 3 then
  161.       turtle.turnRight()
  162.       curF = 0
  163.     elseif curF > -1 then  
  164.       turtle.turnRight()
  165.       curF = curF + 1
  166.     end
  167.   end
  168.   return curF
  169. end
  170.  
  171. function calcF(dir,curF)
  172.   if dir:lower() == "right" then
  173.     if curF >= 3 then
  174.       curF = 0
  175.     else
  176.       curF = curF + 1
  177.     end
  178.   else
  179.     if curF <= 0 then
  180.       curF = 3
  181.     else
  182.       curF = curF-1
  183.     end
  184.   end
  185.   return curF
  186. end
  187.  
  188. function goTo(x,y,z,f,toX,toY,toZ,toF)
  189.   local xDir = 1
  190.   local yDir = 1
  191.   local zDir = 1
  192.   if x > toX then
  193.     xDir = -1
  194.   end
  195.   if y > toY then
  196.     yDir = -1
  197.   end
  198.   if z > toZ then
  199.     zDir = -1
  200.   end
  201.   if y ~= toY then
  202.    
  203.     for curY=(y+yDir),toY,yDir do
  204.       if yDir > 0 then
  205.         tryUp()
  206.       else
  207.         tryDown()
  208.       end
  209.     end
  210.   end
  211.   if x ~= toX then
  212.     if xDir > 0 then
  213.         f = turnTo(f,0)  
  214.     else
  215.       f = turnTo(f,2)
  216.     end
  217.     for curX=(x+xDir),toX,xDir do
  218.       tryForward()
  219.  
  220.     end
  221.   end
  222.   if z ~= toZ then
  223.     if zDir > 0 then
  224.       f = turnTo(f,1)
  225.     else
  226.       f = turnTo(f,3)
  227.     end
  228.     for curZ=(z+zDir),toZ,zDir do
  229.       tryForward()
  230.     end
  231.   end
  232.   f = turnTo(f,toF)
  233. end
  234.  
  235.  
  236. local tArgs = { ... }
  237. local x,y,z
  238. local error = false
  239.  
  240.  
  241. if tArgs[1] then
  242.   x = tonumber(tArgs[1])
  243.   if tArgs[2] then
  244.     y = tonumber(tArgs[2])
  245.     if tArgs[3] then
  246.       z = tonumber(tArgs[3])
  247.     else
  248.       error = true
  249.     end
  250.   else error = true
  251.   end
  252. else error = true
  253. end
  254.  
  255. if error then
  256.   print("          Incorrect usage")
  257.   print("-----------------------------------")
  258.   print("       usage: digger x y z [d]")
  259.   print(" ")
  260.   print("x,y,x are the dimmensions to dig")
  261.   print("If d, I will dig down,up is default")
  262.   print(" ")
  263.   print("Put fuel in the first slot and a")
  264.   print("chest behind me, for proper use.")
  265.   print("-----------------------------------")
  266.   return true
  267. end
  268.  
  269. local yDir = 1
  270. local dDown = false
  271.  
  272. if tArgs[4] == "d" or tArgs[4] == 'd' then
  273.   yDir = -1
  274.   dDown = true
  275. end
  276.  
  277. x = math.abs(x)
  278. y = math.abs(y)
  279. z = math.abs(z)
  280. local f = 0
  281. local curX = 0
  282. local curY = 0
  283. local curZ = 0
  284.  
  285. local function inv()
  286.   if not haveRoom() then
  287.     goTo(curX,curY,curZ,f,0,0,0,2)
  288.     for slot = 2 , 16 do
  289.       turtle.select(slot)
  290.       turtle.drop()
  291.     end
  292.     turtle.select(1)
  293.     goTo(0,0,0,2,curX,curY,curZ,f)
  294.   end
  295. end
  296.  
  297. local function move()
  298.  
  299.   --if turtle.getItemSpace(1) > 0 then
  300.   --  bMan.stack(1)
  301.   --end
  302.   if f == 0 then
  303.     curX = curX + 1
  304.   elseif f == 1 then
  305.     curZ = curZ + 1
  306.   elseif f == 2 then
  307.     curX = curX - 1
  308.   elseif f == 3 then
  309.     curZ = curZ - 1
  310.   else
  311.     print("error!")
  312.   end
  313.   inv()
  314. end
  315.  
  316. local dir = 0
  317.  
  318. local fNeeded = x*y*z
  319.  
  320. if not fuel(1,fNeeded*1.5) then
  321.   print("not enough fuel! try again...")
  322. else
  323.   for cY=1,y do
  324.    
  325.     for cZ=1,z do
  326.    
  327.       for cX=2,x do
  328.         tryForward()
  329.         move()
  330.       end
  331.      
  332.       if cZ < z then
  333.         if dir == 0 then
  334.           right()
  335.           f = calcF("right",f)
  336.           tryForward()
  337.           move()
  338.           right()
  339.           f = calcF("right",f)
  340.           dir = 1
  341.         elseif dir == 1 then
  342.           left()
  343.           f = calcF("left",f)
  344.           tryForward()
  345.           move()
  346.           left()
  347.           f = calcF("left",f)
  348.           dir = 0
  349.         end
  350.       end
  351.     end
  352.     if cY < y then
  353.       if dDown then
  354.         inv()
  355.         tryDown()
  356.         curY = curY -1
  357.       else
  358.         inv()
  359.         tryUp()
  360.         curY = curY + 1
  361.       end
  362.       left(2)
  363.       f = calcF("left",f)
  364.       f = calcF("left",f)
  365.       end
  366.   end
  367. end
  368. print("going home")
  369. goTo(curX,curY,curZ,f,0,0,0,0)
Advertisement
Add Comment
Please, Sign In to add comment