upgrade item keys
This commit is contained in:
@@ -3,7 +3,7 @@ local Util = require('util')
|
||||
local itemDB = require('itemDB')
|
||||
local Peripheral = require('peripheral')
|
||||
|
||||
local ChestProvider = class()
|
||||
local ChestAdapter = class()
|
||||
|
||||
local keys = Util.transpose({
|
||||
'damage',
|
||||
@@ -14,7 +14,7 @@ local keys = Util.transpose({
|
||||
'nbtHash',
|
||||
})
|
||||
|
||||
function ChestProvider:init(args)
|
||||
function ChestAdapter:init(args)
|
||||
local defaults = {
|
||||
items = { },
|
||||
name = 'chest',
|
||||
@@ -33,11 +33,11 @@ function ChestProvider:init(args)
|
||||
end
|
||||
end
|
||||
|
||||
function ChestProvider:isValid()
|
||||
function ChestAdapter:isValid()
|
||||
return not not self.list
|
||||
end
|
||||
|
||||
function ChestProvider:getCachedItemDetails(item, k)
|
||||
function ChestAdapter:getCachedItemDetails(item, k)
|
||||
local key = { item.name, item.damage, item.nbtHash }
|
||||
|
||||
local detail = itemDB:get(key)
|
||||
@@ -64,12 +64,12 @@ function ChestProvider:getCachedItemDetails(item, k)
|
||||
end
|
||||
end
|
||||
|
||||
function ChestProvider:refresh(throttle)
|
||||
function ChestAdapter:refresh(throttle)
|
||||
return self:listItems(throttle)
|
||||
end
|
||||
|
||||
-- provide a consolidated list of items
|
||||
function ChestProvider:listItems(throttle)
|
||||
function ChestAdapter:listItems(throttle)
|
||||
self.cache = { }
|
||||
local items = { }
|
||||
|
||||
@@ -82,13 +82,7 @@ function ChestProvider:listItems(throttle)
|
||||
if not entry then
|
||||
entry = self:getCachedItemDetails(v, k)
|
||||
if entry then
|
||||
entry.dmg = entry.damage
|
||||
entry.id = entry.name
|
||||
entry.count = 0
|
||||
entry.display_name = entry.displayName
|
||||
entry.max_size = entry.maxCount
|
||||
entry.nbt_hash = entry.nbtHash
|
||||
entry.lname = entry.displayName:lower()
|
||||
self.cache[key] = entry
|
||||
table.insert(items, entry)
|
||||
end
|
||||
@@ -96,7 +90,6 @@ function ChestProvider:listItems(throttle)
|
||||
|
||||
if entry then
|
||||
entry.count = entry.count + v.count
|
||||
entry.qty = entry.count
|
||||
end
|
||||
throttle()
|
||||
end
|
||||
@@ -106,24 +99,24 @@ function ChestProvider:listItems(throttle)
|
||||
return items
|
||||
end
|
||||
|
||||
function ChestProvider:getItemInfo(id, dmg, nbtHash)
|
||||
function ChestAdapter:getItemInfo(name, damage, nbtHash)
|
||||
if not self.cache then
|
||||
self:listItems()
|
||||
end
|
||||
local key = table.concat({ id, dmg, nbtHash }, ':')
|
||||
local key = table.concat({ name, damage, nbtHash }, ':')
|
||||
return self.cache[key]
|
||||
end
|
||||
|
||||
function ChestProvider:craft(id, dmg, qty)
|
||||
function ChestAdapter:craft(name, damage, qty)
|
||||
end
|
||||
|
||||
function ChestProvider:craftItems(items)
|
||||
function ChestAdapter:craftItems(items)
|
||||
end
|
||||
|
||||
function ChestProvider:provide(item, qty, slot, direction)
|
||||
function ChestAdapter:provide(item, qty, slot, direction)
|
||||
local stacks = self.list()
|
||||
for key,stack in pairs(stacks) do
|
||||
if stack.name == item.id and stack.damage == item.dmg then
|
||||
if stack.name == item.name and stack.damage == item.damage then
|
||||
local amount = math.min(qty, stack.count)
|
||||
if amount > 0 then
|
||||
self.pushItems(direction or self.direction, key, amount, slot)
|
||||
@@ -136,12 +129,12 @@ function ChestProvider:provide(item, qty, slot, direction)
|
||||
end
|
||||
end
|
||||
|
||||
function ChestProvider:extract(slot, qty, toSlot)
|
||||
function ChestAdapter:extract(slot, qty, toSlot)
|
||||
self.pushItems(self.direction, slot, qty, toSlot)
|
||||
end
|
||||
|
||||
function ChestProvider:insert(slot, qty)
|
||||
function ChestAdapter:insert(slot, qty)
|
||||
self.pullItems(self.direction, slot, qty)
|
||||
end
|
||||
|
||||
return ChestProvider
|
||||
return ChestAdapter
|
||||
@@ -1,6 +1,5 @@
|
||||
local class = require('class')
|
||||
local Util = require('util')
|
||||
local Logger = require('logger')
|
||||
local Peripheral = require('peripheral')
|
||||
|
||||
local MEProvider = class()
|
||||
@@ -58,11 +57,28 @@ local function safeString(text)
|
||||
return text
|
||||
end
|
||||
|
||||
local convertNames = {
|
||||
name = 'id',
|
||||
damage = 'dmg',
|
||||
maxCount = 'max_size',
|
||||
count = 'qty',
|
||||
displayName = 'display_name',
|
||||
maxDamage = 'max_dmg',
|
||||
}
|
||||
|
||||
local function convertItem(item)
|
||||
for k,v in pairs(convertNames) do
|
||||
item[k] = item[v]
|
||||
item[v] = nil
|
||||
end
|
||||
item.displayName = safeString(item.displayName)
|
||||
end
|
||||
|
||||
function MEProvider:refresh()
|
||||
self.items = self.getAvailableItems('all')
|
||||
for _,v in pairs(self.items) do
|
||||
Util.merge(v, v.item)
|
||||
v.name = safeString(v.display_name)
|
||||
convertItem(v)
|
||||
end
|
||||
return self.items
|
||||
end
|
||||
@@ -72,25 +88,24 @@ function MEProvider:listItems()
|
||||
return self.items
|
||||
end
|
||||
|
||||
function MEProvider:getItemInfo(id, dmg)
|
||||
function MEProvider:getItemInfo(name, damage)
|
||||
|
||||
for key,item in pairs(self.items) do
|
||||
if item.id == id and item.dmg == dmg then
|
||||
if item.name == name and item.damage == damage then
|
||||
return item
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function MEProvider:craft(id, dmg, qty)
|
||||
function MEProvider:craft(name, damage, count)
|
||||
|
||||
self:refresh()
|
||||
|
||||
local item = self:getItemInfo(id, dmg)
|
||||
local item = self:getItemInfo(name, damage)
|
||||
|
||||
if item and item.is_craftable then
|
||||
|
||||
Logger.log('MEProvider', 'requested crafting for: ' .. id .. ':' .. dmg .. ' qty: ' .. qty)
|
||||
self.requestCrafting({ id = id, dmg = dmg }, qty)
|
||||
self.requestCrafting({ id = name, dmg = damage }, count)
|
||||
return true
|
||||
end
|
||||
end
|
||||
@@ -109,36 +124,32 @@ function MEProvider:craftItems(items)
|
||||
if count >= #cpus then
|
||||
break
|
||||
end
|
||||
if self:craft(item.id, item.dmg, item.qty) then
|
||||
if self:craft(item.name, item.damage, item.count) then
|
||||
count = count + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function MEProvider:provide(item, qty, slot)
|
||||
function MEProvider:provide(item, count, slot)
|
||||
return pcall(function()
|
||||
self.exportItem({
|
||||
id = item.id,
|
||||
dmg = item.dmg
|
||||
}, self.oside, qty, slot)
|
||||
id = item.name,
|
||||
dmg = item.damage
|
||||
}, self.oside, count, slot)
|
||||
end)
|
||||
end
|
||||
|
||||
function MEProvider:insert(slot, qty)
|
||||
local s, m = pcall(function() self.pullItem(self.oside, slot, qty) end)
|
||||
function MEProvider:insert(slot, count)
|
||||
local s, m = pcall(function() self.pullItem(self.oside, slot, count) end)
|
||||
if not s and m then
|
||||
print('MEProvider:pullItem')
|
||||
print(m)
|
||||
Logger.log('MEProvider', 'Insert failed, trying again')
|
||||
sleep(1)
|
||||
s, m = pcall(function() self.pullItem(self.oside, slot, qty) end)
|
||||
s, m = pcall(function() self.pullItem(self.oside, slot, count) end)
|
||||
if not s and m then
|
||||
print('MEProvider:pullItem')
|
||||
print(m)
|
||||
Logger.log('MEProvider', 'Insert failed again')
|
||||
read()
|
||||
else
|
||||
Logger.log('MEProvider', 'Insert successful')
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -3,7 +3,7 @@ local Util = require('util')
|
||||
local Peripheral = require('peripheral')
|
||||
local itemDB = require('itemDB')
|
||||
|
||||
local RefinedProvider = class()
|
||||
local RefinedAdapter = class()
|
||||
|
||||
local keys = {
|
||||
'damage',
|
||||
@@ -14,7 +14,7 @@ local keys = {
|
||||
'nbtHash',
|
||||
}
|
||||
|
||||
function RefinedProvider:init(args)
|
||||
function RefinedAdapter:init(args)
|
||||
local defaults = {
|
||||
items = { },
|
||||
name = 'refinedStorage',
|
||||
@@ -28,15 +28,15 @@ function RefinedProvider:init(args)
|
||||
end
|
||||
end
|
||||
|
||||
function RefinedProvider:isValid()
|
||||
function RefinedAdapter:isValid()
|
||||
return not not self.listAvailableItems
|
||||
end
|
||||
|
||||
function RefinedProvider:isOnline()
|
||||
function RefinedAdapter:isOnline()
|
||||
return self.getNetworkEnergyStored() > 0
|
||||
end
|
||||
|
||||
function RefinedProvider:getCachedItemDetails(item)
|
||||
function RefinedAdapter:getCachedItemDetails(item)
|
||||
local key = { item.name, item.damage, item.nbtHash }
|
||||
|
||||
local detail = itemDB:get(key)
|
||||
@@ -49,7 +49,6 @@ function RefinedProvider:getCachedItemDetails(item)
|
||||
return
|
||||
end
|
||||
Util.merge(detail, meta)
|
||||
detail.lname = detail.displayName:lower()
|
||||
|
||||
local t = { }
|
||||
for _,k in pairs(keys) do
|
||||
@@ -65,7 +64,7 @@ function RefinedProvider:getCachedItemDetails(item)
|
||||
end
|
||||
end
|
||||
|
||||
function RefinedProvider:listItems()
|
||||
function RefinedAdapter:listItems()
|
||||
local items = { }
|
||||
local list
|
||||
|
||||
@@ -80,10 +79,7 @@ function RefinedProvider:listItems()
|
||||
for _,v in pairs(list) do
|
||||
local item = self:getCachedItemDetails(v)
|
||||
if item then
|
||||
item.display_name = item.displayName
|
||||
item.id = v.name
|
||||
item.count = v.count
|
||||
item.qty = v.count
|
||||
table.insert(items, item)
|
||||
end
|
||||
throttle()
|
||||
@@ -94,7 +90,7 @@ function RefinedProvider:listItems()
|
||||
return items
|
||||
end
|
||||
|
||||
function RefinedProvider:getItemInfo(fingerprint)
|
||||
function RefinedAdapter:getItemInfo(fingerprint)
|
||||
|
||||
local key = { fingerprint.name, fingerprint.damage, fingerprint.nbtHash }
|
||||
|
||||
@@ -106,12 +102,11 @@ function RefinedProvider:getItemInfo(fingerprint)
|
||||
local detail = self.findItem(item)
|
||||
if detail then
|
||||
item.count = detail.count
|
||||
item.qty = detail.count
|
||||
return item
|
||||
end
|
||||
end
|
||||
|
||||
function RefinedProvider:isCrafting(item)
|
||||
function RefinedAdapter:isCrafting(item)
|
||||
for _,task in pairs(self.getCraftingTasks()) do
|
||||
local output = task.getPattern().outputs[1]
|
||||
if output.name == item.name and
|
||||
@@ -123,26 +118,26 @@ function RefinedProvider:isCrafting(item)
|
||||
return false
|
||||
end
|
||||
|
||||
function RefinedProvider:craft(item, qty)
|
||||
function RefinedAdapter:craft(item, qty)
|
||||
local detail = self.findItem(item)
|
||||
if detail then
|
||||
return detail.craft(qty)
|
||||
end
|
||||
end
|
||||
|
||||
function RefinedProvider:craftItems(items)
|
||||
function RefinedAdapter:craftItems(items)
|
||||
return false
|
||||
end
|
||||
|
||||
function RefinedProvider:provide(item, qty, slot)
|
||||
function RefinedAdapter:provide(item, qty, slot)
|
||||
end
|
||||
|
||||
function RefinedProvider:extract(slot, qty)
|
||||
function RefinedAdapter:extract(slot, qty)
|
||||
-- self.pushItems(self.direction, slot, qty)
|
||||
end
|
||||
|
||||
function RefinedProvider:insert(slot, qty)
|
||||
function RefinedAdapter:insert(slot, qty)
|
||||
-- self.pullItems(self.direction, slot, qty)
|
||||
end
|
||||
|
||||
return RefinedProvider
|
||||
return RefinedAdapter
|
||||
@@ -3,11 +3,11 @@ local Util = require('util')
|
||||
|
||||
local Craft = { }
|
||||
|
||||
local function clearGrid(chestProvider)
|
||||
local function clearGrid(chestAdapter)
|
||||
for i = 1, 16 do
|
||||
local count = turtle.getItemCount(i)
|
||||
if count > 0 then
|
||||
chestProvider:insert(i, count)
|
||||
chestAdapter:insert(i, count)
|
||||
if turtle.getItemCount(i) ~= 0 then
|
||||
return false
|
||||
end
|
||||
@@ -39,13 +39,13 @@ local function getItemCount(items, key)
|
||||
return 0
|
||||
end
|
||||
|
||||
local function turtleCraft(recipe, qty, chestProvider)
|
||||
local function turtleCraft(recipe, qty, chestAdapter)
|
||||
|
||||
clearGrid(chestProvider)
|
||||
clearGrid(chestAdapter)
|
||||
|
||||
for k,v in pairs(recipe.ingredients) do
|
||||
local item = splitKey(v)
|
||||
chestProvider:provide({ id = item.name, dmg = item.damage, nbt_hash = item.nbtHash }, qty, k)
|
||||
chestAdapter:provide(item, qty, k)
|
||||
if turtle.getItemCount(k) == 0 then -- ~= qty then
|
||||
-- FIX: ingredients cannot be stacked
|
||||
return false
|
||||
@@ -55,9 +55,9 @@ local function turtleCraft(recipe, qty, chestProvider)
|
||||
return turtle.craft()
|
||||
end
|
||||
|
||||
function Craft.craftRecipe(recipe, count, chestProvider)
|
||||
function Craft.craftRecipe(recipe, count, chestAdapter)
|
||||
|
||||
local items = chestProvider:listItems()
|
||||
local items = chestAdapter:listItems()
|
||||
|
||||
local function sumItems(items)
|
||||
-- produces { ['minecraft:planks:0'] = 8 }
|
||||
@@ -81,7 +81,7 @@ function Craft.craftRecipe(recipe, count, chestProvider)
|
||||
Util.print('Crafting %d %s', icount * count - itemCount, key)
|
||||
if not Craft.craftRecipe(irecipe,
|
||||
icount * count - itemCount,
|
||||
chestProvider) then
|
||||
chestAdapter) then
|
||||
turtle.select(1)
|
||||
return
|
||||
end
|
||||
@@ -89,7 +89,7 @@ Util.print('Crafting %d %s', icount * count - itemCount, key)
|
||||
end
|
||||
end
|
||||
repeat
|
||||
if not turtleCraft(recipe, math.min(count, maxCount), chestProvider) then
|
||||
if not turtleCraft(recipe, math.min(count, maxCount), chestAdapter) then
|
||||
turtle.select(1)
|
||||
return false
|
||||
end
|
||||
@@ -157,10 +157,10 @@ function Craft.getCraftableAmountTest()
|
||||
end
|
||||
|
||||
function Craft.craftRecipeTest(name, count)
|
||||
local ChestProvider = require('chestProvider18')
|
||||
local chestProvider = ChestProvider({ wrapSide = 'top', direction = 'down' })
|
||||
local ChestAdapter = require('chestAdapter18')
|
||||
local chestAdapter = ChestAdapter({ wrapSide = 'top', direction = 'down' })
|
||||
Craft.setRecipes(Util.readTable('usr/etc/recipes.db'))
|
||||
return { Craft.craftRecipe(Craft.recipes[name], count, chestProvider) }
|
||||
return { Craft.craftRecipe(Craft.recipes[name], count, chestAdapter) }
|
||||
end
|
||||
|
||||
return Craft
|
||||
|
||||
@@ -38,6 +38,7 @@ local function dig(action)
|
||||
|
||||
local hi = turtle.getHeadingInfo(direction)
|
||||
local node = { x = turtle.point.x + hi.xd, y = turtle.point.y + hi.yd, z = turtle.point.z + hi.zd }
|
||||
|
||||
if Point.inBox(node, box) then
|
||||
|
||||
local key = toKey(node)
|
||||
@@ -57,8 +58,10 @@ local function move(action)
|
||||
dig(turtle.getAction('forward'))
|
||||
elseif action == 'up' then
|
||||
dig(turtle.getAction('up'))
|
||||
dig(turtle.getAction('forward'))
|
||||
elseif action == 'down' then
|
||||
dig(turtle.getAction('down'))
|
||||
dig(turtle.getAction('forward'))
|
||||
elseif action == 'back' then
|
||||
dig(turtle.getAction('up'))
|
||||
dig(turtle.getAction('down'))
|
||||
@@ -123,7 +126,9 @@ return function(startPt, endPt, firstPt, verbose)
|
||||
box.ey = math.max(startPt.y, endPt.y)
|
||||
box.ez = math.max(startPt.z, endPt.z)
|
||||
|
||||
turtle.pathfind(firstPt)
|
||||
if not turtle.pathfind(firstPt) then
|
||||
error('failed to reach starting point')
|
||||
end
|
||||
|
||||
turtle.setPolicy("attack", { dig = dig }, "assuredMove")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user