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
|
local function hasItemsInStorage(storage)
for _, _ in pairs(storage.list()) do
return true
end
return false
end
local function hasItemsInTurtle()
for ii = 1, 16 do
if turtle.getItemCount(ii) > 0 then
return true
end
end
return false
end
local function fillTurtle()
local success = true
turtle.select(1)
while success do
success = turtle.suck()
end
end
local function emptyTurtle()
for ii = 1, 16 do
turtle.select(ii)
turtle.drop()
end
end
local function getSpecialCategoryIndex(state, item_name)
for idx, categ in pairs(state.categories) do
for _, pattern in pairs(categ) do
if string.match(item_name, pattern) then
return idx
end
end
end
return 0
end
local function hasIdenticalItemsInStorage(state, storage, slot)
local item = turtle.getItemDetail(slot)
if item == nil then
return false
end
local itemSpecialCat = getSpecialCategoryIndex(state, item.name)
local chestSpecialCat = 0
for _, other in pairs(storage.list()) do
if other.name == item.name then
return true
end
if itemSpecialCat > 0 and chestSpecialCat == 0 then
chestSpecialCat = getSpecialCategoryIndex(state, other.name)
if itemSpecialCat == chestSpecialCat then
return true
end
end
end
return false
end
local function storeIdenticalItemsInStorage(state)
local storage = peripheral.wrap("front")
for ii = 1, 16 do
if hasIdenticalItemsInStorage(state, storage, ii) then
turtle.select(ii)
turtle.drop()
end
end
end
local function getEmptySlotInStorage(storage)
return 5 -- TODO
end
local function swapSlotsInStorage(storage, slotA, slotB)
local emptySlot = getEmptySlotInStorage(storage)
storage.pushItems(peripheral.getName(storage), slotA, nil, emptySlot)
storage.pushItems(peripheral.getName(storage), slotB, nil, slotA)
storage.pushItems(peripheral.getName(storage), emptySlot, nil, slotB)
end
return { getSpecialCategoryIndex = getSpecialCategoryIndex, hasItemsInStorage = hasItemsInStorage, hasItemsInTurtle = hasItemsInTurtle, fillTurtle = fillTurtle, emptyTurtle = emptyTurtle, hadIdenticalItemsInStorage = hasIdenticalItemsInStorage, storeIdenticalItemsInStorage = storeIdenticalItemsInStorage, swapSlotsInStorage = swapSlotsInStorage }
|