From f56611d957a7356105ed8159ad8f24c683ad358c Mon Sep 17 00:00:00 2001 From: kepler155c Date: Sun, 21 Oct 2018 04:44:10 -0400 Subject: [PATCH] plethora autocrafting wip --- apps/simpleMiner.lua | 11 +- inventoryManager/{apps => }/Crafter.lua | 0 inventoryManager/apis/networkedAdapter18.lua | 28 ++- .../{apps => }/inventoryManager.lua | 217 +++++++++++++++--- 4 files changed, 217 insertions(+), 39 deletions(-) rename inventoryManager/{apps => }/Crafter.lua (100%) rename inventoryManager/{apps => }/inventoryManager.lua (87%) diff --git a/apps/simpleMiner.lua b/apps/simpleMiner.lua index 8c726b2..c4c8d6e 100644 --- a/apps/simpleMiner.lua +++ b/apps/simpleMiner.lua @@ -219,7 +219,16 @@ local function safeGoto(x, z, y, h) local oldStatus = turtle.getStatus() -- only pathfind above or around other turtles (never down) - Pathing.setBox({ x = turtle.point.x, y = turtle.point.y, z = turtle.point.z, ex = x, ey = y, ez = z }) + local box = Point.normalizeBox({ x = turtle.point.x, y = turtle.point.y, z = turtle.point.z, + ex = x, ey = y, ez = z }) + box.x = box.x - 1 + box.z = box.z - 1 + box.ex = box.ex + 1 + box.ey = box.ey + 1 + box.ez = box.ez + 1 + + Pathing.setBox(box) + while not turtle.pathfind({ x = x, z = z, y = y or turtle.point.y, heading = h }) do --status('stuck') if turtle.isAborted() then diff --git a/inventoryManager/apps/Crafter.lua b/inventoryManager/Crafter.lua similarity index 100% rename from inventoryManager/apps/Crafter.lua rename to inventoryManager/Crafter.lua diff --git a/inventoryManager/apis/networkedAdapter18.lua b/inventoryManager/apis/networkedAdapter18.lua index 9ea368c..99e36aa 100644 --- a/inventoryManager/apis/networkedAdapter18.lua +++ b/inventoryManager/apis/networkedAdapter18.lua @@ -19,15 +19,11 @@ function NetworkedAdapter:init(args) if self.modem and self.modem.getNameLocal then self.localName = self.modem.getNameLocal() - for _, v in pairs(self.modem.getNamesRemote()) do - local remote = Peripheral.get({ name = v }) - if remote and - remote.size and - remote.size() >= 27 and - remote.list and - not (self.remoteDefaults[v] and self.remoteDefaults[v].ignore) then - local adapter = InventoryAdapter.wrap({ side = v, direction = self.localName }) + for k in pairs(self.remoteDefaults) do + local remote = Peripheral.get({ name = k }) + if remote and remote.size and remote.list then + local adapter = InventoryAdapter.wrap({ side = k, direction = self.localName }) if adapter then table.insert(self.remotes, adapter) end @@ -157,6 +153,8 @@ function NetworkedAdapter:insert(slot, qty, toSlot, item) self:listItems() end +debug('attempting to insert ' .. item.name) + local function insert(remote) debug('slot %d -> %s: %s', slot, remote.side, qty) local amount = remote:insert(slot, qty, toSlot) @@ -164,13 +162,21 @@ debug('slot %d -> %s: %s', slot, remote.side, qty) total = total + amount end + -- found a chest locked with this item + for _, remote in pairs(self.remotes) do + if remote.lockWith == key or remote.lockWith == item.name then + insert(remote) + return total + end + end + if self.cache[key] then -- is this item in some chest -- low to high priority if the chest already contains that item for _, remote in Util.rpairs(self.remotes) do if qty <= 0 then break end - if remote.cache and remote.cache[key] then + if remote.cache and remote.cache[key] and not remote.lockWith then insert(remote) end end @@ -181,7 +187,9 @@ debug('slot %d -> %s: %s', slot, remote.side, qty) if qty <= 0 then break end - insert(remote) + if not remote.lockWith then + insert(remote) + end end return total diff --git a/inventoryManager/apps/inventoryManager.lua b/inventoryManager/inventoryManager.lua similarity index 87% rename from inventoryManager/apps/inventoryManager.lua rename to inventoryManager/inventoryManager.lua index 583d3d8..9a5fa5c 100644 --- a/inventoryManager/apps/inventoryManager.lua +++ b/inventoryManager/inventoryManager.lua @@ -90,41 +90,53 @@ if multishell then end local config = { - computerFacing = 'north', -- direction turtle is facing - - inventory = 'network', -- main inventory - - trashDirection = 'up', -- trash/chest in relation to inventory - monitor = 'type/monitor', + monitor = 'type/monitor', + remoteDefaults = { }, } +Config.load('inventoryManager', config) -Config.loadWithCheck('inventoryManager', config) +local modem = Peripheral.get('wired_modem') +if not modem or not modem.getNameLocal then + error('Wired modem is not connected') +end -local inventoryAdapter = config.inventory == 'network' and - InventoryAdapter.wrap({ remoteDefaults = config.remoteDefaults }) or - InventoryAdapter.wrap({ side = config.inventory, facing = config.computerFacing }) +local feederAdapter +local controllerAdapter +local storage = { } +for k,v in pairs(config.remoteDefaults) do + if v.mtype == 'storage' then + storage[k] = v + elseif v.mtype == 'input' then + feederAdapter = InventoryAdapter.wrap({ side = k, direction = modem.getNameLocal() }) + elseif v.mtype == 'controller' then + -- TODO: look for controller + end +end +debug(storage) + +local inventoryAdapter = InventoryAdapter.wrap({ remoteDefaults = storage }) if not inventoryAdapter then error('Invalid inventory configuration') end -local introspectionModule = device['plethora:introspection'] +local introspectionModule = device['plethora:introspection'] or + error('introspection module not found') -local controllerAdapter -if inventoryAdapter.craft then - controllerAdapter = inventoryAdapter -end - -local feederAdapter - -if config.remoteDefaults then - for k, v in pairs(config.remoteDefaults) do - if v.feeder then - local modem = Peripheral.get('wired_modem') - if modem and modem.getNameLocal then - feederAdapter = InventoryAdapter.wrap({ side = k, direction = modem.getNameLocal() }) - end - break +-- TODO: cleanup +for _, v in pairs(modem.getNamesRemote()) do + local remote = Peripheral.get({ name = v }) + if remote.pullItems then + if not config.remoteDefaults[v] then + config.remoteDefaults[v] = { + name = v, + mtype = 'ignore', + } + else + config.remoteDefaults[v].name = v + end + if not config.remoteDefaults[v].mtype then + config.remoteDefaults[v].mtype = 'ignore' end end end @@ -473,6 +485,13 @@ local function craftItems(craftList, allItems) end end +local function queue(fn) + while craftingPaused do + os.sleep(1) + end + fn() +end + local function eject(item, qty) if _G.turtle then local s, m = pcall(function() @@ -674,6 +693,143 @@ local function saveResources() Util.writeTable(RESOURCE_FILE, t) end +local machinesPage = UI.Page { + titleBar = UI.TitleBar { + previousPage = true, + title = 'Machines', + }, + grid = UI.ScrollingGrid { + y = 2, ey = -2, + values = config.remoteDefaults, + columns = { + { heading = 'Name', key = 'displayName' }, + { heading = 'Priority', key = 'priority', width = 5 }, + { heading = 'Type', key = 'mtype', width = 5 }, + }, + sortColumn = 'name', + }, + detail = UI.SlideOut { + backgroundColor = colors.cyan, + form = UI.Form { + x = 1, y = 2, ex = -1, ey = -2, + [7] = UI.Text { + x = 12, y = 1, + width = 20, + value = 'test', + }, + [1] = UI.TextEntry { + formLabel = 'Name', formKey = 'displayName', help = '...', + limit = 64, + }, + [2] = UI.Chooser { + width = 15, + formLabel = 'Type', formKey = 'mtype', + nochoice = 'Storage', + choices = { + { name = 'Storage', value = 'storage' }, + { name = 'Trashcan', value = 'trashcan' }, + { name = 'Input chest', value = 'input' }, + { name = 'Ignore', value = 'ignore' }, + }, + help = 'Check if machine is empty before crafting' + }, + [3] = UI.Chooser { + width = 7, + formLabel = 'Empty', formKey = 'empty', + nochoice = 'No', + choices = { + { name = 'Yes', value = true }, + { name = 'No', value = false }, + }, + help = 'Check if machine is empty before crafting' + }, + [4] = UI.TextEntry { + formLabel = 'Priority', formKey = 'priority', help = '...', + limit = 4, + }, + [5] = UI.TextEntry { + formLabel = 'Max Craft', formKey = 'maxCount', help = '...', + limit = 4, + }, + [6] = UI.TextEntry { + formLabel = 'Lock to', formKey = 'lockWith', help = '...', + limit = 64, + }, + }, + statusBar = UI.StatusBar(), + }, + statusBar = UI.StatusBar { + values = 'Select Machine', + }, + accelerators = { + h = 'toggle_hidden', + } +} + +function machinesPage:enable() + self.grid:update() + UI.Page.enable(self) +end + +function machinesPage.detail:eventHandler(event) + if event.type == 'focus_change' then + self.statusBar:setStatus(event.focused.help) + end + return UI.SlideOut.eventHandler(self, event) +end + +function machinesPage.grid:getDisplayValues(row) + row = Util.shallowCopy(row) + row.displayName = row.displayName or row.name + return row +end + +function machinesPage.grid:getRowTextColor(row, selected) + if row.mtype == 'ignore' then + return colors.lightGray + end + return UI.Grid:getRowTextColor(row, selected) +end + +function machinesPage:eventHandler(event) + if event.type == 'grid_select' then + self.detail.form:setValues(event.selected) + self.detail.form[7].value = event.selected.name +debug(event.selected) + self.detail:show() + + elseif event.type == 'toggle_hidden' then + local selected = self.grid:getSelected() + if selected then + selected.ignore = not selected.ignore +-- Util.writeTable(MACHINES_FILE, machines) + self:draw() + end + + elseif event.type == 'form_complete' then + self.detail.form.values.empty = self.detail.form.values.empty == true or nil + self.detail.form.values.ignore = self.detail.form.values.ignore == true or nil + self.detail.form.values.priority = tonumber(self.detail.form.values.priority) + self.detail.form.values.maxCount = tonumber(self.detail.form.values.maxCount) + if #self.detail.form.values.displayName == 0 then + self.detail.form.values.displayName = nil + end + if #self.detail.form.values.lockWith == 0 then + self.detail.form.values.lockWith = nil + end + Config.update('inventoryManager', config) + self.detail:hide() + self.grid:update() + + elseif event.type == 'form_cancel' then + self.detail:hide() + + else + UI.Page.eventHandler(self, event) + end + return true +end + local itemPage = UI.Page { titleBar = UI.TitleBar { title = 'Limit Resource', @@ -951,6 +1107,7 @@ local listingPage = UI.Page { grid_select_right = 'craft', [ 'control-e' ] = 'eject', [ 'control-s' ] = 'eject_stack', + [ 'control-m' ] = 'machines', }, displayMode = 0, } @@ -988,15 +1145,18 @@ function listingPage:eventHandler(event) elseif event.type == 'eject' then local item = self.grid:getSelected() if item then - eject(item, 1) + queue(function() eject(item, 1) end) end elseif event.type == 'eject_stack' then local item = self.grid:getSelected() if item then - eject(item, itemDB:getMaxCount(item)) + queue(function() eject(item, itemDB:getMaxCount(item)) end) end + elseif event.type == 'machines' then + UI:setPage('machines') + elseif event.type == 'grid_select' then local selected = event.selected UI:setPage('item', selected) @@ -1378,6 +1538,7 @@ UI:setPages({ item = itemPage, learn = learnPage, craft = craftPage, + machines = machinesPage, }) jobMonitor()