Advertisement
promitheus_sal

warehouse_interface

Mar 18th, 2024
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.11 KB | None | 0 0
  1.  
  2. STORAGE_ROOT_DIR = "./"
  3. STORAGE_BIN_LIST_FILENAME = "bins.list"
  4. bins = {}
  5. main_bin = ""
  6.  
  7. function logwarn(e)
  8.     print("WARN> "..e)
  9. end
  10.  
  11. function logerr(e)
  12.     print("ERROR> "..e)
  13. end
  14.  
  15. function logverb(e)
  16.     print("VERBOSE> "..e)
  17. end
  18.  
  19. --read bins
  20. for l in io.lines(STORAGE_ROOT_DIR..STORAGE_BIN_LIST_FILENAME) do
  21.     local a,b,c = l:match("^([^;]+);([^;]+);([^;]+)$")
  22.     if bins[b]~=nil then
  23.         logwarn(("Duplicate bin alias detected: key '%s' already declared ('%s'), ignoring new value"):format(b,bins[b]))
  24.     else
  25.         bins[b]={type=a,id=b,val=c}
  26.     end
  27.  
  28.     for id, dat in pairs(bins) do
  29.         if dat.type=="main" then
  30.             if main_bin~="" then
  31.                 logwarn(string.format("Duplicate main bin detected (%s); ignoring new value",main_bin))
  32.             else
  33.                 main_bin = id
  34.             end
  35.         end
  36.     end
  37. end
  38.  
  39. function list_bins(all,type,blacklist,limit)
  40.     local r = {}
  41.     blacklist = blacklist or {}
  42.     limit=limit or 1000000
  43.     if limit==-1 then limit=1000000 end
  44.     for id,dat in pairs(all) do
  45.         if (not blacklist[id]) and (type==dat.type) then
  46.             r[id] = dat
  47.         end
  48.     end
  49.     return r
  50. end
  51.  
  52. function pushItems(from,to,inslot,limit,toslot)
  53.     local r = peripheral.call(from,"pushItems",to,inslot,limit,toslot)
  54.     print(string.format("Moving %d items from '%s' to '%s'",r,from,to))
  55.     return r
  56. end
  57.  
  58. function getInvSize(inv)
  59.     return peripheral.call(inv,"size")
  60. end
  61.  
  62. function listInv(inv)
  63.     return peripheral.call(inv,"list")
  64. end
  65.  
  66. function itemDetails(inv,slot)
  67.     return peripheral.call(inv,"getItem",slot).getMetadata()
  68. end
  69.  
  70. function store_to(from,fromslot,to,limit)
  71.     return pushItems(from.val,to.val,fromslot,limit)
  72. end
  73.  
  74. function store_all_to(from,to,limit)
  75.     local t=0
  76.     limit=limit or 1000000
  77.     if limit==-1 then limit=1000000 end
  78.     for slot,item in pairs(listInv(from)) do
  79.         t=t+pushItems(from.val,to.val,slot,limit-t)
  80.         if t==limit then return limit end
  81.     end
  82.     return t
  83. end
  84.  
  85. function store_to_any(from,fromslot,tolist,limit)
  86.     local t=0
  87.     limit=limit or 1000000
  88.     if limit==-1 then limit=1000000 end
  89.     local l={}
  90.     for id,dat in pairs(tolist) do
  91.         table.insert(l,dat)
  92.     end
  93.     for _,dat in pairs(l) do
  94.         t=t+pushItems(from.val,dat.val,fromslot,limit-t)
  95.         if t==limit then return limit end
  96.     end
  97.     return t
  98. end
  99.  
  100. function store_all_to_any(from,tolist,limit)
  101.     limit=limit or 1000000
  102.     if limit==-1 then limit=1000000 end
  103.     local t=0
  104.     for slot,item in pairs(listInv(from.val)) do
  105.         t=t+store_to_any(from,slot,tolist,limit-t)
  106.         if t==limit then return limit end
  107.     end
  108.     return t
  109. end
  110.  
  111. function default_filter(txt)
  112.     return function (det)
  113.         local f = txt:lower()
  114.         local a = det.name:lower():find(f)
  115.         local b = det.displayName:lower():find(f)
  116.         return a or b
  117.     end
  118. end
  119.  
  120. function fetch_from(from,to,filter,limit)
  121.     if type(filter)~="function" then if filter then filter=function() return true end else filter=function() return false end end end
  122.     local t=0
  123.     limit=limit or 1000000
  124.     if limit==-1 then limit=1000000 end
  125.     for slot,item in pairs(listInv(from.val)) do
  126.         if filter(itemDetails(from.val,slot)) then
  127.             t=t+store_to(from,slot,to,limit-t)
  128.         end
  129.         if limit==t then return limit end
  130.     end
  131.     return t
  132. end
  133.  
  134. function fetch_from_any(fromlist,to,filter,limit)
  135.     if type(filter)~="function" then if filter then filter=function() return true end else filter=function() return false end end end
  136.     local t=0
  137.     limit=limit or 1000000
  138.     if limit==-1 then limit=1000000 end
  139.     local l = {}
  140.     for id,dat in pairs(fromlist) do
  141.         table.insert(l,dat)
  142.     end
  143.     for _,dat in pairs(l) do
  144.         t=t+fetch_from(dat,to,filter,limit-t)
  145.         if limit==t then return limit end
  146.     end
  147.     return t
  148. end
  149.  
  150. function tokens(str)
  151.     local tok = {}
  152.     local isStr = false
  153.     local cur = ""
  154.     for i=1,#str do
  155.         local nc = str:sub(i,i)
  156.         if nc=="'" then
  157.             isStr = not isStr
  158.         else
  159.             if nc==' ' and not isStr then
  160.                 if cur~='' then
  161.                     table.insert(tok,cur)
  162.                 end
  163.                 cur=''
  164.             else
  165.                 cur=cur..nc
  166.             end
  167.         end
  168.     end
  169.     table.insert(tok,cur)
  170.     return tok
  171. end
  172.  
  173. function exec_command(cmd)
  174.     local tk = tokens(cmd)
  175.     cmd = tk[1]
  176.     if cmd=="storeall" or cmd=='s' then
  177.         print(("Stored %d items."):format(store_all_to_any(bins[main_bin],list_bins(bins,"storage",false))))
  178.     elseif cmd=="fetch" or cmd=='f' then
  179.         print(("Fetched %d x '%s'."):format(fetch_from_any(list_bins(bins,"storage",false),bins[main_bin], default_filter(tk[2]), tonumber(tk[3] or -1)),tk[2]))
  180.     elseif cmd=='moveto' or cmd=='m' then
  181.         local where = tk[2]
  182.         local what = tk[3]
  183.         local limit = tonumber(tk[4])
  184.         print(string.format("Moved %d x '%s' to '%s'.",fetch_from_any(list_bins(bins,"storage",{[where]=true}),bins[where],default_filter(what),limit),what,where))
  185.     elseif cmd=='listbins' or cmd=='lb' then
  186.         print("Listing bins:")
  187.         for id,v in pairs(bins) do
  188.             print(string.format("#'%s': pid=\"%s\" type='%s'",id,v.val,v.type))
  189.         end
  190.     elseif cmd=='main' then
  191.         main_bin = tk[2]
  192.         print("Set '"..tk[2].."' as main bin.")
  193.     elseif cmd=='help' or cmd=='h' then
  194.         print([[
  195. Help:
  196. storeall -> store all items from main bin to any storage
  197. moveto | m <where> <filter> <limit> -> move at most <limit >items that meet <filter> from any storage to <where>
  198. fetch | f <filter> <limit> -> same as "moveto main <filter> <limit>"
  199. main <bin> -> set <bin> as main bin
  200. listbins -> list all bins
  201.         ]])
  202.     elseif cmd=="exit" or cmd=="quit" or cmd=="q" then return false end
  203.     return true
  204. end
  205.  
  206. logverb(string.format("Main bin: %s",main_bin))
  207. logverb("Bins:")
  208. for id,v in pairs(bins) do
  209.     logverb(string.format("%s: type: %s val: %s",id,v.type,v.val))
  210. end
  211.  
  212. while exec_command(io.read('*l')) do end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement