Taminaminam

Computercraft turtle custom tunnel dimensions v2.x

Feb 2nd, 2022 (edited)
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.67 KB | None | 0 0
  1. step = 0
  2. distance_travelled = 0
  3. abort = false
  4. facing_direction = 0
  5. fast = true
  6.  
  7. tunnel_width = 1
  8. tunnel_height = 1
  9.  
  10. DIRECTIONS = {
  11.     FORWARDS = 0,
  12.     LEFT = -1,
  13.     BACKWARDS = -2,
  14.     RIGHT = 1
  15. }
  16.  
  17. function text()
  18.  
  19.     shell.run("clear")
  20.     print("fuel level: ".. turtle.getFuelLevel())
  21.     io.write "turtle will stop when this number reaches zero: "
  22.     io.write (tunnel_length)
  23.     print ""
  24.     print ""
  25.     print "or when the last slot in it's inventory is in use."
  26.  
  27. end
  28.  
  29.  
  30. function Return()
  31.  
  32.     repeat
  33.         turtle.back()
  34.         distance_travelled = distance_travelled-1
  35.     until distance_travelled == 0
  36.  
  37. end
  38.  
  39.  
  40.  
  41. function refuel()
  42.  
  43.     if turtle.getFuelLevel() < 3 then
  44.         for i = 1,16 do
  45.             if turtle.getItemCount(i) > 0 then
  46.                 turtle.select(i)
  47.                 if turtle.refuel(1) then
  48.                     break
  49.                 end
  50.             end
  51.         end
  52.         turtle.select(1)
  53.     end
  54. end
  55.  
  56.  
  57. function abortifinventoryfull()
  58.    
  59.     --if inventory full, skip to step 4 and set remaining tunnel length to 1
  60.     if turtle.getItemCount(16) ~= 0 then   
  61.             tunnel_length = 1   --set remaining tunnel length to 1
  62.             step = 4            --skip to step 4
  63.             abort = true
  64.         end
  65.        
  66. end
  67.  
  68.  
  69. function dig_until_no_blocks_detected_forwards()
  70.  
  71.     -- For gravel or sand, keep diggin in front of the turtle
  72.     -- until there are no more blocks or abort everything if inventory full
  73.     while turtle.detect() == true and not abort do
  74.         turtle.dig()            --dig infront of turtle
  75.         sleep(0.4)              --wait for blocks to fall  
  76.         abortifinventoryfull()  --abort tunnel if inventory is full
  77.     end
  78. end
  79.  
  80. function dig_until_no_blocks_detected_upwards()
  81.  
  82.     -- Dig upwards until there are no more blocks ontop of the turtle or inventory full
  83.     if turtle.detectUp() == true and not abort then
  84.         while turtle.detectUp() == true do  --dig ontop of turtle until there are no more blocks
  85.             turtle.digUp()          --dig ontop of the turtle
  86.             sleep(0.4)              --wait for blocks to fall
  87.             abortifinventoryfull()  --abort tunnel if inventory is full
  88.         end
  89.     end
  90.  
  91. end
  92.  
  93. function dig_until_no_blocks_detected_downwards()
  94.  
  95.     -- Dig upwards until there are no more blocks below the turtle or inventory full
  96.     if turtle.detectDown() == true and not abort then
  97.         while turtle.detectDown() == true do    --dig below turtle until there are no more blocks
  98.             turtle.digDown()        --dig below turtle
  99.             sleep(0.4)              --wait for blocks to fall
  100.             abortifinventoryfull()  --abort tunnel if inventory is full
  101.         end
  102.     end
  103.  
  104. end
  105.  
  106. function digtopandbottom()
  107.    
  108.     --dig all the blocks ontop and below the turtle
  109.     dig_until_no_blocks_detected_upwards()
  110.    
  111.     dig_until_no_blocks_detected_downwards()
  112.    
  113. end
  114.  
  115. function turn_around()
  116.  
  117.     turtle.turnLeft()
  118.     turtle.turnLeft()
  119.  
  120. end
  121.  
  122. function turn_to_direction(dir)
  123.     --  0 = forwards
  124.     -- -1 = left
  125.     -- +1 = right
  126.     -- -2 = backwards
  127.    
  128.     --TODO: implement smarter logic
  129.  
  130.     -- takes the most efficient turning direction to turn to a direction
  131.     if dir == DIRECTIONS.FORWARDS then
  132.         if facing_direction == DIRECTIONS.LEFT then
  133.             turtle.turnRight()
  134.         elseif facing_direction == DIRECTIONS.RIGHT then
  135.             turtle.turnLeft()
  136.         elseif facing_direction == DIRECTIONS.BACKWARDS then
  137.             turn_around()
  138.         end
  139.         facing_direction = DIRECTIONS.FORWARDS
  140.     elseif dir == DIRECTIONS.LEFT then
  141.         if facing_direction == DIRECTIONS.FORWARDS then
  142.             turtle.turnLeft()
  143.         elseif facing_direction == DIRECTIONS.RIGHT then
  144.             turn_around()
  145.         elseif facing_direction == DIRECTIONS.BACKWARDS then
  146.             turtle.turnRight()
  147.         end
  148.         facing_direction = DIRECTIONS.LEFT
  149.     elseif dir == DIRECTIONS.RIGHT then
  150.         if facing_direction == DIRECTIONS.FORWARDS then
  151.             turtle.turnRight()
  152.         elseif facing_direction == DIRECTIONS.LEFT then
  153.             turn_around()
  154.         elseif facing_direction == DIRECTIONS.BACKWARDS then
  155.             turtle.turnLeft()
  156.         end
  157.         facing_direction = DIRECTIONS.RIGHT
  158.     elseif dir == DIRECTIONS.BACKWARDS then
  159.         if facing_direction == DIRECTIONS.FORWARDS then
  160.             turn_around()
  161.         elseif facing_direction == DIRECTIONS.LEFT then
  162.             turtle.turnLeft()
  163.         elseif facing_direction == DIRECTIONS.RIGHT then
  164.             turtle.turnRight()
  165.         end
  166.         facing_direction = DIRECTIONS.BACKWARDS
  167.     end
  168.  
  169. end
  170.  
  171.  
  172. function dig_along_height_upwards()
  173.     for h = 1, tunnel_height, 1 do
  174.         dig_until_no_blocks_detected_upwards()
  175.         turtle.up()
  176.         sleep(0.4)
  177.     end
  178. end
  179.  
  180.  
  181. function dig_along_height_downwards()
  182.     for h = 1, tunnel_height, 1 do
  183.         dig_until_no_blocks_detected_downwards()
  184.         turtle.down()
  185.         sleep(0.4)
  186.     end
  187. end
  188.  
  189.  
  190. function dig_along_height()
  191.     refuel()
  192.  
  193.     dig_along_height_upwards()
  194.     dig_along_height_downwards()
  195.     dig_until_no_blocks_detected_downwards()
  196.     turtle.down()
  197.     sleep(0.4)
  198.  
  199.     dig_along_height_downwards()
  200.     dig_along_height_upwards()
  201.     dig_until_no_blocks_detected_upwards()
  202.     turtle.up()
  203.     sleep(0.4)
  204. end
  205.  
  206. function dig_along_width()
  207.     refuel()
  208.  
  209.     for w = 1, tunnel_width, 1 do
  210.         dig_until_no_blocks_detected_forwards()
  211.         turtle.forward()
  212.         sleep(0.4)
  213.         dig_along_height()
  214.     end
  215. end
  216.  
  217.  
  218. function return_to_center_fast()
  219.     --go back to center by running backwards the entire width
  220.     for w = 1, tunnel_width+1, 1 do
  221.         turtle.back()
  222.         sleep(0.4)
  223.     end
  224. end
  225.  
  226.  
  227. function return_to_center(turn_to_dir)
  228.     --returns the turtle to center fast or by digging half of the plane back
  229.  
  230.     if fast then
  231.         return_to_center_fast() --runs backwards the entire width
  232.     else
  233.         turn_to_direction(turn_to_dir)
  234.         dig_along_width()   --digs the entire height top and bottom along the way back
  235.         dig_until_no_blocks_detected_forwards()
  236.         turtle.forward()
  237.         sleep(0.4)
  238.         dig_along_height()
  239.     end
  240. end
  241.  
  242.  
  243. function dig_plane()
  244.    
  245.     dig_along_height()
  246.  
  247.     -- go left for entire width and dig up and down height
  248.     turn_to_direction(DIRECTIONS.LEFT)
  249.     dig_along_width()
  250.  
  251.     --return to center
  252.     return_to_center(DIRECTIONS.RIGHT)
  253.    
  254.    
  255.     -- go right for entire width and dig up and down height
  256.     turn_to_direction(DIRECTIONS.RIGHT)
  257.     dig_along_width()
  258.  
  259.     --return to center
  260.     return_to_center(DIRECTIONS.LEFT)
  261.  
  262.     turn_to_direction(DIRECTIONS.FORWARDS)
  263.  
  264. end
  265.  
  266.  
  267. function read_number()
  268.     temp = io.read()
  269.     return tonumber(temp)
  270. end
  271.  
  272.  
  273. function start()
  274.  
  275.     shell.run("clear")
  276.     io.write "how long do you want the tunnel: "
  277.     tunnel_length = read_number()
  278.     io.write "how wide (radius) do you want the tunnel: "
  279.     tunnel_width = read_number()
  280.     io.write "how high (radius) do you want the tunnel: "
  281.     tunnel_height = read_number()
  282.     io.write "should the fast version be used? (y/n)"
  283.     answer = io.read()
  284.     if string.lower(answer) == "y" then
  285.         fast = true
  286.     elseif string.lower(answer) == "n" then
  287.         fast = false
  288.     else
  289.         io.write "not a valid answer, defaulting to \"y\""
  290.         fast = true
  291.     end
  292.  
  293.     dig_until_no_blocks_detected_forwards()
  294.     turtle.forward()
  295.     shell.run("clear")
  296.  
  297. end
  298.  
  299. -- NO MORE FUNCTIONS :D
  300.  
  301. function main()
  302.  
  303.     start()
  304.     while true do
  305.         text()
  306.         dig_plane()
  307.         dig_until_no_blocks_detected_forwards()
  308.         turtle.forward()
  309.         tunnel_length = tunnel_length - 1
  310.         distance_travelled = distance_travelled + 1
  311.         if tunnel_length == 0 or abort then
  312.             break
  313.         end
  314.  
  315.     end
  316.     turn_to_direction(DIRECTIONS.FORWARDS)
  317.     Return()
  318.     text()
  319.     sleep(2)
  320.     shell.run("clear")
  321.     print "DONE!"
  322.     print "or out of inventory..."
  323. end
  324.  
  325. main()
Add Comment
Please, Sign In to add comment