Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local pos = 0
- local machines = {}
- local chestInfo = {}
- local dictionary = {}
- function getDict() return dictionary end
- function searchRecipes(item,dmg)
- for machineName, machine in pairs(machines) do
- if machine.recipes[item] then
- if machine.recipes[item].damage == dmg then return {bool = true, machine = machineName} end
- end
- end
- return nil
- end
- function goTo(position)
- while pos ~= position do
- if pos < position then
- turtle.forward()
- pos = pos + 1
- end
- if pos > position then
- turtle.back()
- pos = pos - 1
- end
- end
- end
- function search(direction,item,damage)
- local damage = damage or 0
- local inv = peripheral.wrap(direction)
- local list = inv.list()
- for i=1, inv.size() do
- if list[i] ~= nil then
- if list[i].name == item and list[i].damage == damage then return {slot=i,count=list[i].count} end
- end
- end
- return {count = 0}
- end
- function addMachine(namePre,outputSlot,position,direction,turtleDir)
- machines[namePre] = {pos = position,dir = direction, outSlot = outputSlot, name = namePre, tDir = turtleDir, recipes = {}}
- end
- function setChest(position, direction,turtleDir)
- chestInfo = {pos = position,dir = direction,tDir = turtleDir}
- end
- function searchSelf(item,dmg)
- for i=1, 16 do
- local temp = turtle.getItemDetail(i)
- if temp ~= nil then
- if temp.name == item and temp.damage == dmg then return {slot=i,count=temp.count} end
- end
- end
- return {slot=-1,count=0}
- end
- function addRecipe(machine, result, resultCount,nickname,dmgPre)
- local dmg = dmgPre or 0
- if nickname then dictionary[nickname] = {id = result, damage = dmg} end
- local temp = {}
- for i=1, 16 do
- local itterItem = turtle.getItemDetail(i)
- if itterItem ~= nil then
- temp[i] = {name = itterItem.name,count = itterItem.count,damage = itterItem.damage}
- end
- end
- machines[machine].recipes[result] = {damage = dmg,items = temp, outTotal = resultCount}
- end
- function craft(ammountPre, resultPre, dmg)
- local result = 'nil'
- local damage = 'nil'
- if dictionary[resultPre] then
- result = dictionary[resultPre].id
- damage = dictionary[resultPre].damage
- else
- result = resultPre
- damage = dmg or 0
- end
- local pulls = {}
- local machineInfo = searchRecipes(result,damage)
- if not machineInfo.bool then
- print(result,ammountPre,damage)
- return {bool=false,item=result,count=ammountPre,damage=damage}
- end
- local ammount = math.ceil(ammountPre/machines[machineInfo.machine].recipes[result].outTotal)
- goTo(chestInfo.pos)
- local chest = peripheral.wrap(chestInfo.dir)
- for i, info in pairs(machines[machineInfo.machine].recipes[result].items) do
- local searchInfo = search(chestInfo.dir,info.name,info.damage)
- while searchInfo.count < info.count*ammount do
- if not craft(info.count*ammount-searchInfo.count,info.name,info.damage).bool then return {bool=false} end
- searchInfo = search(chestInfo.dir,info.name,info.damage)
- end
- pulls[info.name] = {count = info.count*ammount, slot = searchInfo.slot,damage=info.damage}
- end
- local slots = {}
- for name, info in pairs(pulls) do
- chest.pushItems(chestInfo.tDir,info.slot,info.count)
- slots[name] = searchSelf(name,info.damage).slot
- end
- goTo(machines[machineInfo.machine].pos)
- local machine = peripheral.wrap(machines[machineInfo.machine].dir)
- for i, info in pairs(machines[machineInfo.machine].recipes[result].items) do
- machine.pullItems(machines[machineInfo.machine].tDir,slots[info.name],64,info.slot)
- end
- local num = 0
- while searchSelf(result,damage).count < machines[machineInfo.machine].recipes[result].outTotal*ammount do
- for i,slot in pairs(machines[machineInfo.machine].outSlot) do
- machine.pushItems(machines[machineInfo.machine].tDir,slot,64)
- end
- os.sleep(.5)
- end
- goTo(chestInfo.pos)
- local chest = peripheral.wrap(chestInfo.dir)
- for i, slot in pairs(machines[machineInfo.machine].outSlot) do
- local temp = turtle.getItemDetail(i)
- chest.pullItems(chestInfo.tDir,i,64,search(chestInfo.dir,temp.name,temp.damage).slot)
- if searchSelf(result).count ~= 0 then chest.pullItems(chestInfo.tDir,i,64) end
- end
- return {bool=true}
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement