View difference between Paste ID: 7eru6x78 and T4dbE7We
SHOW: | | - or go back to the newest paste.
1
--This code is developed by me (onix331) and is available under the GNU General Public License (Version 3)
2
--For all CC-Versions Universal
3
--See http://opensource.org/licenses/GPL-3.0
4
--Have fun
5
6
space = 16--Defines the space of the turtle (If an update comes out and I don't update the program)
7
ver = "2.8"--For updates
8
status = "---"--You can modify the init-status that will be transferred to the host at the beginning
9
10
--It is usually not necessary to modify the next variables 
11
12
fw = 0--Unnecessary to know:P
13
chest = 0--If the turtle has stopped at a chest
14
stop = 0--If the turtle has stopped at a chest
15
shaftrun = 0--How many shafts the turtle has run
16
senderId = 65534--Because RedNet is a bitch:D
17
gravel = 3--Used to avoid gavel, sand, etc.
18
19
function startturtle()
20
	function smartfuel()--Check for fuel and asks for refueling
21
		clear()
22
		local fuelneeded = (length * count) * 2 + count * 3 --Calculate Fuel for mining blocks
23
		local movefuelneeded = ((length * count) * 2 + count) * 0.6  --Calculate Fuel for movement
24
		local fuelneeded = fuelneeded + movefuelneeded
25
		if turtle.getFuelLevel() < fuelneeded then
26
			insert = fuelneeded - turtle.getFuelLevel()
27
			print ("Current fuel-level is too low (", turtle.getFuelLevel(), ")")
28
			print ("Needed:" , fuelneeded)
29
			print ("Please insert ", insert, " fuel anywhere")
30
			print ("When done, press ENTER")
31
			debugfuel = turtle.getFuelLevel()
32
			while true do
33
				local sEvent, param = os.pullEvent("key")
34
				if sEvent == "key" then
35
					if param == 28 then
36
						for i = 1, space do
37
							for i = 1, 64 do
38
								clear()
39
								if turtle.getFuelLevel() < fuelneeded then
40
									shell.run("refuel")
41
								else
42
									turtle.select(1)
43
									break
44
								end
45
							end
46
							if debugfuel > turtle.getFuelLevel() then
47
								turtle.select(debugi)
48
							else
49
								debugi = i
50
								turtle.select(i)
51
							end
52
						end
53
						turtle.select(1)
54
						break
55
					end
56
				end
57
			end
58
		if turtle.getFuelLevel() < fuelneeded then
59
			smartfuel()
60
		end
61
		end
62
	end
63
64
	function pair()
65
		print ("Sending pairing requests")
66
		local i = 0
67
		rednet.broadcast("pair")
68
		os.sleep(0.1)
69
		rednet.broadcast("pair")
70
		os.sleep(0.1)
71
		rednet.broadcast("pair")
72
		os.sleep(0.1)
73
		rednet.broadcast("pair")
74
		os.sleep(0.1)
75
		rednet.broadcast("pair")
76
		os.sleep(0.1)
77
		rednet.broadcast("pair")
78
		os.sleep(0.1)
79
		senderId, message, protocol = rednet.receive()
80
		if message=="succesfull" then
81
			print("Succesfull paired, continuing...")
82
			os.sleep(0.1)
83
		end
84
	end
85
86
	function clear()--To clear the screen
87
		term.clear()
88
		term.setCursorPos(1,1)
89
	end
90
91
	function sendinitial()--Send the initial data
92
		if enablerednet=="y" then
93
			os.sleep(1)
94
			rednet.send(senderId, length)
95
			os.sleep(0.1)
96
			rednet.send(senderId, count)
97
			os.sleep(0.1)
98
			rednet.send(senderId, direction)
99
			os.sleep(0.1)
100
		end
101
	end
102
103
	function senddata()--To send all the data
104
		if enablerednet=="y" then
105
			rednet.send(senderId, lengthleft)
106
			rednet.send(senderId, count)
107
			rednet.send(senderId, turtle.getFuelLevel())
108
			rednet.send(senderId, status)
109
		end
110
	end
111
112
	function main()--Main Program
113
		smartfuel()
114
		if enablerednet=="y" then
115
			pair()
116
			sendinitial()
117
		end
118
		status = "Called main"
119
		senddata()
120
		if called==1 then
121
			called = 0
122
			countn = countn - 1
123
		end
124
		while true do
125
			if tonumber(count)>=1 then	
126
				senddata()
127
				fw = 1
128
				move()
129
				turnaround()
130
				fw = 0
131
				move()
132
				count = count - 1
133
				checkspace()
134
			else
135
				break
136
			end
137
		end
138
	end
139
140
	function returnmining()--To return to the latest shaft
141
		status = "Return to mine..."
142
		senddata()
143
		chest = 0
144
		stop = 1
145
		if direction=="r" then
146
			turtle.turnRight()
147
			for i = 0, shaftrun do
148
				turtle.forward()
149
				turtle.forward()
150
				turtle.forward()
151
			end
152
			turtle.turnLeft()
153
			main()
154
		elseif direction=="l" then
155
			turtle.turnLeft()
156
			for i = 0, shaftrun do
157
				turtle.forward()
158
				turtle.forward()
159
				turtle.forward()
160
			end
161
			turtle.turnRight()
162
			main()
163
		end
164
	end
165
166
	function checkl() --To check if length is valid (>0, not a String), also transforms the string into a number
167
		length = tonumber(length)
168
		if length==nil then
169
			print ("Invalid number!")
170
			length = read()
171
			checkl()
172
		elseif length <= 0 then
173
			print ("Invalid number!")
174
			length = read()
175
			checkl()
176
		end
177
	end
178
179
	function checka()--To check if count is valid (>0, not a String), also transforms the string into a number
180
		count = tonumber(count)
181
		if count==nil then
182
			print ("Invalid number!")
183
			count = read()
184
			checka()
185
		elseif count <= 0 then
186
			print ("Invalid number!")
187
			count = read()
188
			checka()
189
		end
190
	end
191
192
	function checkr()--To check if direction is valid ((r) or (l))
193
		if direction=="r" then
194
			print ("")
195
		elseif direction=="l" then
196
			print ("")
197
		else
198
			print ("Invalid input!")
199
			direction = io.read()
200
			checkr()
201
		end
202
	end
203
204
	function checkfue()--To check if fuel is valid (ok))
