View difference between Paste ID: gL5Vi8SB and 54ss9M6h
SHOW: | | - or go back to the newest paste.
1
----------------------------------------------------------
2
--                 Stripmining turtle                   --
3
----------------------------------------------------------
4
--                   Version 1.3.2                      --
5
----------------------------------------------------------
6
-- Makes the turtle mine straight down to the bedrock.  --
7
-- Usege: dig <max Y> <length>                          --
8
-- <max Y> - maximum height for mining                  --
9
-- <length> - number of blocks to mine forward          --
10
-- (return) - go back to the starting point.            --
11
----------------------------------------------------------
12
-- Based on a script pastebin.com/JbFMHaNg by Eonsv     --
13
-- Tested with CC 1.5                                   --
14
-- Don't forget to use chunkloaders!                    --
15
----------------------------------------------------------
16
17
--Tests wether a file with the filename name exists. 
18
--Returns true if it does and false if not
19
function fileExists(name)
20
   local f = io.open(name,'r')
21
   if (f ~= nil) then
22
		f:close()
23
		return true
24
	else
25
		return false
26
	end
27
end
28
29
local arg = {...}
30
31
--Saves current position to file
32
function save()
33
	local str = ''
34
	str = str..y..'\n'
35
	str = str..x..'\n'
36
	str = str..distance..'\n'
37
	local file = io.open('save', 'w')
38
	file:write(str)
39
	file:close()
40
end
41
42
--Consumes a stack of fuel from the 2nd enderchest
43
function refuel(fuel)
44-
	if(turtle.getFuelLevel() ="unlimited") then
44+
	if(turtle.getFuelLevel()=="unlimited") then
45
		return true
46
	end
47
	print('Refueling')
48
	turtle.select(2)
49
	while(not turtle.placeUp()) do
50
		turtle.digUp()
51
		turtle.attackUp()
52
	end
53
	turtle.select(16)
54
	while(not turtle.suckUp()) do
55
		sleep('No fuel found. Sleeping.', 10)
56
	end
57
	turtle.refuel(64)
58
	turtle.select(2)
59
	turtle.digUp()
60
	if(turtle.getFuelLevel() <= fuel) then
61
		sleep('Refueling failed!', 10)
62
		dump()
63
		refuel(200)
64
	end
65
end
66
67
--Dumps all items into the 1st enderchest
68
function dump()
69
	turtle.select(1)
70
	while(not turtle.placeUp()) do
71
		turtle.digUp()
72
		turtle.attackUp()
73
	end
74
	for i = 3, 16 do
75
		if(turtle.getItemCount(i) > 0) then
76
			turtle.select(i)
77
			while(not turtle.dropUp()) do
78
				sleep('Dropoff chest is full. Sleeping.', 10)
79
			end
80
		end
81
	end
82
	turtle.select(1)
83
	turtle.digUp()
84
	print('Dropoff successful')
85
end
86
87
function dropoff()
88
	local empty = 0
89
	--Calculates number of empty slots
90
	for i = 2, 16 do
91
		if(turtle.getItemCount(i) == 0) then
92
			empty = empty + 1
93
		end
94
	end
95
	--Dumps items if the inventory is full
96
	if(empty == 0) then
97
		dump()
98
	end
99
end
100
101
--Clears the screen
102
function clear()
103
	term.clear()
104
	term.setCursorPos(1, 1)
105
end
106
107
--Sleeps for %time% seconds, while displaying %text% and counting down
108
function sleep(text, time)
109
	for i = 0, time - 1 do
110
		clear()
111
		print(text)
112
		print('Countdown: '..time - i..'s.')
113
		os.sleep(1)
114
	end
115
end
116
	
117
--Returns true if successful, and false if the block is bedrock
118
local function bedrockTest()
119
	local test
120
	test = turtle.down()
121
	return test
122
end
123
124
--Moves up until reaching initial height
125
function moveUp()
126
	for i = 1, y do
127
		refuel(200)
128
		while(not turtle.up()) do
129
			turtle.attackUp()
130
			turtle.digUp()
131
		end
132
	end
133
end
134
135
--Digging down until bedrock
136
function digDown()
137
	local fail = 0
138
	while(true) do
139
		dropoff()
140
		refuel(500)
141
		turtle.digDown()
142
		if(not bedrockTest()) then
143
				while(turtle.detectDown() == false) do
144
					turtle.attackDown()
145
					turtle.down()
146
				end
147
			os.sleep(0.2)
148
			fail = fail + 1
149
			if(fail == 6) then
150
				break
151
			end
152
		end
153
	end
154
end
155
156
--Mining loop
157
function loop()
158
	while(x > 0) do
159
		clear()
160
		print('Blocks left to mine: '..x)
161
		digDown()
162
		print('Hit bedrock, moving up '..y..' blocks')
163
		moveUp()
164
		while(not turtle.forward()) do
165
			turtle.attack()
166
			turtle.dig()
167
		end
168
		dump()
169
		x = x - 1
170
		save()
171
	end
172
end
173
174
--Init sequence
175
function init()
176
	clear()
177
	y = tonumber(arg[1])
178
	y = y - 1
179
	x = tonumber(arg[2])
180
	if(arg[3] ~= nil) then
181
		distance = x
182
		print('Return mode on, distance: '..distance..' blocks')
183
	else distance = 0 
184
	end
185
	while(turtle.getItemCount(1) ~= 1) do
186
		sleep('Wrong number of items in slot 1. Place one dropoff ender chest.', 5)
187
	end
188
	save()
189
	-- Creating startup file to continue mining after server restarts
190
	if(not fileExists('startup')) then
191
		local file = io.open('startup', 'w')
192
		file:write("shell.run(\'dig\')")
193
		file:close()
194
	end
195
	loop()
196
end
197
198
--------
199
--MAIN--
200
--------
201
if(not fileExists('startup')) then
202
	for i = 1, 2 do
203
		if(arg[i] == nil) then
204
			clear()
205
			print('Usage: dig <max Y> <length> return')
206
			print()
207
			print('<max Y> - maximum height for mining (e.g. your Y coordinate).')
208
			print('<length> - number of blocks to mine forward.')
209
			print('return - optional command to make the turtle go back to the starting point.')
210
			do return end
211
		end
212
	end
213
	init()
214
else
215
	clear()
216
	--Reading save file
217
	local file = io.open('save', 'r')
218
	y = tonumber(file:read('*l'))
219
	x = tonumber(file:read('*l'))
220
	distance = tonumber(file:read('*l'))
221
	file:close()
222
	--If rebooted while dumping items
223
	if(turtle.getItemCount(1) == 0) then
224
		turtle.select(1)
225
		turtle.digUp()
226
		dump()
227
		while(turtle.getItemCount(1) == 0) do
228
			sleep('Missing chest in slot 1.', 10)
229
		end
230
	end
231
	loop()
232
end
233
234
--Finishing
235
shell.run('delete', 'save')
236
shell.run('delete', 'startup')
237
print('Done!')
238
239
--Going to the starting point
240
if distance > 0 then
241
	turtle.turnRight()
242
	turtle.turnRight()
243
		while(distance > 0) do
244
		clear()
245
		print('Going back '..distance..' blocks')
246
		while(not turtle.forward()) do
247
			turtle.attack()
248
			turtle.dig()
249
		end
250
		distance = distance - 1
251
	end
252
print('Done!')
253
end