From 984d426db7a407fb3bf6d1b0dcdae50b8f5859a1 Mon Sep 17 00:00:00 2001 From: Olav Bakke Svendsen Date: Fri, 24 May 2024 21:57:29 +0200 Subject: [PATCH] Moving Libraries --- common/dl.lua | 5 +- lib/inventory.lua | 97 ++++++++++++++++++++++++++++ lib/{turtle-utils.lua => turtle.lua} | 0 vineyard/turtle/package | 3 +- 4 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 lib/inventory.lua rename lib/{turtle-utils.lua => turtle.lua} (100%) diff --git a/common/dl.lua b/common/dl.lua index 9400298..8ada45b 100644 --- a/common/dl.lua +++ b/common/dl.lua @@ -30,10 +30,11 @@ for _, file in pairs(files) do if fs.exists(file.to) and file.overwrite then local tmp = textutils.serialize(math.random(100000,999999)) local ok = shell.run("wget "..url.." "..tmp) - if ok then + if ok then -- not working? print("Overwriting "..file.to) fs.delete(file.to) - fs.move(tmp,file.to) +-- fs.move(tmp,file.to) -- fails with no such file + shell.run("mv "..tmp.." "..file.to) -- workaround end else shell.run("wget "..url.." "..file.to) diff --git a/lib/inventory.lua b/lib/inventory.lua new file mode 100644 index 0000000..c047b76 --- /dev/null +++ b/lib/inventory.lua @@ -0,0 +1,97 @@ +local t = {} + +-- index: find items in inventories +-- inventory_address: string +-- items: list of strings or nil/false. if nil/false, indexes all items. +-- returns a table that maps item names to { amount = 0, slots = {} } where +-- amount is the total amount of the item in the inventory and slots is a +-- table where the keys are slot numbers and the values are how many items +-- are in the slot. +t.index = function(inventory_address, items) + local inventory = peripheral.wrap(inventory_address) + local slots = inventory.list() + local index = {} + + local should_index = {} + if items then + for _, name in pairs(items) do + should_index[name] = true + end + end + + for slot, item in pairs(slots) do + if not items or (item.name and should_index[item.name]) then + index[item.name] = index[item.name] or { amount = 0, slots = {} } + index[item.name].amount = index[item.name].amount + item.count + index[item.name].slots[slot] = item.count + end + end + return index +end + +-- find: similar to index, but stops indexing once the required amount of each +-- item is found. +-- inventory_address: string +-- item_amounts: a table that maps item names to amounts +-- returns bool, table where table maps item names to { amount = 0, slots = {} } +-- much like the index function, but where the amounts are less than or equal +-- to the amounts provided in item_amounts and slots is a table where the keys +-- are slot numbers end values are how many items "was found" in the slot. bool +-- is whether all the items in item_amounts were found or not. +t.find = function(inventory_address, item_amounts) + local inventory = peripheral.wrap(inventory_address) + local slots = inventory.list() + + local missing = {} + for name, amount in pairs(item_amounts) do + missing[name].amount = amount + end + + local index = {} + local found_all = false + for slot, item in pairs(slots) do + if missing == {} then + found_all = true + break + end + if item.name and missing[item.name] then + index[item.name] = index[item.name] or { amount = 0, slots = {} } + if item.count < missing[item.name] then + index[item.name].amount = index[item.name].amount + item.count + index[item.name].slots[slot] = item.count + missing[item.name] = missing[item.name] - item.count + else + index[item.name].amount = index[item.name].amount + missing[item.name] + index[item.name].slots[slot] = missing[item.name] + missing[item.name] = nil + end + end + end + return found_all, index +end + +-- move: tries to move items from one inventory to another +-- source_address: string +-- destination_address: string +-- item_amounts: a table that maps item names to amounts +-- returns true on success and false, string on fail +t.move = function(source_address, destination_address, item_amounts) + local source_inventory = peripheral.wrap(source_address) + local ok, index = t.find(source_address, item_amounts) + local err = false + if not ok then + err = "not enough items" + else + for item_name, slots in pairs(index) do + for slot, amount in pairs(slots) do + source_inventory.pushItems(destination_address, slot, amount) + -- todo: pushItems may fail + end + end + end + return not err or table.unpack({ false, "Could not move: "..err }) +end + + + +return t diff --git a/lib/turtle-utils.lua b/lib/turtle.lua similarity index 100% rename from lib/turtle-utils.lua rename to lib/turtle.lua diff --git a/vineyard/turtle/package b/vineyard/turtle/package index 5b91eee..a1ade87 100644 --- a/vineyard/turtle/package +++ b/vineyard/turtle/package @@ -1,2 +1,3 @@ -cc lib/turtle-utils.lua:/lib/turtle-utils.lua:o +cc lib/turtle.lua:/lib/turtle.lua:o +cc lib/inventory.lua:/lib/inventory.lua:o cc vineyard/turtle/harvest.lua:/bin/harvest.lua:o -- 2.30.2