View difference between Paste ID: cHA28muB and Riuuavsw
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              --
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
	print('Refueling')
45
	turtle.select(2)
46
	while(not turtle.placeUp()) do
47
		turtle.digUp()
48
		turtle.attackUp()
49
	end
50
	turtle.select(16)
51
	while(not turtle.suckUp()) do
52
		sleep('No fuel found. Sleeping.', 10)
53
	end
54-
	turtle.refuel(64)
54+
55
	turtle.digUp()
56
end
57
58
--Dumps all items into the 1st enderchest
59
function dump()
60
	turtle.select(1)
61
	while(not turtle.placeUp()) do
62
		turtle.digUp()
63
		turtle.attackUp()
64
	end
65
	for i = 3, 16 do
66
		if(turtle.getItemCount(i) > 0) then
67
			turtle.select(i)
68
			while(not turtle.dropUp()) do
69
				sleep('Dropoff chest is full. Sleeping.', 10)
70
			end
71
		end
72
	end
73
	turtle.select(1)
74
	turtle.digUp()
75
	print('Dropoff successful')
76
end
77
78
function dropoff()
79
	local empty = 0
80
	--Calculates number of empty slots
81
	for i = 2, 16 do
82
		if(turtle.getItemCount(i) == 0) then
83
			empty = empty + 1
84
		end
85
	end
86
	--Dumps items if the inventory is full
87
	if(empty == 0) then
88
		dump()
89
	end
90
end
91
92
--Clears the screen
93
function clear()
94
	term.clear()
95
	term.setCursorPos(1, 1)
96
end
97
98
--Sleeps for %time% seconds, while displaying %text% and counting down
99
function sleep(text, time)
100
	for i = 0, time - 1 do
101
		clear()
102
		print(text)
103
		print('Countdown: '..time - i..'s.')
104
		os.sleep(1)
105
	end
106
end
107
	
108
--Returns true if successful, and false if the block is bedrock
109
local function bedrockTest()
110
	local test
111
	test = turtle.down()
112
	return test
113
end
114
115
--Moves up until reaching initial height
116
function moveUp()
117
	for i = 1, y do
118
		while(not turtle.up()) do
119-
		refuel(200)
119+
120
			turtle.digUp()
121
		end
122
	end
123
end
124
125
--Digging down until bedrock
126
function digDown()
127
	local fail = 0
128
	while(true) do
129
		dropoff()
130
		turtle.digDown()
131
		if(not bedrockTest()) then
132-
		refuel(500)
132+
133
					turtle.attackDown()
134
					turtle.down()
135
				end
136
			os.sleep(0.2)
137
			fail = fail + 1
138
			if(fail == 6) then
139
				break
140
			end
141
		end
142
	end
143
end
144
145
--Mining loop
146
function loop()
147
	while(x > 0) do
148
		clear()
149
		print('Blocks left to mine: '..x)
150
		digDown()
151
		print('Hit bedrock, moving up '..y..' blocks')
152
		moveUp()
153
		while(not turtle.forward()) do
154
			turtle.attack()
155
			turtle.dig()
156
		end
157
		dump()
158
		x = x - 1
159
		save()
160
	end
161
end
162
163
--Init sequence
164
function init()
165
	clear()
166
	y = tonumber(arg[1])
167
	y = y - 1
168
	x = tonumber(arg[2])
169
	if(arg[3] ~= nil) then
170
		distance = x
171
		print('Return mode on, distance: '..distance..' blocks')
172
	else distance = 0 
173
	end
174
	while(turtle.getItemCount(1) ~= 1) do
175
		sleep('Wrong number of items in slot 1. Place one dropoff ender chest.', 5)
176
	end
177
	save()
178
	-- Creating startup file to continue mining after server restarts
179
	if(not fileExists('startup')) then
180-
	while(turtle.getItemCount(2) ~= 1) do
180+
181-
		sleep('Wrong number of items in slot 2. Place one fuel ender chest.', 5)
181+
182
		file:close()
183
	end
184
	loop()
185
end
186
187
--------
188
--MAIN--
189
--------
190
if(not fileExists('startup')) then
191
	for i = 1, 2 do
192
		if(arg[i] == nil) then
193
			clear()
194
			print('Usage: dig <max Y> <length> return')
195
			print()
196
			print('<max Y> - maximum height for mining (e.g. your Y coordinate).')
197
			print('<length> - number of blocks to mine forward.')
198
			print('return - optional command to make the turtle go back to the starting point.')
199
			do return end
200
		end
201
	end
202
	init()
203
else
204
	clear()
205
	--Reading save file
206
	local file = io.open('save', 'r')
207
	y = tonumber(file:read('*l'))
208
	x = tonumber(file:read('*l'))
209
	distance = tonumber(file:read('*l'))
210
	file:close()
211
	--If rebooted while dumping items
212
	if(turtle.getItemCount(1) == 0) then
213
		turtle.select(1)
214
		turtle.digUp()
215
		dump()
216
		while(turtle.getItemCount(1) == 0) do
217
			sleep('Missing chest in slot 1.', 10)
218
		end
219
	end
220
	--If rebooted while refueling
221
	if(turtle.getItemCount(2) == 0) then
222
		turtle.select(2)
223
		turtle.digUp()
224
		while(turtle.getItemCount(1) == 0) do
225
			sleep('Missing chest in slot 2.', 10)
226
		end
227
	end
228
	loop()
229
end
230-
		refuel(500)
230+
231
--Finishing
232
shell.run('delete', 'save')
233
shell.run('delete', 'startup')
234
print('Done!')
235
236
--Going to the starting point
237
if distance > 0 then
238
	turtle.turnRight()
239
	turtle.turnRight()
240
		while(distance > 0) do
241
		clear()
242
		print('Going back '..distance..' blocks')
243
		while(not turtle.forward()) do
244
			turtle.attack()
245
			turtle.dig()
246
		end
247
		distance = distance - 1
248
	end
249
print('Done!')
250
end