From dc555a0b1e8d1c0b3f60a8efe995f44c6f875462 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Fri, 14 Apr 2017 23:41:04 -0400 Subject: [PATCH] bugfix builder - refined storage performance --- apps/builder.lua | 91 +++++++++++++++++++++++++----------- apps/mirror.lua | 3 +- apps/refinedManager.lua | 39 ++++++++++++---- sys/apis/chestProvider18.lua | 61 ++++++++++-------------- sys/apis/ui.lua | 2 +- 5 files changed, 120 insertions(+), 76 deletions(-) diff --git a/apps/builder.lua b/apps/builder.lua index 793b9ef..4e3c327 100644 --- a/apps/builder.lua +++ b/apps/builder.lua @@ -96,12 +96,14 @@ function subDB:seedDB() dmg = 0, id = "minecraft:potatoes", }, + --[[ [ "minecraft:dirt:1" ] = { sdmg = 0, sid = "minecraft:dirt", dmg = 1, id = "minecraft:dirt", }, + ]]-- [ "minecraft:unlit_redstone_torch:0" ] = { sdmg = 0, sid = "minecraft:redstone", @@ -174,6 +176,12 @@ function subDB:seedDB() dmg = 4, id = "minecraft:double_wooden_slab", }, + [ "minecraft:double_wooden_slab:5" ] = { + sdmg = 5, + sid = "minecraft:planks", + dmg = 5, + id = "minecraft:double_wooden_slab", + }, [ "minecraft:lit_redstone_lamp:0" ] = { sdmg = 0, sid = "minecraft:redstone_lamp", @@ -186,12 +194,24 @@ function subDB:seedDB() dmg = 1, id = "minecraft:double_stone_slab", }, + [ "minecraft:double_stone_slab:2" ] = { + sdmg = 0, + sid = "minecraft:planks", + dmg = 2, + id = "minecraft:double_stone_slab", + }, [ "minecraft:double_stone_slab:3" ] = { sdmg = 0, sid = "minecraft:cobblestone", dmg = 3, id = "minecraft:double_stone_slab", }, + [ "minecraft:double_stone_slab:4" ] = { + sdmg = 0, + sid = "minecraft:brick_block", + dmg = 4, + id = "minecraft:double_stone_slab", + }, [ "minecraft:double_stone_slab:5" ] = { sdmg = 0, sid = "minecraft:stonebrick", @@ -826,11 +846,11 @@ function Builder:resupply() turtle.setHeading(0) self:autocraft(supplies) Logger.log('builder', 'Waiting for supplies') - supplyPage.grid:setValues(supplies) + supplyPage:setSupplies(supplies) UI:setPage('supply') end end - + function Builder:placeDown(slot) return turtle.placeDown(slot.index) end @@ -1346,8 +1366,10 @@ function Builder:build() print('failed to place block') end end + self:saveProgress(self.index+1) + elseif (i % 50) == 0 then -- is Air + os.sleep(0) -- sleep in case there are a large # of skipped blocks end - self:saveProgress(self.index+1) if turtle.abort then turtle.status = 'aborting' @@ -1445,8 +1467,7 @@ substitutionPage = UI.Page({ { text = 'Air', event = 'air', help = 'Air' }, }, }), - inName = UI.Text({ y = 4, width = UI.term.width }), - outName = UI.Text({ y = 5, width = UI.term.width }), + info = UI.Window({ y = 4, width = UI.term.width, height = 3 }), grid = UI.ScrollingGrid({ columns = { { heading = 'Name', key = 'name', width = UI.term.width-9 }, @@ -1471,19 +1492,20 @@ substitutionPage.menuBar:add({ }) }) -function substitutionPage:draw() +function substitutionPage.info:draw() - local inName = blocks.blockDB:getName(self.sub.id, self.sub.dmg) - self.inName.value = ' Replace ' .. inName - - self.outName.value = '' - if self.sub.sid then - local outName = blocks.blockDB:getName(self.sub.sid, self.sub.sdmg) - self.outName.value = ' With ' .. outName + local sub = self.parent.sub + local inName = blocks.blockDB:getName(sub.id, sub.dmg) + local outName = '' + if sub.sid then + outName = blocks.blockDB:getName(sub.sid, sub.sdmg) end - --self.grid:adjustWidth() - UI.Page.draw(self) + self:clear() + self:setCursorPos(1, 1) + self:print(' Replace ' .. inName .. '\n') + self:print(' ' .. sub.id .. ':' .. sub.dmg .. '\n', nil, colors.yellow) + self:print(' With ' .. outName) end function substitutionPage:enable() @@ -1529,7 +1551,7 @@ function substitutionPage:eventHandler(event) end self:applySubstitute(event.selected.id, event.selected.dmg) - self:draw() + self.info:draw() elseif event.type == 'text_change' then local text = event.text @@ -1594,11 +1616,10 @@ supplyPage = UI.Page({ }), grid = UI.Grid({ columns = { - { heading = 'Slot', key = 'index', width = 4 }, - { heading = 'Name', key = 'name', width = UI.term.width-12 }, - { heading = 'Need', key = 'need', width = 4 }, + { heading = 'Name', key = 'name', width = UI.term.width - 7 }, + { heading = 'Need', key = 'need', width = 4 }, }, - sortColumn = 'index', + sortColumn = 'name', y = 3, width = UI.term.width, height = UI.term.height - 3 @@ -1672,14 +1693,30 @@ end function supplyPage:disable() Event.cancelNamedTimer('supplyRefresh') end - + +function supplyPage:setSupplies(supplies) + local t = { } + for _,s in pairs(supplies) do + local key = s.id .. ':' .. s.dmg + local entry = t[key] + if not entry then + entry = Util.shallowCopy(s) + t[key] = entry + else + entry.need = entry.need + s.need + end + end + + self.grid:setValues(t) +end + function supplyPage:refresh() self.statusBar:timedStatus('Refreshed ', 3) - local t = Builder:getSupplies() - if #t == 0 then + local supplies = Builder:getSupplies() + if #supplies == 0 then Builder:build() else - self.grid:setValues(t) + self:setSupplies(supplies) self.grid:draw() end end @@ -1799,13 +1836,13 @@ function listingPage:refresh() local block = blocks.blockDB:lookup(b.id, b.dmg) if not block then blocks.blockDB:add(b.id, b.dmg, item.name, b.id) - elseif not blocks.name and item.name then - blocks.blockDB:add(b.id, b.dmg, item.name) + elseif not block.name and item.name then + blocks.blockDB:add(b.id, b.dmg, item.name, b.id) end b.name = item.name b.qty = item.qty b.is_craftable = item.is_craftable - elseif not b.name then + else b.name = blocks.blockDB:getName(b.id, b.dmg) end end diff --git a/apps/mirror.lua b/apps/mirror.lua index c763d57..7fcb295 100644 --- a/apps/mirror.lua +++ b/apps/mirror.lua @@ -1,6 +1,5 @@ require = requireInjector(getfenv(1)) local Terminal = require('terminal') -local process = require('process') local args = { ... } local mon = device[table.remove(args, 1) or 'monitor'] @@ -15,7 +14,7 @@ mon.setCursorPos(1, 1) local oterm = Terminal.copy(term.current()) Terminal.mirror(term.current(), mon) -term.current().getSize = function() return mon.getSize() end +term.current().getSize = mon.getSize if #args > 0 then shell.run(unpack(args)) diff --git a/apps/refinedManager.lua b/apps/refinedManager.lua index 713a8f6..b4d53d4 100644 --- a/apps/refinedManager.lua +++ b/apps/refinedManager.lua @@ -11,6 +11,9 @@ end multishell.setTitle(multishell.getCurrent(), 'Storage Manager') +-- refined storage is slooow +local cache = { } + -- Strip off color prefix local function safeString(text) @@ -29,6 +32,25 @@ local function safeString(text) return text end +function getItemDetails(item) + local key = table.concat({ item.name, item.damage, item.nbtHash }, ':') + + local detail = cache[key] + if not detail then + detail = controller.findItem(item) + if detail then + Util.merge(detail, detail.getMetadata()) + detail.displayName = safeString(detail.displayName) + if detail.maxDamage and detail.maxDamage > 0 and detail.damage > 0 then + detail.displayName = detail.displayName .. ' (damaged)' + end + detail.lname = detail.displayName:lower() + cache[key] = detail + end + end + return detail +end + function listItems() local items = { } local list @@ -38,17 +60,10 @@ function listItems() end) if list then - for k,v in pairs(list) do - local item = controller.findItem(v) - + for _,v in pairs(list) do + local item = getItemDetails(v) if item then - Util.merge(item, item.getMetadata()) - item.displayName = safeString(item.displayName) - if item.maxDamage and item.maxDamage > 0 and item.damage > 0 then - item.displayName = item.displayName .. ' (damaged)' - end - item.lname = item.displayName:lower() - + item.count = v.count table.insert(items, item) end end @@ -463,7 +478,11 @@ function listingPage:enable() end function listingPage:refresh() +local t = os.clock() + self.allItems = listItems() +debug('list items') +debug(os.clock() - t) mergeResources(self.allItems) self:applyFilter() end diff --git a/sys/apis/chestProvider18.lua b/sys/apis/chestProvider18.lua index 631e74a..b0664bd 100644 --- a/sys/apis/chestProvider18.lua +++ b/sys/apis/chestProvider18.lua @@ -7,7 +7,8 @@ function ChestProvider:init(args) args = args or { } - self.stacks = {} + self.items = { } -- consolidated item info + self.stacks = { } -- raw stack info self.name = 'chest' self.direction = args.direction or 'up' self.wrapSide = args.wrapSide or 'bottom' @@ -21,48 +22,36 @@ end function ChestProvider:refresh() if self.p then --self.p.condenseItems() + self.items = { } self.stacks = self.p.list() - local t = { } - for _,s in pairs(self.stacks) do - s.id = s.name - s.dmg = s.damage - s.qty = s.count - local key = s.id .. ':' .. s.dmg - if t[key] and t[key].qty < 64 then - t[key].max_size = t[key].qty - else - t[key] = { - qty = s.qty + for k,s in pairs(self.stacks) do + + local key = s.name .. ':' .. s.damage + local entry = self.items[key] + if not entry then + local meta = self.p.getItemMeta(k) + entry = { + id = s.name, + dmg = s.damage, + name = meta.displayName, + max_size = meta.maxCount, + qty = 0, } + self.items[key] = entry end - end - for _,s in ipairs(self.stacks) do - local key = s.id .. ':' .. s.dmg - if t[key].max_size then - s.max_size = t[key].qty - else - s.max_size = 64 - end + entry.qty = entry.qty + s.count end end - return self.stacks + return self.items end - + function ChestProvider:getItemInfo(id, dmg) - local item = { id = id, dmg = dmg, qty = 0, max_size = 64 } - for k,stack in pairs(self.stacks) do - if stack.id == id and stack.dmg == dmg then - local meta = self.p.getItemMeta(k) - if meta then - item.name = meta.displayName - item.qty = item.qty + meta.count - item.max_size = meta.maxCount - end + + for key,item in pairs(self.items) do + if item.id == id and item.dmg == dmg then + return item end end - if item.name then - return item - end end function ChestProvider:craft(id, dmg, qty) @@ -76,8 +65,8 @@ function ChestProvider:provide(item, qty, slot) if self.p then self:refresh() for key,stack in pairs(self.stacks) do - if stack.id == item.id and stack.dmg == item.dmg then - local amount = math.min(qty, stack.qty) + if stack.name == item.id and stack.damage == item.dmg then + local amount = math.min(qty, stack.count) self.p.pushItems(self.direction, key, amount, slot) qty = qty - amount if qty <= 0 then diff --git a/sys/apis/ui.lua b/sys/apis/ui.lua index 8b5ab9f..9351063 100644 --- a/sys/apis/ui.lua +++ b/sys/apis/ui.lua @@ -1481,7 +1481,7 @@ function UI.Grid:adjustWidth() end for _,col in pairs(self.columns) do - for key,row in pairs(self.values) do + for key,row in ipairs(self.values) do row = self:getDisplayValues(row, key) local value = row[col.key] if value then