View difference between Paste ID: wkKgw4CC and 11qTwuK8
SHOW: | | - or go back to the newest paste.
1-
-- chfirfarm
1+
-- monstercleanup
2-
-- refill fuel in slot FUEL_SLOT 
2+
-- sweeps a (MAX_X+1) x (MAX_Y+1) area, kills monsters and plants torches (chunks must be loaded!)
3-
-- requires at least 4 saplings in slot SAPLING_SLOT 
3+
-- plant turtle ONE BLOCK ABOVE ground/floor so that torches could be placed under the turtle !!!
4-
-- 2x2 planting area in front (and to the right) of the turtle
4+
-- plant turtle to the left bottom corner of the desired cleaning area (this will be the 0,0 position)
5-
-- must start with 4 saplings planted in the planting area or even the tree can be already grown
5+
-- X coordinate increases to the right, Y coordinate increases to the front
6-
-- wood deposit anywhere behind the turtle, but there has to be some stopping block
6+
-- turtle will need to be refueled to minimum of approx. (MAX_X+2)*(MAX_Y+2) (1 coal=80)
7-
-- auxiliary sapling storage somewhere below the starting turtle position (directly under it) where fallen saplings should be collected
7+
-- load slots with torches (or whatever you want to be placed under the turtle as it sweeps the area)
8-
-- if the turtle stops at the wood deposit, it means it's full and needs to be emptied
8+
-- you will need about 2 torches per 100m^2 (+borders)
9
10
local MAX_X = 58
11-
local FUEL_SLOT = 1
11+
local MAX_Y = 58
12-
local SAPLING_SLOT = 2
12+
13-
local SAPLING_TMP_SLOT = 3
13+
local posY = 0
14-
local MIN_SAPLINGS = 5
14+
local orient = 0 -- orientation relative to original placement: 0 = front, 1 = right, 2 = back, 3 = left
15-
local SAPLINGS_BUFFER = 64
15+
local slotIndex = 1
16-
local WOOD_SLOT = 4
16+
17-
local MAX_WOOD = 620
17+
-- ------------------------------------------------------------------------------------------------------------
18-
local MIN_FUEL_FOR_CYCLE = 360
18+
19-
local MAX_WOOD_STORAGE_DISTANCE = 32
19+
20-
local REPEATED_DETECTION_COUNT = 3
20+
21-
local REPEATED_DETECTION_TICK = 1
21+
22-
local NEXT_DETECTION_TICK = 1
22+
23-
local TREE_GROW_SLEEP_TIME = 40
23+
24
end
25-
local orient = 0
25+
26-
local cycleCount = 0
26+
-- ------------------------------------------------------------------------------------------------------------
27-
local distanceTraveled = 0
27+
28-
local auxiliaryDepth = 0
28+
29
	orient = orient+1
30-
function forward(n)
30+
31-
	for i=1,n do
31+
32-
		if turtle.forward() then
32+
33-
			distanceTraveled = distanceTraveled + 1
33+
34-
			if orient==0 then
34+
35-
				posX = posX+1
35+
-- ------------------------------------------------------------------------------------------------------------
36-
			end
36+
function killMonsters()
37-
			if orient==2 then
37+
	
38-
				posX = posX-1
38+
	turtle.attack()
39-
			end
39+
	turtle.attack()
40
	turtle.attackUp()
41-
			return false
41+
	turtle.attackUp()
42
	turtle.attackDown()
43
	turtle.attackDown()
44-
	return true
44+
	turtle.dig() 
45
	turtle.digDown()
46-
function back(n)
46+
	os.sleep(0.7)
47-
	for i=1,n do
47+
48-
		if turtle.back() then
48+
49-
			distanceTraveled = distanceTraveled + 1
49+
-- ------------------------------------------------------------------------------------------------------------
50-
			if orient==0 then
50+
function forward()
51-
				posX = posX-1
51+
	while not turtle.forward() do
52-
			end
52+
		killMonsters()
53-
			if orient==2 then
53+
54-
				posX = posX+1
54+
	if orient==0 then
55-
			end
55+
		posY = posY+1
56
	end
57-
			return false
57+
	if orient==1 then
58
		posX = posX+1
59
	end
60-
	return true
60+
	if orient==2 then
61
		posY = posY-1
62
	end
63
	if orient==3 then
64
		posX = posX-1
65
	end
66
end
67
68
-- ------------------------------------------------------------------------------------------------------------
69
function cleanupPlaceTorch()
70
	while turtle.getItemCount(slotIndex)==0 and slotIndex<16 do
71
		slotIndex = slotIndex + 1
72
	end
73
	if turtle.getItemCount(slotIndex) > 0 then
74
		while not turtle.placeDown() do
75
			killMonsters()