205
		if fuel=="ok" then
206
			print ("")
207
		else
208
			print ("Invalid input!")
209
			fuel = io.read()
210
			checkfue()
211
		end
212
	end
213
214
	function checkred()--To check if enablerednet is valid ((y) or (n))
215
		if enablerednet=="y" then
216
			print ("")
217
		elseif enablerednet=="n" then
218
			print ("")
219
		else
220
			print ("Invalid input!")
221
			enablerednet = io.read()
222
			checkred()
223
		end
224
	end
225
226
	function move()--Basic movement function
227
		status = "Moving..."
228
		for i = 1, length do
229
			senddata()
230
			for i = 1, gravel do
231
				turtle.dig()
232
			end
233
			turtle.forward()
234
			if fw==1 then
235
				lengthleft = lengthleft - 1
236
			end
237
			turtle.digDown()
238
			for i = 1, gravel do
239
				turtle.digUp()
240
			end
241
			for i = 1, gravel do
242
				turtle.digDown()
243
			end
244
		end
245
	end
246
247
	function turnaround()--Turns the turtle at 90 degrees
248
		status = "Turning around..."
249
		senddata()
250
		turtle.turnRight()
251
		turtle.turnRight()
252
	end
253
254
	function nextshaftr()--Next shaft (right)
255
		status = "Going to next shaft..."
256
		senddata()
257
		countn = countn - 1
258
		if countn>=2 then
