grand_mind1

Better Tunnel v1.0

Aug 12th, 2013
992
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.16 KB | None | 0 0
  1. --Begin runChest function
  2. local function runChest()
  3.     if useChests == "Y" or useChests == "y" then
  4.         max = 15
  5.         chests = turtle.getItemCount(chestSlot)
  6.         if chests == 0 then
  7.             print("\nPlease input chests into slot ",max+1,"...")
  8.             while chests == 0 do
  9.                 chests = turtle.getItemCount(chestSlot)
  10.                 sleep(1)
  11.             end
  12.         end
  13.         continue = true
  14.     elseif useChests == "N" or useChests == "n" then
  15.         continue = true
  16.     else
  17.         continue = false
  18.         print("\nInvalid")
  19.         sleep(2)
  20.     end
  21. end
  22. --End runChest function
  23.  
  24. --Begin runTorch function
  25. local function runTorch()
  26.     if useTorches == "Y" or useTorches == "y" then
  27.         if useChests == "Y" or useChests == "y" then
  28.             torchSlot = 15
  29.             torches = turtle.getItemCount(torchSlot)   
  30.             max = 14
  31.         else
  32.             torchSlot = 16
  33.             torches = turtle.getItemCount(torchSlot)
  34.             max = 15
  35.         end
  36.         if torches == 0 then
  37.             print("\nPlease input torches into slot ",max+1,"...")
  38.             while torches == 0 do
  39.                 if useChests == "Y" or useChests == "y" then
  40.                     torches = turtle.getItemCount(15)
  41.                 else
  42.                     torches = turtle.getItemCount(16)
  43.                 end
  44.                 sleep(1)
  45.             end
  46.         end
  47.         continue = true
  48.     elseif useTorches == "N" or useTorches == "n" then
  49.         continue = true
  50.     else
  51.         continue = false
  52.         print("\nInvalid")
  53.         sleep(2)
  54.     end
  55. end
  56. --End runTorch function
  57.  
  58. --Begin dropoff function
  59. local function dropOff()
  60.     turtle.digDown()
  61.     turtle.select(chestSlot)
  62.     turtle.placeDown()
  63.     for i = 1,max do
  64.         turtle.select(i)
  65.         turtle.dropDown()
  66.     end
  67. end
  68. --End dropoff function
  69.  
  70. --Begin dig function
  71. local function dig()
  72.     while turtle.detect() do
  73.         turtle.dig()
  74.     end
  75. end
  76. --End dig function
  77.  
  78. --Begin up function
  79. function up(num)
  80.     curUp = 1
  81.     if num == nil then
  82.         num = 1
  83.     end
  84.     while curUp <= num do
  85.         if not turtle.up() then
  86.             turtle.digUp()
  87.             turtle.attackUp()
  88.         else
  89.             curUp = curUp+1
  90.         end
  91.     end
  92. end
  93. --End up function
  94.  
  95. --Begin forward function
  96. function forward(num)
  97.     curForw = 1
  98.     if num == nil then
  99.         num = 1
  100.     end
  101.     while curForw <= num do
  102.         if not turtle.forward() then
  103.             turtle.dig()
  104.             turtle.attack()
  105.         else
  106.             curForw = curForw+1
  107.         end
  108.     end
  109. end
  110. --End forward function
  111.  
  112. --Begin down function
  113. function down(num)
  114.     curDown = 1
  115.     if num == nil then
  116.         num = 1
  117.     end
  118.     while curDown <= num do
  119.         if not turtle.down() then
  120.             turtle.digDown()
  121.             turtle.attackDown()
  122.         else
  123.             curDown = curDown+1
  124.         end
  125.     end
  126. end
  127. --End down function
  128.  
  129. --Begin the tryRefuel function
  130. local function tryRefuel()
  131.     for i = 1,max do
  132.         if turtle.getItemCount(i) > 0 then
  133.             turtle.select(i)
  134.             if turtle.refuel(1) then
  135.                 return true
  136.             end
  137.         end
  138.     end
  139.     turtle.select(1)
  140.     return false
  141. end
  142. --End the tryRefuel function
  143.  
  144. --Begin refuel function
  145. local function refuel()
  146.     curFuel = turtle.getFuelLevel()
  147.     if curFuel == "unlimited" or curFuel >= 10 then
  148.         return
  149.     else
  150.         if not tryRefuel() then
  151.             print("Please supply more fuel to coninue...")
  152.             while not tryRefuel() do
  153.                 sleep(1)
  154.             end
  155.         end
  156.     end
  157. end
  158. --End refuel function
  159.  
  160. --Begin tunnel function
  161. local function tunnel(num)
  162.     if useTorches == "Y" or useTorches == "y" then
  163.         turtle.digDown()
  164.         turtle.select(torchSlot)
  165.         turtle.placeDown()
  166.         turtle.select(1)
  167.     end
  168.     torch = 1
  169.     for i = 1,num do
  170.         print("Current tunnel length: ",curLength-1)
  171.         refuel()
  172.         turtle.turnLeft()
  173.         dig()
  174.         up()
  175.         dig()
  176.         turtle.turnRight()
  177.         turtle.turnRight()
  178.         dig()
  179.         down()
  180.         dig()
  181.         turtle.turnLeft()
  182.         refuel()
  183.         if useChests == "Y" or useChests == "y" then
  184.             if turtle.getItemCount(max) > 0 then
  185.                 dropOff()
  186.             end
  187.         end
  188.         turtle.select(1)
  189.         turtle.dig()
  190.         forward()
  191.         curLength = curLength+1
  192.         if useTorches == "Y" or useTorches == "y" then
  193.             if torch == 9 then
  194.                 turtle.digDown()
  195.                 turtle.select(torchSlot)
  196.                 turtle.placeDown()
  197.                 turtle.select(1)
  198.                 torch = 1
  199.             else
  200.                 torch = torch+1
  201.             end
  202.         end
  203.     end
  204. end
  205. --End tunnel function
  206.  
  207.  
  208. --Begin Main code
  209. rednet.open("right")
  210. --Declare Variables
  211. max = 16
  212. invalid = true
  213. torchSlot = 15
  214. chestSlot = 16
  215. curLength = 1
  216. useSettings = false
  217. torches = turtle.getItemCount(torchSlot)
  218. chests = turtle.getItemCount(chestSlot)
  219.  
  220. term.clear()
  221. term.setCursorPos(1,1)
  222. print("Better Tunnel v1.0")
  223. sleep(2.5)
  224.  
  225. --If a settings file exists, ask the user if they want to load it
  226. if fs.exists("btSettings") then
  227.     repeat
  228.         term.clear()
  229.         term.setCursorPos(1,1)
  230.         print("A settings file was found.")
  231.         print("Would you like to load it?(Y/N): ")
  232.         load = read()
  233.         if load == "Y" or load == "y" then
  234.             useSettings = true
  235.             invalid = false
  236.         elseif load == "N" or load == "n" then
  237.             useSettings = false
  238.             invalid = false
  239.         else
  240.             invalid = true
  241.             print("\nInvalid")
  242.             sleep(2)
  243.         end
  244.     until invalid == false
  245. else
  246.     useSettings = false
  247. end
  248.  
  249. --If they chose to load it, it will load the setting from the file
  250. if useSettings == true then
  251.     h = fs.open("btSettings", "r")
  252.     comment1 = h.readLine()
  253.     comment2 = h.readLine()
  254.     orders = h.readLine()
  255.     useChests = h.readLine()
  256.     useTorches = h.readLine()
  257.     compID = h.readLine()
  258.     h.close()
  259.     max = 16
  260.     runChest()
  261.     runTorch()
  262. else--If the file didn't exist or the user chose not to load it, the program will ask the user for settings
  263.  
  264.     --Begin the orders loop
  265.     repeat
  266.         term.clear()
  267.         term.setCursorPos(1,1)
  268.         write("Will I be receiving orders via User or Rednet?(U/R): ")
  269.         orders = read()
  270.         vType = type(orders)
  271.  
  272.         if vType == "string" then
  273.             if orders ~= "U" and orders ~= "u" and  orders ~= "R" and orders ~= "r" then
  274.                 print("\nInvalid")
  275.                 sleep(2)
  276.                 invalid = true
  277.             else
  278.                 invalid = false
  279.             end
  280.         else
  281.             print("\nInvalid")
  282.             sleep(2)
  283.             invalid = true
  284.         end
  285.     until invalid == false
  286.     --End the orders loop
  287.  
  288.     --If Rednet was chosen ask the user what ID to be listening for
  289.     if orders == "R" or orders == "r" then
  290.         print("\nTo receive using rednet, use rednet.send() with the message of how long the tunnel should be in blocks.")
  291.         write("\nWhich computer ID should I accept messages from?: ")
  292.         compID = tonumber(read())
  293.     end
  294.  
  295.     --Begin the useChests loop
  296.     repeat
  297.         term.clear()
  298.         term.setCursorPos(1,1)
  299.         write("Should I use chests?(Y/N): ")
  300.         useChests = read()
  301.         vType = type(useChests)
  302.  
  303.         if vType == "string" then
  304.             runChest()
  305.         else
  306.             continue = false
  307.             print("\nInvalid")
  308.             sleep(2)
  309.         end
  310.     until continue == true
  311.     --End the useChests loop
  312.  
  313.  
  314.     continue = false
  315.     --Begin the useTorches loop
  316.     repeat
  317.         term.clear()
  318.         term.setCursorPos(1,1)
  319.         write("Should I use torches?(Y/N): ")
  320.         useTorches = read()
  321.         vType = type(useTorches)
  322.  
  323.         if vType == "string" then
  324.             runTorch()
  325.         else
  326.             continue = false
  327.             print("\nInvalid")
  328.             sleep(2)
  329.         end
  330.     until continue == true
  331.     --End the useTorches loop
  332. end
  333.  
  334. --Run the tunnel function based on the orders chosen
  335. if orders == "R" or orders == "r" then
  336.     term.clear()
  337.     term.setCursorPos(1,1)
  338.     print("You chose to receive orders via Rednet.")
  339.     print("I will listen for orders from the computer with id: "..compID)
  340.     id, msg = rednet.receive()
  341.     if id == tonumber(compID) then
  342.         length = msg
  343.         print("Beginning tunnel with length: "..length)
  344.         tunnel(length)
  345.     end
  346. elseif orders == "U" or orders == "u" then
  347.     repeat
  348.         term.clear()
  349.         term.setCursorPos(1,1)
  350.         print("You chose to receive orders via User.")
  351.         write("How long would you like the tunnel to be?(In blocks): ")
  352.         num = tonumber(read())
  353.         vType = type(num)
  354.         if vType == "number" then
  355.             length = num
  356.             print("Beginning tunnel with length: "..length)
  357.             invalid = false
  358.             tunnel(length)
  359.         else
  360.             print("\nInvalid")
  361.             sleep(2)
  362.             invalid = true
  363.         end
  364.     until invalid == false
  365. end
  366.  
  367. term.clear()
  368. term.setCursorPos(1,1)
  369. print("I'm done!")
  370. print("I completed a tunnel with length: "..length)
  371.  
  372. if not useSettings then
  373.     print("Would you like to save these settings for use in the next tunnel?(Y/N): ")
  374.     save = read()
  375.     if save == "Y" or save == "y" then
  376.         h = fs.open("btSettings","w")
  377.         h.writeLine("--Better Tunnel Settings")
  378.         h.writeLine("--Deleting will reset settings")
  379.         h.close()
  380.         h = fs.open("btSettings","a")
  381.         h.writeLine(orders)
  382.         h.writeLine(useChests)
  383.         h.writeLine(useTorches)
  384.         h.writeLine(compID)
  385.         h.close()
  386.         print("Settings have been saved")
  387.         sleep(2)
  388.     end
  389. end
Advertisement
Add Comment
Please, Sign In to add comment