Advertisement
antrobot123

machineAPI

Jun 20th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.07 KB | None | 0 0
  1. local pos = 0
  2. local machines = {}
  3. local chestInfo = {}
  4. local dictionary = {}
  5.  
  6. function getDict() return dictionary end
  7.  
  8. function searchRecipes(item,dmg)
  9.     for machineName, machine in pairs(machines) do
  10.         if machine.recipes[item] then
  11.             if machine.recipes[item].damage == dmg then return {bool = true, machine = machineName} end
  12.         end
  13.     end
  14.     return nil
  15. end
  16. function goTo(position)
  17.     while pos ~= position do
  18.         if pos < position then
  19.             turtle.forward()
  20.             pos = pos + 1
  21.         end
  22.         if pos > position then
  23.             turtle.back()
  24.             pos = pos - 1
  25.         end
  26.     end
  27. end
  28.  
  29. function search(direction,item,damage)
  30.     local damage = damage or 0
  31.     local inv = peripheral.wrap(direction)
  32.     local list = inv.list()
  33.     for i=1, inv.size() do
  34.         if list[i] ~= nil then
  35.             if list[i].name == item and list[i].damage == damage then return {slot=i,count=list[i].count} end
  36.         end
  37.     end
  38.     return {count = 0}
  39. end
  40.  
  41. function addMachine(namePre,outputSlot,position,direction,turtleDir)
  42.     machines[namePre] = {pos = position,dir = direction, outSlot = outputSlot, name = namePre, tDir = turtleDir, recipes = {}}
  43. end
  44. function setChest(position, direction,turtleDir)
  45.     chestInfo = {pos = position,dir = direction,tDir = turtleDir}
  46. end
  47. function searchSelf(item,dmg)
  48.     for i=1, 16 do
  49.         local temp = turtle.getItemDetail(i)
  50.         if temp ~= nil then
  51.             if temp.name == item and temp.damage == dmg then return {slot=i,count=temp.count} end
  52.         end
  53.     end
  54.     return {slot=-1,count=0}
  55. end
  56. function addRecipe(machine, result, resultCount,nickname,dmgPre)
  57.     local dmg = dmgPre or 0
  58.     if nickname then dictionary[nickname] = {id = result, damage = dmg} end
  59.     local temp = {}
  60.     for i=1, 16 do
  61.         local itterItem = turtle.getItemDetail(i)
  62.         if itterItem ~= nil then
  63.             temp[i] = {name = itterItem.name,count = itterItem.count,damage = itterItem.damage}
  64.         end
  65.     end
  66.     machines[machine].recipes[result] = {damage = dmg,items = temp, outTotal = resultCount}
  67. end
  68.  
  69. function craft(ammountPre, resultPre, dmg)
  70.     local result = 'nil'
  71.     local damage = 'nil'
  72.     if dictionary[resultPre] then
  73.         result = dictionary[resultPre].id
  74.         damage = dictionary[resultPre].damage
  75.     else
  76.         result = resultPre
  77.         damage = dmg or 0
  78.     end
  79.     local pulls = {}
  80.     local machineInfo = searchRecipes(result,damage)
  81.     if not machineInfo.bool then
  82.         print(result,ammountPre,damage)
  83.         return {bool=false,item=result,count=ammountPre,damage=damage}
  84.     end
  85.     local ammount = math.ceil(ammountPre/machines[machineInfo.machine].recipes[result].outTotal)
  86.     goTo(chestInfo.pos)
  87.     local chest = peripheral.wrap(chestInfo.dir)
  88.     for i, info in pairs(machines[machineInfo.machine].recipes[result].items) do
  89.         local searchInfo = search(chestInfo.dir,info.name,info.damage)
  90.         while searchInfo.count < info.count*ammount do
  91.             if not craft(info.count*ammount-searchInfo.count,info.name,info.damage).bool then return {bool=false} end
  92.             searchInfo = search(chestInfo.dir,info.name,info.damage)
  93.         end
  94.         pulls[info.name] = {count = info.count*ammount, slot = searchInfo.slot,damage=info.damage}
  95.     end
  96.     local slots = {}
  97.     for name, info in pairs(pulls) do
  98.         chest.pushItems(chestInfo.tDir,info.slot,info.count)
  99.         slots[name] = searchSelf(name,info.damage).slot
  100.     end
  101.     goTo(machines[machineInfo.machine].pos)
  102.     local machine = peripheral.wrap(machines[machineInfo.machine].dir)
  103.     for i, info in pairs(machines[machineInfo.machine].recipes[result].items) do
  104.         machine.pullItems(machines[machineInfo.machine].tDir,slots[info.name],64,info.slot)
  105.     end
  106.     local num = 0
  107.     while searchSelf(result,damage).count < machines[machineInfo.machine].recipes[result].outTotal*ammount do
  108.         for i,slot in pairs(machines[machineInfo.machine].outSlot) do
  109.             machine.pushItems(machines[machineInfo.machine].tDir,slot,64)
  110.         end
  111.         os.sleep(.5)
  112.     end
  113.     goTo(chestInfo.pos)
  114.     local chest = peripheral.wrap(chestInfo.dir)
  115.     for i, slot in pairs(machines[machineInfo.machine].outSlot) do
  116.         local temp = turtle.getItemDetail(i)
  117.         chest.pullItems(chestInfo.tDir,i,64,search(chestInfo.dir,temp.name,temp.damage).slot)
  118.         if searchSelf(result).count ~= 0 then chest.pullItems(chestInfo.tDir,i,64) end
  119.     end
  120.     return {bool=true}
  121. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement