View difference between Paste ID: 3QHRWnMk and xjsLsCZ9
SHOW: | | - or go back to the newest paste.
1
--[[
2
	Lazy Nub Tunneller v0.03
3
	
4
	Will tell your turtle how to build
5
	an amazingly wonderful tunnel for you,
6
	with torch and chest placement if requested.
7
	
8
	Chest(s) slot = 14
9
	Fuel slot = 15
10
	Torch(s) slot = 16	
11
	
12
	Minimum Length = 1
13
	        Width = 1
14
	        Height = 3 
15
	
16
	If you wish to skip the UI and start tunneller with commands directly
17
	the format is:
18
	
19
	<program name> <Length> <Width> <Height> [Torch Distance]
20
	
21
	ie: lnt 16 1 3 6
22
	
23
	This would start the tunnel with a length of 16 width of 1
24
	height of 3 and torches spaced 6 apart. The torches can be
25
	omitted from the line and none will be placed.
26
	
27
	With this option tunneller assumes you have enough fuel and torches in place.
28
	If you use the command line method it will assume you wish to use chests if the
29
	chest slot is occupied.
30
	
31
	If any of your numbers fail the limits of the program it will
32
	default to normal UI operation.
33
	
34
	ComputerCraft Posting: http://www.computercraft.info/forums2/index.php?/topic/13224-lazy-nub-tunneller/
35
	PasteBin: http://pastebin.com/xjsLsCZ9
36
	YouTube: http://youtu.be/DVF4VAfNfio
37
38
	Load into turtle with:
39
	pastebin get xjsLsCZ9 lnt
40
	
41
	Then start it with:
42
	lnt
43
	
44
	Changes:
45
	June 2	-	Released v0.01
46
	June 3  -	UI fixes command line option added  v0.02
47
	June 4 	-	Added inventory drop-off 
48
	
49
]]--
50
51
	local running = true
52
	local tunnelLength = 0
53
	local tunnelWidth = 0
54
	local tunnelHeight = 0
55
	local tunnel = 0
56
	local chestSlot = 14
57
	local fuelSlot = 15
58
	local torchSlot = 16
59
	local placeChest = false
60
	local placeTorch = false
61
	local torchDistance = 0
62
	local lastEmptySlot = 13
63
	local neededFuel = 0
64
	local args = { ... }
65
	local params = false
66
	local block = 1
67
68
--[[ Robust Turtle
69
 Modified version of 
70
 Robust Turtle API by SpeedR
71
 integrated 
72
]]--
73
74
	local digDelay = .7
75
	local digTries = 25
76
77
local function outOfResource()
78
  print("Ran out of a resource. Block: ",block , ".")
79
  print("Refill, then say something to proceed.")
80
  read()
81
end
82
83
--Digging with gravel/sand detection
84
function dig()
85
  local tries = 0
86
  while turtle.detect() do
87
    turtle.dig()
88
    sleep(digDelay)
89
    tries = tries + 1
90
    if tries>digTries then
91
      print("Error: dug for too long.")
92
      return false
93
    end
94
  end
95
  return true
96
end
97
98
function digUp()
99
  local tries = 0
100
  while turtle.detectUp() do
101
    turtle.digUp()
102
    sleep(digDelay)
103
    tries = tries + 1
104
    if tries>digTries then
105
      print("Error: dug up for too long.")
106
      return false
107
    end
108
  end
109
  return true
110
end
111
112
function digDown()
113
  local tries = 0
114
  while turtle.detectDown() do
115
    turtle.digDown()
116
    sleep(digDelay)
117
    tries = tries + 1
118
    if tries>digTries then
119
      print("Error: dug down for too long.")
120
      return false
121
    end
122
  end
123
  return true
124
end
125
126
127
--Traveling: Goes in the direction no matter what (almost)
128
--Will not be stopped by blocks or mobs
129
function forward(l)
130
  l=l or 1
131
  for i=1,l do
132
    local tries = 0
133
    while turtle.forward() ~= true do
134
      turtle.dig()
135
      turtle.attack()
136
      sleep(digDelay)
137
      tries = tries + 1
138
      if tries>digTries then
