WineCraftHD

TurtleScript

Dec 16th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.42 KB | None | 0 0
  1. local IgnoreSlots = 2
  2. local fuelIndex = 2
  3.  
  4. local TunnelLength = 120
  5.  
  6. -- Set this to false if you don't have a working GPS system
  7. local UseGPS = false
  8. --ALL THIS MUST BE FILLED IN IF YOU DON"T USE GPS
  9. local StartX = 697
  10. local StartY = 125
  11. local StartZ = 1396
  12. -- EX: "SOUTH", "NORTH", "EAST", "WEST"
  13. local ForwardFacingDirection = "WEST"
  14. --END NO GPS FILL INFORMATION
  15.  
  16.  
  17.  
  18. --DO NOT TOUCH ANYTHING BELOW THIS LINE
  19. local mov = {}
  20. local rot = {}
  21. local dig = {}
  22. local PositionInBranchMine = 0
  23. local RotatePosition = "FORWARD"
  24.  
  25. local InsertedIntoLeftWall = false
  26. local InsertedIntoRightWall = false
  27. local InsertionAmount = 0
  28.  
  29. local XX = StartX
  30. local YY = StartY
  31. local ZZ = StartZ
  32. local PrimaryAxis = ""
  33. local SecondaryAxis = ""
  34.  
  35. local function Refuel()
  36.         currentFuelRequired = (10 + (TunnelLength-PositionInBranchMine)*2)/20
  37.         if turtle.getFuelLevel() < currentFuelRequired then
  38.                 turtle.select(fuelIndex)
  39.                 return turtle.refuel(currentFuelRequired)
  40.         else
  41.                 return true
  42.         end
  43. end
  44.  
  45.  
  46. local function readLines(sPath)
  47.   local file = fs.open(sPath, "r") -- open the file
  48.   if file then -- check if it's open
  49.     local tLines = {} -- table to store the lines
  50.     local sLine = file.readLine() -- read a line from the file
  51.     while sLine do -- while there's a line in the file
  52.           table.insert(tLines, sLine) -- add it to the table
  53.           sLine = file.readLine() -- get the next line
  54.     end
  55.     file.close() -- close the file
  56.     return tLines -- return the table with the lines
  57.   end
  58.   return nil -- there was an error opening the file, return nil to let the user know
  59. end
  60.  
  61. local function writeLines(sPath, tLines)
  62.   local file = fs.open(sPath, "w") -- open the file
  63.   if file then -- check if the file is open
  64.     for _, sLine in ipairs(tLines) do -- for each line in the table
  65.           file.writeLine(sLine) -- write the line
  66.     end
  67.     file.close() -- close the file
  68.   end
  69. end
  70.  
  71. function filereadline(filename, line)
  72.   local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  73.   if not tLines then -- if there was an error
  74.     return nil -- return nil/error
  75.   end
  76.   return tLines[line] -- return the line
  77. end
  78.  
  79. function filewriteline(filename, line, text)
  80.   local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  81.   if tLines then -- if there was no error
  82.     tLines[line] = text -- set the line
  83.     writeLines(filename, tLines) -- write the lines back to the file (using the previous functions)
  84.   end
  85. end
  86.  
  87. local function incrementCoordinates(incX,incY,incZ)
  88. XX = XX + incX
  89. YY = YY + incY
  90. ZZ = ZZ + incZ
  91. filewriteline("BranchMineData", 7, XX)
  92. filewriteline("BranchMineData", 8, YY)
  93. filewriteline("BranchMineData", 9, ZZ)
  94. end
  95.  
  96. --directionInteger 0 = forward, 1 = left, 2 = backward, 3 = right, 4 = up, 5 = down
  97. local function updateCoordinates(directionInteger)
  98.         --Displace the direction by the current Rotation
  99.         if directionInteger <= 3 then
  100.                 if directionInteger == 0 then
  101.                         if string.match(RotatePosition,"LEFT") then
  102.                                 directionInteger = 1
  103.                         elseif string.match(RotatePosition,"BACKWARD") then
  104.                                 directionInteger = 2
  105.                         elseif string.match(RotatePosition,"RIGHT") then
  106.                                 directionInteger = 3
  107.                         end
  108.                 elseif directionInteger == 2 then
  109.                         if string.match(RotatePosition,"LEFT") then
  110.                                 directionInteger = 3
  111.                         elseif string.match(RotatePosition,"BACKWARD") then
  112.                                 directionInteger = 0
  113.                         elseif string.match(RotatePosition,"RIGHT") then
  114.                                 directionInteger = 1
  115.                         end
  116.                 end
  117.         end
  118.  
  119.         local incrementPrimary = true
  120.         local incrementBy = 0
  121.         local incrementX = 0
  122.         local incrementY = 0
  123.         local incrementZ = 0
  124.         --if we are moving forward
  125.         if directionInteger == 0 then
  126.                 incrementPrimary = true
  127.                 incrementBy = 1
  128.         --if we are moving left
  129.         elseif directionInteger == 1 then
  130.                 incrementPrimary = false
  131.                 incrementBy = 1
  132.         --if we are moving backward
  133.         elseif directionInteger == 2 then
  134.                 incrementPrimary = true
  135.                 incrementBy = -1
  136.         --if we are moving right
  137.         elseif directionInteger == 3 then
  138.                 incrementPrimary =false
  139.                 incrementBy = -1
  140.         elseif directionInteger == 4 then
  141.                 incrementY = 1
  142.         elseif directionInteger == 5 then
  143.                 incrementY = -1
  144.         end
  145.  
  146.         if incrementPrimary then
  147.                 if PrimaryAxis == "X" then
  148.                         incrementX = incrementBy
  149.                 elseif PrimaryAxis == "-X" then
  150.                         incrementX = -1 * incrementBy
  151.                 elseif PrimaryAxis == "Z" then
  152.                         incrementZ = incrementBy
  153.                 elseif PrimaryAxis == "-Z" then
  154.                         incrementZ = -1 * incrementBy
  155.                 end
  156.         else
  157.                 if SecondaryAxis == "X" then
  158.                         incrementX = incrementBy
  159.                 elseif SecondaryAxis == "-X" then
  160.                         incrementX = -1 * incrementBy
  161.                 elseif SecondaryAxis == "Z" then
  162.                         incrementZ = incrementBy
  163.                 elseif SecondaryAxis == "-Z" then
  164.                         incrementZ = -1 * incrementBy
  165.                 end
  166.         end
  167.         incrementCoordinates(incrementX,incrementY,incrementZ)
  168. end
  169.  
  170. mov.forward = function(times)
  171.         local boolean v = false
  172.         for i=1,times do
  173.                 if turtle.detect() == false then
  174.                         while not turtle.forward() do
  175.                                 Refuel()
  176.                                 sleep(1)
  177.                         end
  178.                         updateCoordinates(0)
  179.                         v = true
  180.                 else
  181.                         v = false
  182.                         return v
  183.                 end
  184.         end
  185.         return v
  186. end
  187.  
  188. mov.back = function(times)
  189.         local boolean v = false
  190.         for i=1,times do
  191.                 while not turtle.back() do
  192.                         Refuel()
  193.                         sleep(1)
  194.                 end
  195.                 updateCoordinates(2)
  196.                 v = true
  197.         end
  198.         return v
  199. end
  200.  
  201. mov.up = function(times)
  202.     print("In mov.up")
  203.         local boolean v = false
  204.         for i=1,times do
  205.                 if turtle.detectUp() == false then
  206.                         while turtle.up() == false do
  207.                                 Refuel()
  208.                                 sleep(1)
  209.                         end
  210.                         YY = YY + 1
  211.     print("Out mov.up")
  212.                         v = true
  213.                 else
  214.     print("Out mov.up")
  215.                         v = false
  216.                         return v
  217.                 end
  218.         end
  219.         return v
  220. end
  221.  
  222. mov.down = function(times)
  223.     print("In mov.down")
  224.         local boolean v = false
  225.         for i=1,times do
  226.                 if turtle.detectDown() == false then
  227.                         while turtle.down() == false do
  228.                                 Refuel()
  229.                                 sleep(1)
  230.                         end
  231.                         YY = YY - 1
  232.     print("Out mov.down")
  233.                         v = true
  234.                 else
  235.     print("Out mov.down")
  236.                         v = false
  237.                         return v
  238.                 end
  239.         end
  240.         return v
  241. end
  242.  
  243. mov.place = function()
  244.     while not turtle.place() do
  245.           sleep(1)
  246.     end
  247.     return true
  248. end
  249.  
  250. rot.right = function()
  251.     while not turtle.turnRight() do
  252.             sleep(1)
  253.     end
  254.        
  255.         if RotatePosition == "FORWARD" then
  256.                 RotatePosition = "RIGHT"
  257.         elseif RotatePosition == "RIGHT" then
  258.                 RotatePosition = "BACKWARD"
  259.         elseif RotatePosition == "BACKWARD" then
  260.                 RotatePosition = "LEFT"
  261.         elseif RotatePosition == "LEFT" then
  262.                 RotatePosition = "FORWARD"
  263.         end
  264.         filewriteline("BranchMineData", 5, RotatePosition)
  265.     return true
  266. end
  267.  
  268. rot.left = function()
  269.     while not turtle.turnLeft() do
  270.             sleep(1)
  271.     end
  272.        
  273.         if RotatePosition == "FORWARD" then
  274.                 RotatePosition = "LEFT"
  275.         elseif RotatePosition == "LEFT" then
  276.                 RotatePosition = "BACKWARD"
  277.         elseif RotatePosition == "BACKWARD" then
  278.                 RotatePosition = "RIGHT"
  279.         elseif RotatePosition == "RIGHT" then
  280.                 RotatePosition = "FORWARD"
  281.         end
  282.         filewriteline("BranchMineData", 5, RotatePosition)
  283.     return true
  284. end
  285.  
  286.  
  287. -- RotateTo is a number where 0 = Forward, 1 = Left, 2 = Backward, and 3 = Right
  288. local function look(RotateTo)
  289.         --This rids one odd occurance
  290.         if RotatePosition == "RIGHT" and RotateTo == 0 then
  291.                 rot.left()
  292.                 return
  293.         end
  294.         --
  295.  
  296.         rotateNumber = 0
  297.         if RotatePosition == "LEFT" then
  298.                 rotateNumber = 1
  299.         elseif RotatePosition == "BACKWARD" then
  300.                 rotateNumber = 2
  301.         elseif RotatePosition == "RIGHT" then
  302.                 rotateNumber = 3
  303.         end
  304.  
  305.         rotateRight = rotateNumber - RotateTo
  306.         if rotateRight >= 0 then
  307.                         for i = 1,rotateRight do
  308.                                 rot.right()
  309.                         end
  310.         else
  311.                         for i = 1,math.abs(rotateRight) do
  312.                                 rot.left()
  313.                         end
  314.         end
  315.  
  316. end
  317.  
  318. dig.forward = function()
  319.     print("In dig.forward")
  320.         if turtle.detect() then
  321.                 while not turtle.dig() do
  322.                                 sleep(1)
  323.                 end
  324.     print("Out dig.forward")
  325.                 return true
  326.         else
  327.                 print("No Block to mine forward")
  328.     print("Out dig.forward")
  329.                 return false
  330.         end
  331. end
  332.  
  333. dig.up = function()
  334.     print("In dig.up")
  335.         if turtle.detectUp() then
  336.                 while not turtle.digUp() do
  337.                                 sleep(1)
  338.                 end
  339.     print("Out dig.up")
  340.                 return true
  341.         else
  342.                 print("No Block to mine up")
  343.     print("Out dig.up")
  344.                 return false
  345.         end
  346. end
  347.  
  348. dig.down = function()
  349.     print("In dig.down")
  350.         if turtle.detectDown() then
  351.                 while not turtle.digDown() do
  352.                                 sleep(1)
  353.                 end
  354.     print("Out dig.down")
  355.                 return true
  356.         else
  357.                 print("No Block to mine down")
  358.     print("Out dig.down")
  359.                 return false
  360.         end
  361. end
  362.  
  363. local function DigMoveForward()
  364.     print("In DigMoveForward")
  365.         if mov.forward(1) == false then
  366.                 dig.forward()
  367.                 sleep(0.5)
  368.                 DigMoveForward()
  369.         end
  370.     print("Out DigMoveForward")
  371. end
  372.  
  373. local function DigMoveUp()
  374.     print("In DigMoveUp")
  375.         while mov.up(1) == false do
  376.                 dig.up()
  377.                 sleep(0.5)
  378.         end
  379.     print("Out DigMoveUp")
  380. end
  381.  
  382. local function DigMoveDown()
  383.     print("In DigMoveDown")
  384.         while mov.down(1) == false do
  385.                 dig.down()
  386.                 sleep(0.5)
  387.         end
  388.     print("Out DigMoveDown")
  389. end
  390.  
  391. local function DepositChest()
  392.     print("In DepositChest")
  393.     turtle.select(1)
  394.     if not turtle.compareUp() then
  395.         print("ERROR: Should be below chest.")
  396.     print("Out DepositChest")
  397.         return
  398.     end
  399.        
  400.     print("Depositing the Load...")
  401.     for i = IgnoreSlots + 1,16 do
  402.             turtle.select(i)
  403.             turtle.dropUp()
  404.     end
  405.     print("Out DepositChest")
  406. end
  407.  
  408. local function ReturnBack()
  409.     print("In ReturnBack")
  410.     turtle.select(1)
  411.     while not turtle.compareUp() do
  412.         DigMoveUp()
  413.     end
  414.     print("Out ReturnBack")
  415. end
  416.  
  417. local function Finish()
  418.     print("In Finish")
  419.     ReturnBack()
  420.     DepositChest()
  421.     DigMoveForward()
  422.     DigMoveForward()
  423.     DigMoveForward()
  424.     DigMoveForward()    
  425.     print("Out Finish")
  426. end
  427.  
  428. local function InventoryFull()
  429.     print("In InventoryFull")
  430.     SlotsFull = 0
  431.     for i=1,16 do
  432.             if turtle.getItemCount(i) > 0 then
  433.                     SlotsFull = SlotsFull + 1
  434.             end
  435.     end
  436.    
  437.     if SlotsFull == 16 then
  438.     print("Out InventoryFull")
  439.             return true;
  440.     else
  441.     print("Out InventoryFull")
  442.             return false;
  443.     end
  444. end
  445.  
  446. function StartMining()
  447.  print("In StartMining")
  448.     BlocksTillEnd = TunnelLength - PositionInBranchMine
  449.  
  450.     while BlocksTillEnd > 0 do
  451.         if Refuel() == false then
  452.                 print("***Out of fuel***")
  453.                 ReturnBack()
  454.                 while Refuel() do
  455.                                    
  456.                 end
  457.                 for i=1,PositionInBranchMine do
  458.                     DigMoveDown()
  459.                 end
  460.         end
  461.        
  462.         if InventoryFull() then
  463.                 print("***Inventory Full***")
  464.                 ReturnBack()
  465.                 DepositChest()
  466.                 for i=1,PositionInBranchMine do
  467.                     DigMoveDown()
  468.                 end
  469.         end
  470.  
  471.         dig.forward()
  472.        
  473.         for i=1,4 do
  474.             DigMoveDown()
  475.             PositionInBranchMine = PositionInBranchMine + 1
  476.             BlocksTillEnd = TunnelLength - PositionInBranchMine
  477.         end            
  478.  
  479.         print("Depth: ")
  480.         print(PositionInBranchMine)
  481.     end
  482.     Finish()
  483.    
  484.  print("Out StartMining")
  485. end
  486.  
  487. local FuelCount = turtle.getItemCount(fuelIndex)
  488.  
  489. if FuelCount == 0 then
  490.         print("please put fuel in the last slot of the Turtle.")
  491.         return
  492. else
  493.         if FuelCount < 10 then
  494.                 print("Please put at least 10 of the fuel you are using in the Turtle.")
  495.                 return
  496.         end
  497. end
  498.  
  499. Refuel()
  500. turtle.select(1)
  501. turtle.placeUp()
  502. StartMining()
Advertisement
Add Comment
Please, Sign In to add comment