This commit is contained in:
kepler155c
2018-10-26 00:30:37 -04:00
parent eb6ab3ed08
commit 6d3389c9c6
7 changed files with 147 additions and 144 deletions

View File

@@ -41,6 +41,13 @@ local function splitKey(key)
return item
end
local function makeRecipeKey(item)
if type(item) == 'string' then
item = splitKey(item)
end
return table.concat({ item.name, item.damage or 0, item.nbtHash }, ':')
end
function Craft.getItemCount(items, item)
if type(item) == 'string' then
item = splitKey(item)
@@ -60,25 +67,25 @@ function Craft.getItemCount(items, item)
return count
end
local function machineCraft(recipe, qty, inventoryAdapter, machineName, oitem)
local key = recipe.result
local request = oitem.ingredients[key]
if not request then
request = {
count = qty,
crafted = 0,
}
oitem.ingredients[recipe.result] = request
function Craft.sumIngredients(recipe)
-- produces { ['minecraft:planks:0'] = 8 }
local t = { }
for _,item in pairs(recipe.ingredients) do
t[item] = (t[item] or 0) + 1
end
-- need a check for crafting tool
return t
end
local function machineCraft(recipe, inventoryAdapter, machineName, request, count)
if request.pending then
request.crafted = request.crafted + (inventoryAdapter.activity[key] or 0)
if request.crafted >= request.count then
request.pending = nil -- TODO: check...
request.statusCode = Craft.STATUS_SUCCESS
local imported = (inventoryAdapter.activity[recipe.result] or 0)
request.pending = request.pending - imported
request.crafted = request.crafted + imported
if request.pending <= 0 then
request.pending = nil
request.statusCode = nil
request.status = nil
return true
end
return
end
@@ -99,7 +106,7 @@ local function machineCraft(recipe, qty, inventoryAdapter, machineName, oitem)
end
for k,v in pairs(recipe.ingredients) do
if inventoryAdapter:provide(splitKey(v), qty, k, machineName) ~= qty then
if inventoryAdapter:provide(splitKey(v), count, k, machineName) ~= count then
-- TODO: suck em back out
request.status = 'unknown error'
request.statusCode = Craft.STATUS_ERROR
@@ -108,21 +115,10 @@ local function machineCraft(recipe, qty, inventoryAdapter, machineName, oitem)
end
request.status = 'processing'
request.statusCode = Craft.STATUS_INFO
request.pending = true
request.pending = count * recipe.count
end
local function turtleCraft(recipe, qty, inventoryAdapter, oitem)
local key = recipe.result
local request = oitem.ingredients[key]
if not request then
request = {
count = qty,
crafted = 0,
}
oitem.ingredients[recipe.result] = request
end
local function turtleCraft(recipe, inventoryAdapter, request, count)
if not clearGrid(inventoryAdapter) then
request.status = 'grid in use'
request.statusCode = Craft.STATUS_ERROR
@@ -131,92 +127,84 @@ local function turtleCraft(recipe, qty, inventoryAdapter, oitem)
for k,v in pairs(recipe.ingredients) do
local item = splitKey(v)
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
]]--
if inventoryAdapter:provide(item, provideQty, k) ~= provideQty then
if inventoryAdapter:provide(item, count, k) ~= count then
-- FIX: ingredients cannot be stacked
--debug('failed ' .. v .. ' - ' .. provideQty)
request.status = 'unknown error'
request.statusCode = Craft.STATUS_ERROR
return
end
end
turtle.select(1)
if turtle.craft() then
request.status = nil
request.statusCode = Craft.STATUS_SUCCESS
request.crafted = request.crafted + count * recipe.count
return true
end
request.status = 'failed to craft'
request.status = 'Failed to craft'
request.statusCode = Craft.STATUS_ERROR
end
function Craft.loadRecipes()
Craft.recipes = { }
Util.merge(Craft.recipes, (Util.readTable(fs.combine(RECIPES_DIR, 'minecraft.db')) or { }).recipes)
local config = Util.readTable('usr/config/recipeBooks.db') or { }
for _, book in pairs(config) do
local recipeFile = Util.readTable(book)
Util.merge(Craft.recipes, recipeFile.recipes)
end
local recipes = Util.readTable(USER_RECIPES) or { }
Util.merge(Craft.recipes, recipes)
for k,v in pairs(Craft.recipes) do
v.result = k
end
Craft.machineLookup = Util.readTable(MACHINE_LOOKUP) or { }
end
function Craft.sumIngredients(recipe)
-- produces { ['minecraft:planks:0'] = 8 }
local t = { }
for _,item in pairs(recipe.ingredients) do
t[item] = (t[item] or 0) + 1
end
-- need a check for crafting tool
return t
end
local function makeRecipeKey(item)
if type(item) == 'string' then
item = splitKey(item)
end
return table.concat({ item.name, item.damage or 0, item.nbtHash }, ':')
end
function Craft.craftRecipe(recipe, count, inventoryAdapter, origItem)
if type(recipe) == 'string' then
recipe = Craft.recipes[recipe]
if not recipe then
origItem.ingredients[recipe.result] = {
status = 'no recipe',
statusCode = Craft.STATUS_ERROR,
}
return 0, 'No recipe'
end
end
for _,key in pairs(Util.keys(origItem.ingredients)) do
local e = origItem.ingredients[key]
if e and e.transient then
origItem.ingredients[key] = nil
end
end
return Craft.craftRecipeInternal(recipe, count, inventoryAdapter, origItem)
end
function Craft.craftRecipeInternal(recipe, count, inventoryAdapter, origItem)
local items = inventoryAdapter:listItems()
if not items then
origItem.ingredients[recipe.result] = {
status = 'Inventory changed',
statusCode = Craft.STATUS_ERROR,
}
return 0, 'Inventory changed'
end
count = math.ceil(count / recipe.count)
local request = origItem.ingredients[recipe.result]
if not request then
request = {
crafted = 0,
count = count,
}
origItem.ingredients[recipe.result] = request
end
if request.pending then
machineCraft(recipe, inventoryAdapter,
Craft.machineLookup[recipe.result], request)
return 0
end
local canCraft = Craft.getCraftableAmount(recipe, count, items, { })
if canCraft == 0 then
local resourceList = Craft.getResourceList(recipe, items, count)
for k,v in pairs(resourceList) do
if v.need > 0 then
if not origItem.ingredients[k] then
origItem.ingredients[k] = {
status = 'No recipe',
statusCode = Craft.STATUS_ERROR,
count = v.need,
crafted = 0,
transient = true,
}
end
end
end
return 0
end
count = math.ceil(canCraft / recipe.count)
local maxCount = recipe.maxCount or math.floor(64 / recipe.count)
for key,icount in pairs(Craft.sumIngredients(recipe)) do
@@ -228,36 +216,33 @@ function Craft.craftRecipe(recipe, count, inventoryAdapter, origItem)
maxCount = math.min(maxCount, itemDB:getMaxCount(key))
if itemCount < need then
local irecipe = Craft.findRecipe(key)
if irecipe then
local iqty = need - itemCount
local crafted = Craft.craftRecipe(irecipe, iqty, inventoryAdapter, origItem)
if crafted ~= iqty then
turtle.select(1)
return 0
end
if not irecipe then
return 0
end
local iqty = need - itemCount
local crafted = Craft.craftRecipeInternal(irecipe, iqty, inventoryAdapter, origItem)
if crafted ~= iqty then
return 0
end
end
end
local crafted = 0
repeat
local requested = math.min(count, maxCount)
local batch = math.min(count, maxCount)
if Craft.machineLookup[recipe.result] then
if not machineCraft(recipe, requested, inventoryAdapter, Craft.machineLookup[recipe.result], origItem) then
break
end
else
if not turtleCraft(recipe, requested, inventoryAdapter, origItem) then
if not machineCraft(recipe, inventoryAdapter,
Craft.machineLookup[recipe.result], request, batch) then
break
end
elseif not turtleCraft(recipe, inventoryAdapter, request, batch) then
break
end
crafted = crafted + requested
crafted = crafted + batch
count = count - maxCount
until count <= 0
turtle.select(1)
return crafted * recipe.count
end
@@ -378,6 +363,27 @@ function Craft.getCraftableAmount(inRecipe, count, items, missing)
return sumItems(inRecipe, { }, math.ceil(count / inRecipe.count))
end
function Craft.loadRecipes()
Craft.recipes = { }
Util.merge(Craft.recipes, (Util.readTable(fs.combine(RECIPES_DIR, 'minecraft.db')) or { }).recipes)
local config = Util.readTable('usr/config/recipeBooks.db') or { }
for _, book in pairs(config) do
local recipeFile = Util.readTable(book)
Util.merge(Craft.recipes, recipeFile.recipes)
end
local recipes = Util.readTable(USER_RECIPES) or { }
Util.merge(Craft.recipes, recipes)
for k,v in pairs(Craft.recipes) do
v.result = k
end
Craft.machineLookup = Util.readTable(MACHINE_LOOKUP) or { }
end
function Craft.canCraft(item, count, items)
return Craft.getCraftableAmount(Craft.recipes[item], count, items) == count
end