From f707f4dc34d3a48ad2ebaaa4130c777a9a219f47 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 5 Jan 2023 05:29:34 +0000 Subject: shop2 --- shop2.lua | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/shop2.lua b/shop2.lua index 37115e8..3659402 100644 --- a/shop2.lua +++ b/shop2.lua @@ -41,7 +41,14 @@ local function parsemeta(m) return out end -local chest = peripheral.wrap"back" +local chest +for _,name in ipairs(peripheral.getNames()) do + if name:match("chest") then chest = peripheral.wrap(name) break end +end +assert(chest,"couldn't find chest") +-- might not work if there are multiple modems but why would you do that +local localname = peripheral.find("modem").getNameLocal() + local function take_stock() -- if you do this in parallel, you avoid having to wait 1 tick for each -- slot of the chest. which is probably good. @@ -71,23 +78,19 @@ local function stock_amt(stockinfo,idesc) return total end -local si = take_stock() -pp(si) - -for iadr,itm in pairs(ITEMS) do - print(iadr,itm.hname,stock_amt(si,itm)) - -end - --- XXX -local function give(amt) +local function give(stockinfo,idesc,amt) amt = amt or 1 -- assumes that we have at least amt in stock already - for i=1,16 do - local x = turtle.getItemDetail(i) - if x and x.name == ITEM then - local a = math.min(amt,x.count) - turtle.select(i) + -- actually no, might as well double check this anyway + assert(stock_amt(stockinfo,idesc) >= amt) + assert(amt>0) + -- make sure current turtle inv slot is empty + assert(turtle.getItemCount() == 0) + + for slotidx,idtl in pairs(stockinfo) do + if item_desc_matches(idesc,idtl) then + local a = math.min(amt,idtl.count) + chest.pushItems(localname, slotidx, a, 1) turtle.drop(a) amt = amt - a if amt <= 0 then break end -- cgit v1.2.3