View difference between Paste ID: dSA6QRrF and gZtywPm1
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
	w = fs.open("coords2","w")
38
	w.write(setSize.."\n"..setDepth.."\n1\n1\n1\n0\n"..setJunkSlots)
39
	w.close()
40
	w = fs.open("startup","w")
41
	w.write('shell.run("dig")')
42
	w.close()
43
	sleep(3)
44
	term.clear()
45
	term.setCursorPos(1,1)
46
	
47
end
48
49
--Gets the necessary variables from the file coords
50
51
function Load()
52
	h = fs.open("coords","r")
53
	size = tonumber(h.readLine(1))
54
	depth = tonumber(h.readLine(2))
55-
--A backup file in case the first one is corrupted.
55+
56
	y = tonumber(h.readLine(4))
57
	z = tonumber(h.readLine(5))
58
	facing = tonumber(h.readLine(6))
59
	junkSlots = tonumber(h.readLine(7))
60
	h.close()
61
end
62
function Load2()
63
	h = fs.open("coords2","r")
64
	size = tonumber(h.readLine(1))
65
	depth = tonumber(h.readLine(2))
66
	x = tonumber(h.readLine(3))
67
	y = tonumber(h.readLine(4))
68
	z = tonumber(h.readLine(5))
69
	facing = tonumber(h.readLine(6))
70
	junkSlots = tonumber(h.readLine(7))
71
	h.close()
72
end
73
function drop(fslot,lslot)
74
	for i=fslot,lslot do
75
		turtle.select(i)
76
		while turtle.drop() == false and turtle.getItemCount(i) ~= 0 do
77-
--Currently saves to two files for backup purposes. Will be broken up later for unloaded chunk handling.
77+
78
			sleep(2)
79
		end
80
	end
81
end
82
function save()
83
        s = fs.open("coords","w")
84
        s.writeLine(size)
85
        s.writeLine(depth)
86
        s.writeLine(x)
87
        s.writeLine(y)
88
        s.writeLine(z)
89
        s.writeLine(facing)
90
		s.writeLine(junkSlots)
91
        s.close()
92
		
93
        s = fs.open("coords2","w")
94
        s.writeLine(size)
95
        s.writeLine(depth)
96
        s.writeLine(x)
97
        s.writeLine(y)
98
        s.writeLine(z)
99
        s.writeLine(facing)
100
		s.writeLine(junkSlots)
101
        s.close()
102
end
103
--save function, prints the turtles orientation and coords to a file
104
function rflforw()
105
	if turtle.forward() == true then
106
		if facing == 0  then
107
		  x = x+1
108
		  save()
109
		elseif facing == 1 then
110
		  y= y-1
111
		  save()
112
		elseif facing == 2  then
113
		  x = x - 1
114
		  save()
115
		elseif facing == 3 then
116
		  y = y + 1
117
		  save()
118
		else
119
			error("Invalid facing variable")
120
		end
121
	else
122
		print("Can't move forward, trying again...")
123
		while turtle.forward() == false do
124
			turtle.dig()
125
			sleep(0.3)
126
		end
127
		if facing == 0  then
128
		  x = x+1
129
		  save()
130
		elseif facing == 1 then
131
		  y= y-1
132
		  save()
133
		elseif facing == 2  then
134
		  x = x - 1
135
		  save()
136
		elseif facing == 3 then
137
		  y = y + 1
138
		  save()
139
		end
140
	end
141
end
142
--refuel-less forward function
143
function dig()
144
  turtle.dig()
145
end
146
function digdown()
147
   turtle.digDown()
148
end
149
function digup()
150
  turtle.digUp()
151
end
152
function refuel()
153
	local char = ""
154
	if turtle.getFuelLevel() < fuelThreshold then
155
		if turtle.getItemCount(16) == 0 then
156
			oldfacing = facing
157
			oldx = x
158
			oldy = y
159
			oldz = z
160
			while facing ~= 1 do
161
				left()
162
			end
163
			while y ~= 1 do
164
				rflforw()
165
			end
166
			while facing ~= 2 do
167
				left()
168
			end
169
			while x ~= 1 do
170
				rflforw()
171
			end
172
			while z ~= 1 do
173
				up()
174
			end
175
			drop(junkSlots + 1, 15)
176
			while facing ~= 1 do
177
				left()
178
			end
179
			turtle.select(16)
180
			turtle.drop()
181
			turtle.suck()
182
			while turtle.getItemCount(16) < 16 do
183
				print('Not enough fuel, Refill the chest and press enter')
184
				temp=read()
185
				turtle.drop()
186
				turtle.suck()
187
				turtle.refuel(1)
188
			end
189
			while facing ~= 0 do
190
				left()
191
			end
192
			while z ~= oldz do
193
				down()
194
			end
195
			while x ~= oldx do
196
				rflforw()
197
			end
198
			while facing ~= 3 do
199
					left()
200
			end
201
			while y ~= oldy do
202
					forw()
203
			end
204
			while facing ~= oldfacing do
205
					left()
206
			end
207
			else
208
				while true do
209
					turtle.select(16)
210
					turtle.refuel(1)
211
					turtle.select(1)
212
					if turtle.getFuelLevel() > fuelThreshold then
213
						break
214
					elseif turtle.getItemCount(16) == 0 then
215
						refuel()
216
					end
217
				end
218
			end
219
	end
220
end
221
function forw()
222
    refuel()
223
	if turtle.forward() == true then
224
		if facing == 0  then
225
		  x = x+1
226
		  save()
227
		elseif facing == 1 then
228
		  y= y-1
229
		  save()
230
		elseif facing == 2  then
231
		  x = x - 1
232
		  save()
233
		elseif facing == 3 then
234
		  y = y + 1
