NayrbEroom

advTunnel

Sep 30th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.89 KB | None | 0 0
  1. --[[
  2. Known Issues:
  3.     Also, allow this to work with fuel, though this was originally
  4.     built with fuel disabled, if it runs in a world that requires fuel
  5.     it may get stuck if its not fueled properly.
  6.  
  7. ]]
  8. turtle.refuel()
  9. local dn = 15 -- Distance notification, basically tells you how far the turtle has dug in increments
  10. local length, walk = ...
  11. local delay = 0.25 -- This is to accomodate Sand falling, or obsidian forming
  12. if length == nil then
  13.     print("No length argument was given")
  14.     return false -- Closes Program
  15. end
  16.  
  17. if walk == 'true' then
  18.     walk = true
  19. else
  20.     walk = false
  21. end
  22.  
  23.  
  24. --These variables keep track of the Turtles position and
  25. --orientation relative to the turtles Starting Position
  26. X = 0
  27. Y = 0
  28. Z = 0
  29. O = 0
  30.  
  31. --[[
  32. Orientation List
  33. Forward = 0
  34. Right = 1
  35. Back = 2
  36. Left = 3
  37. ]]--
  38.  
  39.  
  40. -- These functions are used to keep track of
  41. -- The Turtles Position Relative to its
  42. -- Starting Point
  43.  
  44. function left(q)
  45.     if q == nil then q = 1 end
  46.     for i=1,q do
  47.         turtle.turnLeft()
  48.         if O == 0 then
  49.             O = 3
  50.         else
  51.             O = O - 1
  52.         end
  53.     end
  54.     return true
  55. end
  56.  
  57. function right(q)
  58.     if q == nil then q = 1 end
  59.     for i=1,q do
  60.         turtle.turnRight()
  61.         if O == 3 then
  62.             O = 0
  63.         else
  64.             O = O + 1
  65.         end
  66.     end
  67.     return true
  68. end
  69.  
  70. function up(q)
  71.     if q == nil then q = 1 end
  72.     for i=1,q do
  73.         while not turtle.up() do
  74.             digUp()
  75.             turtle.up()
  76.             sleep(delay)
  77.         end
  78.         Y = Y + 1
  79.     end
  80.     return true
  81. end
  82.  
  83. function down(q)
  84.     if q == nil then q = 1 end
  85.     for i=1,q do
  86.         while not turtle.down() do
  87.             digDown()
  88.             turtle.attackDown()
  89.             sleep(delay)
  90.         end
  91.         Y = Y - 1
  92.     end
  93.     return true
  94. end
  95.  
  96. function forward(q)
  97.     if q == nil then q = 1 end
  98.     for i=1,q do
  99.         while not turtle.forward() do
  100.             turtle.dig()
  101.             turtle.attack()
  102.             sleep(delay)
  103.         end
  104.         if O == 0 then
  105.             X = X + 1
  106.         elseif O == 1 then
  107.             Z = Z + 1
  108.         elseif O == 2 then
  109.             X = X - 1
  110.         elseif O == 3 then
  111.             Z = Z - 1
  112.         end
  113.     end
  114.     return true
  115. end
  116.  
  117. function back()
  118.     right()
  119.     right()
  120.     forward()
  121.     left()
  122.     left()
  123.     return true
  124. end
  125.  
  126. function orient(arg1)
  127.     while arg1 ~= O do
  128.             right()
  129.     end
  130.     return true
  131. end
  132.  
  133. function dig()
  134.     while turtle.detect() do
  135.         turtle.dig()
  136.         sleep(delay)
  137.     end
  138.     return true
  139. end
  140.  
  141. function digUp()
  142.     while turtle.detectUp() do
  143.         turtle.digUp()
  144.         sleep(delay)
  145.     end
  146.     return true
  147. end
  148.  
  149. function digDown()
  150.     while turtle.detectDown() do
  151.         turtle.digDown()
  152.         sleep(delay)
  153.     end
  154.     return true
  155. end
  156.  
  157. --[[ Tells turtle where to go for longer movements,
  158.  such as going to and from the chest.
  159.  This may error if theres blocks in the way, but
  160.  Given the way we will have the turtle move in this
  161.  program, it will be fine. How ever if I were to
  162.  copy this function over to another program, I'd
  163.  probably fix this issue.]]
  164. function go(lx,ly,lz)
  165.     if X < lx then
  166.         orient(0)
  167.         for i=1,(lx-X) do
  168.             forward()
  169.         end
  170.     end
  171.    
  172.     if X > lx then
  173.         orient(2)
  174.         for i=1,X-lx do
  175.             forward()
  176.         end
  177.     end
  178.    
  179.     if Y < ly then
  180.         for i=1,ly-Y do
  181.             up()
  182.         end
  183.     end
  184.    
  185.     if Y > ly then
  186.         for i=1,Y-ly do
  187.             down()
  188.         end
  189.     end
  190.    
  191.     if Z < lz then
  192.         orient(1)
  193.         for i=1,lz-Z do
  194.             forward()
  195.         end
  196.     end
  197.    
  198.     if Z > lz then
  199.         orient(3)
  200.         for i=1,Z-lz do
  201.             forward()
  202.         end
  203.     end
  204. end
  205.  
  206.  
  207.  
  208. -- Deposits Items into a chest
  209. -- Warning! This may error if
  210. -- the chest is full, so make sure
  211. -- its emptied
  212. function deposit()
  213.     for i=1,16 do
  214.         turtle.select(i)
  215.         while turtle.drop() == false and turtle.getItemCount() ~= 0 do -- Waits until turtle can deposit item
  216.             sleep(1)
  217.         end
  218.         turtle.drop()
  219.     end
  220.     turtle.select(1)
  221.     return true
  222. end
  223.  
  224.  
  225.  
  226. --[[
  227. This function is what tells the
  228. turtle to mine and when to go back
  229. to empty its inventory
  230. ]]--
  231. function mine()
  232.     dig()
  233.     forward()
  234.     digUp()
  235.     digDown()
  236.     up()
  237.     left()
  238.     dig()
  239.     right(2)
  240.     dig()
  241.     down()
  242.     dig()
  243.     left(2)
  244.     dig()
  245.     down()
  246.     dig()
  247.     right(2)
  248.     dig()
  249.     left()
  250.     up()
  251. end
  252.  
  253.  
  254. -- Checks if turtle is full
  255. function check()
  256.     if turtle.getItemCount(15) == 0 then -- Checks second last slot. I'd like to leave 2 slots empty just to be safe
  257.         return false
  258.     else
  259.         return true
  260.     end
  261. end
  262.  
  263. function notify(include)
  264.     if X % dn == 0 and X ~= 0 then
  265.         print("The turtle has traveled "..X.."/"..length+include.." blocks so far")
  266.     end
  267.     return true
  268. end
  269.  
  270. local dni = 0 -- Blocks for distance notification to include in length, from the walk program
  271. if walk then
  272.     while turtle.detect() == false do
  273.         forward()
  274.         dni = dni + 1
  275.         notify(dni) -- Notifies Distance
  276.     end
  277. end
  278.  
  279. for i=1,length do
  280.     mine()
  281.     notify(dni) -- Notifies Distance
  282.     if check() then -- Checks if turtle should deposit items
  283.         local lx,ly,lz = X,Y,Z
  284.         go(0,0,0)
  285.         orient(2) -- Faces Chest
  286.         deposit()
  287.         orient(0)
  288.         go(lx,ly,lz)
  289.     end
  290. end
  291.  
  292. -- Returns to chest and deposits items before ending program
  293. go(0,0,0)
  294. orient(2) -- Faces Chest
  295. deposit()
  296. orient(0)
  297. print("Program Ended, thank's for using BowWhalley's Digging Program")
Add Comment
Please, Sign In to add comment