Advertisement
Guest User

autoTunnel

a guest
Jul 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.72 KB | None | 0 0
  1. local h -- Holds file to be manipulated
  2. local input
  3. local success, data -- Turtle inspect values
  4. local stuck = false
  5.  
  6. local running = true
  7. local timeout = 2
  8.  
  9. local constantIdentifiers
  10. = {"Origin X", "Origin Y", "Origin Z", "Origin Side (0 to 3)", "Origin Next to Entrance (0 or 1)", "Signal Block (mod:blockname)"}
  11.  
  12. local originX
  13. local originY
  14. local originZ
  15. local originSide
  16. local originNextToEntrance
  17. local signalBlock
  18.  
  19. local checkpointX
  20. local checkpointY
  21. local checkpointZ
  22. local checkpointFacing
  23.  
  24. local x, y, z
  25. local facing -- 0 to 3 (north to west)
  26. local blocksTravelled -- From origin
  27. local allInventorySlotsUsed
  28.  
  29. local function setConstants()
  30.     h = fs.open("constants", "w")
  31.     print("Enter constants:")
  32.     for i = 1, #constantIdentifiers do
  33.         write("    ")
  34.         write(constantIdentifiers[i])
  35.         write(" = ")
  36.         input = read()
  37.         h.writeLine(input)
  38.     end
  39.     h.close()        
  40. end
  41.  
  42. local function setState(values)
  43.     h = fs.open("state", "w")
  44.     for i = 1, 4 do
  45.         h.writeLine(values[i])
  46.     end
  47.     h.close()        
  48. end
  49.  
  50. local function readConstants()
  51.     h = fs.open("constants", "r")
  52.     originX = tonumber(h.readLine())
  53.     originY = tonumber(h.readLine())
  54.     originZ = tonumber(h.readLine())
  55.     originSide = tonumber(h.readLine())
  56.     originNextToEntrance = tonumber(h.readLine())
  57.     signalBlock = h.readLine()
  58.     h.close()            
  59. end
  60.  
  61. local function readState()
  62.     h = fs.open("state", "r")
  63.     checkpointX = tonumber(h.readLine())
  64.     checkpointY = tonumber(h.readLine())
  65.     checkpointZ = tonumber(h.readLine())
  66.     checkpointFacing = tonumber(h.readLine())
  67.     h.close()    
  68. end
  69.  
  70. local function refuel()
  71.     for i = 1, 16 do
  72.         turtle.select(i)
  73.         turtle.refuel()
  74.     end
  75. end
  76.  
  77. local function getFacing()
  78.     local xOld, yOld, zOld
  79.     repeat
  80.         xOld, yOld, zOld = gps.locate(timeout)
  81.     until xOld and yOld and zOld
  82.     repeat
  83.         turtle.dig()
  84.         stuck = not turtle.forward()
  85.     until not stuck
  86.     repeat
  87.         x, y, z = gps.locate(timeout)
  88.     until x and y and z
  89.     turtle.back()
  90.     if z < zOld then
  91.         return 0
  92.     elseif z > zOld then
  93.         return 2
  94.     elseif x < xOld then
  95.         return 3
  96.     elseif x > xOld then
  97.         return 1
  98.     end                
  99. end
  100.  
  101. local function trackTurnLeft()
  102.     turtle.turnLeft()
  103.     facing = (facing - 1) % 4  
  104. end
  105. local function trackTurnRight()
  106.     turtle.turnRight()
  107.     facing = (facing + 1) % 4
  108. end
  109.  
  110. local function faceDirection(target)
  111.     if facing == (target + 1) % 4 then
  112.         trackTurnLeft()
  113.     else
  114.         while facing ~= target do
  115.             trackTurnRight()
  116.         end
  117.     end
  118. end
  119.  
  120. local function goTo(targetX, targetY, targetZ, targetDirection)
  121.     repeat
  122.         x, y, z = gps.locate(timeout)
  123.     until x and y and z
  124.    
  125.     if z > targetZ then
  126.         faceDirection(0)
  127.         repeat
  128.             turtle.dig()
  129.             turtle.forward()
  130.             repeat
  131.                 x, y, z = gps.locate(timeout)
  132.             until x and y and z
  133.         until z == targetZ        
  134.     elseif z < targetZ then
  135.         faceDirection(2)
  136.         repeat
  137.             turtle.dig()
  138.             turtle.forward()
  139.             repeat
  140.                 x, y, z = gps.locate(timeout)
  141.             until x and y and z
  142.         until z == targetZ
  143.     end
  144.    
  145.     if x > targetX then
  146.         faceDirection(3)
  147.         repeat
  148.             turtle.dig()
  149.             turtle.forward()
  150.             repeat
  151.                 x, y, z = gps.locate(timeout)
  152.             until x and y and z
  153.         until x == targetX
  154.     elseif x < targetX then
  155.         faceDirection(1)
  156.         repeat
  157.             turtle.dig()
  158.             turtle.forward()
  159.             repeat
  160.                 x, y, z = gps.locate(timeout)
  161.             until x and y and z
  162.         until x == targetX                      
  163.     end
  164.    
  165.     if y > targetY then
  166.         repeat
  167.             turtle.digDown()
  168.             turtle.down()
  169.             repeat
  170.                 x, y, z = gps.locate(timeout)
  171.             until x and y and z
  172.         until y == targetY
  173.     elseif y < targetY then
  174.         repeat
  175.             turtle.digUp()
  176.             turtle.up()
  177.             repeat
  178.                 x, y, z = gps.locate(timeout)
  179.             until x and y and z
  180.         until y == targetY
  181.     end
  182.    
  183.     faceDirection(targetDirection)
  184. end
  185.  
  186. local function mineOres()
  187.     for i = 1, 3 do
  188.         success, data = turtle.inspect()
  189.         if success then
  190.             if string.find(string.lower(data.name), "ore") then
  191.                 turtle.dig()
  192.                 turtle.forward()
  193.                 mineOres()
  194.                 turtle.back()
  195.             end
  196.         end
  197.        
  198.         if i == 2 then
  199.             trackTurnRight()
  200.         end
  201.         trackTurnRight()
  202.     end
  203.    
  204.     success, data = turtle.inspectUp()
  205.     if success then
  206.         if string.find(string.lower(data.name), "ore") then
  207.             turtle.digUp()
  208.             turtle.up()
  209.             mineOres()
  210.             turtle.down()
  211.         end
  212.     end
  213.    
  214.     success, data = turtle.inspectDown()
  215.     if success then
  216.         if string.find(string.lower(data.name), "ore") then
  217.             turtle.digDown()
  218.             turtle.down()
  219.             mineOres()
  220.             turtle.up()
  221.         end
  222.     end
  223. end
  224.  
  225. if not fs.exists("constants") then
  226.     setConstants()
  227. end
  228. if not fs.exists("state") then
  229.     setState({"nil", "nil", "nil", "nil"})
  230. end
  231. readConstants()
  232. readState()
  233.  
  234. refuel()
  235.  
  236. facing = getFacing()
  237. if checkpointX ~= nil and checkpointY ~= nil and checkpointZ ~= nil and checkpointFacing ~= nil then
  238.     goTo(checkpointX, checkpointY, checkpointZ, checkpointFacing)
  239. end
  240.  
  241. while running do
  242.     repeat
  243.         x, y, z = gps.locate(timeout)
  244.     until x and y and z
  245.     setState({tostring(x), tostring(y), tostring(z), tostring(facing)})
  246.  
  247.     blocksTravelled = math.abs(x - originX) + math.abs(y - originY) + math.abs(z - originX)
  248.     allInventorySlotsUsed = true
  249.     for i = 1, 16 do
  250.         turtle.select(i)
  251.         if turtle.getItemCount() == 0 then
  252.             allInventorySlotsUsed = false
  253.         end
  254.     end
  255.    
  256.     if blocksTravelled >= turtle.getFuelLevel() + 10 or allInventorySlotsUsed then
  257.         if originNextToEntrance == 0 then
  258.             if facing == (originSide + 1) % 4 then
  259.                 trackTurnRight()
  260.                 trackTurnRight()
  261.             end
  262.         elseif originNextToEntrance == 1 then
  263.             if facing == (originSide - 1) % 4 then
  264.                 trackTurnRight()
  265.                 trackTurnRight()
  266.             end
  267.         end
  268.     end
  269.    
  270.     success, data = turtle.inspectDown()
  271.     if data.name == signalBlock then
  272.         if facing == originSide then
  273.             if originNextToEntrance == 0 then
  274.                 trackTurnRight()
  275.             elseif originNextToEntrance == 1 then
  276.                 trackTurnLeft()
  277.             end
  278.         elseif facing == (originSide + 2) % 4 then
  279.             if originNextToEntrance == 0 then
  280.                 trackTurnLeft()
  281.             elseif originNextToEntrance == 1 then
  282.                 trackTurnRight()
  283.             end
  284.         end
  285.         turtle.select(1)
  286.         turtle.digDown()
  287.     else
  288.         x, y, z = gps.locate(timeout)
  289.         success, data = turtle.inspectUp()
  290.         if originSide == 0 then
  291.             if data.name == signalBlock then
  292.                 if z > originZ then
  293.                     if originNextToEntrance == 0 then
  294.                         trackTurnRight()
  295.                     elseif originNextToEntrance == 1 then
  296.                         trackTurnLeft()
  297.                     end
  298.                 elseif z < originZ then
  299.                     if originNextToEntrance == 0 then
  300.                         trackTurnLeft()
  301.                     elseif originNextToEntrance == 1 then
  302.                         trackTurnRight()
  303.                     end
  304.                 end
  305.                 turtle.select(1)
  306.                 turtle.placeDown()                                    
  307.             end
  308.         elseif originSide == 2 then
  309.             if data.name == signalBlock then
  310.                 if z > originZ then
  311.                     if originNextToEntrance == 0 then
  312.                         trackTurnLeft()
  313.                     elseif originNextToEntrance == 1 then
  314.                         trackTurnRight()
  315.                     end      
  316.                 elseif z < originZ then
  317.                     if originNextToEntrance == 0 then
  318.                         trackTurnRight()
  319.                     elseif originNextToEntrance == 1 then
  320.                         trackTurnLeft()
  321.                     end
  322.                 end
  323.                 turtle.select(1)
  324.                 turtle.placeDown()
  325.             end    
  326.         elseif originSide == 1 then
  327.             if data.name == signalBlock then
  328.                 if x > originX then
  329.                     if originNextToEntrance == 0 then
  330.                         trackTurnLeft()
  331.                     elseif originNextToEntrance == 1 then
  332.                         trackTurnRight()
  333.                     end        
  334.                 elseif x < originX then
  335.                     if originNextToEntrance == 0 then
  336.                         trackTurnRight()
  337.                     elseif originNextToEntrance == 1 then
  338.                         trackTurnLeft()
  339.                     end
  340.                 end
  341.                 turtle.select(1)
  342.                 turtle.placeDown()
  343.             end
  344.         elseif originSide == 3 then
  345.             if data.name == signalBlock then
  346.                 if x > originX then
  347.                     if originNextToEntrance == 0 then
  348.                         trackTurnRight()
  349.                     elseif originNextToEntrance == 1 then
  350.                         trackTurnLeft()
  351.                     end
  352.                 elseif x < originX then
  353.                     if originNextToEntrance == 0 then
  354.                         trackTurnLeft()
  355.                     elseif originNextToEntrance == 1 then
  356.                         trackTurnRight()
  357.                     end
  358.                 end
  359.                 turtle.select(1)
  360.                 turtle.placeDown()    
  361.             end
  362.         end
  363.     end
  364.    
  365.     success, data = turtle.inspect()
  366.     if data.name == signalBlock then
  367.         trackTurnRight()
  368.         trackTurnRight()
  369.     end
  370.    
  371.     repeat
  372.         x, y, z = gps.locate(timeout)
  373.     until x and y and z
  374.    
  375.     setState({tostring(x), tostring(y), tostring(z), tostring(facing)})
  376.     mineOres()
  377.    
  378.     success, data = turtle.inspectDown()
  379.     if data.name == "minecraft:chest" then
  380.         for i = 1, 16 do
  381.             turtle.select(i)
  382.             turtle.dropDown()
  383.         end    
  384.     end
  385.        
  386.     turtle.dig()
  387.     turtle.forward()
  388. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement