From c3d1cdb9d7ccc5cd7502a7bccc4207a17a27c525 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sat, 7 Jan 2023 05:15:11 +0000 Subject: shop2 --- shop2/stock.lua | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'shop2/stock.lua') diff --git a/shop2/stock.lua b/shop2/stock.lua index ce76bf0..71a6844 100644 --- a/shop2/stock.lua +++ b/shop2/stock.lua @@ -27,7 +27,7 @@ local function item_desc_matches(idesc,itemdetail) return idesc.itname == itemdetail.name end -local function stock_amt(stockinfo,idesc) +local function amt_of(stockinfo,idesc) local total = 0 for ix,itm in pairs(stockinfo) do if item_desc_matches(idesc,itm) then @@ -37,3 +37,25 @@ local function stock_amt(stockinfo,idesc) return total end +local function give(stockinfo,idesc,amt) + amt = amt or 1 + -- assumes that we have at least amt in stock already + -- actually no, might as well double check this anyway + assert(amt_of(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 + end + end +end + + +return {take_stock=take_stock,amt_of=amt_of,give=give} -- cgit v1.2.3