View difference between Paste ID: a1m42gxN and iRaQnV4F
SHOW: | | - or go back to the newest paste.
1
term.clear()
2
term.setCursorPos(1,1)
3
sel = 0
4
fuelThreshold = 300
5
if turtle.getFuelLevel() == "unlimited" then
6
        turtle.getFuelLevel = function()
7
                return 2 + fuelThreshold
8
        end
9
end
10
if fs.exists("coords") == false then
11
	print("Hi, I am your mining robot!\n\nI will need to ask you a couple of questions before I start mining:\n\nWhat diameter would you like me to mine?\n")
12
	 setSize =read()
13
	print("\nCool! Now insert the turtles current y coordinate\n")
14
	input = read()
15
	setDepth = input-5
16
	print("\nJust one more thing- put any blocks you don't want me to mine in slots 1-4 and press enter.. or just leave it empty to mine everything (I will be slower the more you put there)\n")
17
	temp=read()
18
	setJunkSlots = 0
19
	for i = 1,4 do
20
		if turtle.getItemCount(i) ~= 0 then
21
			setJunkSlots = setJunkSlots + 1
22
		end
23
	end
24
	print("\nOkay, I will start mining now :D")
25
	
26
	
27
	w = fs.open("coords","w")
28
	w.write(setSize.."\n"..setDepth.."\n1\n1\n1\n0\n"..setJunkSlots)
29
	w.close()
30
	w = fs.open("startup","w")
31
	w.write('shell.run("dig")')
32
	w.close()
33
	sleep(3)
34
	term.clear()
35
	term.setCursorPos(1,1)
36
	
37
end
38
39
40
h = fs.open("coords","r")
41
size = tonumber(h.readLine(1))
42
depth = tonumber(h.readLine(2))
43
x = tonumber(h.readLine(3))
44
y = tonumber(h.readLine(4))
45
z = tonumber(h.readLine(5))
46
facing = tonumber(h.readLine(6))
47
junkSlots = tonumber(h.readLine(7))
48
h.close()
49
 
50
--Gets the necessary variables from the file coords
51
52
function drop(fslot,lslot)
53
	for i=fslot,lslot do
54
		turtle.select(i)
55
		while turtle.drop() == false and turtle.getItemCount(i) ~= 0 do
56
			print("Can't drop items- full?")
57
			sleep(2)
58
		end
59
	end
60
end
61
function save()
62
		os.queueEvent("test")
63
		coroutine.yield()
64
        s = fs.open("coords","w")
65
        s.writeLine(size)
66
        s.writeLine(depth)
67
        s.writeLine(x)
68
        s.writeLine(y)
69
        s.writeLine(z)
70
        s.writeLine(facing)
71
		s.writeLine(junkSlots)
72
        s.close()
73
end
74
--save function, prints the turtles orientation and coords to a file
75
function rflforw()
76
	if turtle.forward() == true then
77
		if facing == 0  then
78
		  x = x+1
79
		  save()
80
		elseif facing == 1 then
81
		  y= y-1
82
		  save()
83
		elseif facing == 2  then
84
		  x = x - 1
85
		  save()
86
		elseif facing == 3 then
87
		  y = y + 1
88
		  save()
89
		else
90
			error("Invalid facing variable")
91
		end
92
	else
93
		print("Can't move forward, trying again...")
94
		while turtle.forward() == false do
95
			turtle.dig()
96
			sleep(0.3)
97
		end
98
		if facing == 0  then
99
		  x = x+1
100
		  save()
101
		elseif facing == 1 then
102
		  y= y-1
103
		  save()
104
		elseif facing == 2  then
105
		  x = x - 1
106
		  save()
107
		elseif facing == 3 then
108
		  y = y + 1
109
		  save()
110
		end
111
	end
112
end
113
--refuel-less forward function
114
function dig()
115
  turtle.dig()
116
end
117
function digdown()
118
   turtle.digDown()
119-
	if turtle.getFuelLevel() < 300 then