235
		  save()
236
		else
237
			error("Invalid facing variable")
238
		end
239
	else
240
		print("Can't move forward, trying again...")
241
		while turtle.forward() == false do
242
			turtle.dig()
243
			sleep(0.3)
244
		end
245
		if facing == 0  then
246
		  x = x+1
247
		  save()
248
		elseif facing == 1 then
249
		  y= y-1
250
		  save()
251
		elseif facing == 2  then
252
		  x = x - 1
253
		  save()
254
		elseif facing == 3 then
255
		  y = y + 1
256
		  save()
257
		end
258
	end
259
end
260
function down()
261
        if turtle.down() == true then
262
                z = z + 1
263
        else
264
				print("Something is blocking my way down, trying again!")
265
                while turtle.down() == false do
266
                        sleep(0.3)
267
						digdown()
268
                end
269
				z= z + 1
270
        end
271
                       
272
end
273
function up()
274
        if turtle.up() == true then
275
                z = z - 1
276
        else
277
				print("Something is blocking my way up, trying again!")
278
                while turtle.up() == false do
279
                        sleep(0.3)
280
						digup()
281
                end
282
				z= z - 1
283
        end
284
                       
285
end
286
function left()
287
        turtle.turnLeft()
288
        facing = facing - 1
289
        facing = facing % 4
290
        save()
291
end
292
function right()
293
        turtle.turnRight()
294
        facing = facing + 1
295
        facing = facing % 4
296
        save()
297
end
298
function comine()
299
	local isJunkd = false
300
	local isJunku = false
301
	sel = sel + 1
302
	if junkSlots == 0 then
303
		sel = 0
304
	else
305
		sel = sel % junkSlots
306
	end
307
	if sel == 0 then
308
		for i = 1 , junkSlots do
309
			turtle.select(i)
310
			if turtle.compareUp()== true then
311
				isJunku = true
312
			end
313
			if turtle.compareDown()== true then
314
				isJunkd = true
315
			end
316
		end
317
	else
318
		for i = junkSlots , 1, -1 do
319
			turtle.select(i)
320
			if turtle.compareUp()== true then
321
				isJunku = true
322
			end
323
			if turtle.compareDown()== true then
324
				isJunkd = true
325
			end
326
		end
327
	end
328
	if isJunku == false then
329
		digup()
330
	end
331
	if isJunkd == false then
332
		digdown()
333
	end
334
		
335
end
336
function diglayer()
337
        for j = 1,size - 1 do
338
                        comine()
339
                        dig()
340
                        forw()
341
                        if turtle.getItemCount(15) > 0 then
342
                                oldfacing = facing
343
                                oldx = x
344
                                oldy = y
345
                                oldz = z
346
                                while facing ~= 1 do
347
                                        left()
348
                                end
349
                                while y ~= 1 do
350
                                        forw()
351
                                end
352
                                while facing ~= 2 do
353
                                        left()
354
                                end
355
                                while x ~= 1 do
356
                                        forw()
357
                                end
358
                                while z ~= 1 do
359
                                        up()
360
                                end
361
                                drop(junkSlots + 1, 15)
362
                                turtle.select(sel + 1)
363
                                while facing ~= 0 do
364
                                        left()
365
                                end
366
                                while z ~= oldz do
367
                                        down()
368
                                end
369
                                while x ~= oldx do
370
                                        forw()
371
                                end
372
                                while facing ~= 3 do
373
                                        left()
374
                                end
375
                                while y ~= oldy do
376
                                        forw()
377
                                end
378
                                while facing ~= oldfacing do
379
                                        left()
380
                                end
381
                        end
382
                end
383
        end
384
function program()
385
	if size % 2 == 0 then
386
		for i = 1 , size / 2 - 1 do
387
			diglayer()
388
			comine()
389
			left()
390
			dig()
391
			forw()
392
			left()
393
			diglayer()
394
			comine()
395
			right()
396
			dig()
397
			forw()
398
			right()
399
			sleep(0.1)
400
		end
401
		diglayer()
402
		comine()
403
		left()
404
		dig()
405
		forw()
406
		left()
407
		diglayer()
408
		comine()
409
		left()
410
		for i = 1, size-1 do
411
			forw()
412
		end
413
		left()
414
	else
415
		for i = 1, (size - 1)/2 do
416
			diglayer()
417
			comine()
418
			left()
419
			dig()
420
			forw()
421
			left()
422
			diglayer()
423
			comine()
424
			right()
425
			dig()
426
			forw()
427
			right()
428
			sleep(0.1)
429
		end
430
		diglayer()
431
		comine()
432
		right()
433
		while y ~= 1 do
434
			forw()
435
		end
436
		right()
437
		while x ~= 1 do
438
			forw()
439
		end
440
		left()
441
		left()
442
	end
443
end
444
if(pcall(Load) == false) then
445
	Load2()
446
end
447
refuel()
448
refuel()
449
if x ~= 1 or y ~= 1 or facing ~= 0 then
450
        while facing ~= 1 do
451
            left()
452
        end
453
        while y ~= 1 do
454
            forw()
455
        end
456
        while facing ~= 2 do
457
            right()
458
        end
459
        while x ~= 1 do
460
            forw()
461
        end
462
        while facing ~= 0 do
463
            left()
464
        end
465
end
466
while z ~= depth do
467
        program()
468
        for i = 1, 3 do
469
			if z == depth then
470
				break
471
			end
472
			digdown()
473
			down()
474
		end
475
end
476
program()
477
while facing ~=2 do
478
        left()
479
end
480
while z ~= 1 do
481
        up()
482
end
483
drop(1,16)
484
fs.delete("startup")
485
fs.delete("coords")
486
print("I am done now :D")