From e45aad7ed0b70f81e1a5e911e8d206007799c0b9 Mon Sep 17 00:00:00 2001 From: kepler155c Date: Wed, 24 Oct 2018 06:51:05 -0400 Subject: [PATCH] milo wip --- apps/farm.lua | 2 +- apps/mirror.lua | 47 +++++++++++++++---- milo/Milo.lua | 76 ++++++++++++++++--------------- milo/apis/milo.lua | 15 +++--- milo/apis/networkedAdapter18.lua | 2 +- milo/core/machines.lua | 33 ++++++++++++-- milo/plugins/listing.lua | 5 +- milo/plugins/potionImportTask.lua | 2 +- 8 files changed, 122 insertions(+), 60 deletions(-) diff --git a/apps/farm.lua b/apps/farm.lua index 2a1988d..fcbcfdc 100644 --- a/apps/farm.lua +++ b/apps/farm.lua @@ -22,7 +22,7 @@ local crops = Util.readTable(CONFIG_FILE) or { ['minecraft:potatoes'] = { seed = 'minecraft:potato', mature = 7, action = 'plant' }, ['minecraft:beetroots'] = - { seed = 'minecraft:beetroot_seeds', mature = 3, 'plant' }, + { seed = 'minecraft:beetroot_seeds', mature = 3, action = 'plant' }, ['minecraft:reeds'] = { action = 'bash' }, ['minecraft:melon_block'] = { action = 'smash' }, ['minecraft:pumpkin'] = { action = 'smash' }, diff --git a/apps/mirror.lua b/apps/mirror.lua index f2817d4..09ea2ae 100644 --- a/apps/mirror.lua +++ b/apps/mirror.lua @@ -1,28 +1,55 @@ _G.requireInjector() local Terminal = require('terminal') +local Util = require('util') local shell = _ENV.shell local term = _G.term +local options = { + scale = { arg = 's', type = 'flag', value = false, + desc = 'Set monitor to .5 text scaling' }, + resize = { arg = 'r', type = 'flag', value = false, + desc = 'Resize terminal to monitor size' }, + execute = { arg = 'e', type = 'string', + desc = 'Execute a program' }, + monitor = { arg = 'm', type = 'string', value = 'monitor', + desc = 'Name of monitor' }, + help = { arg = 'h', type = 'flag', value = false, + desc = 'Displays the options' }, +} + local args = { ... } -local mon = _G.device[table.remove(args, 1) or 'monitor'] +if not Util.getOptions(options, args) then + return +end + +local mon = _G.device[options.monitor.value] if not mon then - error('mirror: Invalid device') + error('mirror: Invalid device') end mon.clear() -mon.setTextScale(.5) + +if options.scale.value then + mon.setTextScale(.5) +end mon.setCursorPos(1, 1) local oterm = Terminal.copy(term.current()) Terminal.mirror(term.current(), mon) -term.current().getSize = mon.getSize - -if #args > 0 then - shell.run(unpack(args)) - Terminal.copy(oterm, term.current()) - - mon.setCursorBlink(false) +if options.resize.value then + term.current().getSize = mon.getSize +end + +debug(args) +debug(options) + +if options.execute.value then + -- TODO: allow args to be passed + shell.run(options.execute.value) -- unpack(args)) + Terminal.copy(oterm, term.current()) + + mon.setCursorBlink(false) end diff --git a/milo/Milo.lua b/milo/Milo.lua index 4ed142d..5abe8a8 100644 --- a/milo/Milo.lua +++ b/milo/Milo.lua @@ -73,6 +73,7 @@ local Util = require('util') local InventoryAdapter = require('inventoryAdapter') +local device = _G.device local fs = _G.fs local multishell = _ENV.multishell local shell = _ENV.shell @@ -92,38 +93,6 @@ if not modem or not modem.getNameLocal then error('Wired modem is not connected') end -local storage = { } -for k,v in pairs(config.remoteDefaults) do - if v.mtype == 'storage' then - storage[k] = v - elseif v.mtype == 'controller' then - -- TODO: look for controller - end -end - -local inventoryAdapter = InventoryAdapter.wrap({ remoteDefaults = storage }) -if not inventoryAdapter then - error('Invalid inventory configuration') -end - --- 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 - local function loadResources() local resources = Util.readTable(Milo.RESOURCE_FILE) or { } for k,v in pairs(resources) do @@ -135,13 +104,50 @@ end local context = { config = config, - inventoryAdapter = inventoryAdapter, resources = loadResources(), userRecipes = Util.readTable(Milo.RECIPES_FILE) or { }, learnTypes = { }, machineTypes = { }, } +local function initStorage() + debug('Initializing storage') + local storage = { } + for k,v in pairs(config.remoteDefaults) do + if v.mtype == 'storage' and device[v.name] then + storage[k] = v + end + end +debug(storage) + context.inventoryAdapter = InventoryAdapter.wrap({ remoteDefaults = storage }) + + if not context.inventoryAdapter then + error('Invalid inventory configuration') + end +end + +Event.on({ 'device_attach' }, function(_, dev) + debug('attach: ' .. dev) + if config.remoteDefaults[dev] and + config.remoteDefaults[dev].mtype == 'storage' then + initStorage() + end +end) + +Event.on({ 'device_detach' }, function(_, dev) + debug('detach: ' .. dev) + if config.remoteDefaults[dev] and + config.remoteDefaults[dev].mtype == 'storage' then + +Milo:pauseCrafting() +debug('Crafting paused') +Milo:showError('Check log') + + initStorage() + end +end) + +initStorage() Milo:init(context) local function loadDirectory(dir) @@ -165,9 +171,7 @@ Milo:clearGrid() local page = UI:getPage('listing') UI:setPage(page) -page:setFocus(page.statusBar.filter) - --- TODO: Event.on('device_detach', function() end) +page:setFocus(page.statusBar.filter) -- todo: move this line into listing code Event.onInterval(5, function() if not Milo:isCraftingPaused() then diff --git a/milo/apis/milo.lua b/milo/apis/milo.lua index e453a9b..01957bc 100644 --- a/milo/apis/milo.lua +++ b/milo/apis/milo.lua @@ -29,6 +29,7 @@ end function Milo:pauseCrafting() self.craftingPaused = true + Milo:showError('Crafting Paused') end function Milo:resumeCrafting() @@ -80,12 +81,12 @@ function Milo:registerTask(task) end function Milo:showError(msg) - term.clear() - self.context.jobList:showError() - print(msg) - print('rebooting in 5 secs') - os.sleep(5) - os.reboot() + --term.clear() + self.context.jobList:showError(msg) + --print(msg) + --print('rebooting in 5 secs') + --os.sleep(5) + --os.reboot() end function Milo:getItem(items, inItem, ignoreDamage, ignoreNbtHash) @@ -381,7 +382,7 @@ function Milo:craftItems(craftList) end self:updateCraftingStatus(craftList) for _,v in pairs(craftList) do - debug(v) + --debug(v) end end diff --git a/milo/apis/networkedAdapter18.lua b/milo/apis/networkedAdapter18.lua index 1313c15..4324a00 100644 --- a/milo/apis/networkedAdapter18.lua +++ b/milo/apis/networkedAdapter18.lua @@ -64,7 +64,7 @@ function NetworkedAdapter:listItems(throttle) return self.items end self.listCount = self.listCount + 1 -debug(self.listCount) +--debug(self.listCount) -- todo: only listItems from dirty remotes -- todo: better handling of empty inventories diff --git a/milo/core/machines.lua b/milo/core/machines.lua index ada5f32..64afc55 100644 --- a/milo/core/machines.lua +++ b/milo/core/machines.lua @@ -1,4 +1,5 @@ local Config = require('config') +local Event = require('event') local itemDB = require('itemDB') local Milo = require('milo') local UI = require('ui') @@ -29,9 +30,35 @@ local machinesPage = UI.Page { }, } +function machinesPage:getList() + -- TODO: remove dedupe naming in perf code ? + for _, v in pairs(device) do + if v.pullItems then + if not context.config.remoteDefaults[v.name] then + context.config.remoteDefaults[v.name] = { + name = v.name, + mtype = 'ignore', + } + end + end + end +end + function machinesPage:enable() + self:getList() self.grid:update() UI.Page.enable(self) + self.handler = Event.on({ 'device_attach', 'device_detach'}, function() + self:getList() + self.grid:update() + self.grid:draw() + self.grid:sync() + end) +end + +function machinesPage:disable() + UI.Page.disable(self) + Event.off(self.handler) end function machinesPage.grid:getDisplayValues(row) @@ -41,6 +68,9 @@ function machinesPage.grid:getDisplayValues(row) end function machinesPage.grid:getRowTextColor(row, selected) + if not device[row.name] then + return colors.red + end if row.mtype == 'ignore' then return colors.lightGray end @@ -205,9 +235,6 @@ function machineWizard:eventHandler(event) UI:setPreviousPage() - elseif event.type == 'collapse' then - self.items:hide() - elseif event.type == 'enable_view' then local current = event.next or event.prev self.titleBar.title = current.title or 'Machine' diff --git a/milo/plugins/listing.lua b/milo/plugins/listing.lua index 421f42d..50688c0 100644 --- a/milo/plugins/listing.lua +++ b/milo/plugins/listing.lua @@ -82,6 +82,7 @@ local listingPage = UI.Page { [ 'control-e' ] = 'eject', [ 'control-s' ] = 'eject_stack', [ 'control-m' ] = 'machines', + [ 'control-l' ] = 'resume', }, displayMode = 0, } @@ -113,10 +114,12 @@ function listingPage.grid:getDisplayValues(row) end function listingPage:eventHandler(event) -debug(event) if event.type == 'quit' then UI:exitPullEvents() + elseif event.type == 'resume' then + Milo:resumeCrafting() + elseif event.type == 'eject' then local item = self.grid:getSelected() if item then diff --git a/milo/plugins/potionImportTask.lua b/milo/plugins/potionImportTask.lua index 68edbfb..eb0db7b 100644 --- a/milo/plugins/potionImportTask.lua +++ b/milo/plugins/potionImportTask.lua @@ -10,7 +10,7 @@ function PotionImportTask:cycle(context) for _, v in pairs(device) do if v.type == 'minecraft:brewing_stand' and v.getBrewTime() == 0 then local list = v.list() - if not list[4] and list[1] then + if not list[4] then for i = 1, 3 do if list[i] then context.inventoryAdapter:insert(i, 1, nil, list[i], v.name)