crafting improvements
This commit is contained in:
@@ -57,10 +57,12 @@ function ChestAdapter:getCachedItemDetails(item, k)
|
||||
if not detail then
|
||||
pcall(function() detail = self.getItemMeta(k) end)
|
||||
if not detail then
|
||||
-- error('Inventory has changed')
|
||||
return
|
||||
end
|
||||
-- NOT SUFFICIENT
|
||||
if detail.name ~= item.name then
|
||||
-- error('Inventory has changed')
|
||||
return
|
||||
end
|
||||
|
||||
@@ -94,11 +96,12 @@ function ChestAdapter:listItems(throttle)
|
||||
local entry = self.cache[key]
|
||||
if not entry then
|
||||
entry = self:getCachedItemDetails(v, k)
|
||||
if entry then
|
||||
entry.count = 0
|
||||
self.cache[key] = entry
|
||||
table.insert(items, entry)
|
||||
if not entry then
|
||||
return -- Inventory has changed
|
||||
end
|
||||
entry.count = 0
|
||||
self.cache[key] = entry
|
||||
table.insert(items, entry)
|
||||
end
|
||||
|
||||
if entry then
|
||||
@@ -129,7 +132,9 @@ end
|
||||
function ChestAdapter:provide(item, qty, slot, direction)
|
||||
local stacks = self.list()
|
||||
for key,stack in Util.rpairs(stacks) do
|
||||
if stack.name == item.name and stack.damage == item.damage then
|
||||
if stack.name == item.name and
|
||||
(not item.damage or stack.damage == item.damage) and
|
||||
stack.nbtHash == item.nbtHash then
|
||||
local amount = math.min(qty, stack.count)
|
||||
if amount > 0 then
|
||||
self.pushItems(direction or self.direction, key, amount, slot)
|
||||
|
||||
@@ -4,6 +4,33 @@ local Util = require('util')
|
||||
|
||||
local itemDB = TableDB({ fileName = 'usr/config/items.db' })
|
||||
|
||||
local function safeString(text)
|
||||
|
||||
local val = text:byte(1)
|
||||
|
||||
if val < 32 or val > 128 then
|
||||
|
||||
local newText = { }
|
||||
local skip = 0
|
||||
for i = 1, #text do
|
||||
val = text:byte(i)
|
||||
if val == 167 then
|
||||
skip = 2
|
||||
end
|
||||
if skip > 0 then
|
||||
skip = skip - 1
|
||||
else
|
||||
if val >= 32 and val <= 128 then
|
||||
newText[#newText + 1] = val
|
||||
end
|
||||
end
|
||||
end
|
||||
return string.char(unpack(newText))
|
||||
end
|
||||
|
||||
return text
|
||||
end
|
||||
|
||||
function itemDB:makeKey(item)
|
||||
return { item.name, item.damage, item.nbtHash }
|
||||
end
|
||||
@@ -15,7 +42,10 @@ function itemDB:splitKey(key, item)
|
||||
if #t[#t] > 8 then
|
||||
item.nbtHash = table.remove(t)
|
||||
end
|
||||
item.damage = tonumber(table.remove(t))
|
||||
local damage = table.remove(t)
|
||||
if damage ~= '*' then
|
||||
item.damage = tonumber(damage)
|
||||
end
|
||||
item.name = table.concat(t, ':')
|
||||
|
||||
return item
|
||||
@@ -28,12 +58,16 @@ function itemDB:get(key)
|
||||
return item
|
||||
end
|
||||
|
||||
if key[2] ~= 0 then
|
||||
if type(key) == 'string' then
|
||||
key = self:makeKey(self:splitKey(key))
|
||||
end
|
||||
|
||||
if not key[2] or key[2] ~= 0 then
|
||||
item = TableDB.get(self, { key[1], 0, key[3] })
|
||||
if item and item.maxDamage > 0 then
|
||||
item = Util.shallowCopy(item)
|
||||
item.damage = key[2]
|
||||
item.displayName = string.format('%s (damage: %d)', item.displayName, item.damage)
|
||||
item.displayName = string.format('%s (damage: %s)', item.displayName, (item.damage or '*'))
|
||||
return item
|
||||
end
|
||||
end
|
||||
@@ -43,6 +77,7 @@ function itemDB:add(key, item)
|
||||
if item.maxDamage > 0 then
|
||||
key = { key[1], 0, key[3] }
|
||||
end
|
||||
item.displayName = safeString(item.displayName)
|
||||
TableDB.add(self, key, item)
|
||||
end
|
||||
|
||||
@@ -58,7 +93,7 @@ function itemDB:getName(item)
|
||||
end
|
||||
|
||||
-- fallback to nameDB
|
||||
return nameDB:getName(item.name .. ':' .. item.damage)
|
||||
return nameDB:getName(item.name .. ':' .. (item.damage or '*'))
|
||||
end
|
||||
|
||||
function itemDB:getMaxCount(item)
|
||||
|
||||
@@ -34,7 +34,7 @@ local function getItemCount(items, key)
|
||||
local item = splitKey(key)
|
||||
for _,v in pairs(items) do
|
||||
if v.name == item.name and
|
||||
v.damage == item.damage and
|
||||
(not item.damage or v.damage == item.damage) and
|
||||
v.nbtHash == item.nbtHash then
|
||||
return v.count
|
||||
end
|
||||
@@ -47,7 +47,15 @@ local function turtleCraft(recipe, qty, inventoryAdapter)
|
||||
|
||||
for k,v in pairs(recipe.ingredients) do
|
||||
local item = splitKey(v)
|
||||
inventoryAdapter:provide(item, qty, k)
|
||||
local provideQty = qty
|
||||
--[[
|
||||
Turtles can only craft 1 item at a time when using a tool.
|
||||
|
||||
if recipe.craftingTools and recipe.craftingTools[k] then
|
||||
provideQty = 1
|
||||
end
|
||||
]]--
|
||||
inventoryAdapter:provide(item, provideQty, k)
|
||||
if turtle.getItemCount(k) == 0 then -- ~= qty then
|
||||
-- FIX: ingredients cannot be stacked
|
||||
return false
|
||||
@@ -127,7 +135,9 @@ function Craft.getCraftableAmount(recipe, count, items, missing)
|
||||
end
|
||||
return canCraft
|
||||
end
|
||||
summedItems[item] = summedItem - 1
|
||||
if not recipe.craftingTools or not recipe.craftingTools[item] then
|
||||
summedItems[item] = summedItem - 1
|
||||
end
|
||||
end
|
||||
canCraft = canCraft + recipe.count
|
||||
end
|
||||
|
||||
@@ -43,7 +43,7 @@ function turtle.craftItem(item, count, inventoryInfo)
|
||||
|
||||
local slot = turtle.select(CRAFTING_TABLE)
|
||||
turtle.equip(side, CRAFTING_TABLE)
|
||||
equipped = turtle.getItemDetail(slot)
|
||||
equipped = turtle.getItemDetail(slot.index)
|
||||
end
|
||||
|
||||
success = Craft.craftRecipe(item, count or 1, inventory)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
_G.requireInjector()
|
||||
|
||||
local Ansi = require('ansi')
|
||||
local ChestAdapter = require('chestAdapter18')
|
||||
local Config = require('config')
|
||||
local Craft = require('turtle.craft')
|
||||
@@ -74,27 +75,18 @@ local resources
|
||||
|
||||
Craft.setRecipes(recipes)
|
||||
|
||||
local function getItem(items, inItem, ignoreDamage)
|
||||
local function getItem(items, inItem, ignoreDamage, ignoreNbtHash)
|
||||
for _,item in pairs(items) do
|
||||
if item.name == inItem.name then
|
||||
if ignoreDamage then
|
||||
return item
|
||||
elseif item.damage == inItem.damage and item.nbtHash == inItem.nbtHash then
|
||||
return item
|
||||
end
|
||||
if item.name == inItem.name and
|
||||
(ignoreDamage or item.damage == inItem.damage) and
|
||||
(ignoreNbtHash or item.nbtHash == inItem.nbtHash) then
|
||||
return item
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function splitKey(key)
|
||||
local t = Util.split(key, '(.-):')
|
||||
local item = { }
|
||||
if #t[#t] > 8 then
|
||||
item.nbtHash = table.remove(t)
|
||||
end
|
||||
item.damage = tonumber(table.remove(t))
|
||||
item.name = table.concat(t, ':')
|
||||
return item
|
||||
return itemDB:splitKey(key)
|
||||
end
|
||||
|
||||
local function getItemQuantity(items, item)
|
||||
@@ -219,6 +211,7 @@ local function craftItem(recipe, items, originalItem, craftList, count)
|
||||
if missing.name then
|
||||
originalItem.status = string.format('%s missing', itemDB:getName(missing.name))
|
||||
originalItem.statusCode = 'missing'
|
||||
debug('missing: ' .. missing.name)
|
||||
end
|
||||
|
||||
if toCraft > 0 then
|
||||
@@ -338,18 +331,18 @@ local function getAutocraftItems()
|
||||
return craftList
|
||||
end
|
||||
|
||||
local function getItemWithQty(items, res, ignoreDamage)
|
||||
local function getItemWithQty(items, res, ignoreDamage, ignoreNbtHash)
|
||||
|
||||
local item = getItem(items, res, ignoreDamage)
|
||||
local item = getItem(items, res, ignoreDamage, ignoreNbtHash)
|
||||
|
||||
if item and ignoreDamage then
|
||||
if item and (ignoreDamage or ignoreNbtHash) then
|
||||
local count = 0
|
||||
|
||||
for _,v in pairs(items) do
|
||||
if item.name == v.name and item.nbtHash == v.nbtHash then
|
||||
if item.maxDamage > 0 or item.damage == v.damage then
|
||||
count = count + v.count
|
||||
end
|
||||
if item.name == v.name and
|
||||
(ignoreDamage or item.damage == v.damage) and
|
||||
(ignoreNbtHash or item.nbtHash == v.nbtHash) then
|
||||
count = count + v.count
|
||||
end
|
||||
end
|
||||
item.count = count
|
||||
@@ -364,7 +357,7 @@ local function watchResources(items)
|
||||
local outputs = { }
|
||||
|
||||
for _,res in pairs(resources) do
|
||||
local item = getItemWithQty(items, res, res.ignoreDamage)
|
||||
local item = getItemWithQty(items, res, res.ignoreDamage, res.ignoreDamage)
|
||||
if not item then
|
||||
item = {
|
||||
damage = res.damage,
|
||||
@@ -377,7 +370,7 @@ local function watchResources(items)
|
||||
|
||||
if res.limit and item.count > res.limit then
|
||||
inventoryAdapter:provide(
|
||||
{ name = item.name, damage = item.damage },
|
||||
{ name = item.name, damage = item.damage, nbtHash = item.nbtHash },
|
||||
item.count - res.limit,
|
||||
nil,
|
||||
config.trashDirection)
|
||||
@@ -505,6 +498,25 @@ local itemPage = UI.Page {
|
||||
},
|
||||
help = 'Output side'
|
||||
},
|
||||
infoButton = UI.Button {
|
||||
x = 2, y = -2,
|
||||
event = 'show_info',
|
||||
text = 'Info',
|
||||
},
|
||||
},
|
||||
info = UI.SlideOut {
|
||||
titleBar = UI.TitleBar {
|
||||
title = "Information",
|
||||
},
|
||||
textArea = UI.TextArea {
|
||||
x = 2, ex = -2, y = 3, ey = -4,
|
||||
backgroundColor = colors.black,
|
||||
},
|
||||
cancel = UI.Button {
|
||||
ex = -2, y = -2, width = 6,
|
||||
text = 'Okay',
|
||||
event = 'hide_info',
|
||||
}
|
||||
},
|
||||
statusBar = UI.StatusBar { }
|
||||
}
|
||||
@@ -535,6 +547,22 @@ function itemPage:eventHandler(event)
|
||||
if event.type == 'form_cancel' then
|
||||
UI:setPreviousPage()
|
||||
|
||||
elseif event.type == 'show_info' then
|
||||
self.info.textArea.value =
|
||||
string.format(
|
||||
[[%sName: %s%s
|
||||
%sID: %s%s
|
||||
%sDamage: %s%s
|
||||
%sNBT: %s%s]],
|
||||
Ansi.yellow, Ansi.reset, self.item.displayName,
|
||||
Ansi.yellow, Ansi.reset, self.item.name,
|
||||
Ansi.yellow, Ansi.reset, self.item.damage,
|
||||
Ansi.yellow, Ansi.reset, self.item.nbtHash or '(none)')
|
||||
self.info:show()
|
||||
|
||||
elseif event.type == 'hide_info' then
|
||||
self.info:hide()
|
||||
|
||||
elseif event.type == 'focus_change' then
|
||||
self.statusBar:setStatus(event.focused.help)
|
||||
self.statusBar:draw()
|
||||
@@ -763,20 +791,65 @@ local function learnRecipe(page)
|
||||
if ingredients then
|
||||
turtle.select(1)
|
||||
if canCraft and turtle.craft() then
|
||||
local recipe = getTurtleInventory()
|
||||
if recipe and recipe[1] then
|
||||
local results = getTurtleInventory()
|
||||
if results and results[1] then
|
||||
clearGrid()
|
||||
|
||||
local key = uniqueKey(recipe[1])
|
||||
local maxCount
|
||||
local newRecipe = {
|
||||
count = recipe[1].count,
|
||||
ingredients = ingredients,
|
||||
}
|
||||
if recipe[1].maxCount ~= 64 then
|
||||
newRecipe.maxCount = recipe[1].maxCount
|
||||
|
||||
for _,v1 in pairs(results) do
|
||||
for _,v2 in pairs(ingredients) do
|
||||
if v1.name == v2.name and
|
||||
v1.nbtHash == v2.nbtHash and
|
||||
(v1.damage == v2.damage or
|
||||
(v1.maxDamage > 0 and v2.maxDamage > 0 and
|
||||
v1.damage ~= v2.damage)) then
|
||||
if not newRecipe.crafingTools then
|
||||
newRecipe.craftingTools = { }
|
||||
end
|
||||
local tool = Util.shallowCopy(v2)
|
||||
if tool.maxDamage > 0 then
|
||||
tool.damage = '*'
|
||||
end
|
||||
|
||||
--[[
|
||||
Turtles can only craft one item at a time using a tool :(
|
||||
]]--
|
||||
maxCount = 1
|
||||
|
||||
newRecipe.craftingTools[uniqueKey(tool)] = true
|
||||
v1.craftingTool = true
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
local recipe
|
||||
for _,v in pairs(results) do
|
||||
if not v.craftingTool then
|
||||
recipe = v
|
||||
recipe.maxCount = maxCount
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if not recipe then
|
||||
error('Failed')
|
||||
end
|
||||
|
||||
newRecipe.count = recipe.count
|
||||
|
||||
local key = uniqueKey(recipe)
|
||||
if recipe.maxCount ~= 64 then
|
||||
newRecipe.maxCount = recipe.maxCount
|
||||
end
|
||||
|
||||
for k,ingredient in pairs(ingredients) do
|
||||
if ingredient.maxDamage > 0 then
|
||||
ingredient.damage = '*'
|
||||
end
|
||||
ingredients[k] = uniqueKey(ingredient)
|
||||
end
|
||||
|
||||
@@ -784,7 +857,7 @@ local function learnRecipe(page)
|
||||
|
||||
Util.writeTable(RECIPES_FILE, recipes)
|
||||
|
||||
local displayName = itemDB:getName(recipe[1])
|
||||
local displayName = itemDB:getName(recipe)
|
||||
|
||||
listingPage.statusBar.filter:setValue(displayName)
|
||||
listingPage.statusBar:timedStatus('Learned: ' .. displayName, 3)
|
||||
|
||||
@@ -161,6 +161,10 @@ local function craftItem(recipe, items, cItem, count)
|
||||
local slot = 1
|
||||
for key,qty in pairs(recipe.ingredients) do
|
||||
local item = itemDB:get(key)
|
||||
if not item then
|
||||
cItem.status = 'failed'
|
||||
return false
|
||||
end
|
||||
local c = count * qty
|
||||
while c > 0 do
|
||||
local maxCount = math.min(c, item.maxCount)
|
||||
@@ -169,7 +173,7 @@ local function craftItem(recipe, items, cItem, count)
|
||||
cItem.status = 'failed'
|
||||
debug(item)
|
||||
debug({ c, maxCount, count })
|
||||
read()
|
||||
--read()
|
||||
return false
|
||||
end
|
||||
c = c - maxCount
|
||||
@@ -198,6 +202,10 @@ local function expandList(list)
|
||||
local item = getItem(items, itemDB:splitKey(key))
|
||||
if not item then
|
||||
item = itemDB:get(key)
|
||||
if not item then
|
||||
--item = itemDB:splitKey(key)
|
||||
break
|
||||
end
|
||||
item.count = 0
|
||||
end
|
||||
local need = qty * count
|
||||
@@ -210,6 +218,9 @@ debug({ key, count, need })
|
||||
list[key].ocount = need
|
||||
list[key].count = 0
|
||||
else
|
||||
if not list[key].ocount then
|
||||
list[key].ocount = 0
|
||||
end
|
||||
list[key].ocount = list[key].ocount + need
|
||||
end
|
||||
debug('adding ' .. key .. ' ' .. need)
|
||||
@@ -471,6 +482,10 @@ function itemPage:eventHandler(event)
|
||||
filtered.ignoreDamage = true
|
||||
end
|
||||
|
||||
if Util.empty(filtered) then
|
||||
filtered = nil
|
||||
end
|
||||
|
||||
resources[uniqueKey(filtered)] = filtered
|
||||
saveResources()
|
||||
|
||||
@@ -553,7 +568,10 @@ function learnPage:enable(target)
|
||||
if target.has_recipe then
|
||||
local recipe = recipes[uniqueKey(target)]
|
||||
screen2.count.value = recipe.count
|
||||
screen2.machine:setIndex(select(2, Util.find(machines, 'index', recipe.machine)))
|
||||
local _, index = Util.find(machines, 'index', recipe.machine)
|
||||
if index then
|
||||
screen2.machine:setIndex(index)
|
||||
end
|
||||
for k,v in pairs(recipe.ingredients) do
|
||||
screen1.ingredients.values[k] =
|
||||
{ name = k, count = v, displayName = itemDB:getName(k) }
|
||||
|
||||
@@ -284,8 +284,8 @@ local function createFurnace()
|
||||
print('Adding a furnace')
|
||||
getCobblestone(8)
|
||||
|
||||
if craftItem(FURNACE) then
|
||||
turtle.drop(COBBLESTONE)
|
||||
if turtle.has(FURNACE) or craftItem(FURNACE) then
|
||||
-- turtle.drop(COBBLESTONE)
|
||||
local furnacePt = { x = GRID.BL.x + 2, y = 1, z = GRID.BL.z + 2 }
|
||||
turtle.placeAt(furnacePt, FURNACE)
|
||||
setState('furnace', furnacePt)
|
||||
@@ -306,7 +306,9 @@ local function createPerimeter()
|
||||
print('Creating a perimeter')
|
||||
|
||||
getCobblestone(GRID_WIDTH * 2 + 1)
|
||||
cook(COBBLESTONE, 2, STONE, OAK_PLANK, 2)
|
||||
if not turtle.has(STONE, 2) then
|
||||
cook(COBBLESTONE, 2, STONE, OAK_PLANK, 2)
|
||||
end
|
||||
turtle.refuel(OAK_PLANK)
|
||||
|
||||
turtle.pathfind(GRID.BL)
|
||||
@@ -337,7 +339,7 @@ local function createPerimeter()
|
||||
end
|
||||
|
||||
local function createChests()
|
||||
if state.chest_1 then
|
||||
if state.chest then
|
||||
return
|
||||
end
|
||||
if state.perimeter and
|
||||
@@ -345,27 +347,24 @@ local function createChests()
|
||||
turtle.canCraft(CHEST, 4, turtle.getSummedInventory()) then
|
||||
|
||||
print('Adding storage')
|
||||
if craftItem(CHEST, 4) then
|
||||
if turtle.has(CHEST, 2) or craftItem(CHEST, 2) then
|
||||
|
||||
local pt = Point.copy(GRID.BL)
|
||||
pt.x = pt.x + 1
|
||||
pt.y = pt.y - 1
|
||||
|
||||
for i = 1, 2 do
|
||||
pt.z = pt.z + 1
|
||||
pt.z = pt.z + 1
|
||||
|
||||
turtle.digDownAt(pt)
|
||||
turtle.placeDown(CHEST)
|
||||
turtle.digDownAt(pt)
|
||||
turtle.placeDown(CHEST)
|
||||
|
||||
pt.z = pt.z + 1
|
||||
pt.z = pt.z + 1
|
||||
|
||||
turtle.digDownAt(pt)
|
||||
turtle.placeDown(CHEST)
|
||||
turtle.digDownAt(pt)
|
||||
turtle.placeDown(CHEST)
|
||||
|
||||
setState('chest_' .. i, Util.shallowCopy(pt))
|
||||
setState('chest', Util.shallowCopy(pt))
|
||||
|
||||
pt.z = pt.z + 1
|
||||
end
|
||||
turtle.drop(DIRT)
|
||||
turtle.refuel(OAK_PLANK)
|
||||
end
|
||||
@@ -375,24 +374,27 @@ end
|
||||
|
||||
local function dropOffItems()
|
||||
|
||||
if state.chest_1 then
|
||||
if state.chest then
|
||||
local slots = turtle.getSummedInventory()
|
||||
|
||||
if state.chest_1 and
|
||||
if state.chest and
|
||||
slots[CHARCOAL] and
|
||||
slots[CHARCOAL].count >= MIN_CHARCOAL and
|
||||
(turtle.getItemCount('minecraft:log') > 0 or
|
||||
turtle.getItemCount('minecraft:log2') > 0) then
|
||||
|
||||
print('Storing logs')
|
||||
turtle.pathfind(Point.above(state.chest_1))
|
||||
turtle.pathfind(Point.above(state.chest))
|
||||
turtle.dropDown('minecraft:log')
|
||||
turtle.dropDown('minecraft:log2')
|
||||
end
|
||||
|
||||
if slots[APPLE] then
|
||||
print('Storing apples')
|
||||
turtle.dropDownAt(state.chest_2, APPLE)
|
||||
for _, sapling in pairs(ALL_SAPLINGS) do
|
||||
if slots[sapling] and slots[sapling].count > MAX_SAPLINGS then
|
||||
turtle.dropDown(sapling, slots[sapling].count - MAX_SAPLINGS)
|
||||
end
|
||||
end
|
||||
|
||||
turtle.dropDown(APPLE)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -421,7 +423,7 @@ local function placeTorches()
|
||||
|
||||
print('Placing torches')
|
||||
|
||||
if craftItem(TORCH, 4) then
|
||||
if turtle.has(TORCH, 4) or craftItem(TORCH, 4) then
|
||||
local pts = { }
|
||||
for x = -4, 4, 8 do
|
||||
for z = -4, 4, 8 do
|
||||
@@ -532,7 +534,7 @@ local function moreTrees()
|
||||
return
|
||||
end
|
||||
|
||||
if not state.chest_1 or turtle.getItemCount('minecraft:sapling') < 15 then
|
||||
if not state.chest or turtle.getItemCount('minecraft:sapling') < 15 then
|
||||
return true
|
||||
end
|
||||
|
||||
@@ -694,7 +696,7 @@ local tasks = {
|
||||
{ desc = 'Emptying furnace', fn = emptyFurnace },
|
||||
{ desc = 'Adding trees', fn = moreTrees },
|
||||
{ desc = 'Chopping', fn = fell },
|
||||
{ desc = 'Snacking', fn = eatSaplings },
|
||||
-- { desc = 'Snacking', fn = eatSaplings },
|
||||
{ desc = 'Creating chest', fn = createChests },
|
||||
{ desc = 'Creating furnace', fn = createFurnace },
|
||||
{ desc = 'Making charcoal', fn = makeSingleCharcoal },
|
||||
|
||||
4010
etc/recipes.db
4010
etc/recipes.db
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user