259
			shaftrun = shaftrun + 1
260
			lengthleft = lengthleftstore
261
			turtle.turnLeft()
262
			turtle.dig()
263
			turtle.digUp()
264
			turtle.digDown()
265
			turtle.forward()
266
			turtle.dig()
267
			turtle.digUp()
268
			turtle.digDown()
269
			turtle.forward()
270
			senddata()
271
			turtle.dig()
272
			turtle.digUp()
273
			turtle.digDown()
274
			turtle.forward()
275
			turtle.digUp()
276
			turtle.digDown()
277
			turtle.turnLeft()
278
		end
279
	end
280
281
	function nextshaftl()--Next shaft (left)
282
		status = "Going to next shaft..."
283
		senddata()
284
		countn = countn - 1
285
		if countn>=2 then	
286
			shaftrun = shaftrun + 1
287
			lengthleft = lengthleftstore
288
			turtle.turnRight()
289
			turtle.dig()
290
			turtle.digUp()
291
			turtle.digDown()
292
			turtle.forward()
293
			turtle.dig()
294
			turtle.digUp()
295
			turtle.digDown()
296
			turtle.forward()
297
			senddata()
298
			turtle.dig()
299
			turtle.digUp()
300
			turtle.digDown()
301
			turtle.forward()
302
			turtle.digUp()
303
			turtle.digDown()
304
			turtle.turnRight()
305
		end
306
	end
307
308
	function returnpoint()--Return to initial point (Chest)
309
		status = "Returning to chest..."
310
		senddata()
311
		if direction=="r" then
312
			turtle.turnRight()
313
			for i = 1, shaftrun do
314
				turtle.forward()
315
				turtle.forward()
316
				senddata()
317
				turtle.forward()
318
			end
319
			if stop==1 then
320
				turtle.forward()
321
				turtle.forward()
322
				senddata()
323
				turtle.forward()
324
			end
325
			turtle.turnLeft()
326
			status = "Dropping things in chest..."
327
			senddata()
328
			for i = 1, space do
329
				turtle.select(i)
330
				turtle.drop()
331
			end
332
			turtle.select(1)
333
		elseif direction=="l" then
334
			turtle.turnLeft()
335
			for i = 1, shaftrun do
336
				turtle.forward()
337
				turtle.forward()
338
				senddata()
339
				turtle.forward()
340
			end
341
			if stop==1 then
342
				turtle.forward()
343
				turtle.forward()
344
				senddata()
345
				turtle.forward()
346
			end
347
			turtle.turnRight()
348
			status = "Dropping things in chest..."
349
			senddata()
350
			for i = 1, space do
351
				turtle.select(i)
352
				turtle.drop()
353
			end
354
			turtle.select(1)
355
		end
356
		turnaround()
357
		if chest==1 then
358
			returnmining()
359
		end
360
	end
361
362
	function checkspace()--Check for space and if necessary, return to initial point to the chest
363
		status = "Checking space"
364
		senddata()
365
		local spaceleft = 0
366
		for i = 1, space do
367
			if turtle.getItemSpace(i) > 10 then
368
				spaceleft = spaceleft + 1
369
			end
370
		end
371
		if spaceleft < 3 then
372
			chest = 1
373
			called = 1
374
			status = "Inventory full, returning to chest"
375
			returnpoint()
376
		elseif direction=="r" then
377
			nextshaftr()
378
		else
379
			nextshaftl()
380
		end
381
	end
382
383
	--Print header
384
	clear()
385
	print ("Strip-mining-program by onix331")
386
	print ("Version ", ver)
387
	os.sleep(2)
388
389
	--Start user input and define some variables
390
	clear()
391
	print ("Length of the single shaft(s):(Type help for help)")
392
	length = read()
393
	if length=="help" then
394
		print ("How long the shafts the turtle will mine be")
395
		length = read()
396
		checkl()
397
	else
398
		checkl()
399
	end
400
	lengthleft = length
