From d65cb871869b0b8ad1d0f5c2669c53e59f9af657 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Tue, 13 Feb 2018 11:59:38 +0800 Subject: [PATCH] Add giant storage --- giant_storage.lua | 86 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 giant_storage.lua diff --git a/giant_storage.lua b/giant_storage.lua new file mode 100644 index 0000000..f1472ab --- /dev/null +++ b/giant_storage.lua @@ -0,0 +1,86 @@ +local component = require("component") +local sides = require("sides") +local serialization = require("serialization") + +local function getSum(device) + local unit=0 + local cnt=0 + local allunit=0 + + local sz=device.getInventorySize(sides.north) + + -- faster enum + local x=device.getAllStacks(sides.north) + while true do + local y=x() + if(y==nil) then + break + end + if(y.size~=nil) then + cnt=cnt+y.size + unit=unit+1 + end + end + + allunit=sz + + sz=device.getInventorySize(sides.south) + + -- faster enum + x=device.getAllStacks(sides.south) + while true do + local y=x() + if(y==nil) then + break + end + if(y.size~=nil) then + cnt=cnt+y.size + unit=unit+1 + end + end + + allunit=allunit+sz + + return cnt,unit,allunit +end + +while true do + local lst=component.list("inventory_controller") + + local sumunit=0 + local totalunit=0 + local sum=0 + local idx=1 + while true do + local addr=lst() + if(addr==nil) then + break + end + print("Counting " .. idx) + local val,unit,allunit=getSum(component.proxy(addr)) + print("ChestGroup " .. idx .. " has " .. val .. " items") + sum=sum+val + sumunit=sumunit+unit + totalunit=totalunit+allunit + idx=idx+1 + end + + local str="Total " .. sum .. " items of " .. totalunit*64 .. " (" .. sum/totalunit/64*100 .. "%). " .. + sumunit .. " of " .. totalunit .. " units used (" .. sumunit/totalunit*100 .. "%) " + + print("Total " .. sum .. " items") + + local tb={} + tb.item=sum + tb.maxitem=totalunit*64 + tb.unit=sumunit + tb.maxunit=totalunit + + print(str) + + component.modem.broadcast(10010,str) + component.modem.broadcast(10011,serialization.serialize(tb)) + + print("Waiting for next loop...") + os.sleep(5) +end