Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function pickEnder(enderNumber,enderChest) --enderNumber is between 0 and 4096, enderChest is the wrapped chest
- b1 = math.floor(enderNumber/ 256) -- find first colour code
- b2 = math.floor((enderNumber - (256 *b1)) / 16)
- b3 = enderNumber % 16
- error = enderChest.setColors(2^b1,2^b2,2^b3)
- return error
- end -- ends pickEnder()
- function readLookUp(fileName)
- if fs.exists(fileName) then
- local file = fs.open(fileName,"r")
- local data = file.readAll()
- file.close()
- return textutils.unserialize(data)
- else
- local data = {}
- return data
- end
- end
- function saveLookUp(data,fileName)
- local file = fs.open(fileName, "w")
- file.write(textutils.serialize(data))
- file.close()
- end
- function inToOut(inSort, outSort, inv, amount)
- inSort.pushItem("west",inv, amount)
- outSort.pullItem("east",1,amount)
- end
- function findHome(inSort, outSort, inv, amount, outBox)-- this function figures out where to send items and deals with them. outBox is a table with a list of possible outputs
- local done = false --- this is triggered to true when the item finds a home
- local countLoop = 0
- local itemTable = {}
- while (done == false) do
- countLoop = countLoop + 1
- if countLoop > #outBox then
- print("Things are backed up")
- sleep(5)
- countLoop = 1
- end
- if outBox[countLoop] == -1 then
- inSort.destroyStack(inv)
- done = true
- else
- pickEnder(outBox[countLoop],outSort)
- outSort.condenseItems()
- itemTable = outSort.getAllStacks()
- if #itemTable < outSort.getInventorySize() then -- if the box isn't full
- inToOut(inSort, outSort, inv, amount)
- done = true
- end
- end
- end
- end
- -- function assumes garbage is north
- function addToScrap(ScrapAddSort,data,outSort) --fixed
- local itemsMoved = 0
- local ana
- local addItem
- pickEnder(1365,ScrapAddSort) -- input from garbage can is 1365
- pickEnder(1364,outSort)
- for inv = 1, ScrapAddSort.getInventorySize() do
- ana = ScrapAddSort.getStackInSlot(inv)
- addItem = 1
- if ana == nil then
- addItem = 0
- else
- for i = 1, #data, 1 do
- if ana.id == data[i].id then
- addItem = 2
- end
- end
- if addItem == 1 then
- -- inserts entry into table
- table.insert(data,{raw_name = ana.raw_name, id = ana.id,name = ana.name, outBox = {1364,-1}})
- saveLookUp(data,"GarbageList")
- elseif addItem > 1 then
- findHome(ScrapAddSort, outSort, inv, ana.qty, data[#data].outBox)
- --inToOut(ScrapAddSort, outSort, inv, ana.qty)
- itemsMoved = itemsMoved + 1
- end
- end
- end
- return itemsMoved, data
- end
- -- function for furnace
- function addToFurnace(fAddSort,oldData) --fixed
- local itemsMoved = 0
- local ana
- local addItem
- local fdata ={}
- local inChestNumber = 3822 -- red red red for ender chest input to furnace
- local outChestNumber = 3809 -- red red orange for ender chest output to furnace
- local outSecondChestNumber = 4091 -- second output if furnace is backed up
- pickEnder(inChestNumber,fAddSort)
- for inv = 1, fAddSort.getInventorySize() do
- ana = fAddSort.getStackInSlot(inv)
- addItem = 1
- if ana == nil then
- addItem = 0
- else
- if #oldData ~= nil then
- for i = 1, #oldData, 1 do
- if ana.id == oldData[i].id then
- addItem = 2
- end
- end
- end
- if addItem > 0 then
- -- inserts entry into table
- table.insert(oldData,{raw_name = ana.raw_name, id = ana.id,name = ana.name, outBox = {outChestNumber,outSecondChestNumber}})
- if addItem == 1 then
- itemsMoved = itemsMoved + 1
- end
- end
- end
- end
- return itemsMoved, oldData
- end
- -- this function assumes garbage is "west" furnace is east and master sorter is north
- function comp(compSort,outSort, data, inputNumbers ) --fixed to work with chest as peripheral -- compSort is input, data is a table with all the list, outSort is the output chest, inputNumbers is a table with a list of input chest to check
- local itemsMoved = 0
- local ana
- local notGarbage
- local j, k = 1 -- used to count
- local defOut = {4091} --default output chest number
- for i = 1, #inputNumbers do --- do this for every input chest
- pickEnder(inputNumbers[i],compSort) -- sets the input ender chest
- for inv = 1, compSort.getInventorySize() do -- scans through the chest on slot at a time
- ana = compSort.getStackInSlot(inv)
- if ana ~= nil then -- if the slot is not empty
- notGarbage = 2
- j = 1
- while (notGarbage == 2) do --j = 1, #data do -- scan through each list one at a time
- k = 1
- notGarbage = 1
- while (notGarbage == 1) do --k = 1, #data[j] -- scan through each item in that list
- if data[j][k] ~= nil then
- if (data[j][k].raw_name ~= nil) then
- if (data[j][k].raw_name == ana.raw_name) then -- if the list has a raw name use that first other wise use id
- findHome(compSort, outSort, inv, ana.qty, data[j][k].outBox)
- itemsMoved = itemsMoved + 1
- notGarbage = 0
- end
- elseif (ana.id == data[j][k].id) and (data[j][k].raw_name == nil) then
- findHome(compSort, outSort, inv, ana.qty, data[j][k].outBox)
- itemsMoved = itemsMoved + 1
- notGarbage = 0
- end
- end
- k = k + 1
- if (k > #data[j]) then notGarbage = 2 end
- end
- j = j + 1
- if (j > #data) then notGarbage = 3 end
- end
- if notGarbage == 3 then -- this should only happen if item is not on list
- findHome(compSort, outSort, inv, ana.qty, defOut)
- itemsMoved = itemsMoved + 1
- end
- end
- end
- end
- return itemsMoved
- end
- --globals
- local inSort = peripheral.wrap("left")
- local outSort = peripheral.wrap("right")
- local sortKey = {} --- this is a multi dimensional table that holds a number of list to be used to compare items too
- local inputChestNumbers = {4095,4093}
- local isPresent = 1
- local temp = 1
- sortKey[1] = readLookUp("GarbageList") -- first item in the list is garbage
- sortKey[2] = {}
- sortKey[3] = readLookUp("seedList")
- -- main program
- while (true) do
- while isPresent ~= 0 do
- temp, sortKey[2] = addToFurnace(inSort, sortKey[2]) --- second list is things to go to furnace
- isPresent = 0
- temp = comp(inSort, outSort, sortKey, inputChestNumbers) --
- isPresent = isPresent + temp
- temp = addToScrap(inSort,sortKey[1], outSort)
- isPresent = isPresent + temp
- sleep(0.4)
- end
- isPresent = 1
- end
- print("done")
Add Comment
Please, Sign In to add comment