View difference between Paste ID: mfLC8Wg2 and LpjREvn8
SHOW: | | - or go back to the newest paste.
1-
-- chtreefarmtest: slightly modified Treefarm by itsmartin (from pastebin.com/tyykzGqj)
1+
-- chtreefarm
2-
--
2+
-- refill fuel in slot FUEL_SLOT 
3-
-- Place fuel in slot 14, saplings in slot 15 and 1 dirt in slot 16.
3+
-- requires at least farmLength saplings in slot SAPLING_SLOT 
4-
-- See http://youtu.be/nJYxL_sg96U for setup
4+
-- wood deposit at the beginning of the farm, saplings deposit at the end
5
6-
function forwardBy(n)
6+
local FUEL_SLOT = 1
7
local SAPLING_SLOT = 2
8
local SAPLING_TMP_SLOT = 3
9
local WOOD_SLOT = 4
10
local MIN_FUEL_FOR_STEP = 26
11
local args = {...}
12-
function moveHomeFarm()
12+
local farmLength = tonumber(args[1])
13-
	forwardBy(5)
13+
local posX = 0
14
local orient = 0
15-
	forwardBy(5)
15+
local cycleCount = 0
16
local treesChopped = 0
17
local saplingsPlanted = 0
18
local distanceTraveled = 0
19
local farmingStarted = false
20-
function plantSapling()
20+
21-
	turtle.select(16)
21+
function forward(n)
22
	for i=1,n do
23-
	turtle.up()
23+
24-
	turtle.select(15)
24+
		distanceTraveled = distanceTraveled + 1
25
		if orient==0 then
26
			posX = posX+1
27
		end
28-
function waitForSaplings()
28+
		if orient==2 then
29-
	turtle.select(15)
29+
			posX = posX-1
30-
	while turtle.getItemCount(15) < 2 do
30+
31
	end
32-
		sleep(2)
32+
33
function left()
34
	turtle.turnLeft()
35
	orient = orient-1
36
	while orient<0 do
37
		orient = orient+4
38-
	local treeFoundCount = 0
38+
39-
	turtle.up()
39+
40-
	while treeFoundCount<10 do
40+
function right()
41-
		while treeFoundCount<10 do
41+
42-
			os.sleep(2)
42+
	orient = orient+1
43-
			sleep(2)
43+
	while orient>3 do
44-
			while not turtle.detect() do
44+
		orient = orient-4
45-
				os.sleep(2)
45+
46-
				sleep(2)
46+
47
function transferSaplings()
48-
			treeFoundCount = treeFoundCount + 1
48+
	result = true
49
	for i = 1,16 do
50
		if not i==SAPLING_SLOT then
51-
		turtle.select(15)
51+
			turtle.select(i)
52
			if turtle.compareTo(SAPLING_SLOT) and turtle.getItemCount(i)>0 then
53-
		sleep(2)
53+
				turtle.transferTo(SAPLING_SLOT)
54
			end
55-
			treeFoundCount = 0
55+
56-
			turtle.up()
56+
57
58
	if turtle.getItemCount(SAPLING_SLOT) < 2 then
59-
	turtle.select(16)
59+
		print("Not enough saplings in slot "..SAPLING_SLOT.."\n")
60-
	turtle.down()
60+
		-- result = false
61-
	turtle.dig()
61+
		while turtle.getItemCount(SAPLING_SLOT) < 2 do
62-
	turtle.forward()
62+
			os.sleep(10)
63
		end
64
	end
65-
function cutTree()
65+
	return result
66-
	turtle.select(1)
66+
67-
	turtle.digUp()
67+
function stripLeaves()
68-
	turtle.up()
68+
	turtle.select(SAPLING_TMP_SLOT)
69-
	local count = 1
69+
	if turtle.detectUp() then
70-
	while count<12 and turtle.compareUp() and turtle.detectUp() do
70+
71
	end
72-
		turtle.up()
72+
	for i=1,3 do
73-
		count = count + 1
73+
		right()
74
		if turtle.detect() then
75-
	for i=1,count do
75+
			turtle.dig()
76
		end
77
	end
78
	transferSaplings()
79
	right()
80-
function moveFarmDeposit()
80+
81
82-
	forwardBy(4)
82+
function chop()
83
	local height = 0
84-
	forwardBy(4)
84+
	while turtle.detect() do
85-
	turtle.select(1)
85+
		turtle.select(WOOD_SLOT)
86-
	turtle.dropDown()
86+
		turtle.dig()
87
		stripLeaves()
88-
	forwardBy(9)
88+
		if turtle.up() then
89
			height = height + 1
90
			distanceTraveled = distanceTraveled + 1
91
		end
92
	end
93
	turtle.select(SAPLING_TMP_SLOT)
94-
	if turtle.getFuelLevel() < 50 then
94+
	for i=1,height do
95-
		turtle.select(14)