401
	lengthleftstore = length
402
403
	clear()
404
	print ("Count of the shafts:(Type help for help)")
405
	count = read()
406
	if count=="help" then
407
		print ("How much shafts the turtle will mine")
408
		count = read()
409
		checka()
410
	else
411
	end
412
	countn = count
413
	countn = countn + 1
414
415
	clear()
416
	print ("Direction of the shafts (Left (l), Right (r):(Type help for help)")
417
	direction = read()
418
	if direction=="help" then
419
		print ("In which direction the shafts will go")
420
		direction = read()
421
		checkr()
422
	else
423
		checkr()
424
	end
425
426
	clear()
427
	print ("Use rednet? (Yes (y), No (n):(Type help for help)")
428
	enablerednet = read()
429
	if enablerednet=="help" then
430
		print ("If RedNet and a hostcomputer should be used (See my thread at http://www.computercraft.info/forums2/index.php?/topic/19835-mining-turtle-stripmining/page__fromsearch__1")
431
		enablerednet = read()
432
	else
433
		checkred()
434
		end
435
	if enablerednet=="y" then
436
		rednet.open("right")--Defines the place of the modem (Usually right on the turtle)
437
	end
438
439
	--Print user inputs
440
	clear()
441
	print ("Input of Length:")
442
	print (length)
443
	print ("Input of count")
444
	print (count)
445
	print ("Direction of the shafts:")
446
	if direction=="r" then
447
		print (direction, "ight")
448
	else
449
		print (direction, "eft")
450
	end
451
452
	print ("3")
453
	os.sleep(1)
454
	print ("2")
455
	os.sleep(1)
456
	print ("1")
457
458
459
	--Fire up the main program
460
	main()
461
462
	returnpoint()
463
464
	status = "Done!"
465
	senddata()--Sends the status "Done!" to the host
466
	print ("Done!")
467
end
468
469
function startcomputer()
470
rednet.open("right")
471
ver = "2.8"--For updates
472
status = "---"--Because i can
473
474
function clear()--To clear the screen and set the cursor to 1,1
475
	term.clear()
476
	term.setCursorPos(1,1)
477
end
478
479
	function receive()
480
		senderId, length, protocol = rednet.receive()
481
		storedlength = length
482
		senderId, count, protocol = rednet.receive()
483
		storedcount = count
484
		senderId, direction, protocol = rednet.receive()
485
		storeddirection = direction
486
			
487
		while true do
488
			clear()
489
			print("Length of the shaft(s): ", storedlength)
490
			print("Count of the shaft(s): ", storedcount)
491
			print("Direction of the shaft(s): ", storeddirection, "ight")
492
			print("Length of the shaft left: ", length)
493
			print("Shafts left: ", count)
494
			print("Fuel: ", fuel)
495
			print("Status: ", status)	
496
			if status=="Done!" then
497
				break
498
			else
499
				senderId, length, protocol = rednet.receive()
500
				senderId, count, protocol = rednet.receive()
501
				senderId, fuel, protocol = rednet.receive()
502
				senderId, status, protocol = rednet.receive()
503
			end
504
		end
505
	end
506
507
	function pair()
508
		senderId, message, protocol = rednet.receive()
509
		if message=="pair" then--To pair to the turtle
510
			for i = 1, 10 do
511
				rednet.send(senderId, "succesfull")
512
				os.sleep(0.1)
513
			end
514
			print("Succesfull paired, continuing...")
515
		end
516
		clear()
517
		receive()
518
	end
519
520
521
	clear()
522
	print ("Strip-mining-program by onix331")
523
	print ("Version ", ver)
524
	print ("Place of the modem can be modified in the program, right is recommended")
525
	print ("Trying to receive pair request")
526
527
	pair()
528
529
	print ("Done!")
530
end
531
532
if turtle then--To find out if program is run on a turtle or computer
533
	startturtle()
534
else
535
	startcomputer()
536
end