139
        print("Error: can't move forward.")
140
        return false
141
      end
142
    end
143
  end
144
  return true
145
end
146
147
function up(l)
148
  l=l or 1
149
  for i=1,l do
150
    local tries = 0
151
    while turtle.up() ~= true do
152
      turtle.digUp()
153
      turtle.attackUp()
154
      sleep(digDelay)
155
      tries = tries + 1
156
      if tries>digTries then
157
        print("Error: can't move up.")
158
        return false
159
      end
160
    end
161
  end
162
  return true
163
end
164
165
function down(l)
166
  l=l or 1
167
  for i=1,l do
168
    local tries = 0
169
    while turtle.down() ~= true do
170
      turtle.digDown()
171
      turtle.attackDown()
172
      sleep(digDelay)
173
      tries = tries + 1
174
      if tries>digTries then
175
        print("Error: can't move down.")
176
        return false
177
      end
178
    end
179
  end
180
  return true
181
end
182
183
function back(l)
184
  l=l or 1
185
  for i=1,l do
186
    if turtle.back() ~= true then
187
      turnAround()
188
      forward()
189
      turnAround()
190
    end
191
  end
192
end
193
194
195
--Place blocks
196
--Does not place when there's already the right block.
197
function place(block)
198
  turtle.select(block)
199
  if turtle.compare()==false then
200
    if turtle.getItemCount(block)==0 then
201
      outOfResource(block)
202
    end
203
    dig()
204
    turtle.place()
205
  end
206
end
207
208
function placeUp(block)
209
  turtle.select(block)
210
  if turtle.compareUp()==false then
211
    if turtle.getItemCount(block)==0 then
212
      outOfResource(block)
213
    end
214
    digUp()
215
    turtle.placeUp()
216
  end
217
end
218
219
function placeDown(block)
220
  turtle.select(block)
221
  if turtle.compareDown()== false then
222
    if turtle.getItemCount(block)== 0 then
223
      outOfResource(block)
224
    end
225
    digDown()
226
    turtle.placeDown()
227
  end
228
end
229
230
function placeRight(block)
231
  turtle.turnRight()
232
  place(block)
233
  turtle.turnLeft()
234
end
235
236
function placeLeft(block)
237
  turtle.turnLeft()
238
  place(block)
239
  turtle.turnRight()
240
end
241
242
function placeBack(block)
243
  turnAround()
244
  place(block)
245
  turnAround()
246
end
247
248
--place row     e.g. placeRow(up, marble, forward, 15)
249
function placeRow(placeDir, block, travelDir, l) 
250
  l=l or 1
251
  for i=1,l do
252
    if placeDir == "forward" then
253
      place(block)
254
    elseif placeDir == "up" then
255
      placeUp(block)
256
    elseif placeDir == "down" then
257
      placeDown(block)
258
    elseif placeDir == "right" then
259
      placeRight(block)
260
    elseif placeDir == "left" then
261
      placeLeft(block)
262
    elseif placeDir == "back" then
263
      placeBack(block)
264
    else
265
      print('"', placeDir, '" is not a valid direction!')
266
      return false
267
    end
268
    if travelDir == "forward" then
269
      forward()
270
    elseif travelDir == "up" then
271
      up()
272
    elseif travelDir == "down" then
273
      down()
274
    elseif travelDir == "right" then
275
      strafeRight()
276
    elseif travelDir == "left" then
277
      strafeLeft()
278
    elseif travelDir == "back" then
279
      back()
280
    else
281
      print('"', travelDir, '" is not a valid direction!')
282
      return false
283
    end
284
  end
285
  return true
286
end
287
288
289
--Turning
290
function turnAround()
291
  turtle.turnRight()
292
  turtle.turnRight()
293
end
294
295
function right()
296
  turtle.turnRight()
297
end
298
299
function left()
300
  turtle.turnLeft()
301
end
302
303
function goRight(l)
304
  l=l or 1
305
  turtle.turnRight()
306
  forward(l)
307
end
308
309
function goLeft(l)
310
  l=l or 1
311
  turtle.turnLeft()
312
  forward(l)