95+
		turtle.digDown()
96
		turtle.down()
97-
		if turtle.getFuelLevel() < 50 then
97+
		distanceTraveled = distanceTraveled + 1
98-
			write("Not enough fuel\n")
98+
99-
			return
99+
	if height>2 then
100
		treesChopped = treesChopped + 1
101
	end
102
end
103-
	if turtle.getItemCount(16) < 1 then
103+
function plant()
104-
		write("Dirt goes in slot 16\n")
104+
	turtle.select(SAPLING_SLOT)
105-
		return
105+
	if turtle.getItemCount(SAPLING_SLOT) < 2 then
106
		if not transferSaplings() then
107-
	if turtle.getItemCount(15) < 2 then
107+
			return false
108-
		write("Multiple saplings go in slot 15\n")
108+
109-
		return
109+
110
111
	turtle.place()
112-
	waitForSaplings()
112+
	os.sleep(0.5)
113-
	moveHomeFarm()
113+
	if turtle.detect() and turtle.compare() then
114-
	plantSapling()
114+
		saplingsPlanted = saplingsPlanted + 1
115-
	waitForTree()
115+
116-
	cutTree()
116+
	os.sleep(2)
117-
	moveFarmDeposit()
117+
	return true
118
end
119
function waitForTree()
120
	local treeDetectedCount = 0
121
	local waitingTime = 0
122
	local height = 0
123
	turtle.select(SAPLING_SLOT)
124
	while treeDetectedCount < 7 do -- because sometimes turtle does not compare reliably
125
		if turtle.compare() then
126
			treeDetectedCount = 0
127
		else
128
			treeDetectedCount = treeDetectedCount + 1
129
		end
130
		os.sleep(2)
131
		waitingTime = waitingTime + 2
132
		if waitingTime > 30 and turtle.detectUp() then
133
			height = 0
134
			while turtle.detectUp() do
135
				turtle.select(SAPLING_TMP_SLOT)
136
				turtle.digUp()
137
				if turtle.up() then
138
					height = height + 1
139
					distanceTraveled = distanceTraveled + 1
140
					turtle.dig()
141
				end
142
			end
143
			for i=1,height do
144
				turtle.down()
145
				distanceTraveled = distanceTraveled + 1
146
			end
147
		end
148
	end
149
end
150
function handleTree()
151
	if not turtle.detect() then
152
		if not plant() then
153
			return false
154
		end
155
	else
156
		waitForTree()
157
		chop()
158
		if not plant() then
159
			return false
160
		end
161
	end
162
	return true
163
end
164
function dumpWood()
165
	for i = 1,16 do
166
		if (not i==SAPLING_SLOT) and (not i==FUEL_SLOT) then
167
			turtle.select(i)
168
			if not turtle.compareTo(SAPLING_SLOT) and turtle.getItemCount(i)>0 then
169
				turtle.dropDown()
170
			end
171
		end
172
	end
173
end
174
175
function step()
176
	local result = true
177
	forward(2)
178
	if posX <= 0 then
179
		right()
180
		right()
181
		dumpWood()
182
		cycleCount = cycleCount + 1
183
		print("cycles finished: "..cycleCount)
184
		print("saplings planted: "..saplingsPlanted)
185
		print("trees chopped: "..treesChopped)
186
		print("distance traveled: "..distanceTraveled)
187
		os.sleep(10)
188
	elseif posX >= farmLength then
189
		right()
190
		right()
191
		turtle.select(SAPLING_TMP_SLOT)
192
		turtle.suckDown()
193
		transferSaplings()
194
		os.sleep(10)
195
	elseif (posX % 4) == orient then
196
		right()
197
		result = handleTree()
198
		left()
199
	elseif (posX % 4) == (2-orient) then
200
		left()
201
		result = handleTree()
202
		right()
203
	end
204
	return result
205
end
206
function checkFuel()
207
	local result = true
208
	if turtle.getFuelLevel() < MIN_FUEL_FOR_STEP then
209
		turtle.select(FUEL_SLOT)
210
		turtle.refuel()
211
	end
212
	if turtle.getFuelLevel() < MIN_FUEL_FOR_STEP then
213
		result = false
214
	end
215
	return result
216
end
217
218
if farmLength==nil or farmLength<2 then
219
	print("Farm length too short.\n")
220
	return
221
end
222
if turtle.getItemCount(SAPLING_SLOT) < farmLength then
223
	print("Not enough saplings in slot "..SAPLING_SLOT.."\n")
224
	return
225
end
226
227
while true do
228
	if not checkFuel() then
229
		print("Not enough fuel\n")
230
		os.sleep(10)
231
		while not checkFuel() do
232
			os.sleep(10)
233
		end
234
		return false
235
	end
236
	if not step() then
237
		print("Step unsuccessfull, exiting.\n")
238
		return false
239
	end
240
	os.sleep(1)
241
end