coaster3000

3x3 Tunnel

May 27th, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.93 KB | None | 0 0
  1. --[[
  2.     3x3
  3.     By Coaster3000
  4.  
  5.     3x3 is licensed under the Creative Commons Attribution-ShareAlike 3.0 United States License.
  6.     To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/.
  7.    
  8.     Extra Permissions: You can freely extract the api for turtle movement tracking. Do note...
  9.     You are required to follow iNav's Licensing. As this part of code is based off of it.
  10.     This is simply a remake of it.
  11.  
  12. -- Link: http://pastebin.com/3KHq3K2R
  13. -- Maker of iNav dd4235
  14. ]]
  15.  
  16. local x,z,y = 0,0,0
  17. local dir = 0
  18.  
  19. local count = 0
  20.  
  21. local function dig()
  22.     while turtle.dig() do
  23.         count = count + 1
  24.         sleep(0.5)
  25.     end
  26.     return true
  27. end
  28. local function digUp()
  29.     while turtle.digUp() do
  30.         count = count + 1
  31.         sleep(0.5)
  32.     end
  33.     return true
  34. end
  35. local function digDown()
  36.     while turtle.digDown() do
  37.         count = count + 1
  38.         sleep(0.5)
  39.     end
  40.     return true
  41. end
  42.  
  43. local function hasFuel( ammount )
  44.     ammount = ammount or 1
  45.     return turtle.getFuelLevel() >= ammount
  46. end
  47.  
  48. local function refuel( ammount )
  49.     ammount = ammount or (turtle.getFuelLevel() + 1)
  50.     if turtle.getFuelLevel() > ammount then
  51.         return true
  52.     end
  53.     for i=1,16 do
  54.         turtle.select(i)
  55.         while turtle.getItemCount(i) > 0 do
  56.             if not turtle.refuel(0) then
  57.                 break
  58.             end
  59.             if hasFuel(ammount) then
  60.                 turtle.select(1)
  61.                 return true
  62.             else
  63.                 turtle.refuel(1)
  64.             end
  65.         end
  66.     end
  67.     turtle.select(1)
  68.     return false
  69. end
  70.  
  71. local function forward()
  72.     local good = turtle.forward()
  73.     if good then
  74.         if dir % 4 == 0 then x = x + 1 end
  75.         if dir % 4 == 1 then z = z + 1 end
  76.         if dir % 4 == 2 then x = x - 1 end
  77.         if dir % 4 == 3 then z = z - 1 end
  78.     end
  79.     return good
  80. end
  81.  
  82. local function back()
  83.     local good = turtle.back()
  84.     if good then
  85.         if dir % 4 == 0 then x = x - 1 end
  86.         if dir % 4 == 1 then z = z - 1 end
  87.         if dir % 4 == 2 then x = x + 1 end
  88.         if dir % 4 == 3 then z = z + 1 end
  89.     end
  90.     return good
  91. end
  92.  
  93.  
  94. local function turnRight()
  95.     local good = turtle.turnRight()
  96.     if good then
  97.         dir = dir + 1
  98.     end
  99.     return good
  100. end
  101. local function turnAround()
  102.     turnRight()
  103.     turnRight()
  104. end
  105.  
  106. local function turnLeft()
  107.     local good = turtle.turnLeft()
  108.     if good then
  109.         dir = dir - 1
  110.     end
  111.     return good
  112. end
  113. local function up()
  114.     local good = turtle.up()
  115.     if good then
  116.         y = y + 1
  117.     end
  118.     return good
  119. end
  120.  
  121. local function down()
  122.     local good = turtle.down()
  123.     if good then
  124.         y = y - 1
  125.     end
  126.     return good
  127. end
  128.  
  129. length = nil
  130. local args = {...}
  131. lastx = 0
  132.  
  133. function resume()
  134.     while (x < lastx) do
  135.         if not forward() then
  136.             break
  137.         end
  138.     end
  139. end
  140.  
  141. if #args > 0 then
  142.     length = tonumber(args[1]) or nil
  143.     if #args > 1 then
  144.         lastx = tonumber(args[2]) or 0
  145.         resume()
  146.     end
  147. end
  148.  
  149. local function layer()
  150.     turnLeft()
  151.     dig()
  152.     turnAround()
  153.     dig()
  154.     turnLeft()
  155. end
  156. fuelNeeded = 0
  157.  
  158. function checkLength()
  159.     while not length do
  160.         term.clear()
  161.         term.setCursorPos(1, 1)
  162.         write("Enter Length: ")
  163.         local d = tonumber(read())
  164.         if (d) and (d > 0) then
  165.             length = d
  166.         else
  167.             print("Invalid Input... Please type a number...")
  168.             sleep(3)
  169.         end
  170.     end
  171.      fuelNeeded = 2 + length * 2 + (length * 4) -- Formula is expanded to show those not soo good at math what calculation is
  172.      if lastx then
  173.         fuelNeeded = fuelNeeded + (lastx * 2)
  174.     end
  175.     while not refuel(fuelNeeded) do
  176.         term.clear()
  177.         term.setCursorPos(1, 1)
  178.         print("Turtle needs more fuel.")
  179.         print(turtle.getFuelLevel().."/"..fuelNeeded)
  180.         print("Press any key to continue refueling")
  181.         os.pullEvent("key")
  182.     end
  183. end
  184.  
  185. function run()
  186.  
  187.     checkLength()
  188.     for i=1,length do
  189.         up()
  190.         up()
  191.         dig()
  192.         forward()
  193.         layer()
  194.         -- Bottom Layer
  195.         digDown()
  196.         down()
  197.         layer()
  198.         -- Mid Layer
  199.         digDown()
  200.         down()
  201.         layer()
  202.     end
  203.     lastx = x
  204.  
  205.     while x > 0 do
  206.         while dir % 4 ~= 0 do
  207.             turnRight()
  208.         end
  209.         back()
  210.     end
  211.     endscreen()
  212. end
  213.  
  214.  
  215. function endscreen()
  216.     term.clear()
  217.     term.setCursorPos(1, 1)
  218.     print("Total Blocks Dug: "..count)
  219.     print("Current Fuel Level: "..turtle.getFuelLevel())
  220.     write("Resume? (Y/N): ")
  221.     length = nil
  222.     local b = read()
  223.     if (b == "y") or (b =="Y") then
  224.         checkLength()
  225.         resume()
  226.         run()
  227.     else
  228.         term.clear()
  229.         term.setCursorPos(1, 1)
  230.     end
  231. end
  232. checkLength()
  233. if lastx then
  234.     resume()
  235. end
  236. run()
Advertisement
Add Comment
Please, Sign In to add comment