119+
120
function digup()
121
  turtle.digUp()
122
end
123
function refuel()
124
	local char = ""
125
	if turtle.getFuelLevel() < fuelThreshold then
126
		if turtle.getItemCount(16) == 0 then
127
			oldfacing = facing
128
			oldx = x
129
			oldy = y
130
			oldz = z
131
			while facing ~= 1 do
132
				left()
133
			end
134
			while y ~= 1 do
135
				rflforw()
136
			end
137
			while facing ~= 2 do
138
				left()
139
			end
140
			while x ~= 1 do
141
				rflforw()
142
			end
143
			while z ~= 1 do
144
				up()
145
			end
146
			drop(junkSlots + 1, 15)
147
			while facing ~= 1 do
148
				left()
149
			end
150
			turtle.select(16)
151
			turtle.drop()
152
			turtle.suck()
153
			while turtle.getItemCount(16) < 16 do
154
				print('Not enough fuel, Refill the chest and press enter')
155
				temp=read()
156
				turtle.drop()
157
				turtle.suck()
158
				turtle.refuel(1)
159
			end
160
			while facing ~= 0 do
161
				left()
162
			end
163
			while z ~= oldz do
164
				down()
165
			end
166
			while x ~= oldx do
167
				rflforw()
168
			end
169
			while facing ~= 3 do
170
					left()
171
			end
172
			while y ~= oldy do
173
					forw()
174
			end
175
			while facing ~= oldfacing do
176
					left()
177-
					if turtle.getFuelLevel() > 300 then
177+
178
			else
179
				while true do
180
					turtle.select(16)
181
					turtle.refuel(1)
182
					turtle.select(1)
183
					if turtle.getFuelLevel() > fuelThreshold then
184
						break
185
					elseif turtle.getItemCount(16) == 0 then
186
						refuel()
187
					end
188
				end
189
			end
190
	end
191
end
192
function forw()
193
    refuel()
194
	if turtle.forward() == true then
195
		if facing == 0  then
196
		  x = x+1
197
		  save()
198
		elseif facing == 1 then
199
		  y= y-1
200
		  save()
201
		elseif facing == 2  then
202
		  x = x - 1
203
		  save()
204
		elseif facing == 3 then
205
		  y = y + 1
206
		  save()
207
		else
208
			error("Invalid facing variable")
209
		end
210
	else
211
		print("Can't move forward, trying again...")
212
		while turtle.forward() == false do
213
			turtle.dig()
214
			sleep(0.3)
215
		end
216
		if facing == 0  then
217
		  x = x+1
218
		  save()
219
		elseif facing == 1 then
220
		  y= y-1
221
		  save()
222
		elseif facing == 2  then
223
		  x = x - 1
224
		  save()
225
		elseif facing == 3 then
226
		  y = y + 1
227
		  save()
228
		end
229
	end
230
end
231
function down()
232
        if turtle.down() == true then
233
                z = z + 1
234
        else
235
				print("Something is blocking my way down, trying again!")
236
                while turtle.down() == false do
237
                        sleep(0.3)
238
						digdown()
239
                end
240
				z= z + 1
241
        end
242
                       
243
end
244
function up()
245
        if turtle.up() == true then
246
                z = z - 1
247
        else
248
				print("Something is blocking my way up, trying again!")
249
                while turtle.up() == false do
250
                        sleep(0.3)
251
						digup()
252
                end
253
				z= z - 1
254
        end
255
                       
256
end
257
function left()
258
        turtle.turnLeft()
259
        facing = facing - 1
260
        facing = facing % 4
261
        save()
262
end
263
function right()
264
        turtle.turnRight()
265
        facing = facing + 1
266
        facing = facing % 4
267
        save()
268
end
269
function comine()
270
	local isJunkd = false
271
	local isJunku = false
272
	sel = sel + 1
273
	if junkSlots == 0 then
274
		sel = 0
275
	else
