Advertisement
Kodos

Fixed Branch Mining Indentation

Aug 17th, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2. Code to-do list:
  3. *Auto chest deposit
  4. *Auto torch placement for branches and better torch logic for tunnel
  5. *Rednet flags
  6. *Finish branch function
  7. *torch placement
  8. *more efficient logic
  9. *Improve efficiency
  10. *Various others
  11. Newest Features:
  12. *Main tunnel function
  13. *Ore priorities
  14. ]]--
  15.  
  16. -- Round to integers if within bounds
  17. function round(num, bounds)
  18.   if(num > 0) then
  19.     if(num + bounds >= math.ceil(num)) then
  20.       return math.ceil(num)
  21.     elseif(num - bounds <= math.floor(num)) then
  22.       return math.floor(num)
  23.     end
  24.   elseif(num < 0) then
  25.     if(num - bounds <= math.floor(num)) then
  26.       return math.floor(num)
  27.     elseif(num + bounds >= math.ceil(num)) then
  28.       return math.ceil(num)
  29.     end
  30.   end
  31.   return num
  32. end
  33.  
  34. -- Test if 2 points are equal in 3D
  35. function testPointEquality(p1,p2)
  36.   if(p1 == nil or p2 == nil) then
  37.     return false
  38.   end
  39.   if(p1[2] == p2[2] and p1[3] == p2[3] and p1[4] == p2[4]) then
  40.     return true
  41.   else return false
  42.   end
  43. end
  44.  
  45. -- Obtain a table of 4 valid gps reference points
  46. function getGPSCoordinates(timeOut)
  47.   local timeOut = timeOut or 2
  48.   local computer = require("computer")
  49.   local event = require("event")
  50.   local timer = computer.uptime()
  51.   local points = {}
  52.   while(computer.uptime() - timer < timeOut*4 and #points<4) do
  53.     --print("Timer: "..computer.uptime() - timer)
  54.    
  55.     --[[    Data is assumed to be meaningful!
  56.     This could be a huge problem, but will usually work if this
  57.     port is only used for gps, a fix should be inplemented
  58.     ]]--
  59.    
  60.     local eventType, recieverAddress, senderAddress, port, distance, x,y,z = event.pull(timeOut, "modem_message")
  61.     if(distance ~= nil and x ~= nil and y ~= nil and z ~= nil) then
  62.       local tmp = {distance, x,y,z}
  63.       tmp = tonumberVector(tmp)
  64.       local same = false
  65.       for i=1, #points do
  66.         if(testPointEquality(points[i], tmp)) then
  67.           same = true
  68.           --print("Same point!")
  69.           break
  70.         end
  71.       end
  72.       if(not same) then
  73.         if(#points<3) then
  74.           points[#points+1] = tmp
  75.           --print("Point added:"..tostring(tmp[1]))
  76.           --print(#points)
  77.         else
  78.           local normPlane = getPlane(points[1],points[2],points[3])
  79.           --print(tostringVector(normPlane))
  80.           if(isInPlane(tmp,points[1],normPlane)) then
  81.             --print("Point in plane!")
  82.           else
  83.             --print("Point not in plane, adding point")
  84.             points[#points+1] = tmp
  85.             --print(#points)
  86.           end
  87.         end
  88.       end
  89.     else
  90.       print("Nil event type")
  91.     end
  92.   end
  93.   --print(#points)
  94.   if(#points >3) then
  95.     return points
  96.   else
  97.     return nil
  98.   end
  99. end
  100.  
  101. -- Split a string into a table at occurrence of sep
  102. function split(inputstr, sep)
  103.   if(inputstr == nil or inputstr == "") then
  104.     return nil
  105.   end
  106.   if sep == nil then
  107.     sep = ","
  108.   end
  109.   local t={} ; i=1
  110.   for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  111.     t[i] = str
  112.     i = i + 1
  113.   end
  114.   return t
  115. end
  116.  
  117. -- Add two vectors
  118. function addVector(p1,p2)
  119.   --p1 + p2
  120.   return {p1[1]+p2[1], p1[2]+p2[2], p1[3]+p2[3]}
  121. end
  122.  
  123. -- Subtract two vectors
  124. function subtractVector(p1,p2)
  125.   --p1 - p2
  126.   return {p1[1]-p2[1], p1[2]-p2[2], p1[3]-p2[3]}
  127. end
  128.  
  129. -- Scale a vector
  130. function scaleVector(p1, scalar)
  131.   --p1 * scalar
  132.   return {p1[1]*scalar, p1[2]*scalar, p1[3]*scalar}
  133. end
  134.  
  135. -- Dot two vectors
  136. function dotVector(p1,p2)
  137.   return p1[1]*p2[1] + p1[2]*p2[2] + p1[3]*p2[3]
  138. end
  139.  
  140. -- Cross two vectors
  141. function crossVector(p1,p2)
  142.   return {p1[2]*p2[3] - p1[3]*p2[2], p1[3]*p2[1] - p1[1]*p2[3], p1[1]*p2[2] - p1[2]*p2[1]}
  143. end
  144.  
  145. -- Returns the magnitude of a vector
  146. function magnitudeVector(p1)
  147.   return math.sqrt(math.pow(p1[1], 2) + math.pow(p1[2], 2) + math.pow(p1[3], 2))
  148. end
  149.  
  150. -- Normalize a vector
  151. function normalizeVector(p1)
  152.   return p1/magnitudeVector(p1)
  153. end
  154.  
  155. -- Get a plane from 3 vectors
  156. function getPlane(p1,p2,p3)
  157.   --find the equation of the plane
  158.   local p12 = subtractVector(p2,p1)
  159.   local p13 = subtractVector(p3,p1)
  160.   local plane = crossVector(p12,p13)
  161.   return plane
  162. end
  163.  
  164. -- Check if a vector is in a plane
  165. function isInPlane(p1,p2,p3)
  166.   local bounds= 0.000001
  167.   --[[
  168.   where:
  169.   p1 is the point in question,
  170.   p2 is a point known to exist on plane p3,
  171.   and p3 is the plane in question
  172.   ]]--
  173.   if(dotVector(subtractVector(p1,p2),p3) < bounds and dotVector(subtractVector(p1,p2),p3) > -bounds) then
  174.     return true
  175.   else
  176.     return false
  177.   end
  178. end
  179.  
  180. -- Rotate a vector around the Z axis
  181. function rotateVectorZAxis(p1,theta)
  182.   return {p1[1]*math.cos(theta) - p1[2]*math.sin(theta), p1[1]*math.sin(theta) + p1[2]*math.cos(theta), p1[3]}
  183. end
  184.  
  185. -- Rotate a vector around the X axis
  186. function rotateVectorXAxis(p1,theta)
  187.   return {p1[1], p1[2]*math.cos(theta) - p1[3]*math.sin(theta), p1[2]*math.sin(theta) + p1[3]*math.cos(theta)}
  188. end
  189.  
  190. -- Rotate a vector around the Y axis
  191. function rotateVectorYAxis(p1,theta)
  192.   return {p1[1]*math.cos(theta) + p1[3]*math.sin(theta), p1[2], p1[3]*math.cos(theta) - p1[1]*math.sin(theta)}
  193. end
  194.  
  195. -- Turn a vector into a string
  196. function tostringVector(p1)
  197.   return tostring(p1[1]) .. "," .. tostring(p1[2]) .. "," .. tostring(p1[3])
  198. end
  199.  
  200. -- Turn a vector into a number
  201. function tonumberVector(p1)
  202.   local tmp = {}
  203.   for i=1, #p1 do
  204.     tmp[i] = tonumber(p1[i])
  205.   end
  206.   return tmp
  207. end
  208.  
  209. -- Round each entry of a vector using the round() function above
  210. function roundVector(p1)
  211.   local bounds = 0.000001
  212.   local tmp = {}
  213.   --[[
  214.   for i=1, #p1 do
  215.   if(p1[i] < bounds and p1[i] > -bounds) then
  216.   tmp[i] = 0
  217. else
  218. tmp[i] = p1[i]
  219. end
  220. end
  221. ]]--
  222. for i=1, #p1 do
  223.   tmp[i] = round(p1[i],bounds)
  224. end
  225. return tmp
  226. end
  227.  
  228. -- Obtain the distance between two vectors
  229. function distanceBetweenPoints(p1,p2)
  230.   return math.sqrt(math.pow(p1[1]-p2[1],2) + math.pow(p1[2]-p2[2],2) + math.pow(p1[3]-p2[3],2))
  231. end
  232.  
  233. -- 2D trilateration
  234. function findPossiblePoints2D(d1,p1,d2,p2)
  235.   local distance = distanceBetweenPoints(p1,p2)
  236.   local a = (math.pow(d1, 2) - math.pow(d2, 2) + math.pow(distance, 2)) / (2 * distance)
  237.   --print(a)
  238.   local intersect = addVector(p1,scaleVector(scaleVector(subtractVector(p2,p1), a), (1/distance)))
  239.   --print(tostringVector(intersect))
  240.   local h = math.sqrt(math.pow(d1, 2) - math.pow(a, 2))
  241.   --print(h)
  242.   local x_plus = intersect[1] + ((h*(p2[2]-p1[2]))/distance)
  243.   local x_minus = intersect[1] - ((h*(p2[2]-p1[2]))/distance)
  244.   local y_plus = intersect[2] + ((h*(p2[1]-p1[1]))/distance)
  245.   local y_minus = intersect[2] - ((h*(p2[1]-p1[1]))/distance)
  246.   return {x_plus,y_minus,0}, {x_minus,y_plus,0}
  247. end
  248.  
  249. -- 3D trilateration
  250. function findPossiblePoints3D(d1,p1,d2,p2,d3,p3)
  251.  
  252.   --print("Trilateration points: "..d1..","..tostringVector(p1)..";"..d2..","..tostringVector(p2)..";"..d3..","..tostringVector(p3))
  253.  
  254.   --offset vectors such that p1 is at 0,0,0
  255.   local offset = scaleVector(p1, -1)
  256.   --print("Offset: "..tostringVector(offset))
  257.   local p1p = addVector(p1, offset)
  258.   --print("p1p: "..tostringVector(p1p))
  259.   local p2p = addVector(p2, offset)
  260.   --print("p2p: "..tostringVector(p2p))
  261.   local p3p = addVector(p3, offset)
  262.   --print("p3p: "..tostringVector(p3p))
  263.  
  264.   --print("Offset vectors: "..tostringVector(p1p).."; "..tostringVector(p2p).."; "..tostringVector(p3p))
  265.  
  266.  
  267.   --find the equation of the plane
  268.   local plane = roundVector(getPlane(p1p,p2p,p3p))
  269.   --print("Plane: "..tostringVector(plane))
  270.  
  271.   -- checked up to here --
  272.  
  273.   --rotate first about the y axis such that p2 is at z = 0
  274.   local angle1
  275.   if(p2p[1] ~= 0)then
  276.     angle1 = math.atan(p2p[3]/p2p[1])
  277.   elseif(p2p[3] > 0) then
  278.     --  90 degrees
  279.     angle1 = 1.57079633
  280.   elseif(p2p[3] < 0) then
  281.     --  -90 degrees
  282.     angle1 = -1.57079633
  283.   end
  284.   --print("Angle1: "..angle1)
  285.   local plane_ry = roundVector(rotateVectorYAxis(plane, angle1))
  286.   --print("plane_ry: "..tostringVector(plane_ry))
  287.   local p1p_ry = roundVector(rotateVectorYAxis(p1p, angle1))
  288.   --print("p1p_ry: "..tostringVector(p1p_ry))
  289.   local p2p_ry = roundVector(rotateVectorYAxis(p2p, angle1))
  290.   --print("p2p_ry: "..tostringVector(p2p_ry))
  291.   local p3p_ry = roundVector(rotateVectorYAxis(p3p, angle1))
  292.   --print("p3p_ry: "..tostringVector(p3p_ry))
  293.  
  294.   --rotate second about the z axis such that p2 is at y = 0 and the line from p1 to p2 is at z=0 y=0, and is hence on the x axis
  295.   local angle2
  296.   if(p2p_ry[1] ~= 0)then
  297.     angle2 = (-1) * math.atan(p2p_ry[2]/p2p_ry[1])
  298.   elseif(p2p_ry[2] > 0) then
  299.     --  -90 degrees
  300.     angle2 = -1.57079633
  301.   elseif(p2p_ry[2] < 0) then
  302.     --  90 degrees
  303.     angle2 = 1.57079633
  304.   end
  305.   --print("Angle2: "..angle2)
  306.   local plane_ryz = roundVector(rotateVectorZAxis(plane_ry, angle2))
  307.   --print("plane_ryz: "..tostringVector(plane_ryz))
  308.   local p1p_ryz = roundVector(rotateVectorZAxis(p1p_ry, angle2))
  309.   --print("p1p_ryz: "..tostringVector(p1p_ryz))
  310.   local p2p_ryz = roundVector(rotateVectorZAxis(p2p_ry, angle2))
  311.   --print("p2p_ryz: "..tostringVector(p2p_ryz))
  312.   local p3p_ryz = roundVector(rotateVectorZAxis(p3p_ry, angle2))
  313.   --print("p3p_ryz: "..tostringVector(p3p_ryz))
  314.  
  315.   --rotate plane about the x axis such that p3 is at z = 0
  316.   local angle3
  317.   if(p3p_ryz[2] ~= 0)then
  318.     angle3 = (-1) * math.atan(p3p_ryz[3]/p3p_ryz[2])
  319.   elseif(p3p_ry[3] > 0) then
  320.     --  -90 degrees
  321.     angle3 = -1.57079633
  322.   elseif(p3p_ry[3] < 0) then
  323.     --  90 degrees
  324.     angle3 = 1.57079633
  325.   end
  326.   --print("Angle3: "..angle3)
  327.   local plane_ryzx = roundVector(rotateVectorXAxis(plane_ryz, angle3))
  328.   --print("plane_ryzx: "..tostringVector(plane_ryzx))
  329.   local p1p_ryzx = roundVector(rotateVectorXAxis(p1p_ryz, angle3))
  330.   --print("p1p_ryzx: "..tostringVector(p1p_ryzx))
  331.   local p2p_ryzx = roundVector(rotateVectorXAxis(p2p_ryz, angle3))
  332.   --print("p2p_ryzx : "..tostringVector(p2p_ryzx ))
  333.   local p3p_ryzx = roundVector(rotateVectorXAxis(p3p_ryz, angle3))
  334.   --print("p3p_ryzx : "..tostringVector(p3p_ryzx ))
  335.  
  336.   --at this point, p1 should be at 0,0,0; p2 x,0,0; and p3 should be at x,y,0
  337.  
  338.   --calculations for finding rotated, offset points
  339.   local xp_ryzx = (math.pow(d1,2) - math.pow(d2,2) + math.pow(p2p_ryzx[1],2))/(2*p2p_ryzx[1])
  340.   local yp_ryzx = ((math.pow(d1,2) - math.pow(d3,2) + math.pow(p3p_ryzx[1],2) + math.pow(p3p_ryzx[2],2))/(2*p3p_ryzx[2])) - ((p3p_ryzx[1]/p3p_ryzx[2])*xp_ryzx)
  341.   local z1p_ryzx = math.sqrt(math.pow(d1,2)-math.pow(xp_ryzx,2)-math.pow(yp_ryzx,2))
  342.   local z2p_ryzx = (-1) * math.sqrt(math.pow(d1,2)-math.pow(xp_ryzx,2)-math.pow(yp_ryzx,2))
  343.  
  344.   --possible rotated, offset points
  345.   local point1p_ryzx = {xp_ryzx, yp_ryzx, z1p_ryzx}
  346.   local point2p_ryzx = {xp_ryzx, yp_ryzx, z2p_ryzx}
  347.  
  348.   --rotate back around the x axis
  349.   plane_ryx = rotateVectorXAxis(plane_ryzx, (-1)*angle3)
  350.   local point1p_ryz = rotateVectorXAxis(point1p_ryzx, (-1)*angle3)
  351.   local point2p_ryz = rotateVectorXAxis(point2p_ryzx, (-1)*angle3)
  352.  
  353.   --rotate back around the z axis
  354.   plane_ry = rotateVectorZAxis(plane_ryz, (-1)*angle2)
  355.   local point1p_ry = rotateVectorZAxis(point1p_ryz, (-1)*angle2)
  356.   local point2p_ry = rotateVectorZAxis(point2p_ryz, (-1)*angle2)
  357.  
  358.   --rotate back around the y axis
  359.   plane = rotateVectorYAxis(plane_ry, (-1)*angle1)
  360.   local point1p = rotateVectorYAxis(point1p_ry, (-1)*angle1)
  361.   local point2p = rotateVectorYAxis(point2p_ry, (-1)*angle1)
  362.   --print(tostringVector(plane))
  363.  
  364.   --remove offset
  365.   local point1 = addVector(point1p, scaleVector(offset,-1))
  366.   local point2 = addVector(point2p, scaleVector(offset,-1))
  367.  
  368.   --print("Possible points are either: "..tostringVector(point1).." or "..tostringVector(point2))
  369.   return roundVector(point1), roundVector(point2)
  370.  
  371. end
  372.  
  373. -- Narrow trilaterations points
  374. function narrow(p1,p2,d4,p4)
  375.   local bounds = 0.00001
  376.   local distance1 = distanceBetweenPoints(p1,p4)
  377.   local error1 = distance1 - d4
  378.   --print(error1)
  379.   if(error1 < bounds and error1 > -bounds) then
  380.     return p1
  381.   end
  382.   local distance2 = distanceBetweenPoints(p2,p4)
  383.   local error2 = distance2 - d4
  384.   --print(error2)
  385.   if(error2 < bounds and error2 > -bounds) then
  386.     return p2
  387.   end
  388.   print("Narrowing function could not obtain a meaningful answer")
  389.     return nil
  390.   end
  391.  
  392.   -- get gps coordinates
  393.   function getLocation(timeout)
  394.     local timeout = timeout or 2
  395.     local coords = getGPSCoordinates(timeout)
  396.     if(coords == nil) then
  397.       print("Coordinate table could not be obtained")
  398.       return nil
  399.     end
  400.     if(#coords < 4) then
  401.       print("Insufficient points")
  402.       return nil
  403.     end
  404.     local d1 = coords[1][1]
  405.     local p1 = {coords[1][2],coords[1][3],coords[1][4]}
  406.     local d2 = coords[2][1]
  407.     local p2 = {coords[2][2],coords[2][3],coords[2][4]}
  408.     local d3 = coords[3][1]
  409.     local p3 = {coords[3][2],coords[3][3],coords[3][4]}
  410.     local d4 = coords[4][1]
  411.     local p4 = {coords[4][2],coords[4][3],coords[4][4]}
  412.     local point1, point2 = findPossiblePoints3D(d1,p1,d2,p2,d3,p3)
  413.     --print("Possible point 1: "..tostringVector(point1))
  414.     --print("Possible point 2: "..tostringVector(point2))
  415.     local location = narrow(point1,point2,d4,p4)
  416.     if(location ~= nil) then
  417.       --print(tostringVector(location))
  418.       return location
  419.     else
  420.       print("Location could not be narrowed")
  421.       return nil
  422.     end
  423.   end
  424.  
  425.   -- Add elements to priorities list
  426.   function addList(name,mine,flag,drop,vein)
  427.     local fileSystem = require("filesystem")
  428.     local shell = require("shell")
  429.     local xlist = {}
  430.     local ifile
  431.     if(fileSystem.exists(shell.resolve("listed"))) then
  432.       --print("addList: File exists")
  433.       ifile = io.open(shell.resolve("listed"), "r")
  434.       i=1
  435.       for line in io.lines(shell.resolve("listed")) do
  436.         xlist[i] = split(line)
  437.         i=i+1
  438.       end
  439.      
  440.       for k,v in pairs(xlist) do
  441.         if(name == v[1]) then
  442.           --print("Ore already in list, not added")
  443.           return false
  444.         end
  445.       end
  446.       ifile:close()
  447.       ifile = io.open(shell.resolve("listed"),"a")
  448.     else
  449.       ifile = io.open(shell.resolve("listed"),"w")
  450.     end
  451.     ifile:write(name..","..mine..","..flag..","..drop..","..vein.."\n")
  452.     ifile:close()
  453.     return true
  454.   end
  455.  
  456.   -- Get priorities list in file format as a list
  457.   function lstToTable()
  458.     local fileSystem = require("filesystem")
  459.     local shell = require("shell")
  460.     if(fileSystem.exists(shell.resolve("listed"))) then
  461.       --print("lstToTable: File exists")
  462.       ifile = io.open(shell.resolve("listed"),"r")
  463.       xlist = {}
  464.       for line in io.lines(shell.resolve("listed")) do
  465.         table.insert(xlist, split(line))
  466.       end
  467.     else
  468.       print("lstToTable: File doesn't exists")
  469.       --      name,mine,flag,drop,vein
  470.       xlist = {
  471.         {'iron_ore','1','0','0','1'},
  472.         {'coal_ore','1','0','0','1'},
  473.         {'gold_ore','1','0','0','1'},
  474.         {'lapis_ore','1','0','0','1'},
  475.         {'redstone_ore','1','0','0','1'},
  476.         {'lit_redstone_ore','1','0','0','1'},
  477.         {'diamond_ore','1','0','0','1'},
  478.         {'quartz_ore','1','0','0','1'},
  479.         {'emerald_ore','1','0','0','1'}
  480.       }
  481.       for k,v in pairs(xlist) do
  482.         addList(v[1],v[2],v[3],v[4],v[5])
  483.       end
  484.     end
  485.     return xlist
  486.   end
  487.  
  488.   -- Clear robot inventory according to priorities list
  489.   function clrInv(xtable)
  490.     local robot = require("robot")
  491.     local component = require("component")
  492.     for i = 1, robot.inventorySize() do -- loop through the slots
  493.       robot.select(i)
  494.       local x = component.inventory_controller.getStackInInternalSlot(i)
  495.       if(x ~= nil) then
  496.         local istr = string.sub(x.name,string.find(x.name,":",0)+1)
  497.         for key,value in pairs(xtable) do
  498.           if(istr == value[1] and value[4] == "1") then
  499.             robot.drop()
  500.           end
  501.         end
  502.       end
  503.     end
  504.     robot.select(1)
  505.   end
  506.  
  507.   -- Sort robot inventory
  508.   function sortInv()
  509.     local robot = require("robot")
  510.     local component = require("component")
  511.     for i = 1, robot.inventorySize() do -- loop through the slots
  512.       robot.select(i)
  513.       local x = component.inventory_controller.getStackInInternalSlot(i)
  514.       if(x ~= nil) then
  515.         for c = i, robot.inventorySize() do
  516.           if(component.inventory_controller.getStackInInternalSlot(c) ~= nil) then
  517.             if(robot.compareTo(c)) then
  518.               robot.select(c)
  519.               robot.transferTo(i)
  520.               robot.select(i)
  521.             end
  522.           end
  523.         end
  524.       end
  525.     end
  526.     robot.select(1)
  527.   end
  528.  
  529.   -- Check for robot inventory space
  530.   function invSpace()
  531.     local robot = require("robot")
  532.     local component = require("component")
  533.     for i = 1, robot.inventorySize() do
  534.       robot.select(i)
  535.       local x = component.inventory_controller.getStackInInternalSlot(i)
  536.       if(x == nil or robot.compare()) then
  537.         robot.select(1)
  538.         return true
  539.       end
  540.     end
  541.     robot.select(1)
  542.     return false
  543.   end
  544.  
  545.   --29--
  546.  
  547.   -- Check for sufficient fuel
  548.   function sufficientFuel() -- must figure out power consumption first, right not just set to half fuel left
  549.     --params for later:
  550.     --[[
  551.     vector1,vector2,fuel,remainder
  552.     ]]--
  553.     local computer = require("computer")
  554.     if(computer.energy()/computer.maxEnergy() < .5) then
  555.       return false
  556.     else
  557.       return true
  558.     end
  559.    
  560.     --[[
  561.     local distance = math.abs(vector1.x - vector2.x) + math.abs(vector1.y - vector2.y) + math.abs(vector1.z - vector2.z)
  562.     if(fuel <= distance + remainder) then
  563.     return false
  564.   else
  565.   return true
  566. end
  567. ]]--
  568. end
  569.  
  570. -- Consume robot fuel (requires generator upgrade) must be ported yet
  571. function consumeFuel(maxFuel) -- Optionally add more fuel types and prioritise fuel types
  572.   local refuel = false
  573.   for i = 1, 16 do -- loop through the slots
  574.     turtle.select(i) -- change to the slot
  575.     local x = turtle.getItemDetail(i)
  576.     if(x ~= nil) then
  577.       local istr = string.sub(x.name,string.find(x.name,":",0)+1)
  578.       if(istr == "planks" or istr == "stick" or istr == "log") then
  579.         turtle.refuel()
  580.         refuel = true
  581.       end
  582.       if(turtle.getFuelLevel() < maxFuel and istr == "coal") then
  583.         turtle.refuel(1)
  584.         refuel = true
  585.       end
  586.     end
  587.   end
  588.   turtle.select(1)
  589.   --print(turtle.getFuelLevel())
  590.   return refuel
  591. end
  592.  
  593. -- Place torch --must be ported yet
  594. function placeTorch(direction) -- Optionally add functionality to place torch when no torch can be placed
  595.   local torch = false
  596.   for i = 1, 16 do -- loop through the slots
  597.     turtle.select(i) -- change to the slot
  598.     local x = turtle.getItemDetail(i)
  599.     if(x ~= nil) then
  600.       local istr = string.sub(x.name,string.find(x.name,":",0)+1)
  601.       if(istr == "torch") then
  602.         if(direction == "up") then
  603.           if(turtle.placeUp()) then
  604.             torch = true
  605.             turtle.select(1)
  606.           end
  607.         elseif(direction == "down")then
  608.           if(turtle.placeDown()) then
  609.             torch = true
  610.             turtle.select(1)
  611.           end
  612.         else
  613.           if(turtle.place()) then
  614.             torch = true
  615.             turtle.select(1)
  616.           end
  617.         end
  618.       end
  619.     end
  620.   end
  621.   turtle.select(1)
  622.   return torch
  623. end
  624.  
  625.  
  626.  
  627.  
  628. -- Get robot heading
  629. function getHeading()
  630.   local robot = require("robot")
  631.   --[[
  632.   --First check for required fuel
  633.   if(turtle.getFuelLevel() < 2) then
  634.   if(not consumeFuel(400)) then
  635.   error("Insufficient fuel")
  636. end
  637. end
  638. ]]--
  639. local start = getLocation()
  640. if(start == nil) then
  641.   print("GPS coordinates could not be obtained")
  642.   return nil
  643. else
  644.   print(tostringVector(start))
  645. end
  646. while(robot.detect()) do
  647.   robot.swing(3) --front
  648. end
  649.  
  650. while (not robot.forward()) do end
  651. os.sleep(.1)
  652. local current = getLocation()
  653. if(current == nil) then
  654.   print("GPS coordinates could not be obtained")
  655.   return nil
  656. else
  657.   print(tostringVector(current))
  658. end
  659. os.sleep(.1)
  660. robot.back()
  661. if(start[3] - current[3] > 0) then
  662.   heading = 'N'
  663. elseif (start[3] - current[3] < 0) then
  664.   heading = 'S'
  665. end
  666. if(start[1] - current[1] > 0) then
  667.   heading = 'W'
  668. elseif (start[1] - current[1] < 0) then
  669.   heading = 'E'
  670. end
  671. return heading
  672. end
  673.  
  674.  
  675.  
  676.  
  677. -- Set robot heading
  678. function setHeading(heading,newHeading)
  679.   if(heading ~= 'N' and heading ~= 'n' and heading ~= 'E' and heading ~= 'e' and heading ~= 'S' and heading ~= 's' and heading ~= 'W' and heading ~= 'w') then
  680.     if(turtle.getFuelLevel() < 2) then
  681.       error("Insufficient fuel")
  682.     end
  683.     local start = vector.new(gps.locate())
  684.     while(turtle.detect()) do
  685.       turtle.dig()
  686.     end
  687.     turtle.forward()
  688.     local current = vector.new(gps.locate())
  689.     turtle.back()
  690.     if(start.z - current.z > 0) then
  691.       heading = 'N'
  692.     elseif (start.z - current.z < 0) then
  693.       heading = 'S'
  694.     end
  695.     if(start.x - current.x > 0) then
  696.       heading = 'W'
  697.     elseif (start.x - current.x < 0) then
  698.       heading = 'E'
  699.     end
  700.   end
  701.  
  702.   if(heading ~= newHeading) then
  703.     if(newHeading == 'N' or newHeading == 'n') then
  704.       if(heading == 'S' or heading == 's') then
  705.         turtle.turnLeft()
  706.         turtle.turnLeft()
  707.       end
  708.       if(heading == 'E' or heading == 'e') then
  709.         turtle.turnLeft()
  710.       end
  711.       if(heading == 'W' or heading == 'w') then
  712.         turtle.turnRight()
  713.       end
  714.     end
  715.     if(newHeading == 'E' or newHeading == 'e') then
  716.       if(heading == 'S' or heading == 's') then
  717.         turtle.turnLeft()
  718.       end
  719.       if(heading == 'N' or heading == 'n') then
  720.         turtle.turnRight()
  721.       end
  722.       if(heading == 'W' or heading == 'w') then
  723.         turtle.turnRight()
  724.         turtle.turnRight()
  725.       end
  726.     end
  727.     if(newHeading == 'S' or newHeading == 's') then
  728.       if(heading == 'N' or heading == 'n') then
  729.         turtle.turnLeft()
  730.         turtle.turnLeft()
  731.       end
  732.       if(heading == 'E' or heading == 'e') then
  733.         turtle.turnRight()
  734.       end
  735.       if(heading == 'W' or heading == 'w') then
  736.         turtle.turnLeft()
  737.       end
  738.     end
  739.     if(newHeading == 'W' or newHeading == 'w') then
  740.       if(heading == 'S' or heading == 's') then
  741.         turtle.turnRight()
  742.       end
  743.       if(heading == 'E' or heading == 'e') then
  744.         turtle.turnLeft()
  745.         turtle.turnLeft()
  746.       end
  747.       if(heading == 'N' or heading == 'n') then
  748.         turtle.turnLeft()
  749.       end
  750.     end
  751.   end
  752. end
  753.  
  754. -- Go to a specific location
  755. function goToLocation(location,heading)
  756.   if(heading ~= 'N' and heading ~= 'n' and heading ~= 'E' and heading ~= 'e' and heading ~= 'S' and heading ~= 's' and heading ~= 'W' and heading ~= 'w') then
  757.     heading = getHeading()
  758.   end
  759.  
  760.   local current = vector.new(gps.locate())
  761.   if(location.x ~= current.x or location.y ~= current.y or location.z ~= current.z) then
  762.     if(turtle.getFuelLevel() < 2) then
  763.       error("Insufficient fuel")
  764.     end
  765.   end
  766.   while(location.y ~= current.y) do
  767.     if(location.y > current.y) then
  768.       while(turtle.detectUp()) do
  769.         turtle.digUp()
  770.       end
  771.       turtle.up()
  772.     else
  773.       while(turtle.detectDown()) do
  774.         turtle.digDown()
  775.       end
  776.       turtle.down()
  777.     end
  778.     current = vector.new(gps.locate())
  779.   end
  780.  
  781.   while(location.z ~= current.z) do
  782.     if(location.z > current.z) then
  783.       setHeading(heading,'S')
  784.       heading = 'S'
  785.     elseif(location.z < current.z) then
  786.       setHeading(heading,'N')
  787.       heading = 'N'
  788.     end
  789.     while(turtle.detect()) do
  790.       turtle.dig()
  791.     end
  792.     turtle.forward()
  793.     current = vector.new(gps.locate())
  794.   end
  795.  
  796.   while(location.x ~= current.x) do
  797.     if(location.x > current.x) then
  798.       setHeading(heading,'E')
  799.       heading = 'E'
  800.     elseif(location.x < current.x) then
  801.       setHeading(heading,'W')
  802.       heading = 'W'
  803.     end
  804.     while(turtle.detect()) do
  805.       turtle.dig()
  806.     end
  807.     turtle.forward()
  808.     current = vector.new(gps.locate())
  809.   end
  810.   return heading
  811. end
  812.  
  813. -- Decide whether to mine whole ore vein based on priorities list
  814. function shouldMineWhole(istr,xlist) -- Optionally add functionality for flagging ores
  815.   for key,value in pairs(xlist) do
  816.     if(istr == value[1] and value[5] == "1") then
  817.       return true
  818.     end
  819.   end
  820.   return false
  821. end
  822.  
  823. -- Mine entire ore vein
  824. function mineVein(start,moves,back,xtable) -- This function needs work on flagging ore, also could be significantly more efficient
  825.   --Establish current GPS location--
  826.   local current = vector.new(gps.locate())
  827.   --Check for sufficient fuel, if not, try to refuel, if refuel fails, function return false and
  828.   --recursion tree will collapse with turtle returning to where it started--
  829.   if(not sufficientFuel(start,current,turtle.getFuelLevel(),5 + moves)) then
  830.     if(not consumeFuel(400)) then
  831.       return -2
  832.     end
  833.   end
  834.   --Check for inventory space, if no inventory space, try to create some. if no space can be created,
  835.   --function return false and recursion tree will collapse, with turtle returning to where it started.
  836.   if(not invSpace()) then
  837.     sortInv()
  838.     clrInv(xtable)
  839.     if(not invSpace()) then
  840.       return -1
  841.     end
  842.   end
  843.  
  844.   --Check above turtle for ores--
  845.   local success,data = turtle.inspect()
  846.   if(success) then
  847.     local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  848.     --print(istr)
  849.     if(shouldMineWhole(istr,xtable)) then
  850.       turtle.dig()
  851.       turtle.forward()
  852.       mineVein(start,moves+1,false,xtable)
  853.       turtle.back()
  854.     end
  855.   end
  856.   if(moves == 0) then
  857.     if(current.y == start.y + 1) then
  858.       local success,data = turtle.inspectUp()
  859.       if(success) then
  860.         local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  861.         --print(istr)
  862.         if(shouldMineWhole(istr,xtable)) then
  863.           turtle.digUp()
  864.           turtle.up()
  865.           mineVein(start,moves+1,true,xtable)
  866.           turtle.down()
  867.         end
  868.       end
  869.     end
  870.     if(current.y == start.y) then
  871.       local success,data = turtle.inspectDown()
  872.       if(success) then
  873.         local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  874.         --print(istr)
  875.         if(shouldMineWhole(istr,xtable)) then
  876.           turtle.digDown()
  877.           turtle.down()
  878.           mineVein(start,moves+1,true,xtable)
  879.           turtle.up()
  880.         end
  881.       end
  882.     end
  883.   end
  884.  
  885.   --will ensure turtle does not check sides on start.
  886.   if(moves < 1) then
  887.     return 1
  888.   end
  889.   turtle.turnLeft()
  890.   local success,data = turtle.inspect()
  891.   if(success) then
  892.     local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  893.     --print(istr)
  894.     if(shouldMineWhole(istr,xtable)) then
  895.       turtle.dig()
  896.       turtle.forward()
  897.       mineVein(start,moves+1,false,xtable)
  898.       turtle.back()
  899.     end
  900.   end
  901.   if(back) then
  902.     turtle.turnLeft()
  903.     local success,data = turtle.inspect()
  904.     if(success) then
  905.       local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  906.       --print(istr)
  907.       if(shouldMineWhole(istr,xtable)) then
  908.         turtle.dig()
  909.         turtle.forward()
  910.         mineVein(start,moves+1,false,xtable)
  911.         turtle.back()
  912.       end
  913.     end
  914.     turtle.turnLeft()
  915.     local success,data = turtle.inspect()
  916.     if(success) then
  917.       local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  918.       --print(istr)
  919.       if(shouldMineWhole(istr,xtable)) then
  920.         turtle.dig()
  921.         turtle.forward()
  922.         mineVein(start,moves+1,false,xtable)
  923.         turtle.back()
  924.       end
  925.     end
  926.     turtle.turnLeft()
  927.     local success,data = turtle.inspectUp()
  928.     if(success) then
  929.       local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  930.       --print(istr)
  931.       if(shouldMineWhole(istr,xtable)) then
  932.         turtle.digUp()
  933.         turtle.up()
  934.         mineVein(start,moves+1,true,xtable)
  935.         turtle.down()
  936.       end
  937.     end
  938.     local success,data = turtle.inspectDown()
  939.     if(success) then
  940.       local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  941.       --print(istr)
  942.       if(shouldMineWhole(istr,xtable)) then
  943.         turtle.digDown()
  944.         turtle.down()
  945.         mineVein(start,moves+1,true,xtable)
  946.         turtle.up()
  947.       end
  948.     end
  949.   else
  950.     turtle.turnRight()
  951.     turtle.turnRight()
  952.     local success,data = turtle.inspect()
  953.     if(success) then
  954.       local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  955.       --print(istr)
  956.       if(shouldMineWhole(istr,xtable)) then
  957.         turtle.dig()
  958.         turtle.forward()
  959.         mineVein(start,moves+1,false,xtable)
  960.         turtle.back()
  961.       end
  962.     end
  963.     turtle.turnLeft()
  964.     local success,data = turtle.inspectUp()
  965.     if(success) then
  966.       local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  967.       --print(istr)
  968.       if(shouldMineWhole(istr,xtable)) then
  969.         turtle.digUp()
  970.         turtle.up()
  971.         mineVein(start,moves+1,true,xtable)
  972.         turtle.down()
  973.       end
  974.     end
  975.     local success,data = turtle.inspectDown()
  976.     if(success) then
  977.       local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  978.       --print(istr)
  979.       if(shouldMineWhole(istr,xtable)) then
  980.         turtle.digDown()
  981.         turtle.down()
  982.         mineVein(start,moves+1,true,xtable)
  983.         turtle.up()
  984.       end
  985.     end
  986.   end
  987.   return 1
  988. end
  989.  
  990. -- Mine branch
  991. function mineBranch(branchStart,branchHeading,currentHeading,branchLimit,fuelRemainder,plist,torchLength) -- Still needs optimization and functions to replace repeated blocks
  992.   -- Search for GPS signal --
  993.   local current = vector.new(gps.locate())
  994.   if(current.x == nil or current.y == nil or current.z == nil) then
  995.     -- GPS signal could not be established --
  996.     error("Could not establish GPS signal, please ensure GPS servers are running and try again")
  997.   end
  998.   -- GPS signal established --
  999.   if(not sufficientFuel(current,branchStart,turtle.getFuelLevel(),fuelRemainder + 5)) then
  1000.     if(not consumeFuel(4000)) then
  1001.       error("Insufficient fuel!")
  1002.     end
  1003.   end
  1004.   if(currentHeading ~= 'N' and currentHeading ~= 'n' and currentHeading ~= 'E' and currentHeading ~= 'e' and currentHeading ~= 'S' and currentHeading ~= 's' and currentHeading ~= 'W' and currentHeading ~= 'w') then
  1005.     currentHeading = getHeading()
  1006.   end
  1007.  
  1008.   --print("Heading: ", currentHeading)
  1009.  
  1010.   if(branchStart.y ~= current.y) then
  1011.     -- If y level is not the same --
  1012.     currentHeading = goToLocation(vector.new(current.x,branchStart.y,current.z), currentHeading)
  1013.     current = vector.new(gps.locate())
  1014.   end
  1015.   -- Ensure z level is the same as starting y level --
  1016.   if(branchHeading == 'N' or branchHeading == 'n' or branchHeading == 'S' or branchHeading == 's') then
  1017.     if(branchStart.z ~= current.z) then
  1018.       -- If z level is not the same --
  1019.       currentHeading = goToLocation(vector.new(branchStart.x,current.y,current.z), currentHeading)
  1020.       current = vector.new(gps.locate())
  1021.     end
  1022.   end
  1023.   -- Ensure x level is the same as starting y level --
  1024.   if(branchHeading == 'E' or branchHeading == 'e' or branchHeading == 'W' or branchHeading == 'w') then
  1025.     if(branchStart.x ~= current.x) then
  1026.       -- If x level is not the same --
  1027.       currentHeading = goToLocation(vector.new(current.x,current.y,branchStart.z), currentHeading)
  1028.       current = vector.new(gps.locate())
  1029.     end
  1030.   end
  1031.   setHeading(currentHeading,branchHeading)
  1032.   currentHeading = branchHeading
  1033.   --[[
  1034.   Done
  1035.   0 : Not done, still mining
  1036.   1 : Is done, mining was successful
  1037.   -1    : Inventory full
  1038.   -2    : Insufficient fuel
  1039.   ]]--
  1040.   done = 0
  1041.   while(done == 0) do
  1042.     current = vector.new(gps.locate())
  1043.     if(current.x == nil or current.y == nil or current.z == nil) then
  1044.       -- GPS signal could not be established --
  1045.       error("Could not establish GPS signal, please ensure GPS servers are running and try again")
  1046.     else
  1047.       -- Ensure distance from branchStart is less than branchLimit --
  1048.       -- Along the North/South line --
  1049.       if(branchHeading == 'N' or branchHeading == 'n' or branchHeading == 'S' or branchHeading == 's')then
  1050.         -- Check if distance from branchStart is within branchLimit --
  1051.         if(math.abs(branchStart.z - current.z)>=branchLimit) then
  1052.           -- Distance from branchStart is not within branchLimit --
  1053.           currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,current.z),currentHeading)
  1054.           currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading)
  1055.           done = 1
  1056.         else
  1057.           -- Distance from branchStart is within branchLimit --
  1058.           -- Check for sufficient fuel --
  1059.           if(sufficientFuel(current,branchStart,turtle.getFuelLevel(),fuelRemainder + 5)) then
  1060.             if(invSpace()) then
  1061.               if(current.y == branchStart.y) then
  1062.                 turtle.turnLeft()
  1063.                 mineVein(branchStart,0,false,plist)
  1064.                 turtle.turnRight()
  1065.                 turtle.turnRight()
  1066.                 mineVein(branchStart,0,false,plist)
  1067.                 turtle.turnLeft()
  1068.                 while(turtle.detect()) do
  1069.                   turtle.dig()
  1070.                 end
  1071.                 turtle.forward()
  1072.                 turtle.turnLeft()
  1073.                 mineVein(branchStart,0,false,plist)
  1074.                 turtle.turnRight()
  1075.                 turtle.turnRight()
  1076.                 mineVein(branchStart,0,false,plist)
  1077.                 turtle.turnLeft()
  1078.                 while(turtle.detectUp()) do
  1079.                   turtle.digUp()
  1080.                 end
  1081.                 turtle.up()
  1082.               elseif(current.y - 1 == branchStart.y) then
  1083.                 turtle.turnLeft()
  1084.                 mineVein(branchStart,0,false,plist)
  1085.                 turtle.turnRight()
  1086.                 turtle.turnRight()
  1087.                 mineVein(branchStart,0,false,plist)
  1088.                 turtle.turnLeft()
  1089.                 while(turtle.detect()) do
  1090.                   turtle.dig()
  1091.                 end
  1092.                 turtle.forward()
  1093.                 turtle.turnLeft()
  1094.                 mineVein(branchStart,0,false,plist)
  1095.                 turtle.turnRight()
  1096.                 turtle.turnRight()
  1097.                 mineVein(branchStart,0,false,plist)
  1098.                 turtle.turnLeft()
  1099.                 while(turtle.detectDown()) do
  1100.                   turtle.digDown()
  1101.                 end
  1102.                 turtle.down()
  1103.               else
  1104.                 currentHeading = goToLocation(vector.new(current.x,branchStart.y,current.z),currentHeading)
  1105.               end
  1106.             else
  1107.               sortInv()
  1108.               clrInv(plist)
  1109.               if(not invSpace()) then
  1110.                 currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,current.z),currentHeading)
  1111.                 currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading)
  1112.                 done = -1
  1113.               end
  1114.             end
  1115.           else
  1116.             if(not consumeFuel(4000)) then
  1117.               currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,current.z),currentHeading)
  1118.               currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading)
  1119.               done = -2
  1120.             end
  1121.           end
  1122.         end
  1123.         -- Along the East/West line --
  1124.       elseif(branchHeading == 'E' or branchHeading == 'e' or branchHeading == 'W' or branchHeading == 'w') then
  1125.         -- Check if distance from branchStart is within branchLimit --
  1126.         if(math.abs(branchStart.x - current.x)>=branchLimit) then
  1127.           -- Distance from branchStart is not within branchLimit --
  1128.           currentHeading = goToLocation(vector.new(current.x,branchStart.y,branchStart.z),currentHeading)
  1129.           currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading)
  1130.           done = 1
  1131.         else
  1132.           -- Distance from branchStart is within branchLimit --
  1133.           -- Check for sufficient fuel --
  1134.           if(sufficientFuel(current,branchStart,turtle.getFuelLevel(),fuelRemainder + 5)) then
  1135.             if(invSpace()) then
  1136.               if(current.y == branchStart.y) then
  1137.                 turtle.turnLeft()
  1138.                 mineVein(branchStart,0,false,plist)
  1139.                 turtle.turnRight()
  1140.                 turtle.turnRight()
  1141.                 mineVein(branchStart,0,false,plist)
  1142.                 turtle.turnLeft()
  1143.                 while(turtle.detect()) do
  1144.                   turtle.dig()
  1145.                 end
  1146.                 turtle.forward()
  1147.                 turtle.turnLeft()
  1148.                 mineVein(branchStart,0,false,plist)
  1149.                 turtle.turnRight()
  1150.                 turtle.turnRight()
  1151.                 mineVein(branchStart,0,false,plist)
  1152.                 turtle.turnLeft()
  1153.                 while(turtle.detectUp()) do
  1154.                   turtle.digUp()
  1155.                 end
  1156.                 turtle.up()
  1157.               elseif(current.y - 1 == branchStart.y) then
  1158.                 turtle.turnLeft()
  1159.                 mineVein(branchStart,0,false,plist)
  1160.                 turtle.turnRight()
  1161.                 turtle.turnRight()
  1162.                 mineVein(branchStart,0,false,plist)
  1163.                 turtle.turnLeft()
  1164.                 while(turtle.detect()) do
  1165.                   turtle.dig()
  1166.                 end
  1167.                 turtle.forward()
  1168.                 turtle.turnLeft()
  1169.                 mineVein(branchStart,0,false,plist)
  1170.                 turtle.turnRight()
  1171.                 turtle.turnRight()
  1172.                 mineVein(branchStart,0,false,plist)
  1173.                 turtle.turnLeft()
  1174.                 while(turtle.detectDown()) do
  1175.                   turtle.digDown()
  1176.                 end
  1177.                 turtle.down()
  1178.               else
  1179.                 currentHeading = goToLocation(vector.new(current.x,branchStart.y,current.z),currentHeading)
  1180.               end
  1181.             else
  1182.               sortInv()
  1183.               clrInv(plist)
  1184.               if(not invSpace()) then
  1185.                 currentHeading = goToLocation(vector.new(current.x,branchStart.y,branchStart.z),currentHeading)
  1186.                 currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading)
  1187.                 done = -1
  1188.               end
  1189.             end
  1190.           else
  1191.             if(not consumeFuel(4000)) then
  1192.               currentHeading = goToLocation(vector.new(current.x,branchStart.y,branchStart.z),currentHeading)
  1193.               currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading)
  1194.               done = -2
  1195.             end
  1196.           end
  1197.         end
  1198.       end
  1199.     end
  1200.   end
  1201.   return done
  1202. end
  1203.  
  1204. -- Mine tunnel
  1205. function mTunnel(tunnelStart, tunnelHeading, currentHeading, tunnelLength, fuelRemainder, plist, torchDistance, branchSpacing, branchLimit,branchTorchLength)
  1206.   -- Search for GPS signal --
  1207.   local current = vector.new(gps.locate())
  1208.   if(current.x == nil or current.y == nil or current.z == nil) then
  1209.     -- GPS signal could not be established --
  1210.     error("Could not establish GPS signal, please ensure GPS servers are running and try again")
  1211.   end
  1212.   -- GPS signal established --
  1213.   if(not sufficientFuel(current,tunnelStart,turtle.getFuelLevel(),fuelRemainder+10)) then
  1214.     if(not consumeFuel(4000)) then
  1215.       error("Insufficient fuel!")
  1216.     end
  1217.   end
  1218.   if(currentHeading ~= 'N' and currentHeading ~= 'n' and currentHeading ~= 'E' and currentHeading ~= 'e' and currentHeading ~= 'S' and currentHeading ~= 's' and currentHeading ~= 'W' and currentHeading ~= 'w') then
  1219.     currentHeading = getHeading()
  1220.   end
  1221.  
  1222.   print("Heading: ", currentHeading)
  1223.  
  1224.   -- Ensure y level is the same as starting y level --
  1225.   if(tunnelStart.y ~= current.y) then
  1226.     -- If y level is not the same --
  1227.     currentHeading = goToLocation(vector.new(current.x,tunnelStart.y,current.z), currentHeading)
  1228.     current = vector.new(gps.locate())
  1229.   end
  1230.   -- Ensure z level is the same as starting z level if on N/S axis --
  1231.   if(tunnelHeading == 'N' or tunnelHeading == 'n' or tunnelHeading == 'S' or tunnelHeading == 's') then
  1232.     if(tunnelStart.z ~= current.z) then
  1233.       -- If z level is not the same --
  1234.       currentHeading = goToLocation(vector.new(tunnelStart.x,current.y,current.z), currentHeading)
  1235.       setHeading(currentHeading,tunnelHeading)
  1236.       currentHeading = tunnelHeading
  1237.       turtle.back()
  1238.       current = vector.new(gps.locate())
  1239.     end
  1240.   end
  1241.   -- Ensure x level is the same as starting x level if on E/W axis --
  1242.   if(tunnelHeading == 'E' or tunnelHeading == 'e' or tunnelHeading == 'W' or tunnelHeading == 'w') then
  1243.     if(tunnelStart.x ~= current.x) then
  1244.       -- If x level is not the same --
  1245.       currentHeading = goToLocation(vector.new(current.x,current.y,tunnelStart.z), currentHeading)
  1246.       setHeading(currentHeading,tunnelHeading)
  1247.       currentHeading = tunnelHeading
  1248.       turtle.back()
  1249.       current = vector.new(gps.locate())
  1250.     end
  1251.   end
  1252.   if(currentHeading ~= tunnelHeading) then
  1253.     setHeading(currentHeading,tunnelHeading)
  1254.     currentHeading = tunnelHeading
  1255.   end
  1256.   --[[
  1257.   Done
  1258.   0 : Not done, still mining
  1259.   1 : Is done, mining was successful
  1260.   -1    : Inventory full
  1261.   -2    : Insufficient fuel
  1262.   ]]--
  1263.   done = 0
  1264.   while(done == 0) do
  1265.     current = vector.new(gps.locate())
  1266.     if(current.x == nil or current.y == nil or current.z == nil) then
  1267.       -- GPS signal could not be established --
  1268.       error("Could not establish GPS signal, please ensure GPS servers are running and try again")
  1269.     else
  1270.       -- Ensure distance from tunnelStart is less than tunnelLength --
  1271.       -- Along the North/South line --
  1272.       if(tunnelHeading == 'N' or tunnelHeading == 'n' or tunnelHeading == 'S' or tunnelHeading == 's')then
  1273.         -- Check if distance from tunnelStart is within tunnelLength --
  1274.         if(math.abs(tunnelStart.z - current.z)>=tunnelLength) then
  1275.           -- Distance from tunnelStart is not within tunnelLength --
  1276.           currentHeading = goToLocation(vector.new(tunnelStart.x,tunnelStart.y,current.z),currentHeading)
  1277.           currentHeading = goToLocation(vector.new(tunnelStart.x,tunnelStart.y,tunnelStart.z),currentHeading)
  1278.           done = 1
  1279.         else
  1280.           -- Distance from tunnelStart is within tunnelLength --
  1281.           -- Check for sufficient fuel --
  1282.           if(sufficientFuel(current,tunnelStart,turtle.getFuelLevel(),fuelRemainder + 10)) then
  1283.             if(invSpace()) then
  1284.               term.clear()
  1285.               term.setCursorPos(1,1)
  1286.               print((tunnelStart.z - current.z)%branchSpacing)
  1287.               while(turtle.detect()) do
  1288.                 turtle.dig()
  1289.               end
  1290.               turtle.forward()
  1291.               while(turtle.detectUp()) do
  1292.                 turtle.digUp()
  1293.               end
  1294.               turtle.turnLeft()
  1295.               while(turtle.detect()) do
  1296.                 turtle.dig()
  1297.               end
  1298.               turtle.forward()
  1299.               if(tunnelHeading == 'N' or tunnelHeading == 'n') then
  1300.                 if((tunnelStart.z - current.z)%branchSpacing == branchSpacing - 1 and done == 0) then
  1301.                   done = mineBranch(vector.new(gps.locate()),"w","w",branchLimit,fuelRemainder+5,plist,branchTorchLength)
  1302.                   if(done == 1) then
  1303.                     done = 0
  1304.                   end
  1305.                   turtle.turnLeft()
  1306.                   turtle.turnLeft()
  1307.                 end
  1308.               else
  1309.                 if((tunnelStart.z - current.z)%branchSpacing == branchSpacing - 1 and done == 0) then
  1310.                   done = mineBranch(vector.new(gps.locate()),"e","e",branchLimit,fuelRemainder+5,plist,branchTorchLength)
  1311.                   if(done == 1) then
  1312.                     done = 0
  1313.                   end
  1314.                   turtle.turnLeft()
  1315.                   turtle.turnLeft()
  1316.                 end
  1317.               end
  1318.               while(turtle.detectUp()) do
  1319.                 turtle.digUp()
  1320.               end
  1321.               turtle.up()
  1322.               while(turtle.detectUp()) do
  1323.                 turtle.digUp()
  1324.               end
  1325.               turtle.up()
  1326.               turtle.turnLeft()
  1327.               turtle.turnLeft()
  1328.               while(turtle.detect()) do
  1329.                 turtle.dig()
  1330.               end
  1331.               turtle.forward()
  1332.               if((tunnelStart.z - current.z)%torchDistance - 1 == 0 and math.abs(tunnelStart.z - current.z) >= torchDistance - 1) then
  1333.                 turtle.turnLeft()
  1334.                 turtle.turnLeft()
  1335.                 placeTorch()
  1336.                 turtle.turnLeft()
  1337.                 turtle.turnLeft()
  1338.               end
  1339.               while(turtle.detect()) do
  1340.                 turtle.dig()
  1341.               end
  1342.               turtle.forward()
  1343.               while(turtle.detectDown()) do
  1344.                 turtle.digDown()
  1345.               end
  1346.               turtle.down()
  1347.               if((tunnelStart.z - current.z)%torchDistance - 1 == 0 and math.abs(tunnelStart.z - current.z) >= torchDistance - 1) then
  1348.                 placeTorch("up")
  1349.               end
  1350.               while(turtle.detectDown()) do
  1351.                 turtle.digDown()
  1352.               end
  1353.               turtle.down()
  1354.               if(tunnelHeading == 'S' or tunnelHeading == 's') then
  1355.                 if((tunnelStart.z - current.z)%branchSpacing == branchSpacing - 1 and done == 0) then
  1356.                   done = mineBranch(vector.new(gps.locate()),"w","w",branchLimit,fuelRemainder+5,plist,branchTorchLength)
  1357.                   if(done == 1) then
  1358.                     done = 0
  1359.                   end
  1360.                   turtle.turnLeft()
  1361.                   turtle.turnLeft()
  1362.                 end
  1363.               else
  1364.                 if((tunnelStart.z - current.z)%branchSpacing == branchSpacing - 1 and done == 0) then
  1365.                   done = mineBranch(vector.new(gps.locate()),"e","e",branchLimit,fuelRemainder+5,plist,branchTorchLength)
  1366.                   if(done == 1) then
  1367.                     done = 0
  1368.                   end
  1369.                   turtle.turnLeft()
  1370.                   turtle.turnLeft()
  1371.                 end
  1372.               end
  1373.               turtle.back()
  1374.               turtle.turnLeft()
  1375.             else
  1376.               sortInv()
  1377.               clrInv(plist)
  1378.               if(not invSpace()) then
  1379.                 currentHeading = goToLocation(vector.new(tunnelStart.x,tunnelStart.y,current.z),currentHeading)
  1380.                 currentHeading = goToLocation(vector.new(tunnelStart.x,tunnelStart.y,tunnelStart.z),currentHeading)
  1381.                 done = -1
  1382.               end
  1383.             end
  1384.           else
  1385.             if(not consumeFuel(4000)) then
  1386.               currentHeading = goToLocation(vector.new(tunnelStart.x,tunnelStart.y,current.z),currentHeading)
  1387.               currentHeading = goToLocation(vector.new(tunnelStart.x,tunnelStart.y,tunnelStart.z),currentHeading)
  1388.               done = -2
  1389.             end
  1390.           end
  1391.         end
  1392.         -- Along the East/West line --
  1393.       elseif(tunnelHeading == 'E' or tunnelHeading == 'e' or tunnelHeading == 'W' or tunnelHeading == 'w') then
  1394.         -- Check if distance from tunnelStart is within tunnelLength --
  1395.         if(math.abs(tunnelStart.x - current.x)>=tunnelLength) then
  1396.           -- Distance from tunnelStart is not within tunnelLength --
  1397.           currentHeading = goToLocation(vector.new(current.x,tunnelStart.y,tunnelStart.z),currentHeading)
  1398.           currentHeading = goToLocation(vector.new(tunnelStart.x,tunnelStart.y,tunnelStart.z),currentHeading)
  1399.           done = 1
  1400.         else
  1401.           -- Distance from tunnelStart is within tunnelLength --
  1402.           -- Check for sufficient fuel --
  1403.           if(sufficientFuel(current,tunnelStart,turtle.getFuelLevel(),fuelRemainder + 10)) then
  1404.             if(invSpace()) then
  1405.               while(turtle.detect()) do
  1406.                 turtle.dig()
  1407.               end
  1408.               turtle.forward()
  1409.               while(turtle.detectUp()) do
  1410.                 turtle.digUp()
  1411.               end
  1412.               turtle.turnLeft()
  1413.               while(turtle.detect()) do
  1414.                 turtle.dig()
  1415.               end
  1416.               turtle.forward()
  1417.               if(tunnelHeading == 'E' or tunnelHeading == 'e') then
  1418.                 if((tunnelStart.x - current.x)%branchSpacing == branchSpacing - 1 and done == 0) then
  1419.                   done = mineBranch(vector.new(gps.locate()),"n","n",branchLimit,fuelRemainder+5,plist,branchTorchLength)
  1420.                   if(done == 1) then
  1421.                     done = 0
  1422.                   end
  1423.                   turtle.turnLeft()
  1424.                   turtle.turnLeft()
  1425.                 end
  1426.               else
  1427.                 if((tunnelStart.x - current.x)%branchSpacing == branchSpacing - 1 and done == 0) then
  1428.                   done = mineBranch(vector.new(gps.locate()),"s","s",branchLimit,fuelRemainder+5,plist,branchTorchLength)
  1429.                   if(done == 1) then
  1430.                     done = 0
  1431.                   end
  1432.                   turtle.turnLeft()
  1433.                   turtle.turnLeft()
  1434.                 end
  1435.               end
  1436.               while(turtle.detectUp()) do
  1437.                 turtle.digUp()
  1438.               end
  1439.               turtle.up()
  1440.               while(turtle.detectUp()) do
  1441.                 turtle.digUp()
  1442.               end
  1443.               turtle.up()
  1444.               turtle.turnLeft()
  1445.               turtle.turnLeft()
  1446.               while(turtle.detect()) do
  1447.                 turtle.dig()
  1448.               end
  1449.               turtle.forward()
  1450.               if((tunnelStart.x - current.x)%torchDistance - 1 == 0 and math.abs(tunnelStart.x - current.x) >= torchDistance - 1) then
  1451.                 turtle.turnLeft()
  1452.                 turtle.turnLeft()
  1453.                 placeTorch()
  1454.                 turtle.turnLeft()
  1455.                 turtle.turnLeft()
  1456.               end
  1457.               while(turtle.detect()) do
  1458.                 turtle.dig()
  1459.               end
  1460.               turtle.forward()
  1461.               while(turtle.detectDown()) do
  1462.                 turtle.digDown()
  1463.               end
  1464.               turtle.down()
  1465.               if((tunnelStart.x - current.x)%torchDistance - 1 == 0 and math.abs(tunnelStart.x - current.x) >= torchDistance - 1) then
  1466.                 placeTorch("up")
  1467.               end
  1468.               while(turtle.detectDown()) do
  1469.                 turtle.digDown()
  1470.               end
  1471.               turtle.down()
  1472.               if(tunnelHeading == 'W' or tunnelHeading == 'w') then
  1473.                 if((tunnelStart.x - current.x)%branchSpacing == branchSpacing - 1 and done == 0) then
  1474.                   done = mineBranch(vector.new(gps.locate()),"n","n",branchLimit,fuelRemainder+5,plist,branchTorchLength)
  1475.                   if(done == 1) then
  1476.                     done = 0
  1477.                   end
  1478.                   turtle.turnLeft()
  1479.                   turtle.turnLeft()
  1480.                 end
  1481.               else
  1482.                 if((tunnelStart.x - current.x)%branchSpacing == branchSpacing - 1 and done == 0) then
  1483.                   done = mineBranch(vector.new(gps.locate()),"s","s",branchLimit,fuelRemainder+5,plist,branchTorchLength)
  1484.                   if(done == 1) then
  1485.                     done = 0
  1486.                   end
  1487.                   turtle.turnLeft()
  1488.                   turtle.turnLeft()
  1489.                 end
  1490.               end
  1491.               turtle.back()
  1492.               turtle.turnLeft()
  1493.             else
  1494.               sortInv()
  1495.               clrInv(plist)
  1496.               if(not invSpace()) then
  1497.                 currentHeading = goToLocation(vector.new(current.x,tunnelStart.y,tunnelStart.z),currentHeading)
  1498.                 currentHeading = goToLocation(vector.new(tunnelStart.x,tunnelStart.y,tunnelStart.z),currentHeading)
  1499.                 done = -1
  1500.               end
  1501.             end
  1502.           else
  1503.             if(not consumeFuel(4000)) then
  1504.               currentHeading = goToLocation(vector.new(current.x,tunnelStart.y,tunnelStart.z),currentHeading)
  1505.               currentHeading = goToLocation(vector.new(tunnelStart.x,tunnelStart.y,tunnelStart.z),currentHeading)
  1506.               done = -2
  1507.             end
  1508.           end
  1509.         end
  1510.       end
  1511.     end
  1512.   end
  1513.   return done
  1514. end
  1515.  
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.  
  1523. -- API requirements --
  1524. local term = require("term")
  1525. local component = require("component")
  1526. local event = require("event")
  1527. --local fileSystem = require("filesystem")
  1528. --local shell = require("shell")
  1529.  
  1530. local gps_port = 3665
  1531. component.modem.open(gps_port)
  1532. print(getHeading())
  1533.  
  1534.  
  1535.  
  1536.  
  1537.  
  1538. --[[
  1539. print("Enter length: ")
  1540. c = tonumber(io.read())
  1541.  
  1542. if(fs.exists("GPS_DATA")) then
  1543. gpsData = fs.open("GPS_DATA","r")
  1544. local start = vector.new(tonumber(gpsData.readLine()),tonumber(gpsData.readLine()),tonumber(gpsData.readLine()))
  1545. sHeading = gpsData.readLine()
  1546. if(start.x == nil or start.y == nil or start.z == nil) then
  1547. -- GPS_DATA Invalid --
  1548. term.clear()
  1549. term.setCursorPos(1,1)
  1550. error("Invalid GPS information")
  1551. else
  1552. -- GPS_DATA Valid --
  1553. term.clear()
  1554. term.setCursorPos(1,1)
  1555. print("GPS_DATA exists, start: (",start.x,",",start.y,",",start.z,")")
  1556. print("Heading: ", sHeading)
  1557. -- Search for GPS signal --
  1558. local current = vector.new(gps.locate())
  1559. if(current.x == nil or current.y == nil or current.z == nil) then
  1560. -- GPS signal could not be established --
  1561. error("Could not establish GPS signal, please ensure GPS servers are running and try again")
  1562. else
  1563. -- GPS signal established --
  1564. print("GPS locate, current: (",current.x,",",current.y,",",current.z,")")
  1565. plist = lstToTable()
  1566. lst = {4,10,plist,0}
  1567. print(mTunnel(start,sHeading,"",c,10,lstToTable(),4,4,4,0))
  1568. end
  1569. end
  1570. else
  1571. -- GPS_DATA not found --
  1572. term.clear()
  1573. term.setCursorPos(1,1)
  1574. error("File 'GPS_DATA' does not exist, please run program to initiate mining")
  1575. end
  1576. ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement