Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- prfCrystal = peripheral.wrap("crystallizer_1")
- prfChest = peripheral.wrap("container_chest_0")
- dirChest = "north"
- dirCrystal = "south"
- function getInv(prfSource)
- return prfSource.getAllStacks()
- end
- function filterInv(prfSource, intId, intDmg)
- intDmg = intDmg or 0
- arrSource = getInv(prfSource)
- arrInv = {}
- for i,v in pairs(arrSource) do
- if (v["id"] == intId and v["dmg"] == intDmg) then
- arrInv[i] = v["qty"]
- end
- end
- return arrInv
- end
- function countId(prfSource, intId, intDmg) -- return the number of intId:intDmg in inventory prfSource
- intCount = 0
- intDmg = intDmg or 0
- arrInv = getInv(prfSource)
- for i,v in pairs(arrInv) do
- if (v["id"] == intId and v["dmg"] == intDmg) then
- intCount = intCount + v["qty"]
- end
- end
- return intCount
- end
- function filterInCrystallizer(prfSource, intItem, intDmg)
- intDmg = intDmg or 0
- arrContent = getInv(prfSource)
- arrInv = {0,0,0,0,0}
- for i=1, 5 do
- if arrContent[i] then
- if (arrContent[i]["id"] == intItem and arrContent[i]["dmg"] == intDmg) then
- arrInv[i] = arrContent[i]["qty"]
- end
- end
- end
- return arrInv
- end
- function inCrystallizer(prfSource)
- arrContent = getInv(prfSource)
- arrInv= {}
- tmp = {}
- for i=1, 5 do
- if arrContent[i] then
- tmp["id"] = arrContent[i]["id"]
- tmp["dmg"] = arrContent[i]["dmg"]
- arrInv[i] = tmp
- end
- end
- return arrInv
- end
- function equalize(prfSource, prfDest)
- intQty = 0
- intSplit = 0
- intId = 0
- intDmg = 0
- intOdd = 0
- intPullList = {0,0,0,0,0}
- destInv = inCrystallizer(prfDest)
- prfSource.condenseItems()
- if table.getn(destInv) == 0 then
- if table.getn(getInv(prfSource)) == 0 then return end
- tgt = prfSource.getStackInSlot(1)
- intId = tgt["id"]
- intDmg = tgt["dmg"]
- else
- for _,v in pairs(destInv) do
- intId = v["id"]
- intDmg = v["dmg"]
- end
- end
- intQty = countId(prfSource, intId, intDmg) + countId(prfDest, intId, intDmg)
- intSplit = math.min(64, math.floor(intQty / 5))
- if intSplit ~= 64 then
- intOdd = intQty % 5
- end
- term.clear()
- print (intId .. ":" .. intDmg .. " " .. intQty .. " " .. intSplit .. "*5 + " .. intOdd)
- for i=1, 5 do
- tmpPull = intSplit - filterInCrystallizer(prfDest, intId, intDmg)[i]
- if i <= intOdd then tmpPull = tmpPull + 1 end
- sourceInv = filterInv(prfSource, intId, intDmg)
- for j,k in pairs(sourceInv) do
- tmpPull = tmpPull - prfDest.pullItem(dirCrystal, j, tmpPull, i)
- if tmpPull == 0 then break end
- end
- end
- end
- -- main loop
- while true do
- equalize(prfChest, prfCrystal)
- sleep(0.5)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement