-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleFelling.lua
204 lines (189 loc) · 5.77 KB
/
simpleFelling.lua
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
local Helper = require("Helper")
-- TODO/Future Plan: add states here too (like Farming) but not sure if i will do it
local args = {...}
local acceptedFuels = {
"minecraft:coal_block",
"minecraft:coal",
"minecraft:lava_bucket",
"bloodmagic:lava_crystal"
}
local fuelSlots = {}
for x = 1, #acceptedFuels do
local slot = Helper.GetItem(acceptedFuels[x])
if(slot ~= nil) then
table.insert(fuelSlots, slot)
end
end
local saplingSlots = {}
for i = 1, 16, 1 do
local data = turtle.getItemDetail(i)
if(data ~= nil) then
if(string.match(data.name, "sapling") ~= nil) then
table.insert(saplingSlots, i)
end
end
end
-- fuels the turtle and updates the fueltable
local function fuelling()
if(turtle.getFuelLevel ~= "unlimited" and turtle.getFuelLevel() < 50) then
for x = 1, #fuelSlots do
local data = turtle.getItemDetail(fuelSlots[x])
if(data ~= nil) then
turtle.select(fuelSlots[x])
turtle.refuel()
return
else
table.remove(fuelSlots, x)
end
end
if(next(fuelSlots) == nil) then
local isnil = true
term.setTextColor( colors.red )
print("No Fuel Any more")
term.setTextColor( colors.white )
while(isnil) do
for x = 1, #acceptedFuels do
local slot = Helper.GetItem(acceptedFuels[x])
if(slot ~= nil) then
table.insert(fuelSlots, slot)
isnil = false
end
end
end
term.setTextColor( colors.yellow )
print("Got Fuel, Continuing")
term.setTextColor( colors.white )
fuelling()
end
end
end
-- just walks the desired length forward
local function walk(length)
for i = 1, length, 1 do
turtle.forward()
end
end
local function deposit()
turtle.turnLeft()
turtle.turnLeft()
local isBlock, block = turtle.inspect()
if(isBlock) then
if(string.match(block.name, "chest") ~= nil) then
local unallowedSlots = {}
for x = 1, #fuelSlots do
local data = turtle.getItemDetail(fuelSlots[x])
if(data ~= nil and data.name ~= "minecraft:bucket") then
table.insert(unallowedSlots, fuelSlots[x])
else
table.remove(fuelSlots, x)
end
end
for x = 1, #saplingSlots do
local data = turtle.getItemDetail(saplingSlots[x])
if(data ~= nil and data.name ~= "minecraft:bucket") then
table.insert(unallowedSlots, saplingSlots[x])
else
table.remove(saplingSlots, x)
end
end
for i = 1, 16, 1 do
turtle.select(i)
local allowed = true
for x = 1, #unallowedSlots do
if(i == unallowedSlots[x]) then
allowed = false
end
end
if(allowed)then
turtle.drop()
end
end
turtle.turnLeft()
turtle.turnLeft()
return
end
end
turtle.turnLeft()
turtle.turnLeft()
print("Please place a chest behind the turtle")
end
local function allAroundDig()
for i = 1, 4, 1 do
turtle.dig()
turtle.turnLeft()
end
end
local function placeSapling()
for x = 1, #saplingSlots do
local data = turtle.getItemDetail(saplingSlots[x])
if(data ~= nil) then
turtle.select(saplingSlots[x])
turtle.place()
return
else
table.remove(saplingSlots, x)
end
end
if(next(saplingSlots) == nil) then
local isnil = true
term.setTextColor( colors.red )
print("No saplings any more")
term.setTextColor( colors.white )
while(isnil) do
for i = 1, 16, 1 do
local data = turtle.getItemDetail(i)
if(data ~= nil) then
if(string.match(data.name, "sapling") ~= nil) then --FIXME
-- Too long without yielding
-- i have no idea right now why this happens sooooo i will just leave this comment here
table.insert(saplingSlots, i)
end
end
end
end
term.setTextColor( colors.yellow )
print("Got saplings, Continuing")
term.setTextColor( colors.white )
placeSapling()
end
end
local function fell()
turtle.dig()
turtle.forward()
local goneUp = 1
while(turtle.inspectUp()) do
-- TODO: check if leaves are in front and only then do the allAroundDig
allAroundDig()
turtle.digUp()
turtle.up(0)
goneUp = goneUp + 1;
end
for i = 1, goneUp, 1 do
turtle.down()
end
turtle.back()
print("cut down the tree")
end
term.setTextColor( colors.yellow )
-- check if fuel exists
if(next(fuelSlots) == nil ) then
print("WARNING: you do not have any fuel, the turtle is likely to run out of juice \nCurrent Fuel: ", turtle.getFuelLevel())
end
if(next(saplingSlots) == nil ) then
print("WARNING: you do not have any saplings, the turtle is likely not able to place a new tree")
end
-- Normal Operation
term.setTextColor( colors.white )
print("Startup finished")
while true do
fuelling()
local isBlock, block = turtle.inspect()
if(isBlock) then
if(string.match(block.name, "log") ~= nil) then
fell()
deposit()
end
else
placeSapling()
end
end