313
end
314
315
function strafeRight(l)
316
  l=l or 1
317
  goRight(l)
318
  turtle.turnLeft()
319
end
320
321
function strafeLeft(l)
322
  l=l or 1
323
  goLeft(l)
324
  turtle.turnRight()
325
end
326
--[[ End of Robust Turtle 
327
]]--
328
329
330
function dumpInv()							--Dumps inventory to chest
331
	print("Placing chest..")
332
	placeDown(chestSlot)
333
	for i = 1,lastEmptySlot do
334
		turtle.select(i)
335
		turtle.dropDown()
336
	end
337
	print("Emptied into chest..")
338
	turtle.select(1)	
339
end
340
341
function checkSpace()  
342
--[[
343
	will remove noiseblocks and compress in future version maybe
344
	if turtle.getItemCount(lastEmptySlot) > 0 then
345
		print("Trying to make room...")
346
		for i = 2,lastEmptySlot,1 do
347
            if turtle.getItemCount(i)> 0 then
348
                for j = i-1,1,-1 do
349
                    turtle.select(i-j)
350
                    turtle.transferTo(j)
351
					ut = read()
352
				end
353
			end
354
        end
355
		]]--
356
		if turtle.getItemCount(lastEmptySlot) > 0 then
357
			dumpInv()
358
		end
359
	
