View difference between Paste ID: a2aeeGVj and 648NVQUh
SHOW: | | - or go back to the newest paste.
1
-- Function to check if turtle has sufficient fuel --
2
function sufficientFuel(vector1,vector2,fuel,remainder)
3
	local distance = math.abs(vector1.x - vector2.x) + math.abs(vector1.y - vector2.y) + math.abs(vector1.z - vector2.z)
4
	if(fuel <= distance + remainder) then
5
		return false
6
	else
7
		return true
8
	end
9
end
10
11
12
-- Function to refuel turtle appropriately --
13
function consumeFuel(maxFuel)
14
	local refuel = false
15
	for i = 1, 16 do
16
		turtle.select(i)
17
		local x = turtle.getItemDetail(i)
18
		if(x ~= nil) then
19
			local istr = string.sub(x.name,string.find(x.name,":",0)+1)
20
			print(istr,"    t")
21
			if(istr == "planks" or istr == "stick" or istr == "log") then
22
				turtle.refuel()
23
				refuel = true
24
			end
25
			if(turtle.getFuelLevel() < maxFuel and istr == "coal") then
26
				turtle.refuel(1)
27
				refuel = true
28
			end
29
		end
30
	end
31
	turtle.select(1)
32
	print(turtle.getFuelLevel())
33
	return refuel
34
end
35
36
37
-- Function to check for inv space --
38
function invSpace()
39
	for i = 1, 16 do
40
		turtle.select(i)
41
		local x = turtle.getItemDetail(i)
42
		if(x == nil or turtle.compare()) then
43
			turtle.select(1)
44
			return true
45
		end
46
	end
47
	turtle.select(1)
48
	return false
49
end
50
51
52
-- Function to sort inventory --
53
function sortInv()
54
	for i = 1, 16 do -- loop through the slots
55
		turtle.select(i)
56
		if(turtle.getItemDetail(i) ~= nil) then
57
			for c = i, 16 do
58
				if(turtle.getItemDetail(c) ~= nil) then
59
					if(turtle.compareTo(c)) then
60
						turtle.select(c)
61
						turtle.transferTo(i)
62
						turtle.select(i)
63
					end
64
				end
65
			end
66
		end
67
	end
68
	turtle.select(1)
69
end
70
71
-- Function to clear turtle inventory according to priorities list --
72
function clrInv(xtable)
73
	for i = 1, 16 do -- loop through the slots
74
		turtle.select(i)
75
		local x = turtle.getItemDetail(i)
76-
function mineVein(start,moves,back)
76+
77
			local istr = string.sub(x.name,string.find(x.name,":",0)+1)
78
			for key,value in pairs(xtable) do
79
				if(istr == value[1] and value[4] == "1") then
80
					turtle.drop()
81
				end
82
			end
83
		end
84
	end
85
	turtle.select(1)
86
end
87
88
-- Function to decide whether to mine whole ore vein based on priorities list --
89
function shouldMineWhole(istr,xlist)
90
	for key,value in pairs(xlist) do
91
		if(istr == value[1] and value[5] == "1") then
92
			return true
93
		end
94
	end
95-
		if(istr == "iron_ore") then
95+
96
end
97
98-
			mineVein(start,moves+1,false)
98+
function mineVein(start,moves,back,xtable)
99
	--Establish current GPS location--
100
	local current = vector.new(gps.locate())
101
	--Check for sufficient fuel, if not, try to refuel, if refuel fails, function return false and 
102
	--recursion tree will collapse with turtle returning to where it started--
103
	if(not sufficientFuel(start,current,turtle.getFuelLevel(),5 + moves)) then
104
		if(not consumeFuel(400)) then
105
			return false
106
		end
107
	end
108
	--Check for inventory space, if no inventory space, try to create some. if no space can be created, 
109
	--function return false and recursion tree will collapse, with turtle returning to where it started.
110-
		if(istr == "iron_ore") then
110+
111
		sortInv()
112
		clrInv(xtable)
113-
			mineVein(start,moves+1,false)
113+
114
			return false
115
		end
116
	end
117
	
118
	--Check above turtle for ores--
119
	local success,data = turtle.inspect()
120
	if(success) then
121
		local istr = string.sub(data.name,string.find(data.name,":",0)+1)
122
		print(istr)
123-
			if(istr == "iron_ore") then
123+
		if(shouldMineWhole(istr,xtable)) then
124
			turtle.dig()
125
			turtle.forward()
126-
				mineVein(start,moves+1,false)
126+
			mineVein(start,moves+1,false,xtable)
127
			turtle.back()
128
		end
129
	end
130
	if(moves == 0) then
131
		if(current.y == start.y + 1) then
132
			local success,data = turtle.inspectUp()
133
			if(success) then
134
				local istr = string.sub(data.name,string.find(data.name,":",0)+1)
135-
			if(istr == "iron_ore") then
135+
				print(istr)
136
				if(shouldMineWhole(istr,xtable)) then
137
					turtle.digUp()
138-
				mineVein(start,moves+1,false)
138+
					turtle.up()
139
					mineVein(start,moves+1,true,xtable)
