Advertisement
montana_1

BranchMining - oc

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