276
		sel = sel % junkSlots
277
	end
278
	if sel == 0 then
279
		for i = 1 , junkSlots do
280
			turtle.select(i)
281
			if turtle.compareUp()== true then
282
				isJunku = true
283
			end
284
			if turtle.compareDown()== true then
285
				isJunkd = true
286
			end
287
		end
288
	else
289
		for i = junkSlots , 1, -1 do
290
			turtle.select(i)
291
			if turtle.compareUp()== true then
292
				isJunku = true
293
			end
294
			if turtle.compareDown()== true then
295
				isJunkd = true
296
			end
297
		end
298
	end
299
	if isJunku == false then
300
		digup()
301
	end
302
	if isJunkd == false then
303
		digdown()
304
	end
305
		
306
end
307
function diglayer()
308
        for j = 1,size - 1 do
309
                        comine()
310
                        dig()
311
                        forw()
312
                        if turtle.getItemCount(15) > 0 then
313
                                oldfacing = facing
314
                                oldx = x
315
                                oldy = y
316
                                oldz = z
317
                                while facing ~= 1 do
318
                                        left()
319
                                end
320
                                while y ~= 1 do
321
                                        forw()
322
                                end
323
                                while facing ~= 2 do
324
                                        left()
325
                                end
326
                                while x ~= 1 do
327
                                        forw()
328
                                end
329
                                while z ~= 1 do
330
                                        up()
331
                                end
332
                                drop(junkSlots + 1, 15)
333
                                turtle.select(sel + 1)
334
                                while facing ~= 0 do
335
                                        left()
336
                                end
337
                                while z ~= oldz do
338
                                        down()
339
                                end
340
                                while x ~= oldx do
341
                                        forw()
342
                                end
343
                                while facing ~= 3 do
344
                                        left()
345
                                end
346
                                while y ~= oldy do
347
                                        forw()
348
                                end
349
                                while facing ~= oldfacing do
350
                                        left()
351
                                end
352
                        end
353
                end
354
        end
355
function program()
356
	if size % 2 == 0 then
357
		for i = 1 , size / 2 - 1 do
358
			diglayer()
359
			comine()
360
			left()
361
			dig()
362
			forw()
363
			left()
364
			diglayer()
365
			comine()
366
			right()
367
			dig()
368
			forw()
369
			right()
370
		end
371
		diglayer()
372
		comine()
373
		left()
374
		dig()
375
		forw()
376
		left()
377
		diglayer()
378
		comine()
379
		left()
380
		for i = 1, size-1 do
381
			forw()
382
		end
383
		left()
384
	else
385
		for i = 1, (size - 1)/2 do
386
			diglayer()
387
			comine()
388
			left()
389
			dig()
390
			forw()
391
			left()
392
			diglayer()
393
			comine()
394
			right()
395
			dig()
396
			forw()
397
			right()
398
		end
399
		diglayer()
400
		comine()
401
		right()
402
		while y ~= 1 do
403
			forw()
404
		end
405
		right()
406
		while x ~= 1 do
407
			forw()
408
		end
409
		left()
410
		left()
411
	end
412
end
413
refuel()
414
refuel()
415
if x ~= 1 or y ~= 1 or facing ~= 0 then
416
        while facing ~= 1 do
417
            left()
418
        end
419
        while y ~= 1 do
420
            forw()
421
        end
422
        while facing ~= 2 do
423
            right()
424
        end
425
        while x ~= 1 do
426
            forw()
427
        end
428
        while facing ~= 0 do
429
            left()
430
        end
431
end
432
while z ~= depth do
433
        program()
434
        for i = 1, 3 do
435
			if z == depth then
436
				break
437
			end
438
			digdown()
439
			down()
440
		end
441
end
442
program()
443
while facing ~=2 do
444
        left()
445
end
446
while z ~= 1 do
447
        up()
448
end
449
drop(1,16)
450
fs.delete("startup")
451
fs.delete("coords")
452
print("I am done now :D")