360
end
361
362
--[[ MAIN START ]]--
363
364
if (#args < 3 or #args > 4) then				-- Parse command line arguments if found
365
    params = false
366
	print("Command Line Option Skipped...")
367
	print("program name <Length> <Width> <Height> [Torch Distance]")
368
else
369
	params = true
370
	tunnelLength = tonumber(args[1])
371
	tunnelWidth = tonumber(args[2])
372
	tunnelHeight = tonumber(args[3])
373
	if #args == 4 then
374
		torchDistance = tonumber(args[4])
375
		placeTorch = true
376
		if type( torchDistance ) ~= "number" or torchDistance < 1 then
377
			params = false
378
		end
379
	end
380
	if turtle.getItemCount(chestSlot) > 0 then
381
		placeChest = true
382
	end
383
	if type(tunnelLength) ~= "number" or tunnelLength < 1 or
384
	   type(tunnelWidth) ~= "number" or tunnelWidth < 1 or
385
	   type(tunnelHeight) ~= "number" or tunnelHeight < 3 or
386-
	   turtle.getFuelLevel() < ((tunnelLength*tunnelWidth)+((tunnelHeight-3)*(tunnelLength*tunnelWidth)))+1 then
386+
	   turtle.getFuelLevel() < 1 then
387
		params = false
388
	end
389
end
390
391
if params == false then							-- Start UI if invalid or no command line
392
	term.clear()
393
	term.setCursorPos (1, 1)
394
	print("Lazy Nub Tunneller v0.03")
395
	print("=====================")
396
	while tunnelLength < 1 do
397
		print("What length of tunnel (min = 1): ")
398
		tunnelLength = tonumber(read())
399
		if type( tunnelLength ) ~= "number" or tunnelLength == 0 then
400
			print ("I was expecting a number 1 or higher...")
401
			print ("Lets try that again.")
402
			tunnelLength = 0
403
		end
404
	end
405
	while tunnelWidth < 1 do
406
		print("What width of tunnel (min = 1): ")
407
		tunnelWidth = tonumber(read())
408
		if type( tunnelWidth ) ~= "number" or tunnelWidth == 0 then
409
			print ("I was expecting a number 1 or higher...")
410
			print ("Lets try that again.")
411
			tunnelWidth = 0
412
		end
413
	end
414
	while tunnelHeight < 3 do
415
		print("What height of tunnel (min = 3): ")
416
		tunnelHeight = tonumber(read())
417
		if type( tunnelHeight ) ~= "number" or tunnelHeight < 3 then
418
			print ("I was expecting a number 3 or higher...")
419
			print ("Lets try that again.")
420
			tunnelHeight = 0
421
		end
422
	end
423
	print("Use Torches (y/n): ")
424
	ut = read()
425
	if ut == "y" or ut == "Y" then
426
		placeTorch = true
427
		while torchDistance < 1 do
428
			print("Place How Far Apart (suggest 6): ")
429
			torchDistance = tonumber(read())
430
			if type( torchDistance ) ~= "number" or torchDistance < 1 then
431
				print ("I was expecting a number 1 or higher...")
432
				print ("Lets try that again.")
433
				torchDistance = 0
434
			end
435
		end
436
		
437
		-- check if enough torches.. still need to verify this check total
438
439
		if ((tunnelLength/torchDistance)*(tunnelWidth/torchDistance)) > (turtle.getItemCount(torchSlot)) then
440
			turtle.select(torchSlot)
441
			print("Place about "..((math.ceil(tunnelLength/torchDistance)*math.ceil(tunnelWidth/torchDistance))+2).." in slot "..torchSlot.." <ENTER>")
442
			ut = read()
443
			turtle.select(1)
444
		end
445
	end
446
	print("Use Chests (y/n): ")
447
	ut = read()
448
	if ut == "y" or ut == "Y" then
449
		placeChest = true
450
		turtle.select(chestSlot)
451
		while turtle.getItemCount(chestSlot) < 1 do
452
			print("Place at least 1 chest in slot "..chestSlot)
453
			print("Press <ENTER> when done.")
454
			ut = read()
455
		end
456
		turtle.select(1)
457
	end
458
459
	-- check for fuel in turtle at start matches needed level
460
	
461
	neededFuel = ((tunnelLength*tunnelWidth)+((tunnelHeight-3)*(tunnelLength*tunnelWidth)))+1
462
	if turtle.getFuelLevel() < neededFuel then
463
		turtle.select(fuelSlot)
464
		turtle.refuel(1)
465
		turtle.select(1)
466
	end
467
	if turtle.getFuelLevel() < neededFuel then
468
		print("Fuel Level at: "..turtle.getFuelLevel())
469
		while turtle.getFuelLevel() < neededFuel do
470
			turtle.select(fuelSlot)
471
			print ("Place fuel item into slot "..fuelSlot.." and press <Enter>")
472
			ut = read()
473
			turtle.refuel(1)
474
			print("Fuel Level at: "..turtle.getFuelLevel())
475
		end
476
		turtle.select(1)
477
	end
478
end
479
480
-- Main loop
481
	
482
while running do
483
	up()
484
	for tunnel = 1,tunnelLength do
485
		forward()
486
		digUp()
487
		digDown()
488
		if placeChest == true then
489
			checkSpace()
490
		end
491
		if tunnel%torchDistance == 0 then
492
			placeDown(torchSlot)
493
		end
494
		if tunnelHeight > 3 then
495
			for Height = 4,tunnelHeight,1 do
496
				up()
497
				digUp()
498
			end
499
			for Height = tunnelHeight,4,-1 do
500
				down()
501
			end
502
		end
503
		if tunnelWidth > 1 then
504
			if tunnel%2 == 0 then
505
				left()
506
			else
507
				right()
508
			end
509
			for width = 2,tunnelWidth do
510
				forward()
511
				digUp()
512
				digDown()
513
				if placeChest == true then
514
					checkSpace()
515
				end
516
				if ((width%torchDistance == 0) and (tunnel%torchDistance == 0)) then
517
					placeDown(torchSlot)
518
				end				
519
				if tunnelHeight > 3 then
520
					for Height = 4,tunnelHeight,1 do
521
						up()
522
						digUp()
523
					end
524
					for Height = tunnelHeight,4,-1 do
525
						down()
526
					end
527
				end	
528
			end
529
			if tunnel%2 == 0 then
530
				right()
531
			else
532
				left()
533
			end
534
		end
535
	end
536
	down()
537
	if placeTorch == true then
538
		turnAround()
539
		place(torchSlot)
540
		turnAround()
541
	end
542
	if placeChest then
543
		if tunnelLength%2==0 then
544
			left()
545
		else
546
			right()
547
		end
548
		back()
549
		up()
550
		dumpInv()
551
		forward()
552
		if tunnelLength%2==0 then
553
			right()
554
		else
555
			left()
556
		end
557
		down()
558
	end
559
	running = false
560
end