-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturtlex.lua
306 lines (249 loc) · 6.49 KB
/
turtlex.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
local turtlex = {}
function turtlex.repeatFunc(amount, func, ...)
amount = amount or 1
if amount < 0 then
error("amount must be >= 0")
end
for i = 1, amount do
func(unpack(arg))
end
end
-- Movement functions
function turtlex.turnLeft(amount)
turtlex.repeatFunc(amount, turtle.turnLeft)
end
function turtlex.turnRight(amount)
turtlex.repeatFunc(amount, turtle.turnRight)
end
function turtlex.turnAround()
turtlex.turnLeft(2)
end
function turtlex.getTurnFunction(direction)
if direction == "right" then
return turtlex.turnRight
elseif direction == "left" then
return turtlex.turnLeft
end
end
function turtlex.getOppositeDirection(direction)
if direction == "left" then return "right"
elseif direction == "right" then return "left"
elseif direction == "up" then return "down"
elseif direction == "down" then return "up"
elseif direction == "forward" then return "back"
elseif direction == "back" then return "forward"
end
end
function turtlex.turn(direction, amount)
turtlex.getTurnFunction(direction)(amount)
end
function turtlex.turnOpposite(direction)
turtlex.turn(turtlex.getOppositeDirection(direction))
end
function turtlex.forward(amount, force)
if force == nil then
force = true
end
turtlex.repeatFunc(amount,
function()
if force then
turtle.dig()
turtle.suck()
end
turtle.forward()
end
)
end
function turtlex.back(amount)
turtlex.repeatFunc(amount, turtle.back)
end
function turtlex.up(amount)
turtlex.repeatFunc(amount, turtle.up)
end
function turtlex.down(amount)
turtlex.repeatFunc(amount, turtle.down)
end
-- Dig functions
function turtlex.getDigFunction(direction)
if direction == "up" then
return turtle.digUp
elseif direction == "down" then
return turtle.digDown
elseif direction == nil or direction == "forward" then
return turtle.digDown
end
end
function turtlex.dig(direction)
local func = turtlex.getDigFunction(direction)
assert(func)
func()
end
-- Suck functions
function turtlex.getSuckFunction(direction)
if direction == "up" then
return turtle.suckUp
elseif direction == "down" then
return turtle.suckDown
elseif direction == nil or direction == "forward" then
return turtle.suck
end
end
function turtlex.suck(direction)
local func = turtlex.getSuckFunction(direction)
assert(func)
func()
end
-- Place functions
function turtlex.getPlaceFunction(direction)
if direction == "up" then
return turtle.placeUp
elseif direction == "down" then
return turtle.placeDown
elseif direction == nil or direction == "forward" then
return turtle.place
end
end
function turtlex.place(direction)
local func = turtlex.getPlaceFunction(direction)
assert(func)
func()
end
-- Drop functions
function turtlex.getDropFunction(direction)
if direction == "up" then
return turtle.dropUp
elseif direction == "down" then
return turtle.dropDown
elseif direction == nil or direction == "forward" then
return turtle.drop
end
end
function turtlex.drop(direction, ...)
local func = turtlex.getDropFunction(direction)
assert(func)
func(...)
end
-- Inspection functions
function turtlex.getInspectFunction(direction)
if direction == "down" then
return turtle.inspectDown
elseif direction == "up" then
return turtle.inspectUp
elseif direction == "forward" or direction == nil then
return turtle.inspect
end
end
function turtlex.getBlockData(direction)
local inspectFunc = turtlex.getInspectFunction(direction)
assert(inspectFunc)
local success, data = inspectFunc()
if success then
return data
end
end
function turtlex.getItemData(slot)
slot = slot or turtle.getSelectedSlot()
return turtle.getItemDetail(slot)
end
function turtlex.hasTag(data, tag)
if data and data.tags then
return data.tags[tag] ~= nil
end
return false
end
-- Inventory functions
function turtlex.findItem(name, startSlot)
startSlot = startSlot or turtle.getSelectedSlot()
for i = 1, 16 do
local slot = (startSlot - 2 + i) % 16 + 1
local data = turtlex.getItemData(slot)
if data and ((not name) or (data.name == name)) then
return slot
end
end
end
function turtlex.selectItem(name, startSlot)
local slot = turtlex.findItem(name, startSlot)
if slot then
turtle.select(slot)
end
return slot
end
function turtlex.getTotalItemCount(name)
local total = 0
for slot = 1, 16 do
local data = turtlex.getItemData(slot)
if data and data.name == name then
total = total + turtle.getItemCount(slot)
end
end
return total
end
function turtlex.consolidate()
local i, slotA, slotB
for i = 1, 16 do
local countI = turtle.getItemCount(i)
slotA = turtlex.findItem(nil, i)
if slotA >= i then
while true do
local remainingA = turtle.getItemSpace(slotA)
-- Exit condition 1
if remainingA == 0 then
break
end
local data = turtlex.getItemData(slotA)
assert(data)
slotB = turtlex.findItem(data.name, slotA + 1)
-- Exit condition 2
if (slotB == nil) or (slotB <= slotA) then
break
end
local countB = turtle.getItemCount(slotB)
local itemsToTransfer = math.min(remainingA, countB)
assert(itemsToTransfer > 0)
-- Move items
local existingSlot = turtle.getSelectedSlot()
turtle.select(slotB)
local success = turtle.transferTo(slotA, itemsToTransfer)
turtle.select(existingSlot)
-- Exit condition 3
if not success then
break
end
end -- Swap loop
elseif countI and slotA > i then -- slotA ~= i, but contains something. Move it to i.
local existingSlot = turtle.getSelectedSlot()
turtle.select(slotA)
turtle.transferTo(i)
turtle.select(existingSlot)
end
end-- Slot loop
end
function turtlex.getNextEmptySlot(startSlot)
startSlot = startSlot or turtle.getSelectedSlot()
for i = 1, 16 do
local slot = (startSlot - 2 + i) % 16 + 1
local data = turtlex.getItemData(slot)
if not data then
return slot
end
end
end
function turtlex.getEmptySlots()
emptySlots = {}
for slot = 1, 16 do
if turtle.getItemCount(slot) == 0 then
table.insert(emptySlots, slot)
end
end
return emptySlots
end
function turtlex.emptyInventory(direction)
local existingSlot = turtle.getSelectedSlot()
for slot = 1, 16 do
turtle.select(slot)
turtlex.drop(direction)
end
turtle.select(existingSlot)
end
return turtlex