-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvset
executable file
·164 lines (142 loc) · 4.25 KB
/
invset
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
function printApi(obj)
local count = 0
local text= ""
for i,v in pairs(p) do
text = text .. i .. "\n"
end
textutils.pagedPrint(text)
end
function printInventory(inv)
local text = ""
for i = 0,inv.getSizeInventory() do
local slot = inv.getStackInSlot(i)
if slot ~= nil then
text = text.."Slot number "..i.."\n"
for key, value in pairs(slot) do
text = text..key.. " = " .. tostring(value) .. "\n"
end
end
end
textutils.pagedPrint(text)
end
function printArray(arr,interactive)
local text = ""
for i,v in ipairs(arr) do
text = text .. i .. ":" .. v .. "\n"
end
if (interactive) then
textutils.pagedPrint(text)
else
print(text)
end
end
local DEBUG = true
---------------------------------- Item CLASS
local Item = {}
Item.meta = {}
Item.CLASSNAME = "Item"
Item.uuid = function(id,dmg)
if (dmg == nil ) then dmg = 0 end
return id + dmg * 32768
end
Item.className = function()
return Item.CLASSNAME
end
Item.equals = function(self, other)
if (other == nil) then
return false
end
if (self.id == other.id and self.dmg == other.dmg) then
return true
else
return false
end
end
function Item:new(id, dmg, ignoreDmg)
if ignoreDmg == nil then ignoreDmg = false end
assert(type(ignoreDmg) == "boolean", "ignoreDmg must be boolean")
assert(id ~= nil and id >= 0)
assert(dmg ~= nil and dmg >= 0)
local o = {}
o.id = id
o.dmg = dmg
o.ignoreDmg = ignoreDmg
setmetatable(o, self)
self.__index = self
return o
end
----------------------------------- Item CLASS END
---------------------------------- ItemStack CLASS
local ItemStack = {}
function ItemStack:new(item, qty)
local o = {}
assert(item ~= nil and item.className ~= nil and item.className() == Item.CLASSNAME, "Item must be of type "..Item.CLASSNAME)
assert(qty ~= nil and qty >= 0, "Qty must be non-negative number")
o.item = item
o.qty = qty
setmetatable(o, self)
self.__index = self
return o
end
function ItemStack.sameItem(self, other)
if other == nil then return false end
return self.item:equals(other.item)
end
---------------------------------- ItemStack CLASS END
local IGNORE_DMG = -1
function newItemStack(id, dmg, qty)
local ignoreDmg = false
if dmg == IGNORE_DMG then
ignoreDmg = true
dmg = 0
end
return ItemStack:new(Item:new(id, dmg, ignoreDmg), qty)
end
function getItemFromSlotContents(opSlot)
return Item:new(opSlot.id, opSlot.dmg)
end
function fillTarget(src, srcToTurtleDirection, tgt, tgtToTurtleDirection, config)
local allPlaced = true
for slotNumber, itemStack in pairs(config) do
if DEBUG then print('Checking target slot '..slotNumber) end
local needsPlacing = true
local tgtSlot = tgt.getStackInSlot(slotNumber)
if tgtSlot ~= nil then -- check if item is correct, otherwise remove it
if DEBUG then print('Slot contains item') end
if not(itemStack.item:equals(getItemFromSlotContents(tgtSlot))) then
if DEBUG then print('Slot contains wrong type of item. Removing.') end
assert(tgt.pushIntoSlot(tgtToTurtleDirection, slotNumber, 64, 0))
turtle.select(1)
turtle.dropUp()
else
needsPlacing = false
end
end
if needsPlacing then
if DEBUG then print('Looking for item to place in source chest') end
for i = 0, src.getSizeInventory()-1 do
local srcSlot = src.getStackInSlot(i)
if srcSlot ~= nil and itemStack.item:equals(getItemFromSlotContents(srcSlot)) then
print('Item found in source slot '..i)
assert(src.pushIntoSlot(srcToTurtleDirection, i, 1, 0))
assert(tgt.pullIntoSlot(tgtToTurtleDirection, 0, 1, slotNumber))
needsPlacing = false
end
end
end
-- if item still needs placing, then we were not successful - report, that another iteration will be needed
if needsPlacing then
allPlaced = false
end
end
return allPlaced
end
local config = {}
config[0] = newItemStack(4,IGNORE_DMG, 1)
config[5] = newItemStack(3,IGNORE_DMG, 1)
local src = peripheral.wrap("top")
local tgt = peripheral.wrap("bottom")
-- while not placed all items, try to place then wait for refill of source chest
while not(fillTarget(src,tgt,config)) do
sleep(5)
end