76-
function reorderSaplings()
76+
77-
	local saplingsReordered = 0
77+
78-
	for i = 1,16 do
78+
79-
		if i~=SAPLING_SLOT then
79+
80-
			turtle.select(i)
80+
-- ------------------------------------------------------------------------------------------------------------
81-
			os.sleep(0.1)
81+
function cleanupStepAction()
82-
			if turtle.compareTo(SAPLING_SLOT) and (turtle.getItemCount(i)>0) then
82+
	-- kill monsters all around
83-
				while (turtle.getItemCount(i)>0) and (turtle.getItemCount(SAPLING_SLOT)<64) and (turtle.transferTo(SAPLING_SLOT,1)) do
83+
	for i=1,4 do
84-
					saplingsReordered = saplingsReordered + 1
84+
		killMonsters()
85-
					os.sleep(0.1)
85+
		turtle.turnRight()
86-
				end
86+
87-
			end
87+
88
	if (posX % 10 == 0 and posY % 10 == 0) or ((posX+5) % 10 == 0 and (posY+5) % 10 == 0) then
89
		cleanupPlaceTorch()
90-
	print("reordered "..saplingsReordered.." saplings")
90+
91
92-
function dumpExcessSaplings()
92+
93-
	for i = 1,16 do
93+
94-
		if (i~=SAPLING_SLOT) and (i~=FUEL_SLOT) then
94+
-- ------------------------------------------------------------------------------------------------------------
95-
			turtle.select(i)
95+
function cleanupStepMove()
96-
			if turtle.compareTo(SAPLING_SLOT) and (turtle.getItemCount(i)>0) then
96+
97-
				turtle.drop()
97+
98-
			end
98+
	if orient==0 then
99
		if posY<MAX_Y then
100
			forward()
101
		elseif posX<MAX_X then
102
			right()
103-
function checkSaplings()
103+
			forward()
104-
	reorderSaplings()
104+
105-
	if turtle.getItemCount(SAPLING_SLOT) < MIN_SAPLINGS then
105+
106-
		print("insufficient saplings, waiting for decaying leaves...")
106+
			result = false
107-
		os.sleep(60)
107+
108-
		print("refilling saplings from auxiliary storage...")
108+
	elseif orient==2 then
109-
		while turtle.down() do
109+
		if posY>0 then
110-
			auxiliaryDepth = auxiliaryDepth + 1
110+
			forward()
111-
			distanceTraveled = distanceTraveled + 1
111+
		elseif posX<MAX_X then
112
			left()
113
			forward()
114-
		turtle.select(SAPLING_TMP_SLOT)
114+
115-
		if turtle.suckDown(SAPLINGS_BUFFER) then
115+
116-
			print("some saplings found in auxiliary storage")
116+
			result = false
117
		end
118-
			print("getting more saplings failed")
118+
119
120-
		reorderSaplings()
120+
121-
		if turtle.getItemCount(SAPLING_SLOT) >= SAPLINGS_BUFFER then
121+
122-
			print("returning excess saplings...")
122+
123-
			dumpExcessSaplings()
123+
-- ------------------------------------------------------------------------------------------------------------
124
function cleanupFinish()
125-
		while auxiliaryDepth>0 do
125+
	-- move back to starting position
126-
			turtle.up()
126+
	while orient~=3 do 
127-
			auxiliaryDepth = auxiliaryDepth - 1
127+
		right()
128-
			distanceTraveled = distanceTraveled + 1
128+
129
	while posX>0 do
130
		forward()
131-
	if turtle.getItemCount(SAPLING_SLOT) < MIN_SAPLINGS then
131+
132-
		print("Not enough saplings in slot "..SAPLING_SLOT..", waiting for saplings...\n")
132+
	while orient~=2 do 
133-
		while turtle.getItemCount(SAPLING_SLOT) < MIN_SAPLINGS do
133+
		left()
134-
			os.sleep(10)
134+
135
	while posY>0 do
136
		forward()
137
	end
138
end
139
140
-- ------------------------------------------------------------------------------------------------------------
141-
function chop() --needs 5 saplings and 80 fuel
141+
function main()
142-
	print("chopping tree...")
142+
	local cycle = true
143-
	local height = 0
143+
	while cycle do
144-
	turtle.select(WOOD_SLOT)
144+
		cleanupStepAction()
145-
	turtle.dig()
145+
		cycle = cleanupStepMove() -- returns false if finished
146-
	forward(1)
146+
147
	cleanupFinish() -- move back to starting position
148-
	while turtle.detect() or turtle.detectUp() do
148+
149-
		turtle.select(WOOD_SLOT)
149+
150-
		turtle.dig()
150+
151-
		turtle.digUp()
151+
-- ============================================================================================================
152-
		if turtle.up() then
152+
153-
			height = height + 1
153+
main()