plethora crafter rename to milo - wip
This commit is contained in:
55
milo/apis/inventoryAdapter.lua
Normal file
55
milo/apis/inventoryAdapter.lua
Normal file
@@ -0,0 +1,55 @@
|
||||
local Adapter = { }
|
||||
|
||||
function Adapter.wrap(args)
|
||||
local adapters = {
|
||||
'networkedAdapter18',
|
||||
'refinedAdapter',
|
||||
'meAdapter18',
|
||||
'chestAdapter18',
|
||||
|
||||
-- adapters for version 1.7
|
||||
'meAdapter',
|
||||
'chestAdapter',
|
||||
}
|
||||
|
||||
for _,adapterType in ipairs(adapters) do
|
||||
local adapter = require(adapterType)(args)
|
||||
|
||||
if adapter:isValid() then
|
||||
|
||||
-- figure out which direction to push/pull items from an inventory
|
||||
-- based on the side the inventory is attached and which way the
|
||||
-- turtle/computer is facing
|
||||
if args and args.facing and adapter.side and not adapter.direction then
|
||||
local horz = { top = 'down', bottom = 'up' }
|
||||
adapter.direction = horz[adapter.side]
|
||||
|
||||
if not adapter.direction then
|
||||
local sides = {
|
||||
front = 0,
|
||||
right = 1,
|
||||
back = 2,
|
||||
left = 3,
|
||||
}
|
||||
-- pretty sure computer/turtle have sides reversed
|
||||
local cards = {
|
||||
east = 0,
|
||||
south = 1,
|
||||
west = 2,
|
||||
north = 3,
|
||||
}
|
||||
local icards = {
|
||||
[ 0 ] = 'west',
|
||||
[ 1 ] = 'north',
|
||||
[ 2 ] = 'east',
|
||||
[ 3 ] = 'south',
|
||||
}
|
||||
adapter.direction = icards[(cards[args.facing] + sides[adapter.side]) % 4]
|
||||
end
|
||||
end
|
||||
return adapter
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return Adapter
|
||||
388
milo/apis/milo.lua
Normal file
388
milo/apis/milo.lua
Normal file
@@ -0,0 +1,388 @@
|
||||
local Config = require('config')
|
||||
local Craft = require('turtle.craft')
|
||||
local itemDB = require('itemDB')
|
||||
local Util = require('util')
|
||||
|
||||
local os = _G.os
|
||||
local term = _G.term
|
||||
local turtle = _G.turtle
|
||||
|
||||
local Milo = {
|
||||
RECIPES_FILE = 'usr/config/recipes.db',
|
||||
RESOURCE_FILE = 'usr/config/resources.db',
|
||||
|
||||
STATUS_INFO = 'info',
|
||||
STATUS_WARNING = 'warning',
|
||||
STATUS_ERROR = 'error',
|
||||
|
||||
tasks = { },
|
||||
craftingStatus = { },
|
||||
}
|
||||
|
||||
function Milo:init(context)
|
||||
self.context = context
|
||||
end
|
||||
|
||||
function Milo:getContext()
|
||||
return self.context
|
||||
end
|
||||
|
||||
function Milo:pauseCrafting()
|
||||
self.craftingPaused = true
|
||||
end
|
||||
|
||||
function Milo:resumeCrafting()
|
||||
self.craftingPaused = false
|
||||
end
|
||||
|
||||
function Milo:isCraftingPaused()
|
||||
return self.craftingPaused
|
||||
end
|
||||
|
||||
function Milo:getState(key)
|
||||
if not self.state then
|
||||
self.state = { }
|
||||
Config.load('milo.state', self.state)
|
||||
end
|
||||
return self.state[key]
|
||||
end
|
||||
|
||||
function Milo:setState(key, value)
|
||||
if not self.state then
|
||||
self.state = { }
|
||||
Config.load('milo.state', self.state)
|
||||
end
|
||||
self.state[key] = value
|
||||
Config.update('milo.state', self.state)
|
||||
end
|
||||
|
||||
function Milo:uniqueKey(item)
|
||||
return table.concat({ item.name, item.damage, item.nbtHash }, ':')
|
||||
end
|
||||
|
||||
function Milo:getCraftingStatus()
|
||||
return self.craftingStatus
|
||||
end
|
||||
|
||||
function Milo:resetCraftingStatus()
|
||||
self.craftingStatus = { }
|
||||
self.context.inventoryAdapter.activity = { }
|
||||
end
|
||||
|
||||
function Milo:updateCraftingStatus(list)
|
||||
for k,v in pairs(list) do
|
||||
self.craftingStatus[k] = v
|
||||
end
|
||||
end
|
||||
|
||||
function Milo:registerTask(task)
|
||||
table.insert(self.tasks, task)
|
||||
end
|
||||
|
||||
function Milo:showError(msg)
|
||||
term.clear()
|
||||
self.context.jobList:showError()
|
||||
print(msg)
|
||||
print('rebooting in 5 secs')
|
||||
os.sleep(5)
|
||||
os.reboot()
|
||||
end
|
||||
|
||||
function Milo:getItem(items, inItem, ignoreDamage, ignoreNbtHash)
|
||||
for _,item in pairs(items) do
|
||||
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
|
||||
|
||||
function Milo:getItemWithQty(res, ignoreDamage, ignoreNbtHash)
|
||||
local items = self:listItems()
|
||||
local item = self:getItem(items, res, ignoreDamage, ignoreNbtHash)
|
||||
|
||||
if item and (ignoreDamage or ignoreNbtHash) then
|
||||
local count = 0
|
||||
|
||||
for _,v in pairs(items) do
|
||||
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
|
||||
end
|
||||
|
||||
return item
|
||||
end
|
||||
|
||||
function Milo:clearGrid()
|
||||
local function clear()
|
||||
turtle.eachFilledSlot(function(slot)
|
||||
self.context.inventoryAdapter:insert(slot.index, slot.count, nil, slot)
|
||||
end)
|
||||
|
||||
for i = 1, 16 do
|
||||
if turtle.getItemCount(i) ~= 0 then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
return clear() or clear()
|
||||
end
|
||||
|
||||
function Milo:eject(item, qty)
|
||||
local s, m = pcall(function()
|
||||
self.context.inventoryAdapter:provide(item, qty)
|
||||
turtle.emptyInventory()
|
||||
end)
|
||||
if not s and m then
|
||||
debug(m)
|
||||
end
|
||||
end
|
||||
|
||||
function Milo:mergeResources(t)
|
||||
for _,v in pairs(self.context.resources) do
|
||||
local item = self:getItem(t, v)
|
||||
if item then
|
||||
Util.merge(item, v)
|
||||
else
|
||||
item = Util.shallowCopy(v)
|
||||
item.count = 0
|
||||
table.insert(t, item)
|
||||
end
|
||||
end
|
||||
|
||||
for k in pairs(Craft.recipes) do
|
||||
local v = itemDB:splitKey(k)
|
||||
local item = self:getItem(t, v)
|
||||
if not item then
|
||||
item = Util.shallowCopy(v)
|
||||
item.count = 0
|
||||
table.insert(t, item)
|
||||
end
|
||||
item.has_recipe = true
|
||||
end
|
||||
|
||||
for _,v in pairs(t) do
|
||||
if not v.displayName then
|
||||
v.displayName = itemDB:getName(v)
|
||||
end
|
||||
v.lname = v.displayName:lower()
|
||||
end
|
||||
end
|
||||
|
||||
function Milo:saveResources()
|
||||
local t = { }
|
||||
|
||||
for k,v in pairs(self.context.resources) do
|
||||
v = Util.shallowCopy(v)
|
||||
local keys = Util.transpose({ 'auto', 'low', 'limit',
|
||||
'ignoreDamage', 'ignoreNbtHash',
|
||||
'rsControl', 'rsDevice', 'rsSide' })
|
||||
|
||||
for _,key in pairs(Util.keys(v)) do
|
||||
if not keys[key] then
|
||||
v[key] = nil
|
||||
end
|
||||
end
|
||||
if not Util.empty(v) then
|
||||
t[k] = v
|
||||
end
|
||||
end
|
||||
|
||||
Util.writeTable(self.RESOURCE_FILE, t)
|
||||
end
|
||||
|
||||
-- Return a list of everything in the system
|
||||
function Milo:listItems()
|
||||
for _ = 1, 5 do
|
||||
self.items = self.context.inventoryAdapter:listItems()
|
||||
if self.items then
|
||||
break
|
||||
end
|
||||
-- jobList:showError('Error - retrying in 3 seconds')
|
||||
os.sleep(3)
|
||||
end
|
||||
if not self.items then
|
||||
self:showError('Error - rebooting in 5 seconds')
|
||||
end
|
||||
|
||||
return self.items
|
||||
end
|
||||
|
||||
function Milo:addCraftingRequest(item, craftList, count)
|
||||
local key = self:uniqueKey(item)
|
||||
local request = craftList[key]
|
||||
if not craftList[key] then
|
||||
request = { name = item.name, damage = item.damage, nbtHash = item.nbtHash, count = 0 }
|
||||
request.displayName = itemDB:getName(request)
|
||||
craftList[key] = request
|
||||
end
|
||||
request.count = request.count + count
|
||||
return request
|
||||
end
|
||||
|
||||
-- Craft
|
||||
function Milo:craftItem(recipe, items, originalItem, craftList, count)
|
||||
local missing = { }
|
||||
local toCraft = Craft.getCraftableAmount(recipe, count, items, missing)
|
||||
if missing.name then
|
||||
originalItem.status = string.format('%s missing', itemDB:getName(missing.name))
|
||||
originalItem.statusCode = self.STATUS_WARNING
|
||||
end
|
||||
|
||||
local crafted = 0
|
||||
|
||||
if toCraft > 0 then
|
||||
crafted = Craft.craftRecipe(recipe, toCraft, self.context.inventoryAdapter)
|
||||
self:clearGrid()
|
||||
items = self:listItems()
|
||||
count = count - crafted
|
||||
end
|
||||
|
||||
if count > 0 and items then
|
||||
local ingredients = Craft.getResourceList4(recipe, items, count)
|
||||
for _,ingredient in pairs(ingredients) do
|
||||
if ingredient.need > 0 then
|
||||
local item = self:addCraftingRequest(ingredient, craftList, ingredient.need)
|
||||
if Craft.findRecipe(item) then
|
||||
item.status = string.format('%s missing', itemDB:getName(ingredient))
|
||||
item.statusCode = self.STATUS_WARNING
|
||||
else
|
||||
item.status = 'no recipe'
|
||||
item.statusCode = self.STATUS_ERROR
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return crafted
|
||||
end
|
||||
|
||||
-- Craft as much as possible regardless if all ingredients are available
|
||||
function Milo:forceCraftItem(inRecipe, items, originalItem, craftList, inCount)
|
||||
local summed = { }
|
||||
local throttle = Util.throttle()
|
||||
|
||||
local function sumItems(recipe, count)
|
||||
count = math.ceil(count / recipe.count)
|
||||
local craftable = count
|
||||
|
||||
for key,iqty in pairs(Craft.sumIngredients(recipe)) do
|
||||
throttle()
|
||||
local item = itemDB:splitKey(key)
|
||||
local summedItem = summed[key]
|
||||
if not summedItem then
|
||||
summedItem = Util.shallowCopy(item)
|
||||
summedItem.recipe = Craft.findRecipe(item)
|
||||
summedItem.count = Craft.getItemCount(items, key)
|
||||
summedItem.need = 0
|
||||
summedItem.used = 0
|
||||
summedItem.craftable = 0
|
||||
summed[key] = summedItem
|
||||
end
|
||||
|
||||
local total = count * iqty -- 4 * 2
|
||||
local used = math.min(summedItem.count, total) -- 5
|
||||
local need = total - used -- 3
|
||||
|
||||
if recipe.craftingTools and recipe.craftingTools[key] then
|
||||
if summedItem.count > 0 then
|
||||
summedItem.used = 1
|
||||
summedItem.need = 0
|
||||
need = 0
|
||||
elseif not summedItem.recipe then
|
||||
summedItem.need = 1
|
||||
need = 1
|
||||
else
|
||||
need = 1
|
||||
end
|
||||
else
|
||||
summedItem.count = summedItem.count - used
|
||||
summedItem.used = summedItem.used + used
|
||||
end
|
||||
|
||||
if need > 0 then
|
||||
if not summedItem.recipe then
|
||||
craftable = math.min(craftable, math.floor(used / iqty))
|
||||
summedItem.need = summedItem.need + need
|
||||
else
|
||||
local c = sumItems(summedItem.recipe, need) -- 4
|
||||
craftable = math.min(craftable, math.floor((used + c) / iqty))
|
||||
summedItem.craftable = summedItem.craftable + c
|
||||
end
|
||||
end
|
||||
end
|
||||
if craftable > 0 then
|
||||
craftable = Craft.craftRecipe(recipe, craftable * recipe.count,
|
||||
self.context.inventoryAdapter) / recipe.count
|
||||
self:clearGrid()
|
||||
end
|
||||
|
||||
return craftable * recipe.count
|
||||
end
|
||||
|
||||
local count = sumItems(inRecipe, inCount)
|
||||
|
||||
if count < inCount then
|
||||
for _,ingredient in pairs(summed) do
|
||||
if ingredient.need > 0 then
|
||||
local item = self:addCraftingRequest(ingredient, craftList, ingredient.need)
|
||||
if Craft.findRecipe(item) then
|
||||
item.status = string.format('%s missing', itemDB:getName(ingredient))
|
||||
item.statusCode = self.STATUS_WARNING
|
||||
else
|
||||
item.status = '(no recipe)'
|
||||
item.statusCode = self.STATUS_ERROR
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return count
|
||||
end
|
||||
|
||||
function Milo:craft(recipe, items, item, craftList)
|
||||
item.status = nil
|
||||
item.statusCode = nil
|
||||
item.crafted = 0
|
||||
|
||||
if self:isCraftingPaused() then
|
||||
return
|
||||
end
|
||||
|
||||
if not self:clearGrid() then
|
||||
item.status = 'Grid obstructed'
|
||||
item.statusCode = self.STATUS_ERROR
|
||||
return
|
||||
end
|
||||
|
||||
if item.forceCrafting then
|
||||
item.crafted = self:forceCraftItem(recipe, items, item, craftList, item.count)
|
||||
else
|
||||
item.crafted = self:craftItem(recipe, items, item, craftList, item.count)
|
||||
end
|
||||
end
|
||||
|
||||
function Milo:craftItems(craftList)
|
||||
for _,key in pairs(Util.keys(craftList)) do
|
||||
local item = craftList[key]
|
||||
if item.count > 0 then
|
||||
local recipe = Craft.recipes[key]
|
||||
if recipe then
|
||||
self:craft(recipe, self:listItems(), item, craftList)
|
||||
elseif not self.context.controllerAdapter then
|
||||
item.status = '(no recipe)'
|
||||
item.statusCode = self.STATUS_ERROR
|
||||
end
|
||||
end
|
||||
end
|
||||
self:updateCraftingStatus(craftList)
|
||||
for _,v in pairs(craftList) do
|
||||
debug(v)
|
||||
end
|
||||
end
|
||||
|
||||
return Milo
|
||||
216
milo/apis/networkedAdapter18.lua
Normal file
216
milo/apis/networkedAdapter18.lua
Normal file
@@ -0,0 +1,216 @@
|
||||
local class = require('class')
|
||||
local Util = require('util')
|
||||
local InventoryAdapter = require('inventoryAdapter')
|
||||
local Peripheral = require('peripheral')
|
||||
|
||||
local NetworkedAdapter = class()
|
||||
|
||||
function NetworkedAdapter:init(args)
|
||||
local defaults = {
|
||||
name = 'Networked Adapter',
|
||||
remotes = { },
|
||||
remoteDefaults = { },
|
||||
dirty = true,
|
||||
listCount = 0,
|
||||
activity = { },
|
||||
}
|
||||
Util.merge(self, defaults)
|
||||
Util.merge(self, args)
|
||||
|
||||
if not self.side or self.side == 'network' then
|
||||
self.modem = Peripheral.get('wired_modem')
|
||||
|
||||
if self.modem and self.modem.getNameLocal then
|
||||
self.localName = self.modem.getNameLocal()
|
||||
|
||||
for k in pairs(self.remoteDefaults) do
|
||||
local remote = Peripheral.get({ name = k })
|
||||
if remote and remote.size and remote.list then
|
||||
local adapter = InventoryAdapter.wrap({ side = k, direction = self.localName })
|
||||
if adapter then
|
||||
table.insert(self.remotes, adapter)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for _, remote in pairs(self.remotes) do
|
||||
Util.merge(remote, self.remoteDefaults[remote.side])
|
||||
end
|
||||
|
||||
table.sort(self.remotes, function(a, b)
|
||||
if not a.priority then
|
||||
return false
|
||||
elseif not b.priority then
|
||||
return true
|
||||
end
|
||||
return a.priority > b.priority
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
function NetworkedAdapter:isValid()
|
||||
return #self.remotes > 0
|
||||
end
|
||||
|
||||
function NetworkedAdapter:refresh(throttle)
|
||||
self.dirty = true
|
||||
return self:listItems(throttle)
|
||||
end
|
||||
|
||||
-- provide a consolidated list of items
|
||||
function NetworkedAdapter:listItems(throttle)
|
||||
if not self.dirty then
|
||||
return self.items
|
||||
end
|
||||
self.listCount = self.listCount + 1
|
||||
debug(self.listCount)
|
||||
|
||||
-- todo: only listItems from dirty remotes
|
||||
-- todo: better handling of empty inventories
|
||||
|
||||
local cache = { }
|
||||
local items = { }
|
||||
throttle = throttle or Util.throttle()
|
||||
|
||||
for _, remote in pairs(self.remotes) do
|
||||
if not remote:listItems(throttle) then
|
||||
debug('no List: ' .. remote.name)
|
||||
--error('Listing failed: ' .. remote.name)
|
||||
end
|
||||
local rcache = remote.cache or { }
|
||||
|
||||
-- TODO: add a method in each adapter that only updates a passed cache
|
||||
for key,v in pairs(rcache) do
|
||||
if v.count > 0 then
|
||||
local entry = cache[key]
|
||||
if not entry then
|
||||
entry = Util.shallowCopy(v)
|
||||
entry.count = v.count
|
||||
cache[key] = entry
|
||||
table.insert(items, entry)
|
||||
else
|
||||
entry.count = entry.count + v.count
|
||||
end
|
||||
|
||||
throttle()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
self.dirty = false
|
||||
self.cache = cache
|
||||
self.items = items
|
||||
return items
|
||||
end
|
||||
|
||||
function NetworkedAdapter:getItemInfo(item)
|
||||
if not self.cache then
|
||||
self:listItems()
|
||||
end
|
||||
local key = table.concat({ item.name, item.damage, item.nbtHash }, ':')
|
||||
local items = self.cache or { }
|
||||
return items[key]
|
||||
end
|
||||
|
||||
function NetworkedAdapter:provide(item, qty, slot, direction)
|
||||
local key = table.concat({ item.name, item.damage, item.nbtHash }, ':')
|
||||
local total = 0
|
||||
|
||||
for _, remote in ipairs(self.remotes) do
|
||||
debug('%s -> slot %d: %d %s', remote.side, slot or -1, qty, item.name)
|
||||
local amount = remote:provide(item, qty, slot, direction)
|
||||
if amount > 0 then
|
||||
self.dirty = true
|
||||
remote.dirty = true
|
||||
local entry = self.activity[key] or 0
|
||||
self.activity[key] = entry + amount
|
||||
end
|
||||
qty = qty - amount
|
||||
total = total + amount
|
||||
if qty <= 0 then
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
return total
|
||||
end
|
||||
|
||||
function NetworkedAdapter:extract(slot, qty, toSlot)
|
||||
|
||||
error('extract not supported')
|
||||
local total = 0
|
||||
for _, remote in pairs(self.remotes) do
|
||||
debug('extract %d slot:%d', qty, slot)
|
||||
local amount = remote:extract(slot, qty, toSlot)
|
||||
qty = qty - amount
|
||||
total = total + amount
|
||||
if qty <= 0 then
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
return total
|
||||
end
|
||||
|
||||
function NetworkedAdapter:insert(slot, qty, toSlot, item, source)
|
||||
local total = 0
|
||||
|
||||
-- toSlot is not really valid with this adapter
|
||||
if toSlot then
|
||||
error('NetworkedAdapter: toSlot is not valid')
|
||||
end
|
||||
|
||||
local key = table.concat({ item.name, item.damage, item.nbtHash }, ':')
|
||||
|
||||
if not self.cache then
|
||||
self:listItems()
|
||||
end
|
||||
|
||||
local function insert(remote)
|
||||
local amount = remote:insert(slot, qty, toSlot, source or self.direction)
|
||||
if amount > 0 then
|
||||
debug('%s(%d) -> %s: %d', source or self.localName, slot, remote.side, amount)
|
||||
self.dirty = true
|
||||
remote.dirty = true
|
||||
local entry = self.activity[key] or 0
|
||||
self.activity[key] = entry + amount
|
||||
end
|
||||
qty = qty - amount
|
||||
total = total + amount
|
||||
end
|
||||
|
||||
-- found a chest locked with this item
|
||||
for _, remote in pairs(self.remotes) do
|
||||
if remote.lockWith == key or remote.lockWith == item.name then
|
||||
insert(remote)
|
||||
return total
|
||||
end
|
||||
end
|
||||
|
||||
if self.cache[key] then -- is this item in some chest
|
||||
-- low to high priority if the chest already contains that item
|
||||
for _, remote in Util.rpairs(self.remotes) do
|
||||
if qty <= 0 then
|
||||
break
|
||||
end
|
||||
if remote.cache and remote.cache[key] and not remote.lockWith then
|
||||
insert(remote)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- high to low priority
|
||||
for _, remote in ipairs(self.remotes) do
|
||||
if qty <= 0 then
|
||||
break
|
||||
end
|
||||
if not remote.lockWith then
|
||||
insert(remote)
|
||||
end
|
||||
end
|
||||
|
||||
return total
|
||||
end
|
||||
|
||||
return NetworkedAdapter
|
||||
351
milo/apis/turtle/craft.lua
Normal file
351
milo/apis/turtle/craft.lua
Normal file
@@ -0,0 +1,351 @@
|
||||
local itemDB = require('itemDB')
|
||||
local Util = require('util')
|
||||
|
||||
local device = _G.device
|
||||
local fs = _G.fs
|
||||
local turtle = _G.turtle
|
||||
|
||||
local RECIPES_DIR = 'usr/etc/recipes'
|
||||
local USER_RECIPES = 'usr/config/recipes.db'
|
||||
local MACHINE_LOOKUP = 'usr/config/machine_crafting.db'
|
||||
|
||||
local Craft = { }
|
||||
|
||||
local function clearGrid(inventoryAdapter)
|
||||
turtle.eachFilledSlot(function(slot)
|
||||
inventoryAdapter:insert(slot.index, slot.count, nil, slot)
|
||||
end)
|
||||
|
||||
for i = 1, 16 do
|
||||
if turtle.getItemCount(i) ~= 0 then
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
return true
|
||||
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
|
||||
end
|
||||
|
||||
function Craft.getItemCount(items, item)
|
||||
if type(item) == 'string' then
|
||||
item = splitKey(item)
|
||||
end
|
||||
|
||||
local count = 0
|
||||
for _,v in pairs(items) do
|
||||
if v.name == item.name and
|
||||
(not item.damage or v.damage == item.damage) and
|
||||
v.nbtHash == item.nbtHash then
|
||||
if item.damage then
|
||||
return v.count
|
||||
end
|
||||
count = count + v.count
|
||||
end
|
||||
end
|
||||
return count
|
||||
end
|
||||
|
||||
local function machineCraft(recipe, qty, inventoryAdapter, machineName)
|
||||
local machine = device[machineName]
|
||||
if not machine then
|
||||
debug('machine not found')
|
||||
else
|
||||
for k in pairs(recipe.ingredients) do
|
||||
if machine.getItemMeta(k) then
|
||||
debug('machine in use: ' .. k)
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
for k,v in pairs(recipe.ingredients) do
|
||||
inventoryAdapter:provide(splitKey(v), qty, k, machineName)
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
local function turtleCraft(recipe, qty, inventoryAdapter)
|
||||
if not clearGrid(inventoryAdapter) then
|
||||
return false
|
||||
end
|
||||
|
||||
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
|
||||
]]--
|
||||
inventoryAdapter:provide(item, provideQty, k)
|
||||
if turtle.getItemCount(k) == 0 then -- ~= qty then
|
||||
-- FIX: ingredients cannot be stacked
|
||||
--debug('failed ' .. v .. ' - ' .. provideQty)
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
return turtle.craft()
|
||||
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)
|
||||
if type(recipe) == 'string' then
|
||||
recipe = Craft.recipes[recipe]
|
||||
if not recipe then
|
||||
return 0, 'No recipe'
|
||||
end
|
||||
end
|
||||
|
||||
local items = inventoryAdapter:listItems()
|
||||
if not items then
|
||||
return 0, 'Inventory changed'
|
||||
end
|
||||
|
||||
count = math.ceil(count / recipe.count)
|
||||
local maxCount = recipe.maxCount or math.floor(64 / recipe.count)
|
||||
|
||||
for key,icount in pairs(Craft.sumIngredients(recipe)) do
|
||||
local itemCount = Craft.getItemCount(items, key)
|
||||
local need = icount * count
|
||||
if recipe.craftingTools and recipe.craftingTools[key] then
|
||||
need = 1
|
||||
end
|
||||
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)
|
||||
if crafted ~= iqty then
|
||||
turtle.select(1)
|
||||
return 0
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local crafted = 0
|
||||
repeat
|
||||
-- fix
|
||||
if Craft.machineLookup[recipe.result] then
|
||||
machineCraft(recipe, math.min(count, maxCount), inventoryAdapter, Craft.machineLookup[recipe.result])
|
||||
break
|
||||
elseif not turtleCraft(recipe, math.min(count, maxCount), inventoryAdapter) then
|
||||
turtle.select(1)
|
||||
break
|
||||
end
|
||||
crafted = crafted + math.min(count, maxCount)
|
||||
count = count - maxCount
|
||||
until count <= 0
|
||||
|
||||
turtle.select(1)
|
||||
return crafted * recipe.count
|
||||
end
|
||||
|
||||
function Craft.findRecipe(key)
|
||||
if type(key) ~= 'string' then
|
||||
key = itemDB:makeKey(key)
|
||||
end
|
||||
|
||||
local item = itemDB:splitKey(key)
|
||||
if item.damage then
|
||||
return Craft.recipes[makeRecipeKey(item)]
|
||||
end
|
||||
|
||||
-- handle cases where the request is like : IC2:reactorVent:*
|
||||
for rkey,recipe in pairs(Craft.recipes) do
|
||||
local r = itemDB:splitKey(rkey)
|
||||
if item.name == r.name and
|
||||
(not item.nbtHash or r.nbtHash == item.nbtHash) then
|
||||
return recipe
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- determine the full list of ingredients needed to craft
|
||||
-- a quantity of a recipe.
|
||||
function Craft.getResourceList(inRecipe, items, inCount)
|
||||
local summed = { }
|
||||
|
||||
local function sumItems(recipe, key, count)
|
||||
local item = itemDB:splitKey(key)
|
||||
local summedItem = summed[key]
|
||||
if not summedItem then
|
||||
summedItem = Util.shallowCopy(item)
|
||||
summedItem.recipe = Craft.findRecipe(key)
|
||||
summedItem.count = Craft.getItemCount(items, item)
|
||||
summedItem.displayName = itemDB:getName(item)
|
||||
summedItem.total = 0
|
||||
summedItem.need = 0
|
||||
summedItem.used = 0
|
||||
summed[key] = summedItem
|
||||
end
|
||||
local total = count
|
||||
local used = math.min(summedItem.count, total)
|
||||
local need = total - used
|
||||
|
||||
if recipe.craftingTools and recipe.craftingTools[key] then
|
||||
summedItem.total = 1
|
||||
if summedItem.count > 0 then
|
||||
summedItem.used = 1
|
||||
summedItem.need = 0
|
||||
need = 0
|
||||
elseif not summedItem.recipe then
|
||||
summedItem.need = 1
|
||||
need = 1
|
||||
else
|
||||
need = 1
|
||||
end
|
||||
else
|
||||
summedItem.total = summedItem.total + total
|
||||
summedItem.count = summedItem.count - used
|
||||
summedItem.used = summedItem.used + used
|
||||
if not summedItem.recipe then
|
||||
summedItem.need = summedItem.need + need
|
||||
end
|
||||
end
|
||||
|
||||
if need > 0 and summedItem.recipe then
|
||||
need = math.ceil(need / summedItem.recipe.count)
|
||||
for ikey,iqty in pairs(Craft.sumIngredients(summedItem.recipe)) do
|
||||
sumItems(summedItem.recipe, ikey, math.ceil(need * iqty))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
inCount = math.ceil(inCount / inRecipe.count)
|
||||
for ikey,iqty in pairs(Craft.sumIngredients(inRecipe)) do
|
||||
sumItems(inRecipe, ikey, math.ceil(inCount * iqty))
|
||||
end
|
||||
|
||||
return summed
|
||||
end
|
||||
|
||||
function Craft.getResourceList4(inRecipe, items, count)
|
||||
local summed = Craft.getResourceList(inRecipe, items, count)
|
||||
-- filter down to just raw materials
|
||||
return Util.filter(summed, function(a) return a.used > 0 or a.need > 0 end)
|
||||
end
|
||||
|
||||
-- given a certain quantity, return how many of those can be crafted
|
||||
function Craft.getCraftableAmount(inRecipe, count, items, missing)
|
||||
local function sumItems(recipe, summedItems, count)
|
||||
local canCraft = 0
|
||||
|
||||
for _ = 1, count do
|
||||
for _,item in pairs(recipe.ingredients) do
|
||||
local summedItem = summedItems[item] or Craft.getItemCount(items, item)
|
||||
|
||||
local irecipe = Craft.findRecipe(item)
|
||||
if irecipe and summedItem <= 0 then
|
||||
summedItem = summedItem + sumItems(irecipe, summedItems, 1)
|
||||
end
|
||||
if summedItem <= 0 then
|
||||
if missing and not irecipe then
|
||||
missing.name = item
|
||||
end
|
||||
return canCraft
|
||||
end
|
||||
if not recipe.craftingTools or not recipe.craftingTools[item] then
|
||||
summedItems[item] = summedItem - 1
|
||||
end
|
||||
end
|
||||
canCraft = canCraft + recipe.count
|
||||
end
|
||||
|
||||
return canCraft
|
||||
end
|
||||
|
||||
return sumItems(inRecipe, { }, math.ceil(count / inRecipe.count))
|
||||
end
|
||||
|
||||
function Craft.canCraft(item, count, items)
|
||||
return Craft.getCraftableAmount(Craft.recipes[item], count, items) == count
|
||||
end
|
||||
|
||||
function Craft.setRecipes(recipes)
|
||||
Craft.recipes = recipes
|
||||
end
|
||||
|
||||
function Craft.getCraftableAmountTest()
|
||||
local results = { }
|
||||
Craft.setRecipes(Util.readTable('usr/etc/recipes.db'))
|
||||
|
||||
local items = {
|
||||
{ name = 'minecraft:planks', damage = 0, count = 5 },
|
||||
{ name = 'minecraft:log', damage = 0, count = 2 },
|
||||
}
|
||||
results[1] = { item = 'chest', expected = 1,
|
||||
got = Craft.getCraftableAmount(Craft.recipes['minecraft:chest:0'], 2, items) }
|
||||
|
||||
items = {
|
||||
{ name = 'minecraft:log', damage = 0, count = 1 },
|
||||
{ name = 'minecraft:coal', damage = 1, count = 1 },
|
||||
}
|
||||
results[2] = { item = 'torch', expected = 4,
|
||||
got = Craft.getCraftableAmount(Craft.recipes['minecraft:torch:0'], 4, items) }
|
||||
|
||||
return results
|
||||
end
|
||||
|
||||
function Craft.craftRecipeTest(name, count)
|
||||
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, chestAdapter) }
|
||||
end
|
||||
|
||||
Craft.loadRecipes()
|
||||
|
||||
return Craft
|
||||
Reference in New Issue
Block a user