View difference between Paste ID: SB25hEMC and 11qTwuK8
SHOW: | | - or go back to the newest paste.
1-
-- chfirfarm
1+
-- quarry
2-
-- refill fuel in slot FUEL_SLOT 
2+
-- "swapper" (engineering turtle (turtle+crescent hammer)) must face NORTH
3-
-- requires at least 4 saplings in slot SAPLING_SLOT 
3+
-- "mover" (digging turtle) must face the swapper from the right (should face west)
4-
-- 2x2 planting area in front (and to the right) of the turtle
4+
-- the quarry, 3 enderchests and a power cell must be placed at the beginning
5-
-- must start with 4 saplings planted in the planting area or even the tree can be already grown
5+
6-
-- wood deposit anywhere behind the turtle, but there has to be some stopping block
6+
local args = {...}
7-
-- auxiliary sapling storage somewhere below the starting turtle position (directly under it) where fallen saplings should be collected
7+
local role = args[1]
8-
-- if the turtle stops at the wood deposit, it means it's full and needs to be emptied
8+
9
local QUARRY_SLOT = 2
10
local ENERGY_CELL_SLOT = 3
11
local CHARGED_CELLS_ENDERCHEST_SLOT = 4
12-
local SAPLING_SLOT = 2
12+
local EMPTY_CELLS_ENDERCHEST_SLOT = 5
13-
local SAPLING_TMP_SLOT = 3
13+
local ITEMS_ENDERCHEST_SLOT = 6
14-
local MIN_SAPLINGS = 5
14+
local TRASH_SLOT = 7
15-
local SAPLINGS_BUFFER = 64
15+
local RELOCATE_DISTANCE = 9
16-
local WOOD_SLOT = 4
16+
local MIN_FUEL_FOR_CYCLE = RELOCATE_DISTANCE + 12
17-
local MAX_WOOD = 620
17+
local STEPS_COUNT = 3
18-
local MIN_FUEL_FOR_CYCLE = 360
18+
local STEP_SLEEP = 360
19-
local MAX_WOOD_STORAGE_DISTANCE = 32
19+
local MEETING_SLEEP = 5
20-
local REPEATED_DETECTION_COUNT = 3
20+
local MOVING_SLEEP = 1
21-
local REPEATED_DETECTION_TICK = 1
21+
local QUARRY_STOP_SLEEP = 45 -- wait for the quarry to stop after disconnecting power
22-
local NEXT_DETECTION_TICK = 1
22+
local WAIT_FOR_MOVER = 20
23-
local TREE_GROW_SLEEP_TIME = 40
23+
24-
local posX = 0
24+
function saveState(state)
25-
local orient = 0
25+
	f = fs.open("state","w")
26-
local cycleCount = 0
26+
	f.write(tostring(state))
27-
local distanceTraveled = 0
27+
	f.close()
28-
local auxiliaryDepth = 0
28+
29
30-
function forward(n)
30+
function swapper_recharge()
31-
	for i=1,n do
31+
	turtle.select(ENERGY_CELL_SLOT)
32-
		if turtle.forward() then
32+
33-
			distanceTraveled = distanceTraveled + 1
33+
	turtle.dropDown()
34-
			if orient==0 then
34+
	while turtle.getItemCount(ENERGY_CELL_SLOT)==0 do
35-
				posX = posX+1
35+
		turtle.suckUp()
36-
			end
36+
37-
			if orient==2 then
37+
38-
				posX = posX-1
38+
39-
			end
39+
40-
		else
40+
function swapper_pack()
41-
			return false
41+
	turtle.select(ENERGY_CELL_SLOT)
42
	turtle.dig()
43
	turtle.dropDown()
44-
	return true
44+
	sleep(QUARRY_STOP_SLEEP)
45
	turtle.back() -- make room for mover
46-
function back(n)
46+
47-
	for i=1,n do
47+
	while not(turtle.forward()) do
48-
		if turtle.back() then
48+
		sleep(1)
49-
			distanceTraveled = distanceTraveled + 1
49+
50-
			if orient==0 then
50+
	sleep(1)
51-
				posX = posX-1
51+
	while not(turtle.forward()) do
52-
			end
52+
		sleep(1)
53-
			if orient==2 then
53+
54-
				posX = posX+1
54+
55-
			end
55+
	print("giving mover time to relocate in front of swapper")
56-
		else
56+
	sleep(WAIT_FOR_MOVER)
57-
			return false
57+
58
59
function swapper_relocate()
60-
	return true
