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