Advertisement
osmarks

AutoKit

May 9th, 2020
1,505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.50 KB | None | 0 0
  1. local box_side = "down"
  2. local box = peripheral.find "thermalexpansion:storage_strongbox"
  3. local interface = peripheral.find "appliedenergistics2:interface"
  4.  
  5. local kitfile = ...
  6. if not kitfile then error "provide a kit file" end
  7. local kit = dofile(kitfile)
  8.  
  9. for slot, stack in pairs(box.list()) do
  10.     local name = stack.name .. "@" .. tostring(stack.damage)
  11.     print(stack.count, name, "already present")
  12.     for i, it in pairs(kit) do
  13.         if it[1] == stack.name or it[1] == name then
  14.             it[2] = it[2] - stack.count
  15.             if it[2] <= 0 then table.remove(kit, i) end
  16.             break
  17.         end
  18.     end
  19. end
  20.  
  21. local function free_crafting_CPUs()
  22.     local count = 0
  23.     for _, CPU in pairs(interface.getCraftingCPUs()) do
  24.         if not CPU.busy then count = count + 1 end
  25.     end
  26.     return count
  27. end
  28.  
  29. local max_concurrency = math.max(free_crafting_CPUs() / 2, 1)
  30. print("Using max", max_concurrency, "crafting CPUs")
  31.  
  32. local function display_kit_item(i)
  33.     return ("%s x%d"):format(i[1], i[2])
  34. end
  35.  
  36. local function export(item, count)
  37.     local total = 0
  38.     while total < count do
  39.         local new = item.export(box_side, count - total)
  40.         if new == 0 then error "no items available or storage full" end
  41.         total = total + new
  42.     end
  43. end
  44.  
  45. local tasks = {}
  46.  
  47. while true do
  48.     if #tasks < max_concurrency and #kit > 0 then
  49.         -- pop off next item
  50.         local nexti = table.remove(kit, 1)
  51.         local item = interface.findItem(nexti[1])
  52.         if not item then error(display_kit_item(nexti) .. " not found?") end
  53.         local desired = nexti[2]
  54.         local existing = item.getMetadata().count
  55.         if existing < desired then
  56.             local crafting_job = item.craft(desired - existing)
  57.             print("Queueing", display_kit_item(nexti))
  58.             table.insert(tasks, { job = crafting_job, itemtype = nexti, item = item })
  59.         end
  60.         if existing > 0 then
  61.             export(item, math.min(existing, desired))
  62.             print("Exporting existing", display_kit_item { nexti[1], math.min(existing, desired) })
  63.         end
  64.     else
  65.         for i, task in pairs(tasks) do
  66.             local status = task.job.status()
  67.             if status == "canceled" then
  68.                 error("Job for " .. display_kit_item(task.itemtype) .. " cancelled by user")
  69.                 table.remove(tasks, i)
  70.             elseif status == "missing" then
  71.                 error("Job for " .. display_kit_item(task.itemtype) .. " missing items")
  72.                 table.remove(tasks, i)
  73.             elseif status == "finished" then
  74.                 print("Exporting", display_kit_item(task.itemtype))
  75.                 export(task.item, task.itemtype[2])
  76.                 table.remove(tasks, i)
  77.             end
  78.         end
  79.         sleep(1)
  80.     end
  81.     if #tasks == 0 and #kit == 0 then print "Done!" break end
  82. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement