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