60+
	for i=1,RELOCATE_DISTANCE do
61
		sleep(MOVING_SLEEP)
62-
function left()
62+
		while not turtle.forward() do
63
			print("couldn't move forward, sleeping for a while")
64-
	orient = orient-1
64+
			sleep(MOVING_SLEEP)
65-
	while orient<0 do
65+
66-
		orient = orient+4
66+
67
end
68
69-
function right()
69+
function swapper_unpack()
70
	turtle.turnRight()
71-
	orient = orient+1
71+
	while not(turtle.forward()) do
72-
	while orient>3 do
72+
		print("couldn't move forward, sleeping for a while")
73-
		orient = orient-4
73+
		sleep(1)
74
	end
75
	sleep(1)
76-
function reorderSaplings()
76+
	while not(turtle.forward()) do
77-
	local saplingsReordered = 0
77+
		print("couldn't move forward, sleeping for a while")
78-
	for i = 1,16 do
78+
		sleep(1)
79-
		if i~=SAPLING_SLOT then
79+
80-
			turtle.select(i)
80+
81-
			os.sleep(0.1)
81+
	sleep(WAIT_FOR_MOVER)
82-
			if turtle.compareTo(SAPLING_SLOT) and (turtle.getItemCount(i)>0) then
82+
	while not turtle.forward() do
83-
				while (turtle.getItemCount(i)>0) and (turtle.getItemCount(SAPLING_SLOT)<64) and (turtle.transferTo(SAPLING_SLOT,1)) do
83+
		sleep(MEETING_SLEEP)
84-
					saplingsReordered = saplingsReordered + 1
84+
85-
					os.sleep(0.1)
85+
	swapper_recharge()
86-
				end
86+
87-
			end
87+
88
function swapper_cycle()
89
	for i=1,(STEPS_COUNT-1) do
90-
	print("reordered "..saplingsReordered.." saplings")
90+
		print("giving quarry time")
91
		sleep(STEP_SLEEP)
92-
function dumpExcessSaplings()
92+
		print("recharging quarry")
93-
	for i = 1,16 do
93+
		swapper_recharge()
94-
		if (i~=SAPLING_SLOT) and (i~=FUEL_SLOT) then
94+
95-
			turtle.select(i)
95+
	print("giving quarry time")
96-
			if turtle.compareTo(SAPLING_SLOT) and (turtle.getItemCount(i)>0) then
96+
	sleep(STEP_SLEEP)
97-
				turtle.drop()
97+
	print("packing")
98-
			end
98+
	swapper_pack()
99
	print("relocating")
100
	swapper_relocate()
101
	print("unpacking")
102
	swapper_unpack()
103-
function checkSaplings()
103+
104-
	reorderSaplings()
104+
105-
	if turtle.getItemCount(SAPLING_SLOT) < MIN_SAPLINGS then
105+
function mover_cleanup()
106-
		print("insufficient saplings, waiting for decaying leaves...")
106+
	turtle.select(TRASH_SLOT)
107-
		os.sleep(60)
107+
108-
		print("refilling saplings from auxiliary storage...")
108+
	turtle.digUp()
109-
		while turtle.down() do
109+
	turtle.digDown()
110-
			auxiliaryDepth = auxiliaryDepth + 1
110+
	turtle.dropDown()
111-
			distanceTraveled = distanceTraveled + 1
111+
	turtle.drop()
112
end
113
114-
		turtle.select(SAPLING_TMP_SLOT)
114+
function mover_pack()
115-
		if turtle.suckDown(SAPLINGS_BUFFER) then
115+
	turtle.select(CHARGED_CELLS_ENDERCHEST_SLOT)
116-
			print("some saplings found in auxiliary storage")
116+
	turtle.digUp()
117-
		else
117+
	turtle.select(EMPTY_CELLS_ENDERCHEST_SLOT)
118-
			print("getting more saplings failed")
118+
	turtle.digDown()
119
	turtle.turnLeft()
120-
		reorderSaplings()
120+
	turtle.forward()
121-
		if turtle.getItemCount(SAPLING_SLOT) >= SAPLINGS_BUFFER then
121+
122-
			print("returning excess saplings...")
122+
	turtle.select(QUARRY_SLOT)
123-
			dumpExcessSaplings()
123+
124
	turtle.turnLeft()
125-
		while auxiliaryDepth>0 do
125+
	turtle.forward()
126-
			turtle.up()
126+
127-
			auxiliaryDepth = auxiliaryDepth - 1
