Advertisement
promitheus_sal

warehouse_manager.lua

Nov 20th, 2022 (edited)
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.61 KB | None | 0 0
  1. links = {}
  2.  
  3. function log(t) print(t) end
  4.  
  5. function loadLink(l)
  6.     local newLink = {}
  7.     for i in string.gmatch(l,"[^!;]+![^!;]+;") do
  8.         local k,v = string.match(i,"([^!]+)!([^;]+)")
  9.         newLink[k] = v
  10.         --[[
  11.             {
  12.                 from, fromslot, to, toslot, limit, filter
  13.             }
  14.         ]]--
  15.     end
  16.     return newLink
  17. end
  18.  
  19. for l in io.lines("/warehouse_links.list") do
  20.     table.insert(links,loadLink(l))
  21. end
  22.  
  23. function tryPush(from, to, fromslot, toslot, limit, filter)
  24.     limit = limit or 64
  25.     detail = peripheral.call(from,"getItemDetail",fromslot)
  26.     if filter then
  27.         if detail.name~=filter then
  28.             return 0
  29.         end
  30.     end
  31.  
  32.     local r = peripheral.call(from,"pushItemsTo",to,fromslot,limit,toslot)
  33.     log(("[%d x %s] (%d@%s)->(%d@%s)"):format(detail.count,detail.name,fromslot,from,toslot,to))
  34.     return r
  35. end
  36.  
  37. function handleLink(link)
  38.     if link.fromslot==-1 then
  39.         for i=1,peripheral.call(link.from,"size") do
  40.             tryPush(link.from, link.to,link.fromslot, link.toslot, link.limit, link.filter)
  41.         end
  42.     elseif link.fromslot then
  43.         peripheral.call(link.from,"pushItemsTo", link.to, link.fromslot, link.max or 64, link.toslot)
  44.     else
  45.         for i=1,peripheral.call(link.from,"size") do
  46.             if tryPush(link.from, link.to, link.fromslot, link.toslot, link.limit, link.filter)>0 then
  47.                 return
  48.             end
  49.         end
  50.     end
  51. end
  52.  
  53. function handleLinks()
  54.     for _,link in pairs(links) do
  55.         handleLink(link)
  56.     end
  57. end
  58.  
  59. while true do
  60.     handleLinks()
  61.     sleep(3)
  62. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement