plethora crafter rename to milo - wip
This commit is contained in:
12
milo/.package
Normal file
12
milo/.package
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
required = {
|
||||
'opus-develop-1.8',
|
||||
},
|
||||
title = 'Inventory manager for Opus OS',
|
||||
description = [[
|
||||
Unstable Branch
|
||||
]],
|
||||
licence = 'MIT',
|
||||
--location = '',
|
||||
--mount = 'packages/opus-neural gitfs kepler155c/opus-neural/master',
|
||||
}
|
||||
187
milo/Milo.lua
Normal file
187
milo/Milo.lua
Normal file
@@ -0,0 +1,187 @@
|
||||
--[[
|
||||
Provides: autocrafting, resource limits, on-demand crafting, storage stocker.
|
||||
|
||||
Using a turtle allows for crafting of items eliminating the need for AE/RS
|
||||
molecular assemblers / crafters.
|
||||
|
||||
Inventory setup:
|
||||
Turtle/computer must be touching at least one type of inventory
|
||||
|
||||
Generic inventory block such as:
|
||||
Vanilla chest
|
||||
RFTools modular storage
|
||||
Storage drawers controller
|
||||
and many others...
|
||||
|
||||
Applied energistics
|
||||
AE cable or interface (depending upon AE/MC version)
|
||||
|
||||
Refined storage
|
||||
TODO: add required block
|
||||
|
||||
Turtle crafting:
|
||||
1. The turtle must have a crafting table equipped.
|
||||
2. Equip the turtle with an introspection module.
|
||||
|
||||
Controller (optional):
|
||||
Provides the ability to request crafting from AE / RS
|
||||
|
||||
Applied Energistics
|
||||
In versions 1.7x, AE can be used for both inventory access and crafting
|
||||
requests.
|
||||
|
||||
In versions 1.8+, AE can only be used to request crafting.
|
||||
|
||||
Refined Storage
|
||||
In versions 1.8x, inventory access works depending upon version.
|
||||
|
||||
Turtle/computer must be touching an interface for inventory access. If only
|
||||
requesting crafting, the controller must be either be touching or connected
|
||||
via CC cables.
|
||||
|
||||
Configuration:
|
||||
Configuration file is usr/config/milo
|
||||
|
||||
monitor : valid options include:
|
||||
type/monitor - will use the first monitor found
|
||||
side/north - specify a direction (top/bottom/east/etc)
|
||||
name/monitor_1 - specify the exact name of the peripheral
|
||||
|
||||
|
||||
|
||||
-- Internal
|
||||
Imports are at < 20
|
||||
|
||||
]]--
|
||||
|
||||
--[[
|
||||
limit
|
||||
organize
|
||||
replenish
|
||||
autocraft
|
||||
]]
|
||||
|
||||
_G.requireInjector()
|
||||
|
||||
local Config = require('config')
|
||||
local Event = require('event')
|
||||
local itemDB = require('itemDB')
|
||||
local Milo = require('milo')
|
||||
local Peripheral = require('peripheral')
|
||||
local UI = require('ui')
|
||||
local Util = require('util')
|
||||
|
||||
local InventoryAdapter = require('inventoryAdapter')
|
||||
|
||||
local fs = _G.fs
|
||||
local multishell = _ENV.multishell
|
||||
local shell = _ENV.shell
|
||||
|
||||
if multishell then
|
||||
multishell.setTitle(multishell.getCurrent(), 'Milo')
|
||||
end
|
||||
|
||||
local config = {
|
||||
monitor = 'type/monitor',
|
||||
remoteDefaults = { },
|
||||
}
|
||||
Config.load('milo', config)
|
||||
|
||||
local modem = Peripheral.get('wired_modem')
|
||||
if not modem or not modem.getNameLocal then
|
||||
error('Wired modem is not connected')
|
||||
end
|
||||
|
||||
local storage = { }
|
||||
for k,v in pairs(config.remoteDefaults) do
|
||||
if v.mtype == 'storage' then
|
||||
storage[k] = v
|
||||
elseif v.mtype == 'controller' then
|
||||
-- TODO: look for controller
|
||||
end
|
||||
end
|
||||
|
||||
local inventoryAdapter = InventoryAdapter.wrap({ remoteDefaults = storage })
|
||||
if not inventoryAdapter then
|
||||
error('Invalid inventory configuration')
|
||||
end
|
||||
|
||||
-- TODO: cleanup
|
||||
for _, v in pairs(modem.getNamesRemote()) do
|
||||
local remote = Peripheral.get({ name = v })
|
||||
if remote.pullItems then
|
||||
if not config.remoteDefaults[v] then
|
||||
config.remoteDefaults[v] = {
|
||||
name = v,
|
||||
mtype = 'ignore',
|
||||
}
|
||||
else
|
||||
config.remoteDefaults[v].name = v
|
||||
end
|
||||
if not config.remoteDefaults[v].mtype then
|
||||
config.remoteDefaults[v].mtype = 'ignore'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function loadResources()
|
||||
local resources = Util.readTable(Milo.RESOURCE_FILE) or { }
|
||||
for k,v in pairs(resources) do
|
||||
Util.merge(v, itemDB:splitKey(k))
|
||||
end
|
||||
|
||||
return resources
|
||||
end
|
||||
|
||||
local context = {
|
||||
config = config,
|
||||
inventoryAdapter = inventoryAdapter,
|
||||
resources = loadResources(),
|
||||
userRecipes = Util.readTable(Milo.RECIPES_FILE) or { },
|
||||
learnTypes = { },
|
||||
machineTypes = { },
|
||||
}
|
||||
|
||||
Milo:init(context)
|
||||
|
||||
local function loadDirectory(dir)
|
||||
for _, file in pairs(fs.list(dir)) do
|
||||
local s, m = Util.run(_ENV, fs.combine(dir, file))
|
||||
if not s and m then
|
||||
error(m or 'Unknown error')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local programDir = fs.getDir(shell.getRunningProgram())
|
||||
loadDirectory(fs.combine(programDir, 'core'))
|
||||
loadDirectory(fs.combine(programDir, 'plugins'))
|
||||
|
||||
table.sort(Milo.tasks, function(a, b)
|
||||
return a.priority < b.priority
|
||||
end)
|
||||
|
||||
Milo:clearGrid()
|
||||
|
||||
local page = UI:getPage('listing')
|
||||
UI:setPage(page)
|
||||
page:setFocus(page.statusBar.filter)
|
||||
|
||||
-- TODO: Event.on('device_detach', function() end)
|
||||
|
||||
Event.onInterval(5, function()
|
||||
if not Milo:isCraftingPaused() then
|
||||
Milo:resetCraftingStatus()
|
||||
context.inventoryAdapter:refresh()
|
||||
|
||||
for _, task in ipairs(Milo.tasks) do
|
||||
local s, m = pcall(function() task:cycle(context) end)
|
||||
if not s and m then
|
||||
Util.print(task)
|
||||
error(m)
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
UI:pullEvents()
|
||||
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
|
||||
230
milo/core/machines.lua
Normal file
230
milo/core/machines.lua
Normal file
@@ -0,0 +1,230 @@
|
||||
local Config = require('config')
|
||||
local itemDB = require('itemDB')
|
||||
local Milo = require('milo')
|
||||
local UI = require('ui')
|
||||
local Util = require('util')
|
||||
|
||||
local colors = _G.colors
|
||||
local device = _G.device
|
||||
|
||||
local context = Milo:getContext()
|
||||
|
||||
local machinesPage = UI.Page {
|
||||
titleBar = UI.TitleBar {
|
||||
previousPage = true,
|
||||
title = 'Machines',
|
||||
},
|
||||
grid = UI.ScrollingGrid {
|
||||
y = 2, ey = -2,
|
||||
values = context.config.remoteDefaults,
|
||||
columns = {
|
||||
{ heading = 'Name', key = 'displayName' },
|
||||
{ heading = 'Priority', key = 'priority', width = 5 },
|
||||
{ heading = 'Type', key = 'mtype', width = 5 },
|
||||
},
|
||||
sortColumn = 'displayName',
|
||||
},
|
||||
statusBar = UI.StatusBar {
|
||||
values = 'Select Machine',
|
||||
},
|
||||
}
|
||||
|
||||
function machinesPage:enable()
|
||||
self.grid:update()
|
||||
UI.Page.enable(self)
|
||||
end
|
||||
|
||||
function machinesPage.grid:getDisplayValues(row)
|
||||
row = Util.shallowCopy(row)
|
||||
row.displayName = row.displayName or row.name
|
||||
return row
|
||||
end
|
||||
|
||||
function machinesPage.grid:getRowTextColor(row, selected)
|
||||
if row.mtype == 'ignore' then
|
||||
return colors.lightGray
|
||||
end
|
||||
return UI.Grid:getRowTextColor(row, selected)
|
||||
end
|
||||
|
||||
function machinesPage:eventHandler(event)
|
||||
if event.type == 'grid_select' then
|
||||
UI:setPage('machineWizard', event.selected)
|
||||
else
|
||||
UI.Page.eventHandler(self, event)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local machineWizard = UI.Page {
|
||||
titleBar = UI.TitleBar { title = 'Configure' },
|
||||
wizard = UI.Wizard {
|
||||
y = 2, ey = -2,
|
||||
pages = {
|
||||
general = UI.Window {
|
||||
index = 1,
|
||||
backgroundColor = colors.cyan,
|
||||
form = UI.Form {
|
||||
x = 1, y = 1, ex = -1, ey = 3,
|
||||
manualControls = true,
|
||||
[1] = UI.TextEntry {
|
||||
formLabel = 'Name', formKey = 'displayName',
|
||||
help = 'Set a friendly name',
|
||||
limit = 64, pruneEmpty = true,
|
||||
},
|
||||
[2] = UI.Chooser {
|
||||
width = 15,
|
||||
formLabel = 'Type', formKey = 'mtype',
|
||||
nochoice = 'Storage',
|
||||
choices = {
|
||||
{ name = 'Storage', value = 'storage' },
|
||||
{ name = 'Trashcan', value = 'trashcan' },
|
||||
{ name = 'Input chest', value = 'input' },
|
||||
{ name = 'Ignore', value = 'ignore' },
|
||||
{ name = 'Machine', value = 'machine' },
|
||||
},
|
||||
help = 'Select type',
|
||||
},
|
||||
},
|
||||
grid = UI.ScrollingGrid {
|
||||
y = 5, ey = -2, x = 2, ex = -2,
|
||||
columns = {
|
||||
{ heading = 'Slot', key = 'slot', width = 4 },
|
||||
{ heading = 'Name', key = 'displayName', },
|
||||
{ heading = 'Qty', key = 'count' , width = 3 },
|
||||
},
|
||||
sortColumn = 'slot',
|
||||
help = 'Contents of inventory',
|
||||
},
|
||||
},
|
||||
confirmation = UI.Window {
|
||||
title = 'Confirm changes',
|
||||
index = 2,
|
||||
notice = UI.TextArea {
|
||||
x = 2, ex = -2, y = 2, ey = -2,
|
||||
value =
|
||||
[[Press accept to save the changes.
|
||||
|
||||
The settings will take effect immediately!]],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
statusBar = UI.StatusBar {
|
||||
backgroundColor = colors.cyan,
|
||||
},
|
||||
notification = UI.Notification { },
|
||||
}
|
||||
|
||||
function machineWizard.wizard.pages.general:enable()
|
||||
UI.Window.enable(self)
|
||||
self:focusFirst()
|
||||
end
|
||||
|
||||
function machineWizard.wizard.pages.general:setMachine(machine)
|
||||
local inventory
|
||||
|
||||
if device[machine.name] and device[machine.name].list then
|
||||
inventory = device[machine.name].list()
|
||||
for k,v in pairs(inventory) do
|
||||
v.slot = k
|
||||
end
|
||||
end
|
||||
|
||||
self.grid:setValues(inventory or { })
|
||||
end
|
||||
|
||||
function machineWizard.wizard.pages.general.grid:getDisplayValues(row)
|
||||
row = Util.shallowCopy(row)
|
||||
row.displayName = itemDB:getName(row)
|
||||
return row
|
||||
end
|
||||
|
||||
function machineWizard.wizard.pages.general:validate()
|
||||
return self.form:save()
|
||||
end
|
||||
|
||||
function machineWizard.wizard:eventHandler(event)
|
||||
if event.type == 'nextView' and
|
||||
Util.find(self.pages, 'enabled', true) == self.pages.general then
|
||||
|
||||
if self.pages.general.form:save() then
|
||||
local index = 2
|
||||
for _, page in pairs(self.pages) do
|
||||
if page.mtype == machineWizard.machine.mtype then
|
||||
page.index = index
|
||||
index = index + 1
|
||||
elseif page.index ~= 1 then
|
||||
page.index = nil
|
||||
end
|
||||
end
|
||||
self.pages.confirmation.index = index
|
||||
return UI.Wizard.eventHandler(self, event)
|
||||
end
|
||||
else
|
||||
return UI.Wizard.eventHandler(self, event)
|
||||
end
|
||||
end
|
||||
|
||||
function machineWizard:enable(machine)
|
||||
self.machine = Util.deepCopy(machine)
|
||||
self.wizard.pages.general.form:setValues(self.machine)
|
||||
self.wizard.pages.general.form[1].shadowText = machine.name
|
||||
|
||||
-- restore indices
|
||||
for _, page in pairs(self.wizard.pages) do
|
||||
if not page.oindex then
|
||||
page.oindex = page.index
|
||||
end
|
||||
page.index = page.oindex
|
||||
end
|
||||
|
||||
UI.Page.enable(self)
|
||||
|
||||
for _, v in pairs(self.wizard.pages) do
|
||||
if v.setMachine then
|
||||
v:setMachine(self.machine)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function machineWizard:eventHandler(event)
|
||||
if event.type == 'cancel' then
|
||||
UI:setPreviousPage()
|
||||
|
||||
elseif event.type == 'accept' then
|
||||
|
||||
-- todo: no need for calling this function - use validate instead
|
||||
for _, v in pairs(self.wizard.pages) do
|
||||
if v.save and v.index then -- only save if the page was valid for this mtype
|
||||
v:save(self.machine)
|
||||
end
|
||||
end
|
||||
context.config.remoteDefaults[self.machine.name] = self.machine
|
||||
Config.update('milo', context.config)
|
||||
|
||||
UI:setPreviousPage()
|
||||
|
||||
elseif event.type == 'collapse' then
|
||||
self.items:hide()
|
||||
|
||||
elseif event.type == 'enable_view' then
|
||||
local current = event.next or event.prev
|
||||
self.titleBar.title = current.title or 'Machine'
|
||||
self.titleBar:draw()
|
||||
|
||||
elseif event.type == 'focus_change' then
|
||||
self.statusBar:setStatus(event.focused.help)
|
||||
|
||||
elseif event.type == 'form_invalid' then
|
||||
self.notification:error(event.message)
|
||||
self:setFocus(event.field)
|
||||
|
||||
else
|
||||
return UI.Page.eventHandler(self, event)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
UI:addPage('machines', machinesPage)
|
||||
UI:addPage('machineWizard', machineWizard)
|
||||
24
milo/plugins/autocraftTask.lua
Normal file
24
milo/plugins/autocraftTask.lua
Normal file
@@ -0,0 +1,24 @@
|
||||
local Milo = require('milo')
|
||||
local Util = require('util')
|
||||
|
||||
local Autocraft = {
|
||||
priority = 100,
|
||||
}
|
||||
|
||||
function Autocraft:cycle(context)
|
||||
local list = { }
|
||||
|
||||
for _,res in pairs(context.resources) do
|
||||
if res.auto then
|
||||
res = Util.shallowCopy(res)
|
||||
res.count = 256
|
||||
list[Milo:uniqueKey(res)] = res
|
||||
end
|
||||
end
|
||||
|
||||
if not Util.empty(list) then
|
||||
Milo:craftItems(list)
|
||||
end
|
||||
end
|
||||
|
||||
Milo:registerTask(Autocraft)
|
||||
177
milo/plugins/demandCraft.lua
Normal file
177
milo/plugins/demandCraft.lua
Normal file
@@ -0,0 +1,177 @@
|
||||
local Craft = require('turtle.craft')
|
||||
local itemDB = require('itemDB')
|
||||
local Milo = require('milo')
|
||||
local UI = require('ui')
|
||||
local Util = require('util')
|
||||
|
||||
local colors = _G.colors
|
||||
|
||||
local demandCrafting = { }
|
||||
|
||||
local craftPage = UI.Page {
|
||||
titleBar = UI.TitleBar { },
|
||||
wizard = UI.Wizard {
|
||||
y = 2, ey = -2,
|
||||
pages = {
|
||||
quantity = UI.Window {
|
||||
index = 1,
|
||||
text = UI.Text {
|
||||
x = 6, y = 3,
|
||||
value = 'Quantity',
|
||||
},
|
||||
count = UI.TextEntry {
|
||||
x = 15, y = 3, width = 10,
|
||||
limit = 6,
|
||||
value = 1,
|
||||
},
|
||||
ejectText = UI.Text {
|
||||
x = 6, y = 4,
|
||||
value = 'Eject',
|
||||
},
|
||||
eject = UI.Chooser {
|
||||
x = 15, y = 4, width = 7,
|
||||
value = true,
|
||||
nochoice = 'No',
|
||||
choices = {
|
||||
{ name = 'Yes', value = true },
|
||||
{ name = 'No', value = false },
|
||||
},
|
||||
},
|
||||
},
|
||||
resources = UI.Window {
|
||||
index = 2,
|
||||
grid = UI.ScrollingGrid {
|
||||
y = 2, ey = -2,
|
||||
columns = {
|
||||
{ heading = 'Name', key = 'displayName' },
|
||||
{ heading = 'Total', key = 'total' , width = 5 },
|
||||
{ heading = 'Used', key = 'used' , width = 5 },
|
||||
{ heading = 'Need', key = 'need' , width = 5 },
|
||||
},
|
||||
sortColumn = 'displayName',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
function craftPage:enable(item)
|
||||
self.item = item
|
||||
self:focusFirst()
|
||||
self.titleBar.title = itemDB:getName(item)
|
||||
-- self.wizard.pages.quantity.eject.value = true
|
||||
UI.Page.enable(self)
|
||||
end
|
||||
|
||||
function craftPage.wizard.pages.resources.grid:getDisplayValues(row)
|
||||
local function dv(v)
|
||||
if v == 0 then
|
||||
return ''
|
||||
end
|
||||
return Util.toBytes(v)
|
||||
end
|
||||
row = Util.shallowCopy(row)
|
||||
row.total = Util.toBytes(row.total)
|
||||
row.used = dv(row.used)
|
||||
row.need = dv(row.need)
|
||||
return row
|
||||
end
|
||||
|
||||
function craftPage.wizard.pages.resources.grid:getRowTextColor(row, selected)
|
||||
if row.need > 0 then
|
||||
return colors.orange
|
||||
end
|
||||
return UI.Grid:getRowTextColor(row, selected)
|
||||
end
|
||||
|
||||
function craftPage.wizard:eventHandler(event)
|
||||
if event.type == 'nextView' then
|
||||
local count = tonumber(self.pages.quantity.count.value)
|
||||
if not count or count <= 0 then
|
||||
self.pages.quantity.count.backgroundColor = colors.red
|
||||
self.pages.quantity.count:draw()
|
||||
return false
|
||||
end
|
||||
self.pages.quantity.count.backgroundColor = colors.black
|
||||
end
|
||||
return UI.Wizard.eventHandler(self, event)
|
||||
end
|
||||
|
||||
function craftPage.wizard.pages.resources:enable()
|
||||
local items = Milo:listItems()
|
||||
local count = tonumber(self.parent.quantity.count.value)
|
||||
local recipe = Craft.findRecipe(craftPage.item)
|
||||
if recipe then
|
||||
local ingredients = Craft.getResourceList4(recipe, items, count)
|
||||
for _,v in pairs(ingredients) do
|
||||
v.displayName = itemDB:getName(v)
|
||||
end
|
||||
self.grid:setValues(ingredients)
|
||||
else
|
||||
self.grid:setValues({ })
|
||||
end
|
||||
return UI.Window.enable(self)
|
||||
end
|
||||
|
||||
function craftPage:eventHandler(event)
|
||||
if event.type == 'cancel' then
|
||||
UI:setPreviousPage()
|
||||
|
||||
elseif event.type == 'accept' then
|
||||
local key = Milo:uniqueKey(self.item)
|
||||
demandCrafting[key] = Util.shallowCopy(self.item)
|
||||
demandCrafting[key].count = tonumber(self.wizard.pages.quantity.count.value)
|
||||
demandCrafting[key].ocount = demandCrafting[key].count
|
||||
demandCrafting[key].forceCrafting = true
|
||||
demandCrafting[key].eject = self.wizard.pages.quantity.eject.value == true
|
||||
UI:setPreviousPage()
|
||||
else
|
||||
return UI.Page.eventHandler(self, event)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local demandCraftingTask = {
|
||||
priority = 20,
|
||||
}
|
||||
|
||||
function demandCraftingTask:cycle(context)
|
||||
local demandCrafted = { }
|
||||
|
||||
-- look directly at the adapter import activity to determine
|
||||
-- if the item was imported into storage from any source.
|
||||
-- The item does NOT need to come from the machine that did
|
||||
-- the crafting.
|
||||
for _,key in pairs(Util.keys(demandCrafting)) do
|
||||
local item = demandCrafting[key]
|
||||
|
||||
local imported = context.inventoryAdapter.activity[key]
|
||||
if imported then
|
||||
item.crafted = math.min(imported, item.count)
|
||||
item.count = math.max(0, item.count - item.crafted)
|
||||
context.inventoryAdapter.activity[key] = imported - item.crafted
|
||||
end
|
||||
demandCrafted[key] = item
|
||||
end
|
||||
|
||||
if Util.size(demandCrafted) > 0 then
|
||||
Milo:craftItems(demandCrafted)
|
||||
end
|
||||
|
||||
for _,key in pairs(Util.keys(demandCrafting)) do
|
||||
local item = demandCrafting[key]
|
||||
if item.crafted then
|
||||
item.count = math.max(0, item.count - item.crafted)
|
||||
if item.count <= 0 then
|
||||
item.statusCode = 'success'
|
||||
demandCrafting[key] = nil
|
||||
if item.eject then
|
||||
Milo:eject(item, item.ocount)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
UI:addPage('craft', craftPage)
|
||||
Milo:registerTask(demandCraftingTask)
|
||||
31
milo/plugins/exportTask.lua
Normal file
31
milo/plugins/exportTask.lua
Normal file
@@ -0,0 +1,31 @@
|
||||
local itemDB = require('itemDB')
|
||||
local Milo = require('milo')
|
||||
|
||||
local device = _G.device
|
||||
|
||||
local ExportTask = {
|
||||
priority = 5,
|
||||
}
|
||||
|
||||
function ExportTask:cycle(context)
|
||||
for target, v in pairs(context.config.remoteDefaults) do
|
||||
if v.exports then
|
||||
local machine = device[target]
|
||||
if machine and machine.getItemMeta then
|
||||
for _, entry in pairs(v.exports) do
|
||||
local slot = machine.getItemMeta(entry.slot) or { count = 0 }
|
||||
local maxCount = slot.maxCount or itemDB:getMaxCount(entry.name)
|
||||
local count = maxCount - slot.count
|
||||
if count > 0 then
|
||||
context.inventoryAdapter:provide(
|
||||
itemDB:splitKey(entry.name), count, entry.slot, target)
|
||||
end
|
||||
end
|
||||
else
|
||||
debug('Invalid export target: ' .. target)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Milo:registerTask(ExportTask)
|
||||
213
milo/plugins/exportView.lua
Normal file
213
milo/plugins/exportView.lua
Normal file
@@ -0,0 +1,213 @@
|
||||
local itemDB = require('itemDB')
|
||||
local Milo = require('milo')
|
||||
local UI = require('ui')
|
||||
local Util = require('util')
|
||||
|
||||
local colors = _G.colors
|
||||
local device = _G.device
|
||||
|
||||
local itemSlideout = UI.SlideOut {
|
||||
backgroundColor = colors.cyan,
|
||||
menuBar = UI.MenuBar {
|
||||
buttons = {
|
||||
{ text = 'Save', event = 'save' },
|
||||
{ text = 'Cancel', event = 'cancel' },
|
||||
{ text = 'Refresh', event = 'refresh', x = -9 },
|
||||
},
|
||||
},
|
||||
grid = UI.ScrollingGrid {
|
||||
y = 2, ey = -6,
|
||||
columns = {
|
||||
{ heading = 'Name', key = 'displayName', width = 31 },
|
||||
{ heading = 'Qty', key = 'count' , width = 5 },
|
||||
},
|
||||
sortColumn = 'displayName',
|
||||
help = 'Select item to export',
|
||||
},
|
||||
filter = UI.TextEntry {
|
||||
x = 2, ex = 18, y = -3,
|
||||
limit = 50,
|
||||
shadowText = 'filter',
|
||||
backgroundColor = colors.lightGray,
|
||||
backgroundFocusColor = colors.lightGray,
|
||||
},
|
||||
form = UI.Form {
|
||||
x = 21, y = -4, height = 3,
|
||||
margin = 1,
|
||||
manualControls = true,
|
||||
[1] = UI.Chooser {
|
||||
width = 7,
|
||||
formLabel = 'Slot', formKey = 'slot',
|
||||
nochoice = 1,
|
||||
help = 'Export into this slot',
|
||||
},
|
||||
[2] = UI.Chooser {
|
||||
width = 7,
|
||||
formLabel = 'Ignore Dmg', formKey = 'ignoreDamage',
|
||||
pruneEmpty = true,
|
||||
nochoice = 'No',
|
||||
choices = {
|
||||
{ name = 'Yes', value = true },
|
||||
{ name = 'No', value = false },
|
||||
},
|
||||
help = 'Ignore damage of item when exporting'
|
||||
},
|
||||
[3] = UI.Chooser {
|
||||
width = 7,
|
||||
formLabel = 'Ignore NBT', formKey = 'ignoreNbtHash',
|
||||
pruneEmpty = true,
|
||||
nochoice = 'No',
|
||||
choices = {
|
||||
{ name = 'Yes', value = true },
|
||||
{ name = 'No', value = false },
|
||||
},
|
||||
help = 'Ignore NBT of item when exporting'
|
||||
},
|
||||
},
|
||||
statusBar = UI.StatusBar {
|
||||
backgroundColor = colors.cyan,
|
||||
},
|
||||
}
|
||||
|
||||
function itemSlideout:filterItems(t, filter)
|
||||
if filter and #filter > 0 then
|
||||
local r = {}
|
||||
filter = filter:lower()
|
||||
for _,v in pairs(t) do
|
||||
if string.find(v.lname, filter) then
|
||||
table.insert(r, v)
|
||||
end
|
||||
end
|
||||
return r
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
function itemSlideout.grid:enable()
|
||||
if not self.allItems then
|
||||
self.allItems = Milo:listItems()
|
||||
Milo:mergeResources(self.allItems)
|
||||
self:setValues(self.allItems)
|
||||
end
|
||||
UI.Grid.enable(self)
|
||||
end
|
||||
|
||||
function itemSlideout:show(machine, entry)
|
||||
self.machine = machine
|
||||
self.entry = entry
|
||||
|
||||
self.form.choices = { }
|
||||
local m = device[machine.name]
|
||||
for k = 1, m.size() do
|
||||
table.insert(self.form[1].choices, {
|
||||
name = k,
|
||||
value = k,
|
||||
})
|
||||
end
|
||||
|
||||
if not entry.slot then
|
||||
entry.slot = 1
|
||||
end
|
||||
self.form:setValues(entry)
|
||||
|
||||
UI.SlideOut.show(self)
|
||||
self:setFocus(self.filter)
|
||||
--self.filter:focus()
|
||||
end
|
||||
|
||||
function itemSlideout:eventHandler(event)
|
||||
if event.type == 'text_change' then
|
||||
local t = self:filterItems(self.grid.allItems, event.text)
|
||||
self.grid:setValues(t)
|
||||
self.grid:draw()
|
||||
|
||||
elseif event.type == 'focus_change' then
|
||||
self.statusBar:setStatus(event.focused.help)
|
||||
|
||||
elseif event.type == 'save' then
|
||||
local selected = self.grid:getSelected()
|
||||
if not selected then
|
||||
self.statusBar:setStatus('Select an item to export')
|
||||
else
|
||||
self.form:save()
|
||||
self.form.values.name = itemDB:makeKey(selected)
|
||||
table.insert(self.machine.exports, self.form.values)
|
||||
self:hide()
|
||||
end
|
||||
|
||||
elseif event.type == 'cancel' then
|
||||
self:hide()
|
||||
|
||||
elseif event.type == 'refresh' then
|
||||
self.allItems = Milo:listItems()
|
||||
Milo:mergeResources(self.allItems)
|
||||
local t = self:filterItems(self.allItems, self.filter.value)
|
||||
self.grid:setValues(t)
|
||||
self.grid:draw()
|
||||
self.filter:focus()
|
||||
else
|
||||
return UI.SlideOut.eventHandler(self, event)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local exportView = UI.Window {
|
||||
mtype = 'machine',
|
||||
title = 'Export item into machine',
|
||||
index = 3,
|
||||
grid = UI.ScrollingGrid {
|
||||
x = 2, ex = -6, y = 2, ey = -2,
|
||||
columns = {
|
||||
{ heading = 'Slot', key = 'slot', width = 4 },
|
||||
{ heading = 'Item', key = 'displayName' },
|
||||
},
|
||||
sortColumn = 'slot',
|
||||
},
|
||||
add = UI.Button {
|
||||
x = -4, y = 4,
|
||||
text = '+', event = 'add_export', help = '...',
|
||||
},
|
||||
remove = UI.Button {
|
||||
x = -4, y = 6,
|
||||
text = '-', event = 'remove_export', help = '...',
|
||||
},
|
||||
}
|
||||
|
||||
function exportView:save(machine)
|
||||
machine.exports = not Util.empty(self.grid.values) and self.grid.values or nil
|
||||
return true
|
||||
end
|
||||
|
||||
function exportView:setMachine(machine)
|
||||
self.machine = machine
|
||||
if not self.machine.exports then
|
||||
self.machine.exports = { }
|
||||
end
|
||||
self.grid:setValues(machine.exports)
|
||||
end
|
||||
|
||||
function exportView.grid:getDisplayValues(row)
|
||||
row = Util.shallowCopy(row)
|
||||
row.displayName = itemDB:getName(row.name)
|
||||
return row
|
||||
end
|
||||
|
||||
function exportView:eventHandler(event)
|
||||
if event.type == 'grid_select' then
|
||||
itemSlideout:show(self.machine, self.grid:getSelected())
|
||||
|
||||
elseif event.type == 'add_export' then
|
||||
itemSlideout:show(self.machine, { })
|
||||
|
||||
elseif event.type == 'remove_export' then
|
||||
local row = self.grid:getSelected()
|
||||
if row then
|
||||
Util.removeByValue(self.grid.values, row)
|
||||
self.grid:update()
|
||||
self.grid:draw()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
UI:getPage('machineWizard'):add({ items = itemSlideout })
|
||||
UI:getPage('machineWizard').wizard:add({ export = exportView })
|
||||
27
milo/plugins/importTask.lua
Normal file
27
milo/plugins/importTask.lua
Normal file
@@ -0,0 +1,27 @@
|
||||
local Milo = require('milo')
|
||||
|
||||
local device = _G.device
|
||||
|
||||
local ImportTask = {
|
||||
priority = 3,
|
||||
}
|
||||
|
||||
function ImportTask:cycle(context)
|
||||
for source, v in pairs(context.config.remoteDefaults) do
|
||||
if v.imports then
|
||||
local machine = device[source]
|
||||
if machine and machine.getItemMeta then
|
||||
for slotNo in pairs(v.imports) do
|
||||
local slot = machine.getItemMeta(slotNo)
|
||||
if slot then
|
||||
context.inventoryAdapter:insert(slotNo, slot.count, nil, slot, source)
|
||||
end
|
||||
end
|
||||
else
|
||||
debug('Invalid import source: ' .. source)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Milo:registerTask(ImportTask)
|
||||
56
milo/plugins/importView.lua
Normal file
56
milo/plugins/importView.lua
Normal file
@@ -0,0 +1,56 @@
|
||||
local UI = require('ui')
|
||||
local Util = require('util')
|
||||
|
||||
local device = _G.device
|
||||
|
||||
local importView = UI.Window {
|
||||
mtype = 'machine',
|
||||
title = 'Import item from machine',
|
||||
index = 4,
|
||||
grid = UI.ScrollingGrid {
|
||||
y = 2, ey = -2,
|
||||
columns = {
|
||||
{ heading = 'Slot', key = 'slot', width = 4 },
|
||||
{ heading = 'Import', key = 'import' },
|
||||
},
|
||||
sortColumn = 'slot',
|
||||
help = 'Double-click to toggle'
|
||||
},
|
||||
}
|
||||
|
||||
function importView:setMachine(machine)
|
||||
local m = device[machine.name]
|
||||
|
||||
local t = { }
|
||||
for k = 1, m.size() do
|
||||
t[k] = { slot = k }
|
||||
end
|
||||
|
||||
if machine.imports then
|
||||
for k,v in pairs(machine.imports) do
|
||||
t[k] = { slot = k, import = v }
|
||||
end
|
||||
end
|
||||
|
||||
self.grid:setValues(t)
|
||||
end
|
||||
|
||||
function importView:save(machine)
|
||||
local t = { }
|
||||
for k,v in pairs(self.grid.values) do
|
||||
if v.import then
|
||||
t[k] = true
|
||||
end
|
||||
end
|
||||
machine.imports = not Util.empty(t) and t or nil
|
||||
return true
|
||||
end
|
||||
|
||||
function importView:eventHandler(event)
|
||||
if event.type == 'grid_select' then
|
||||
event.selected.import = not event.selected.import
|
||||
self.grid:draw()
|
||||
end
|
||||
end
|
||||
|
||||
UI:getPage('machineWizard').wizard:add({ import = importView })
|
||||
24
milo/plugins/inputChestTask.lua
Normal file
24
milo/plugins/inputChestTask.lua
Normal file
@@ -0,0 +1,24 @@
|
||||
local Milo = require('milo')
|
||||
|
||||
local device = _G.device
|
||||
|
||||
local InputChest = {
|
||||
priority = 1,
|
||||
}
|
||||
|
||||
function InputChest:cycle(context)
|
||||
for name,v in pairs(context.config.remoteDefaults) do
|
||||
if v.mtype == 'input' then
|
||||
local inventory = device[name]
|
||||
|
||||
local list = inventory and inventory.list and inventory.list()
|
||||
if list then
|
||||
for slotNo, slot in pairs(list) do
|
||||
context.inventoryAdapter:insert(slotNo, slot.count, nil, slot, name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Milo:registerTask(InputChest)
|
||||
248
milo/plugins/item.lua
Normal file
248
milo/plugins/item.lua
Normal file
@@ -0,0 +1,248 @@
|
||||
local Ansi = require('ansi')
|
||||
local Milo = require('milo')
|
||||
local UI = require('ui')
|
||||
local Util = require('util')
|
||||
|
||||
local colors = _G.colors
|
||||
local device = _G.device
|
||||
|
||||
local context = Milo:getContext()
|
||||
|
||||
local itemPage = UI.Page {
|
||||
titleBar = UI.TitleBar {
|
||||
title = 'Limit Resource',
|
||||
previousPage = true,
|
||||
event = 'form_cancel',
|
||||
},
|
||||
form = UI.Form {
|
||||
x = 1, y = 2, height = 10, ex = -1,
|
||||
[1] = UI.TextEntry {
|
||||
width = 7,
|
||||
formLabel = 'Min', formKey = 'low', help = 'Craft if below min'
|
||||
},
|
||||
[2] = UI.TextEntry {
|
||||
width = 7,
|
||||
formLabel = 'Max', formKey = 'limit', help = 'Eject if above max'
|
||||
},
|
||||
--[[
|
||||
[3] = UI.Chooser {
|
||||
width = 7,
|
||||
formLabel = 'Autocraft', formKey = 'auto',
|
||||
nochoice = 'No',
|
||||
choices = {
|
||||
{ name = 'Yes', value = true },
|
||||
{ name = 'No', value = false },
|
||||
},
|
||||
help = 'Craft until out of ingredients'
|
||||
},
|
||||
]]
|
||||
[4] = UI.Chooser {
|
||||
width = 7,
|
||||
formLabel = 'Ignore Dmg', formKey = 'ignoreDamage',
|
||||
nochoice = 'No',
|
||||
choices = {
|
||||
{ name = 'Yes', value = true },
|
||||
{ name = 'No', value = false },
|
||||
},
|
||||
help = 'Ignore damage of item'
|
||||
},
|
||||
[5] = UI.Chooser {
|
||||
width = 7,
|
||||
formLabel = 'Ignore NBT', formKey = 'ignoreNbtHash',
|
||||
nochoice = 'No',
|
||||
choices = {
|
||||
{ name = 'Yes', value = true },
|
||||
{ name = 'No', value = false },
|
||||
},
|
||||
help = 'Ignore NBT of item'
|
||||
},
|
||||
--[[
|
||||
[6] = UI.Button {
|
||||
x = 2, y = -2, width = 10,
|
||||
formLabel = 'Redstone',
|
||||
event = 'show_rs',
|
||||
text = 'Configure',
|
||||
},
|
||||
]]
|
||||
infoButton = UI.Button {
|
||||
x = 2, y = -2,
|
||||
event = 'show_info',
|
||||
text = 'Info',
|
||||
},
|
||||
},
|
||||
rsControl = UI.SlideOut {
|
||||
backgroundColor = colors.cyan,
|
||||
titleBar = UI.TitleBar {
|
||||
title = "Redstone Control",
|
||||
},
|
||||
form = UI.Form {
|
||||
y = 2,
|
||||
[1] = UI.Chooser {
|
||||
width = 7,
|
||||
formLabel = 'RS Control', formKey = 'rsControl',
|
||||
nochoice = 'No',
|
||||
choices = {
|
||||
{ name = 'Yes', value = true },
|
||||
{ name = 'No', value = false },
|
||||
},
|
||||
help = 'Control via redstone'
|
||||
},
|
||||
[2] = UI.Chooser {
|
||||
width = 25,
|
||||
formLabel = 'RS Device', formKey = 'rsDevice',
|
||||
--choices = devices,
|
||||
help = 'Redstone Device'
|
||||
},
|
||||
[3] = UI.Chooser {
|
||||
width = 10,
|
||||
formLabel = 'RS Side', formKey = 'rsSide',
|
||||
--nochoice = 'No',
|
||||
choices = {
|
||||
{ name = 'up', value = 'up' },
|
||||
{ name = 'down', value = 'down' },
|
||||
{ name = 'east', value = 'east' },
|
||||
{ name = 'north', value = 'north' },
|
||||
{ name = 'west', value = 'west' },
|
||||
{ name = 'south', value = 'south' },
|
||||
},
|
||||
help = 'Output side'
|
||||
},
|
||||
},
|
||||
},
|
||||
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 { }
|
||||
}
|
||||
|
||||
function itemPage:enable(item)
|
||||
self.item = Util.shallowCopy(item)
|
||||
|
||||
self.form:setValues(self.item)
|
||||
self.titleBar.title = item.displayName or item.name
|
||||
|
||||
UI.Page.enable(self)
|
||||
self:focusFirst()
|
||||
end
|
||||
|
||||
function itemPage.rsControl:enable()
|
||||
local devices = self.form[1].choices
|
||||
Util.clear(devices)
|
||||
for _,dev in pairs(device) do
|
||||
if dev.setOutput then
|
||||
table.insert(devices, { name = dev.name, value = dev.name })
|
||||
end
|
||||
end
|
||||
|
||||
if Util.size(devices) == 0 then
|
||||
table.insert(devices, { name = 'None found', values = '' })
|
||||
end
|
||||
|
||||
UI.SlideOut.enable(self)
|
||||
end
|
||||
|
||||
function itemPage.rsControl:eventHandler(event)
|
||||
if event.type == 'form_cancel' then
|
||||
self:hide()
|
||||
elseif event.type == 'form_complete' then
|
||||
self:hide()
|
||||
else
|
||||
return UI.SlideOut.eventHandler(self, event)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function itemPage:eventHandler(event)
|
||||
if event.type == 'form_cancel' then
|
||||
UI:setPreviousPage()
|
||||
|
||||
elseif event.type == 'show_rs' then
|
||||
self.rsControl:show()
|
||||
|
||||
elseif event.type == 'show_info' then
|
||||
local value =
|
||||
string.format('%s%s%s\n%s\n',
|
||||
Ansi.orange, self.item.displayName, Ansi.reset,
|
||||
self.item.name)
|
||||
|
||||
if self.item.nbtHash then
|
||||
value = value .. self.item.nbtHash .. '\n'
|
||||
end
|
||||
|
||||
value = value .. string.format('\n%sDamage:%s %s',
|
||||
Ansi.yellow, Ansi.reset, self.item.damage)
|
||||
|
||||
if self.item.maxDamage and self.item.maxDamage > 0 then
|
||||
value = value .. string.format(' (max: %s)', self.item.maxDamage)
|
||||
end
|
||||
|
||||
if self.item.maxCount then
|
||||
value = value .. string.format('\n%sStack Size: %s%s',
|
||||
Ansi.yellow, Ansi.reset, self.item.maxCount)
|
||||
end
|
||||
|
||||
self.info.textArea.value = value
|
||||
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()
|
||||
|
||||
elseif event.type == 'form_complete' then
|
||||
local values = self.form.values
|
||||
local originalKey = Milo:uniqueKey(self.item)
|
||||
|
||||
local filtered = Util.shallowCopy(values)
|
||||
filtered.low = tonumber(filtered.low)
|
||||
filtered.limit = tonumber(filtered.limit)
|
||||
|
||||
if filtered.auto ~= true then
|
||||
filtered.auto = nil
|
||||
end
|
||||
|
||||
if filtered.rsControl ~= true then
|
||||
filtered.rsControl = nil
|
||||
filtered.rsSide = nil
|
||||
filtered.rsDevice = nil
|
||||
end
|
||||
|
||||
if filtered.ignoreDamage == true then
|
||||
filtered.damage = 0
|
||||
else
|
||||
filtered.ignoreDamage = nil
|
||||
end
|
||||
|
||||
if filtered.ignoreNbtHash == true then
|
||||
filtered.nbtHash = nil
|
||||
else
|
||||
filtered.ignoreNbtHash = nil
|
||||
end
|
||||
context.resources[originalKey] = nil
|
||||
context.resources[Milo:uniqueKey(filtered)] = filtered
|
||||
|
||||
filtered.count = nil
|
||||
Milo:saveResources()
|
||||
|
||||
UI:setPreviousPage()
|
||||
|
||||
else
|
||||
return UI.Page.eventHandler(self, event)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
UI:addPage('item', itemPage)
|
||||
65
milo/plugins/jobList.lua
Normal file
65
milo/plugins/jobList.lua
Normal file
@@ -0,0 +1,65 @@
|
||||
local Milo = require('milo')
|
||||
local Peripheral = require('peripheral')
|
||||
local UI = require('ui')
|
||||
|
||||
local colors = _G.colors
|
||||
|
||||
local context = Milo:getContext()
|
||||
local mon = Peripheral.lookup(context.config.monitor) or
|
||||
error('Monitor is not attached')
|
||||
local display = UI.Device {
|
||||
device = mon,
|
||||
textScale = .5,
|
||||
}
|
||||
|
||||
local jobList = UI.Page {
|
||||
parent = display,
|
||||
grid = UI.Grid {
|
||||
sortColumn = 'displayName',
|
||||
backgroundFocusColor = colors.black,
|
||||
columns = {
|
||||
{ heading = 'Qty', key = 'count', width = 6 },
|
||||
{ heading = 'Crafting', key = 'displayName', width = display.width / 2 - 10 },
|
||||
{ heading = 'Status', key = 'status', width = display.width - 10 },
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
function jobList:showError(msg)
|
||||
self.grid:clear()
|
||||
self.grid:centeredWrite(math.ceil(self.grid.height / 2), msg)
|
||||
self:sync()
|
||||
end
|
||||
|
||||
function jobList:updateList(craftList)
|
||||
self.grid:setValues(craftList)
|
||||
self.grid:update()
|
||||
self:draw()
|
||||
self:sync()
|
||||
end
|
||||
|
||||
function jobList.grid:getRowTextColor(row, selected)
|
||||
if row.statusCode == Milo.STATUS_ERROR then
|
||||
return colors.red
|
||||
elseif row.statusCode == Milo.STATUS_WARNING then
|
||||
return colors.yellow
|
||||
elseif row.statusCode == Milo.STATUS_INFO then
|
||||
return colors.lime
|
||||
end
|
||||
return UI.Grid:getRowTextColor(row, selected)
|
||||
end
|
||||
|
||||
jobList:enable()
|
||||
jobList:draw()
|
||||
jobList:sync()
|
||||
|
||||
local JobListTask = {
|
||||
priority = 80,
|
||||
}
|
||||
|
||||
function JobListTask:cycle()
|
||||
jobList:updateList(Milo:getCraftingStatus())
|
||||
end
|
||||
|
||||
Milo:registerTask(JobListTask)
|
||||
context.jobList = jobList
|
||||
61
milo/plugins/learn.lua
Normal file
61
milo/plugins/learn.lua
Normal file
@@ -0,0 +1,61 @@
|
||||
local Milo = require('milo')
|
||||
local UI = require('ui')
|
||||
|
||||
local context = Milo:getContext()
|
||||
|
||||
local learnPage = UI.Dialog {
|
||||
height = 6, width = UI.term.width - 6,
|
||||
title = 'Learn Recipe',
|
||||
chooser = UI.Chooser {
|
||||
x = 8, y = 3,
|
||||
width = 20,
|
||||
},
|
||||
cancel = UI.Button {
|
||||
x = 3, y = -2,
|
||||
text = 'Cancel', event = 'cancel'
|
||||
},
|
||||
accept = UI.Button {
|
||||
ex = -3, y = -2,
|
||||
width = 8,
|
||||
text = 'Ok', event = 'accept',
|
||||
},
|
||||
}
|
||||
|
||||
function learnPage:enable()
|
||||
self.chooser.choices = { }
|
||||
|
||||
for k in pairs(context.learnTypes) do
|
||||
table.insert(self.chooser.choices, {
|
||||
name = k,
|
||||
value = k,
|
||||
})
|
||||
end
|
||||
self.chooser.value =
|
||||
Milo:getState('learnType') or
|
||||
self.chooser.choices[1].value
|
||||
|
||||
self:focusFirst()
|
||||
UI.Dialog.enable(self)
|
||||
end
|
||||
|
||||
function learnPage:disable()
|
||||
UI.Dialog.disable(self)
|
||||
end
|
||||
|
||||
function learnPage:eventHandler(event)
|
||||
if event.type == 'cancel' then
|
||||
UI:setPreviousPage()
|
||||
|
||||
elseif event.type == 'accept' then
|
||||
local choice = self.chooser.value
|
||||
|
||||
Milo:setState('learnType', choice)
|
||||
UI:setPage(context.learnTypes[choice])
|
||||
|
||||
else
|
||||
return UI.Dialog.eventHandler(self, event)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
UI:addPage('learn', learnPage)
|
||||
35
milo/plugins/limitTask.lua
Normal file
35
milo/plugins/limitTask.lua
Normal file
@@ -0,0 +1,35 @@
|
||||
local Milo = require('milo')
|
||||
|
||||
local LimitTask = {
|
||||
priority = 10,
|
||||
}
|
||||
|
||||
function LimitTask:cycle(context)
|
||||
local trashcan
|
||||
|
||||
for k,v in pairs(context.config.remoteDefaults) do
|
||||
if v.mtype == 'trashcan' then
|
||||
trashcan = k
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if not trashcan then
|
||||
return
|
||||
end
|
||||
|
||||
for _,res in pairs(context.resources) do
|
||||
if res.limit then
|
||||
local item = Milo:getItemWithQty(res, res.ignoreDamage, res.ignoreNbtHash)
|
||||
if item and item.count > res.limit then
|
||||
context.inventoryAdapter:provide(
|
||||
{ name = item.name, damage = item.damage, nbtHash = item.nbtHash },
|
||||
item.count - res.limit,
|
||||
nil,
|
||||
trashcan)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Milo:registerTask(LimitTask)
|
||||
224
milo/plugins/listing.lua
Normal file
224
milo/plugins/listing.lua
Normal file
@@ -0,0 +1,224 @@
|
||||
local Craft = require('turtle.craft')
|
||||
local itemDB = require('itemDB')
|
||||
local Milo = require('milo')
|
||||
local UI = require('ui')
|
||||
local Util = require('util')
|
||||
|
||||
local colors = _G.colors
|
||||
local os = _G.os
|
||||
|
||||
local context = Milo:getContext()
|
||||
|
||||
local function queue(fn)
|
||||
while Milo:isCraftingPaused() do
|
||||
os.sleep(1)
|
||||
end
|
||||
fn()
|
||||
end
|
||||
|
||||
local function filterItems(t, filter, displayMode)
|
||||
if filter or displayMode > 0 then
|
||||
local r = { }
|
||||
if filter then
|
||||
filter = filter:lower()
|
||||
end
|
||||
for _,v in pairs(t) do
|
||||
if not filter or string.find(v.lname, filter, 1, true) then
|
||||
if not displayMode or
|
||||
displayMode == 0 or
|
||||
displayMode == 1 and v.count > 0 or
|
||||
displayMode == 2 and v.has_recipe then
|
||||
table.insert(r, v)
|
||||
end
|
||||
end
|
||||
end
|
||||
return r
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
local listingPage = UI.Page {
|
||||
menuBar = UI.MenuBar {
|
||||
buttons = {
|
||||
{ text = 'Learn', event = 'learn' },
|
||||
{ text = 'Forget', event = 'forget' },
|
||||
{ text = 'Craft', event = 'craft' },
|
||||
{ text = 'Refresh', event = 'refresh', x = -9 },
|
||||
},
|
||||
},
|
||||
grid = UI.Grid {
|
||||
y = 2, ey = -2,
|
||||
columns = {
|
||||
{ heading = ' Qty', key = 'count' , width = 4, justify = 'right' },
|
||||
{ heading = 'Name', key = 'displayName' },
|
||||
{ heading = 'Min', key = 'low' , width = 4 },
|
||||
{ heading = 'Max', key = 'limit' , width = 4 },
|
||||
},
|
||||
sortColumn = 'displayName',
|
||||
},
|
||||
statusBar = UI.StatusBar {
|
||||
filter = UI.TextEntry {
|
||||
x = 1, ex = -4,
|
||||
limit = 50,
|
||||
shadowText = 'filter',
|
||||
shadowTextColor = colors.gray,
|
||||
backgroundColor = colors.cyan,
|
||||
backgroundFocusColor = colors.cyan,
|
||||
accelerators = {
|
||||
[ 'enter' ] = 'craft',
|
||||
},
|
||||
},
|
||||
display = UI.Button {
|
||||
x = -3,
|
||||
event = 'toggle_display',
|
||||
value = 0,
|
||||
text = 'A',
|
||||
},
|
||||
},
|
||||
notification = UI.Notification(),
|
||||
accelerators = {
|
||||
r = 'refresh',
|
||||
q = 'quit',
|
||||
[ 'control-e' ] = 'eject',
|
||||
[ 'control-s' ] = 'eject_stack',
|
||||
[ 'control-m' ] = 'machines',
|
||||
},
|
||||
displayMode = 0,
|
||||
}
|
||||
|
||||
function listingPage.statusBar:draw()
|
||||
return UI.Window.draw(self)
|
||||
end
|
||||
|
||||
function listingPage.grid:getRowTextColor(row, selected)
|
||||
if row.is_craftable then
|
||||
return colors.yellow
|
||||
end
|
||||
if row.has_recipe then
|
||||
return colors.cyan
|
||||
end
|
||||
return UI.Grid:getRowTextColor(row, selected)
|
||||
end
|
||||
|
||||
function listingPage.grid:getDisplayValues(row)
|
||||
row = Util.shallowCopy(row)
|
||||
row.count = row.count > 0 and Util.toBytes(row.count) or ''
|
||||
if row.low then
|
||||
row.low = Util.toBytes(row.low)
|
||||
end
|
||||
if row.limit then
|
||||
row.limit = Util.toBytes(row.limit)
|
||||
end
|
||||
return row
|
||||
end
|
||||
|
||||
function listingPage:eventHandler(event)
|
||||
debug(event)
|
||||
if event.type == 'quit' then
|
||||
UI:exitPullEvents()
|
||||
|
||||
elseif event.type == 'eject' then
|
||||
local item = self.grid:getSelected()
|
||||
if item then
|
||||
queue(function() Milo:eject(item, 1) end)
|
||||
end
|
||||
|
||||
elseif event.type == 'eject_stack' then
|
||||
local item = self.grid:getSelected()
|
||||
if item then
|
||||
queue(function() Milo:eject(item, itemDB:getMaxCount(item)) end)
|
||||
end
|
||||
|
||||
elseif event.type == 'machines' then
|
||||
UI:setPage('machines')
|
||||
|
||||
elseif event.type == 'details' or event.type == 'grid_select_right' then
|
||||
local item = self.grid:getSelected()
|
||||
if item then
|
||||
UI:setPage('item', item)
|
||||
end
|
||||
|
||||
elseif event.type == 'refresh' then
|
||||
self:refresh()
|
||||
self.grid:draw()
|
||||
self.statusBar.filter:focus()
|
||||
|
||||
elseif event.type == 'toggle_display' then
|
||||
local values = {
|
||||
[0] = 'A',
|
||||
[1] = 'I',
|
||||
[2] = 'C',
|
||||
}
|
||||
|
||||
event.button.value = (event.button.value + 1) % 3
|
||||
self.displayMode = event.button.value
|
||||
event.button.text = values[event.button.value]
|
||||
event.button:draw()
|
||||
self:applyFilter()
|
||||
self.grid:draw()
|
||||
|
||||
elseif event.type == 'learn' then
|
||||
UI:setPage('learn')
|
||||
|
||||
elseif event.type == 'craft' or event.type == 'grid_select' then
|
||||
local item = self.grid:getSelected()
|
||||
if Craft.findRecipe(item) or true then -- or item.is_craftable then
|
||||
UI:setPage('craft', self.grid:getSelected())
|
||||
else
|
||||
self.notification:error('No recipe defined')
|
||||
end
|
||||
|
||||
elseif event.type == 'forget' then
|
||||
local item = self.grid:getSelected()
|
||||
if item then
|
||||
local key = Milo:uniqueKey(item)
|
||||
|
||||
if context.userRecipes[key] then
|
||||
context.userRecipes[key] = nil
|
||||
Util.writeTable(Milo.RECIPES_FILE, context.userRecipes)
|
||||
Craft.loadRecipes()
|
||||
end
|
||||
|
||||
if context.resources[key] then
|
||||
context.resources[key] = nil
|
||||
Milo:saveResources()
|
||||
end
|
||||
|
||||
self.notification:info('Forgot: ' .. item.name)
|
||||
self:refresh()
|
||||
self.grid:draw()
|
||||
end
|
||||
|
||||
elseif event.type == 'text_change' then
|
||||
self.filter = event.text
|
||||
if #self.filter == 0 then
|
||||
self.filter = nil
|
||||
end
|
||||
self:applyFilter()
|
||||
self.grid:draw()
|
||||
self.statusBar.filter:focus()
|
||||
|
||||
else
|
||||
UI.Page.eventHandler(self, event)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function listingPage:enable()
|
||||
self:refresh()
|
||||
self:setFocus(self.statusBar.filter)
|
||||
UI.Page.enable(self)
|
||||
end
|
||||
|
||||
function listingPage:refresh()
|
||||
self.allItems = Milo:listItems()
|
||||
Milo:mergeResources(self.allItems)
|
||||
self:applyFilter()
|
||||
end
|
||||
|
||||
function listingPage:applyFilter()
|
||||
local t = filterItems(self.allItems, self.filter, self.displayMode)
|
||||
self.grid:setValues(t)
|
||||
end
|
||||
|
||||
UI:addPage('listing', listingPage)
|
||||
172
milo/plugins/machineLearn.lua
Normal file
172
milo/plugins/machineLearn.lua
Normal file
@@ -0,0 +1,172 @@
|
||||
local Craft = require('turtle.craft')
|
||||
local itemDB = require('itemDB')
|
||||
local Milo = require('milo')
|
||||
local UI = require('ui')
|
||||
local Util = require('util')
|
||||
|
||||
local colors = _G.colors
|
||||
local device = _G.device
|
||||
local turtle = _G.turtle
|
||||
|
||||
local MACHINE_LOOKUP = 'usr/config/machine_crafting.db'
|
||||
|
||||
local context = Milo:getContext()
|
||||
|
||||
local function getTurtleInventory()
|
||||
local introspectionModule = device['plethora:introspection'] or
|
||||
error('Introspection module not found')
|
||||
|
||||
local list = { }
|
||||
for i = 1,16 do
|
||||
list[i] = introspectionModule.getInventory().getItemMeta(i)
|
||||
end
|
||||
return list
|
||||
end
|
||||
|
||||
local machineLearnWizard = UI.Page {
|
||||
titleBar = UI.TitleBar { title = 'Learn a crafting recipe' },
|
||||
wizard = UI.Wizard {
|
||||
y = 2, ey = -2,
|
||||
pages = {
|
||||
machine = UI.Window {
|
||||
index = 1,
|
||||
grid = UI.ScrollingGrid {
|
||||
y = 2, ey = -2,
|
||||
values = context.config.remoteDefaults,
|
||||
columns = {
|
||||
{ heading = 'Name', key = 'displayName' },
|
||||
},
|
||||
sortColumn = 'displayName',
|
||||
},
|
||||
},
|
||||
confirmation = UI.Window {
|
||||
index = 2,
|
||||
notice = UI.TextArea {
|
||||
x = 2, ex = -2, y = 2, ey = -2,
|
||||
backgroundColor = colors.black,
|
||||
value =
|
||||
[[Place items in slots according to the machine's inventory.
|
||||
|
||||
Place the result in the last slot of the turtle.
|
||||
|
||||
Example: Slot 1 is the top slot in a furnace.]],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
notification = UI.Notification { },
|
||||
}
|
||||
|
||||
local pages = machineLearnWizard.wizard.pages
|
||||
local machine
|
||||
|
||||
function pages.machine.grid:getDisplayValues(row)
|
||||
row = Util.shallowCopy(row)
|
||||
row.displayName = row.displayName or row.name
|
||||
return row
|
||||
end
|
||||
|
||||
function pages.machine:validate()
|
||||
|
||||
-- TODO: validation should only be invoked when moving forward (i think)
|
||||
-- TODO: index number validation in wizard
|
||||
|
||||
local selected = self.grid:getSelected()
|
||||
if not selected then
|
||||
machineLearnWizard.notification:error('No machines configured')
|
||||
return
|
||||
end
|
||||
|
||||
machine = device[selected.name]
|
||||
if not machine then
|
||||
machineLearnWizard.notification:error('Machine not found')
|
||||
return
|
||||
end
|
||||
|
||||
if not machine.size then
|
||||
machineLearnWizard.notification:error('Invalid machine')
|
||||
return
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
function pages.confirmation:validate()
|
||||
local inventory = getTurtleInventory()
|
||||
local result = inventory[16]
|
||||
local slotCount = machine.size()
|
||||
|
||||
inventory[16] = nil
|
||||
|
||||
if not result then
|
||||
machineLearnWizard.notification:error('Result must be placed in last slot')
|
||||
return
|
||||
end
|
||||
|
||||
if Util.empty(inventory) then
|
||||
machineLearnWizard.notification:error('Ingredients not present')
|
||||
return
|
||||
end
|
||||
|
||||
for k in pairs(inventory) do
|
||||
if k > slotCount then
|
||||
machineLearnWizard.notification:error(
|
||||
'Slot ' .. k .. ' is not valid\nThe valid slots are 1 - ' .. machine.size())
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
local recipe = {
|
||||
count = result.count,
|
||||
ingredients = { },
|
||||
maxCount = result.maxCount ~= 64 and result.maxCount or nil,
|
||||
}
|
||||
|
||||
for k,v in pairs(inventory) do
|
||||
recipe.ingredients[k] = Milo:uniqueKey(v)
|
||||
end
|
||||
|
||||
local key = Milo:uniqueKey(result)
|
||||
|
||||
-- save the recipe
|
||||
context.userRecipes[key] = recipe
|
||||
Util.writeTable(Milo.RECIPES_FILE, context.userRecipes)
|
||||
Craft.loadRecipes()
|
||||
|
||||
-- save the machine association
|
||||
Craft.machineLookup[key] = machine.name
|
||||
Util.writeTable(MACHINE_LOOKUP, Craft.machineLookup)
|
||||
|
||||
local listingPage = UI:getPage('listing')
|
||||
local displayName = itemDB:getName(result)
|
||||
|
||||
listingPage.statusBar.filter:setValue(displayName)
|
||||
listingPage.notification:success('Learned: ' .. displayName)
|
||||
listingPage.filter = displayName
|
||||
listingPage:refresh()
|
||||
listingPage.grid:draw()
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
function machineLearnWizard:enable()
|
||||
Milo:pauseCrafting()
|
||||
UI.Page.enable(self)
|
||||
end
|
||||
|
||||
function machineLearnWizard:disable()
|
||||
Milo:resumeCrafting()
|
||||
UI.Page.disable(self)
|
||||
end
|
||||
|
||||
function machineLearnWizard:eventHandler(event)
|
||||
if event.type == 'cancel' or event.type == 'accept' then
|
||||
turtle.emptyInventory()
|
||||
UI:setPage('listing')
|
||||
else
|
||||
return UI.Page.eventHandler(self, event)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
context.learnTypes['Machine processing'] = machineLearnWizard
|
||||
24
milo/plugins/potionImportTask.lua
Normal file
24
milo/plugins/potionImportTask.lua
Normal file
@@ -0,0 +1,24 @@
|
||||
local Milo = require('milo')
|
||||
|
||||
local device = _G.device
|
||||
|
||||
local PotionImportTask = {
|
||||
priority = 3,
|
||||
}
|
||||
|
||||
function PotionImportTask:cycle(context)
|
||||
for _, v in pairs(device) do
|
||||
if v.type == 'minecraft:brewing_stand' and v.getBrewTime() == 0 then
|
||||
local list = v.list()
|
||||
if not list[4] and list[1] then
|
||||
for i = 1, 3 do
|
||||
if list[i] then
|
||||
context.inventoryAdapter:insert(i, 1, nil, list[i], v.name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Milo:registerTask(PotionImportTask)
|
||||
46
milo/plugins/replenishTask.lua
Normal file
46
milo/plugins/replenishTask.lua
Normal file
@@ -0,0 +1,46 @@
|
||||
local itemDB = require('itemDB')
|
||||
local Milo = require('milo')
|
||||
|
||||
local ReplenishTask = {
|
||||
priority = 30,
|
||||
}
|
||||
|
||||
function ReplenishTask:cycle(context)
|
||||
local craftList = { }
|
||||
|
||||
for _,res in pairs(context.resources) do
|
||||
if res.low then
|
||||
local item = Milo:getItemWithQty(res, res.ignoreDamage, res.ignoreNbtHash)
|
||||
if not item then
|
||||
item = {
|
||||
damage = res.damage,
|
||||
nbtHash = res.nbtHash,
|
||||
name = res.name,
|
||||
displayName = itemDB:getName(res),
|
||||
count = 0
|
||||
}
|
||||
end
|
||||
|
||||
if item.count < res.low then
|
||||
if res.ignoreDamage then
|
||||
item.damage = 0
|
||||
end
|
||||
local key = Milo:uniqueKey(res)
|
||||
|
||||
craftList[key] = {
|
||||
damage = item.damage,
|
||||
nbtHash = item.nbtHash,
|
||||
count = res.low - item.count,
|
||||
name = item.name,
|
||||
displayName = item.displayName,
|
||||
status = '',
|
||||
rsControl = res.rsControl,
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Milo:craftItems(craftList)
|
||||
end
|
||||
|
||||
Milo:registerTask(ReplenishTask)
|
||||
44
milo/plugins/storageView.lua
Normal file
44
milo/plugins/storageView.lua
Normal file
@@ -0,0 +1,44 @@
|
||||
local UI = require('ui')
|
||||
|
||||
local colors = _G.colors
|
||||
|
||||
local storageView = UI.Window {
|
||||
mtype = 'storage',
|
||||
title = 'Storage Options',
|
||||
index = 2,
|
||||
backgroundColor = colors.cyan,
|
||||
form = UI.Form {
|
||||
x = 1, y = 2, ex = -1, ey = -2,
|
||||
manualControls = true,
|
||||
[1] = UI.TextEntry {
|
||||
formLabel = 'Priority', formKey = 'priority',
|
||||
help = 'Larger values get precedence',
|
||||
limit = 4,
|
||||
validate = 'numeric', pruneEmpty = true,
|
||||
},
|
||||
[2] = UI.TextEntry {
|
||||
formLabel = 'Lock to', formKey = 'lockWith',
|
||||
help = 'Locks chest to a single item type',
|
||||
width = 18, limit = 64, pruneEmpty = true,
|
||||
},
|
||||
[3] = UI.Button {
|
||||
x = -9, ey = -4,
|
||||
text = 'Detect', help = 'Determine what is currently present',
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
function storageView:enable()
|
||||
UI.Window.enable(self)
|
||||
self:focusFirst()
|
||||
end
|
||||
|
||||
function storageView:validate()
|
||||
return self.form:save()
|
||||
end
|
||||
|
||||
function storageView:setMachine(machine)
|
||||
self.form:setValues(machine)
|
||||
end
|
||||
|
||||
UI:getPage('machineWizard').wizard:add({ storage = storageView })
|
||||
175
milo/plugins/turtleLearn.lua
Normal file
175
milo/plugins/turtleLearn.lua
Normal file
@@ -0,0 +1,175 @@
|
||||
local Craft = require('turtle.craft')
|
||||
local itemDB = require('itemDB')
|
||||
local Milo = require('milo')
|
||||
local UI = require('ui')
|
||||
local Util = require('util')
|
||||
|
||||
local device = _G.device
|
||||
local turtle = _G.turtle
|
||||
|
||||
local context = Milo:getContext()
|
||||
|
||||
local function getTurtleInventory()
|
||||
local introspectionModule = device['plethora:introspection'] or
|
||||
error('Introspection module not found')
|
||||
|
||||
local list = { }
|
||||
for i = 1,16 do
|
||||
list[i] = introspectionModule.getInventory().getItemMeta(i)
|
||||
end
|
||||
return list
|
||||
end
|
||||
|
||||
local function learnRecipe()
|
||||
local ingredients = getTurtleInventory()
|
||||
|
||||
if not ingredients then
|
||||
return false, 'No recipe defined'
|
||||
end
|
||||
|
||||
turtle.select(1)
|
||||
if not turtle.craft() then
|
||||
return false, 'Failed to craft'
|
||||
end
|
||||
|
||||
local results = getTurtleInventory()
|
||||
if not results or not results[1] then
|
||||
return false, 'Failed to craft'
|
||||
end
|
||||
|
||||
local maxCount
|
||||
local newRecipe = {
|
||||
ingredients = ingredients,
|
||||
}
|
||||
|
||||
local numResults = 0
|
||||
for _,v in pairs(results) do
|
||||
if v.count > 0 then
|
||||
numResults = numResults + 1
|
||||
end
|
||||
end
|
||||
if numResults > 1 then
|
||||
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[Milo:uniqueKey(tool)] = true
|
||||
v1.craftingTool = true
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local recipe
|
||||
for _,v in pairs(results) do
|
||||
if not v.craftingTool then
|
||||
recipe = v
|
||||
if maxCount then
|
||||
recipe.maxCount = maxCount
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if not recipe then
|
||||
debug(results)
|
||||
debug(newRecipe)
|
||||
error('Failed - view system log')
|
||||
end
|
||||
|
||||
newRecipe.count = recipe.count
|
||||
|
||||
local key = Milo:uniqueKey(recipe)
|
||||
if recipe.maxCount ~= 64 then
|
||||
newRecipe.maxCount = recipe.maxCount
|
||||
end
|
||||
for k,ingredient in pairs(Util.shallowCopy(ingredients)) do
|
||||
if ingredient.maxDamage > 0 then
|
||||
-- ingredient.damage = '*' -- I don't think this is right
|
||||
end
|
||||
ingredients[k] = Milo:uniqueKey(ingredient)
|
||||
end
|
||||
|
||||
context.userRecipes[key] = newRecipe
|
||||
Util.writeTable(Milo.RECIPES_FILE, context.userRecipes)
|
||||
Craft.loadRecipes()
|
||||
|
||||
turtle.emptyInventory()
|
||||
|
||||
return recipe
|
||||
end
|
||||
|
||||
local turtleLearnWizard = UI.Page {
|
||||
titleBar = UI.TitleBar { title = 'Learn a crafting recipe' },
|
||||
wizard = UI.Wizard {
|
||||
y = 2, ey = -3,
|
||||
pages = {
|
||||
confirmation = UI.Window {
|
||||
index = 1,
|
||||
notice = UI.TextArea {
|
||||
x = 2, ex = -2, y = 2, ey = -2,
|
||||
value =
|
||||
[[Place recipe in turtle!]],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
notification = UI.Notification { },
|
||||
}
|
||||
|
||||
function turtleLearnWizard:enable()
|
||||
Milo:pauseCrafting()
|
||||
UI.Page.enable(self)
|
||||
end
|
||||
|
||||
function turtleLearnWizard:disable()
|
||||
Milo:resumeCrafting()
|
||||
UI.Page.disable(self)
|
||||
end
|
||||
|
||||
function turtleLearnWizard.wizard.pages.confirmation:validate()
|
||||
local recipe, msg = learnRecipe(self)
|
||||
|
||||
if recipe then
|
||||
local listingPage = UI:getPage('listing')
|
||||
local displayName = itemDB:getName(recipe)
|
||||
|
||||
listingPage.statusBar.filter:setValue(displayName)
|
||||
listingPage.notification:success('Learned: ' .. displayName)
|
||||
listingPage.filter = displayName
|
||||
listingPage:refresh()
|
||||
listingPage.grid:draw()
|
||||
|
||||
return true
|
||||
else
|
||||
turtleLearnWizard.notification:error(msg)
|
||||
end
|
||||
end
|
||||
|
||||
function turtleLearnWizard:eventHandler(event)
|
||||
if event.type == 'cancel' or event.type == 'accept' then
|
||||
UI:setPage('listing')
|
||||
else
|
||||
return UI.Page.eventHandler(self, event)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
context.learnTypes['Turtle crafting'] = turtleLearnWizard
|
||||
Reference in New Issue
Block a user