127+
	turtle.select(ITEMS_ENDERCHEST_SLOT)
128-
			distanceTraveled = distanceTraveled + 1
128+
129
end
130
function mover_unpack()
131-
	if turtle.getItemCount(SAPLING_SLOT) < MIN_SAPLINGS then
131+
	mover_cleanup()
132-
		print("Not enough saplings in slot "..SAPLING_SLOT..", waiting for saplings...\n")
132+
	turtle.select(ITEMS_ENDERCHEST_SLOT)
133-
		while turtle.getItemCount(SAPLING_SLOT) < MIN_SAPLINGS do
133+
134-
			os.sleep(10)
134+
135
	turtle.forward()
136
	turtle.turnLeft()
137
	mover_cleanup()
138
	turtle.select(QUARRY_SLOT)
139
	turtle.place()
140
	turtle.turnRight()
141-
function chop() --needs 5 saplings and 80 fuel
141+
	turtle.forward()
142-
	print("chopping tree...")
142+
143-
	local height = 0
143+
	mover_cleanup()
144-
	turtle.select(WOOD_SLOT)
144+
	turtle.select(EMPTY_CELLS_ENDERCHEST_SLOT)
145
	turtle.placeDown()
146-
	forward(1)
146+
	turtle.select(CHARGED_CELLS_ENDERCHEST_SLOT)
147
	turtle.placeUp()
148-
	while turtle.detect() or turtle.detectUp() do
148+
149-
		turtle.select(WOOD_SLOT)
149+
150-
		turtle.dig()
150+
	turtle.back()
151-
		turtle.digUp()
151+
	sleep(MEETING_SLEEP)
152-
		if turtle.up() then
152+
153-
			height = height + 1
153+
function mover_relocate()
154-
			distanceTraveled = distanceTraveled + 1
154+
	for i=1,RELOCATE_DISTANCE do
155
		mover_cleanup()
156
		turtle.forward()
157-
	right()
157+
158-
	turtle.dig() --just in case
158+
159-
	forward(1)
159+
160-
	left()
160+
function mover_cycle()
161
	sleep(STEPS_COUNT*STEP_SLEEP+QUARRY_STOP_SLEEP)
162-
	--just in case
162+
	while not(turtle.forward()) do
163-
	while turtle.detectUp() do
163+
		sleep(MEETING_SLEEP)
164-
		turtle.select(WOOD_SLOT)
164+
165-
		turtle.dig()
165+
	sleep(MEETING_SLEEP)
166-
		turtle.digUp()
166+
167-
		if turtle.up() then
167+
	mover_pack()
168-
			height = height + 1
168+
	sleep(2)
169-
			distanceTraveled = distanceTraveled + 1
169+
	mover_relocate()
170
	mover_unpack()
171
end
172
173-
	for i=1,height do
173+
function check_fuel()
174-
		turtle.select(WOOD_SLOT)
174+
175-
		turtle.dig()
175+
		print("not enough fuel, waiting for fuel")
176-
		turtle.digDown()
176+
		while turtle.getFuelLevel() < MIN_FUEL_FOR_CYCLE do
177-
		turtle.select(SAPLING_TMP_SLOT)
177+
			sleep(2)
178-
		turtle.suckDown()
178+
			turtle.select(FUEL_SLOT)
179-
		turtle.down()
179+
			turtle.refuel()
180-
		turtle.suck()
180+
181-
		distanceTraveled = distanceTraveled + 1
181+
182
end
183-
	turtle.select(WOOD_SLOT)
183+
184
-- ***********************************************************************
185-
	-- replant while repositioning
185+
186-
	print("replanting saplings...")
186+
if role=="swapper" then
187-
	turtle.select(SAPLING_TMP_SLOT)
187+
	print("starting swapper")
188-
	turtle.suck()
188+
	while true do
189-
	turtle.select(SAPLING_SLOT)
189+
		check_fuel()
190
		swapper_cycle()
191-
	right()
191+
192-
	back(1)	
192+
elseif role=="mover" then
193-
	turtle.select(SAPLING_TMP_SLOT)
193+
	print("starting mover")
194-
	turtle.suck()
194+
	while true do
195-
	turtle.select(SAPLING_SLOT)
195+
		check_fuel()
196
		mover_cycle()
197-
	left()
197+
198-
	turtle.select(SAPLING_TMP_SLOT)
198+
else
199-
	turtle.suck()
199+
	print("invalid parameter, expecting 'mover' or 'swapper'")
200-
	turtle.select(SAPLING_SLOT)
200+