-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.lua
More file actions
184 lines (156 loc) · 3.94 KB
/
tree.lua
File metadata and controls
184 lines (156 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
trees = 3
function fuelCheck()
if turtle.getFuelLevel() < 10 then
for i=1, 16 do
turtle.select(i)
local detail = turtle.getItemDetail()
if detail and detail.name == "minecraft:coal" then
turtle.refuel()
return 1
end
end
end
return turtle.getFuelLevel()
end
function mineAround()
changeDirection(0)
turtle.dig()
changeDirection(0)
turtle.dig()
changeDirection(0)
turtle.dig()
changeDirection(0)
turtle.dig()
end
function changeDirection(lr)
local f = fs.open("state", "r")
local state = tonumber(f.readLine())
if lr == 0 then -- turn right
turtle.turnRight()
state = (state+1)%4
else -- turn left
turtle.turnLeft()
if state == 0 then
state = 3
else
state = state - 1
end
end
print(state)
f.close()
local f = fs.open("state", "w")
f.write(state)
f.close()
end
function harvestTree()
local _, res = turtle.inspectUp()
while _ and res and fuelCheck() do
turtle.digUp()
turtle.up()
mineAround()
turtle.forward()
mineAround()
turtle.back()
turtle.back()
mineAround()
turtle.forward()
_, res = turtle.inspectUp()
end
end
function goBack()
while not turtle.detectDown() and fuelCheck() do
turtle.down()
end
end
function replant()
for i=1, 16 do
turtle.select(i)
local detail = turtle.getItemDetail()
if detail and detail.name == "minecraft:sapling" then
return turtle.place()
end
end
print("OUT OF SAPLINGS")
return 0
end
function deposit()
local _, ischest = turtle.inspectDown()
if _ and ischest.name == "minecraft:chest" then
for i=1, 16 do
turtle.select(i)
local detail = turtle.getItemDetail()
if detail and detail.name == "minecraft:log" then
turtle.dropDown()
elseif detail and detail.name == "minecraft:sapling" and detail.count > trees+10 then
turtle.dropDown(10)
end
end
else
print("NO CHEST DETECTED AT BOTTOM")
end
end
function grabfuel()
changeDirection(0)
local _, ischest = turtle.inspect()
if turtle.getFuelLevel() < trees*10*4 then
if _ and ischest then
turtle.suck((trees*10*4/80)+trees*3+5)
else
print("NO CHEST DETECTED ABOVE")
end
end
changeDirection(1)
end
function begin()
while true do
for i=1, trees do
local _, islog = turtle.inspect()
if _ and islog.name == "minecraft:log" then
grabfuel()
fuelCheck()
turtle.dig()
turtle.forward()
harvestTree()
goBack()
turtle.back()
replant()
end
if i ~= trees then
changeDirection(1)
turtle.forward()
turtle.forward()
turtle.forward()
changeDirection(0)
end
end
changeDirection(0)
for i=1,(trees-1)*3 do turtle.forward() end
changeDirection(1)
deposit()
os.sleep(10)
end
end
function returnBase()
goBack()
local f = fs.open("state", "r")
local dir = tonumber(f.readLine())
print("beginning: "..dir)
if dir == 1 then
changeDirection(0)
changeDirection(0)
elseif dir == 2 then
changeDirection(0)
elseif dir == 0 then
changeDirection(1)
end
while turtle.back() do
end
changeDirection(0)
while turtle.forward() do end
changeDirection(1)
end
function onBoot()
returnBase()
begin()
end
onBoot()