craniumkid22

Modded Tunnel

Dec 4th, 2012
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.88 KB | None | 0 0
  1. local tArgs = { ... }
  2. -- Config for refueling itself
  3. local refuelSelf = false
  4.  
  5. -- Various checks and balances. You can skip this part.
  6.     -- check usage of arguments
  7.     if #tArgs < 1 or #tArgs > 2 then
  8.         print( "Usage: "..shell.getRunningProgram().." <length> [height]" )
  9.         return
  10.     end
  11.  
  12.     -- check if height argument was specified
  13.     -- if not, default 3x3 tunnel will be dug out
  14.     if not tArgs[2] then
  15.         height = 3
  16.     else
  17.         height = tonumber(tArgs[2])
  18.     end
  19.  
  20.     -- why would you run this on anything other than a turtle?
  21.     if not turtle then
  22.         print( "You must run this on a turtle." )
  23.         return
  24.     end
  25.  
  26.     -- check if length is positive
  27.     local length = tonumber( tArgs[1] )
  28.     if length < 1 then
  29.         print( "Tunnel length must be positive" )
  30.         return
  31.     end
  32.  
  33. -- Various variables
  34. local depth = 0
  35. local collected = 0
  36. local facing = 0
  37. local currHeight = 0
  38. local oldRight = turtle.turnRight()
  39. local oldLeft = turtle.turnLeft()
  40. local oldDig = turtle.dig()
  41. local oldDigUp = turtle.digUp()
  42. local oldDigDown = turtle.digDown()
  43.  
  44. -- Redefining some of the turtle functions
  45. local function turtle.turnRight()
  46.     facing = facing + 1
  47.     if facing == 4 then
  48.         facing = 0
  49.     end
  50.     return oldRight()
  51. end
  52.  
  53. local function turtle.turnLeft()
  54.     facing = facing - 1
  55.     if facing == -4 then
  56.         facing = 0
  57.     end
  58.     return oldLeft()
  59. end
  60.  
  61. -- Refuel function
  62. local function refuel()
  63.     local fuelLevel = turtle.getFuelLevel()
  64.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  65.         return
  66.     end
  67.    
  68.     local function tryRefuel()
  69.         for n=1,16 do
  70.             if turtle.getItemCount(n) > 0 then
  71.                 turtle.select(n)
  72.                 if refuelSelf then
  73.                     if turtle.refuel(1) then
  74.                         turtle.select(1)
  75.                         return true
  76.                     end
  77.                 end
  78.             end
  79.         end
  80.         turtle.select(1)
  81.         return false
  82.     end
  83.    
  84.     if not tryRefuel() then
  85.         print( "Add more fuel to continue." )
  86.         while not tryRefuel() do
  87.             sleep(1)
  88.         end
  89.         print( "Resuming Tunnel." )
  90.     end
  91. end
  92.  
  93. -- Return to base if full
  94. local function rtb()
  95.     local returnHeight = currHeight
  96.     local returnFacing = facing
  97.     while currHeight ~= 0 do
  98.         turtle.down()
  99.         currHeight = currHeight - 1
  100.     end
  101.     if facing < 0 then
  102.         while facing ~= 0 do
  103.             turtle.turnRight()
  104.             facing = facing + 1
  105.         end
  106.     elseif facing > 0 then
  107.         while facing ~= 0 do
  108.             turtle.turnLeft()
  109.             facing = facing - 1
  110.         end
  111.     end
  112.     turtle.turnRight()
  113.     turtle.turnRight()
  114.     for i = 1, depth do
  115.         refuel()
  116.         turtle.forward()
  117.     end
  118.     for i = 1, 16 do
  119.         turtle.select(i)
  120.         turtle.drop()
  121.     end
  122.     turtle.turnRight()
  123.     turtle.turnRight()
  124.     for i = 1, depth do
  125.         refuel()
  126.         while not turtle.forward()
  127.             turtle.dig()
  128.         end
  129.     end
  130.     for i = 1, returnHeight do
  131.         turtle.up()
  132.     end
  133.     if returnFacing < 0 then
  134.         turtle.turnLeft()
  135.     else
  136.         turtle.turnRight()
  137.     end
  138. end
  139.  
  140. local function turtle.dig()
  141.     for i = 1, 16 do
  142.         if turtle.compare(i) then
  143.             turtle.select(i)
  144.             return oldDig()
  145.         end
  146.     end
  147.     for i = 1, 16 do
  148.         if turtle.getItemCount(i) < 64 then
  149.             turtle.select(i)
  150.             return oldDig()
  151.         end
  152.     end
  153.     rtb()
  154. end
  155.  
  156. local function turtle.digUp()
  157.     for i = 1, 16 do
  158.         if turtle.compareUp(i) then
  159.             turtle.select(i)
  160.             return oldDigUp()
  161.         end
  162.     end
  163.     for i = 1, 16 do
  164.         if turtle.getItemCount(i) < 64 then
  165.             turtle.select(i)
  166.             return oldDigUp()
  167.         end
  168.     end
  169.     rtb()
  170. end
  171.  
  172. local function turtle.digDown()
  173.     for i = 1, 16 do
  174.         if turtle.compareDown(i) then
  175.             turtle.select(i)
  176.             return oldDigDown()
  177.         end
  178.     end
  179.     for i = 1, 16 do
  180.         if turtle.getItemCount(i) < 64 then
  181.             turtle.select(i)
  182.             return oldDigDown()
  183.         end
  184.     end
  185.     rtb()
  186. end
  187.  
  188. -- Count of items collected
  189. local function collect()
  190.     collected = collected + 1
  191.     if math.fmod(collected, 25) == 0 then
  192.         print( "Mined "..collected.." items." )
  193.     end
  194. end
  195.  
  196. -- Try digging something
  197. local function tryDig()
  198.     while turtle.detect() do
  199.         if turtle.dig() then
  200.             collect()
  201.             sleep(0.5)
  202.         else
  203.             return false
  204.         end
  205.     end
  206.     return true
  207. end
  208.  
  209. local function tryDigUp()
  210.     while turtle.detectUp() do
  211.         if turtle.digUp() then
  212.             collect()
  213.             sleep(0.5)
  214.         else
  215.             return false
  216.         end
  217.     end
  218.     return true
  219. end
  220.  
  221. local function tryDigDown()
  222.     while turtle.detectDown() do
  223.         if turtle.digDown() then
  224.             collect()
  225.             sleep(0.5)
  226.         else
  227.             return false
  228.         end
  229.     end
  230.     return true
  231. end
  232.  
  233. local function tryUp()
  234.     refuel()
  235.     while not turtle.up() do
  236.         if turtle.detectUp() then
  237.             if not tryDigUp() then
  238.                 return false
  239.             end
  240.         elseif turtle.attackUp() then
  241.             collect()
  242.         else
  243.             sleep( 0.5 )
  244.         end
  245.     end
  246.     return true
  247. end
  248.  
  249. local function tryDown()
  250.     refuel()
  251.     while not turtle.down() do
  252.         if turtle.detectDown() then
  253.             if not tryDigDown() then
  254.                 return false
  255.             end
  256.         elseif turtle.attackDown() then
  257.             collect()
  258.         else
  259.             sleep( 0.5 )
  260.         end
  261.     end
  262.     return true
  263. end
  264.  
  265. local function tryForward()
  266.     refuel()
  267.     while not turtle.forward() do
  268.         if turtle.detect() then
  269.             if not tryDig() then
  270.                 return false
  271.             end
  272.         elseif turtle.attack() then
  273.             collect()
  274.         else
  275.             sleep( 0.5 )
  276.         end
  277.     end
  278.     return true
  279. end
  280.  
  281. -- Start tunneling
  282. print( "Tunnelling..." )
  283.  
  284. for n=1,length do
  285.     tryDigUp()
  286.     turtle.turnLeft()
  287.     tryDig()
  288.     for i = 1, height do
  289.         tryUp()
  290.         currHeight = currHeight + 1
  291.         tryDig()
  292.     end
  293.     turtle.turnRight()
  294.     turtle.turnRight()
  295.     tryDig()
  296.     for i = 1, height do
  297.         tryDown()
  298.         currHeight = currHeight - 1
  299.         tryDig()
  300.     end
  301.     turtle.turnLeft()
  302.    
  303.     if n<length then
  304.         tryDig()
  305.         if not tryForward() then
  306.             print( "Aborting Tunnel." )
  307.             break
  308.         else
  309.             depth = depth + 1
  310.         end
  311.     else
  312.         print( "Tunnel complete." )
  313.     end
  314.  
  315. end
  316.  
  317.  
  318. print( "Returning to start..." )
  319.  
  320. -- Return to where we started
  321. turtle.turnLeft()
  322. turtle.turnLeft()
  323. while depth > 0 do
  324.     if turtle.forward() then
  325.         depth = depth - 1
  326.     else
  327.         turtle.dig()
  328.     end
  329. end
  330. -- Drop items into the chest
  331. for i = 1, 16 do
  332.     turtle.select(i)
  333.     turtle.drop()
  334. end
  335. turtle.turnRight()
  336. turtle.turnRight()
  337.  
  338.  
  339. print( "Tunnel complete." )
  340. print( "Mined "..collected.." items total." )
Advertisement
Add Comment
Please, Sign In to add comment