View difference between Paste ID: fjLjT5n0 and xUh8Hzp4
SHOW: | | - or go back to the newest paste.
1
--recursive digging
2
-- usage: recdig <mode> [list]
3
4
local sides = { "top", "front", "bottom", "left", "right", "back" }
5
6
local targs = { ... }
7
8
--local mode = "any"		-- dig any block detected
9
--local mode = "white"	-- takes a whitelist of blocks in form of inventory slots
10
--local mode = "black"	-- takes a blacklist of blocks in form of inventory slots
11
local mode = ""
12
local list = {}
13
14
function checkInput()
15
	local valid = true
16
	if #targs == 0 then
17
		valid = false
18
	end
19
	if #targs == 1 and targs[1] ~= "any" then
20
		valid = false
21
	end
22
	if #targs > 1 then
23
		if targs[1] ~= "white" and targs[1] ~= "black" then
24
			valid = false
25
		end
26
		for i = 2,#targs do
27
			if tonumber(targs[i]) < 1 or tonumber(targs[i]) > 16 then
28
				valid = false
29
			end
30
		end
31
	end
32
	if not valid then
33
		print("usage: recdig <mode> [list]")
34
		print("modes: any, white, black")
35
		print("list: list of slots used for black- or white-listing")
36
	end
37
	return valid
38
end
39
40
function turnAround()
41
	turtle.turnLeft()
42
	turtle.turnLeft()
43
end
44
45
function checkSides()
46
	sleep(0.25)
47
	for s = 1,#sides do
48
		print("checking "..sides[s])
49
		checkSide(sides[s])
50
	end
51
end
52
53
function wantThis(mode, side)		-- returns true or false depending on whether the block we're checking is one we want
54
	if mode == "any" then
55
		if side == "up" then
56
			return turtle.detectUp()
57
		elseif side == "down" then
58
			return turtle.detectDown()
59
		elseif side == "front" then
60
			return turtle.detect()
61
		end
62
	elseif mode == "white" then
63
		local ret = false
64
		for l = 1,#list do
65
			turtle.select(l)
66
			if side == "up" then
67
				if turtle.compareUp() then
68
					ret = true
69
				end
70
			elseif side == "down" then
71
				if turtle.compareDown() then
72
					ret = true
73
				end
74
			elseif side == "front" then
75
				if turtle.compare() then
76
					ret = true
77
				end
78
			end
79
		end
80
		return ret
81
	elseif mode == "black" then
82
		local ret = true
83
		for l = 1,#list do
84
			turtle.select(l)
85
			if side == "up" then
86
				if turtle.compareUp() or not turtle.detectUp() then
87
					ret = false
88
				end
89
			elseif side == "down" then
90
				if turtle.compareDown() or not turtle.detectDown() then
91
					ret = false
92
				end
93
			elseif side == "front" then
94
				if turtle.compare() or not turtle.detect() then
95
					ret = false
96
				end
97
			end
98
		end
99
		return ret
100
	end
101
end
102
103
function checkSide(side)
104
	if side == "top" then
105
		if wantThis(mode, "up") then
106
			print("block above detected")
107
			blocksmined = blocksmined + 1
108
			while not turtle.up() do
109
				if not turtle.digUp() then
110
					turtle.attackUp()
111
				end
112
			end
113
			checkSides()
114
			while not turtle.down() do
115
				turtle.attackDown()
116
			end
117
		end
118
	elseif side == "front" then
119
		if wantThis(mode, "front") then
120
			print("blcok in front detected")
121
			blocksmined = blocksmined + 1
122
			while not turtle.forward() do
123
				if not turtle.dig() then
124
					turtle.attack()
125
				end
126
			end
127
			checkSides()
128
			turnAround()
129
			while not turtle.forward() do
130
				turtle.attack()
131
			end
132
			turnAround()
133
		end
134
	elseif side == "bottom" then
135
		if wantThis(mode, "down") then
136
			print("block below detected")
137
			blocksmined = blocksmined + 1
138
			while not turtle.down() do
139
				if not turtle.digDown() then
140
					turtle.attackDown()
141
				end
142
			end
143
			checkSides()
144
			while not turtle.up() do
145
				turtle.attackUp()
146
			end
147
		end
148
	elseif side == "left" then
149
		turtle.turnLeft()
150
		if wantThis(mode, "front") then
151
			print("block left detected")
152
			blocksmined = blocksmined + 1
153
			while not turtle.forward() do
154
				if not turtle.dig() then
155
					turtle.attack()
156
				end
157
			end
158
			checkSides()
159
			turnAround()
160
			while not turtle.forward() do
161
				turtle.attack()
162
			end
163
			turnAround()
164
		end
165
		turtle.turnRight()
166
	elseif side == "right" then
167
		turtle.turnRight()
168
		if wantThis(mode, "front") then
169
			print("block right detected")
170
			blocksmined = blocksmined + 1
171
			while not turtle.forward() do
172
				if not turtle.dig() then
173
					turtle.attack()
174
				end
175
			end
176
			checkSides()
177
			turnAround()
178
			while not turtle.forward() do
179
				turtle.attack()
180
			end
181
			turnAround()
182
		end
183
		turtle.turnLeft()
184
	elseif side == "back" then
185
		turnAround()
186
		if wantThis(mode, "front") then
187
			print("block behind detected")
188
			blocksmined = blocksmined + 1
189
			while not turtle.forward() do
190
				if not turtle.dig() then
191
					turtle.attack()
192
				end
193
			end
194
			checkSides()
195
			turnAround()
196
			while not turtle.forward() do
197
				turtle.attack()
198
			end
199
			turnAround()
200
		end
201
		turnAround()
202
	end
203
end
204
205
if checkInput() then
206
	blocksmined = 0
207
	startfuel = turtle.getFuelLevel()
208
	mode = targs[1]
209
	if #targs > 1 then
210
		for i = 2,#targs do
211
			list[i-1] = targs[i]
212
		end
213
	end
214-
	local yesno = read()
214+
215
		print("Ready to start recursive mining in any-mode. I will mine all blocks I can reach until I run out of blocks or fuel.")
216
	elseif mode == "black" then
217
		print("Ready to start recursive mining in "..mode.."-list mode. I will mine all blocks not on the supplied list")
218
	elseif mode == "white" then
219
		print("Ready to start recursive mining in "..mode.."-list mode. I will mine all blocks on the supplied list")
220
	end
221
	print("ready to start? y/n")
222
	--local yesno = read()
223
	local yesno = "y"
224
	if yesno ~= "y" then
225
		exit()
226
	else
227
		checkSides()
228
	end
229
	endfuel = turtle.getFuelLevel()
230
	fuelconsumed = startfuel - endfuel
231
	print("Mined "..blocksmined.." blocks using "..fuelconsumed.." moves")
232
end