140
					turtle.down()
141
				end
142
			end
143
		end
144
		--
145
		if(current.y == start.y) then
146
			local success,data = turtle.inspectDown()
147-
			if(istr == "iron_ore") then
147+
			if(success) then
148
				local istr = string.sub(data.name,string.find(data.name,":",0)+1)
149
				print(istr)
150-
				mineVein(start,moves+1,true)
150+
				if(shouldMineWhole(istr,xtable)) then
151
					turtle.digDown()
152
					turtle.down()
153
					mineVein(start,moves+1,true,xtable)
154
					turtle.up()
155
				end
156
			end
157
		end
158
	end
159-
			if(istr == "iron_ore") then
159+
160
	--will ensure turtle does not check sides on start.
161
	if(moves < 1) then
162-
				mineVein(start,moves+1,true)
162+
163
	end
164
	turtle.turnLeft()
165
	local success,data = turtle.inspect()
166
	if(success) then
167
		local istr = string.sub(data.name,string.find(data.name,":",0)+1)
168
		print(istr)
169
		if(shouldMineWhole(istr,xtable)) then
170
			turtle.dig()
171
			turtle.forward()
172
			mineVein(start,moves+1,false,xtable)
173
			turtle.back()
174-
			if(istr == "iron_ore") then
174+
175
	end
176
	if(back) then
177-
				mineVein(start,moves+1,false)
177+
178
		local success,data = turtle.inspect()
179
		if(success) then
180
			local istr = string.sub(data.name,string.find(data.name,":",0)+1)
181
			print(istr)
182
			if(shouldMineWhole(istr,xtable)) then
183
				turtle.dig()
184
				turtle.forward()
185
				mineVein(start,moves+1,false,xtable)
186-
			if(istr == "iron_ore") then
186+
187
			end
188
		end
189-
				mineVein(start,moves+1,true)
189+
190
		local success,data = turtle.inspect()
191
		if(success) then
192
			local istr = string.sub(data.name,string.find(data.name,":",0)+1)
193
			print(istr)
194
			if(shouldMineWhole(istr,xtable)) then
195
				turtle.dig()
196
				turtle.forward()
197
				mineVein(start,moves+1,false,xtable)
198-
			if(istr == "iron_ore") then
198+
199
			end
200
		end
201-
				mineVein(start,moves+1,true)
201+
202
		local success,data = turtle.inspectUp()
203
		if(success) then
204
			local istr = string.sub(data.name,string.find(data.name,":",0)+1)
205
			print(istr)
206
			if(shouldMineWhole(istr,xtable)) then
207
				turtle.digUp()
208
				turtle.up()
209
				mineVein(start,moves+1,true,xtable)
210
				turtle.down()
211
			end
212
		end
213-
mineVein(start,0,false)
213+
214
		local success,data = turtle.inspectDown()
215
		if(success) then
216
			local istr = string.sub(data.name,string.find(data.name,":",0)+1)
217
			print(istr)
218
			if(shouldMineWhole(istr,xtable)) then
219
				turtle.digDown()
220
				turtle.down()
221
				mineVein(start,moves+1,true,xtable)
222
				turtle.up()
223
			end
224
		end
225
		--
226
	else
227
		turtle.turnRight()
228
		turtle.turnRight()
229
		local success,data = turtle.inspect()
230
		if(success) then
231
			local istr = string.sub(data.name,string.find(data.name,":",0)+1)
232
			print(istr)
233
			if(shouldMineWhole(istr,xtable)) then
234
				turtle.dig()
235
				turtle.forward()
236
				mineVein(start,moves+1,false,xtable)
237
				turtle.back()
238
			end
239
		end
240
		turtle.turnLeft()
241
		local success,data = turtle.inspectUp()
242
		if(success) then
243
			local istr = string.sub(data.name,string.find(data.name,":",0)+1)
244
			print(istr)
245
			if(shouldMineWhole(istr,xtable)) then
246
				turtle.digUp()
247
				turtle.up()
248
				mineVein(start,moves+1,true,xtable)
249
				turtle.down()
250
			end
251
		end
252
		--
253
		local success,data = turtle.inspectDown()
254
		if(success) then
255
			local istr = string.sub(data.name,string.find(data.name,":",0)+1)
256
			print(istr)
257
			if(shouldMineWhole(istr,xtable)) then
258
				turtle.digDown()
259
				turtle.down()
260
				mineVein(start,moves+1,true,xtable)
261
				turtle.up()
262
			end
263
		end
264
		--
265
	end
266
	return true
267
end
268
269
gpsData = fs.open("GPS_DATA","r")
270
local start = vector.new(gps.locate())
271
272
if(fs.exists("listed")) then
273
	ifile = fs.open("listed","r")
274
	xlist = {}
275
	x = split(ifile.readLine(),',')
276
	while(x ~= nil and x ~= "") do
277
		table.insert(xlist, x)
278
		x = split(ifile.readLine(),',')
279
	end
280
else
281
	print("Listed file does not exist, cannot continue")
282
end
283
284
mineVein(start,0,false,xlist)