milo manipulator + crafting fixes

This commit is contained in:
kepler155c
2018-11-12 11:44:24 -05:00
parent 3f095ff522
commit 52ea4e039c
6 changed files with 63 additions and 43 deletions

View File

@@ -139,7 +139,7 @@ local function harvest(blocks)
elseif b.action == 'bump' then elseif b.action == 'bump' then
if turtle.faceAgainst(b) then if turtle.faceAgainst(b) then
turtle.equip('right', 'plethora:module:3') turtle.equip('right', 'plethora:module:3')
os.sleep(.5) os.sleep(.3)
-- search the ground for the dropped cactus -- search the ground for the dropped cactus
local sensed = peripheral.call('right', 'sense') local sensed = peripheral.call('right', 'sense')
turtle.equip('right', 'minecraft:diamond_pickaxe') turtle.equip('right', 'minecraft:diamond_pickaxe')

View File

@@ -154,16 +154,7 @@ function Milo:getMatches(items, item, ignoreDamage, ignoreNbtHash)
end end
function Milo:clearGrid() function Milo:clearGrid()
turtle.eachFilledSlot(function(slot) return Craft.clearGrid(self.context.storage)
self.context.storage:import(self.context.localName, slot.index, slot.count, slot)
end)
for i = 1, 16 do
if turtle.getItemCount(i) ~= 0 then
return false
end
end
return true
end end
function Milo:getTurtleInventory() function Milo:getTurtleInventory()

View File

@@ -218,13 +218,14 @@ function Storage:updateCache(adapter, key, count)
else else
-- TODO: all items imported should be updated in itemdb -- TODO: all items imported should be updated in itemdb
-- error here if not -- error here if not
local item = itemDB:get(key) or itemDB:splitKey(key) local item = itemDB:get(key)
if item.displayName then if item then
entry = Util.shallowCopy(item) entry = Util.shallowCopy(item)
entry.count = count entry.count = count
entry.key = key entry.key = key
adapter.cache[key] = entry adapter.cache[key] = entry
else else
debug('STORAGE: item missing details')
-- TODO: somehow update itemdb with this maybe new item -- TODO: somehow update itemdb with this maybe new item
adapter.dirty = true adapter.dirty = true
self.dirty = true self.dirty = true
@@ -243,7 +244,7 @@ local function sn(name)
if #t ~= 2 then if #t ~= 2 then
return name return name
end end
return table.concat(table.unpack(t), '_') return table.concat(t, '_')
end end
function Storage:export(target, slot, count, item) function Storage:export(target, slot, count, item)
@@ -253,9 +254,11 @@ function Storage:export(target, slot, count, item)
local function provide(adapter) local function provide(adapter)
local amount = adapter:provide(item, count, slot, target or self.localName) local amount = adapter:provide(item, count, slot, target or self.localName)
if amount > 0 then if amount > 0 then
_G._debug('EXT: %s(%d): %s -> %s%s', _G._debug('EXT: %s(%d): %s -> %s%s',
item.name, amount, sn(adapter.name), sn(target or self.localName), item.displayName or item.name, amount, sn(adapter.name), sn(target or self.localName),
slot and string.format('[%d]', slot) or '') slot and string.format('[%d]', slot) or '')
self:updateCache(adapter, key, -amount) self:updateCache(adapter, key, -amount)
self:updateCache(self, key, -amount) self:updateCache(self, key, -amount)
end end
@@ -302,7 +305,7 @@ function Storage:import(source, slot, count, item)
if amount > 0 then if amount > 0 then
_G._debug('INS: %s(%d): %s[%d] -> %s', _G._debug('INS: %s(%d): %s[%d] -> %s',
item.name, amount, item.displayName or item.name, amount,
sn(source or self.localName), slot, sn(adapter.name)) sn(source or self.localName), slot, sn(adapter.name))
self:updateCache(adapter, key, amount) self:updateCache(adapter, key, amount)

View File

@@ -4,6 +4,7 @@ local Util = require('util')
local device = _G.device local device = _G.device
local fs = _G.fs local fs = _G.fs
local turtle = _G.turtle local turtle = _G.turtle
local intro = device['plethora:introspection']
local Craft = { local Craft = {
STATUS_INFO = 'info', STATUS_INFO = 'info',
@@ -16,20 +17,6 @@ local Craft = {
MACHINE_LOOKUP = 'usr/config/machine_crafting.db', MACHINE_LOOKUP = 'usr/config/machine_crafting.db',
} }
local function clearGrid(storage)
turtle.eachFilledSlot(function(slot)
storage:import(storage.localName, slot.index, slot.count, 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 function splitKey(key)
local t = Util.split(key, '(.-):') local t = Util.split(key, '(.-):')
local item = { } local item = { }
@@ -48,6 +35,27 @@ local function makeRecipeKey(item)
return table.concat({ item.name, item.damage or 0, item.nbtHash }, ':') return table.concat({ item.name, item.damage or 0, item.nbtHash }, ':')
end end
function Craft.clearGrid(storage)
turtle.eachFilledSlot(function(slot)
local item = slot
if intro then
item = intro.getInventory().getItemMeta(slot.index)
if not itemDB:get(item) then
itemDB:add(item)
end
end
storage:import(storage.localName, slot.index, slot.count, item)
end)
for i = 1, 16 do
if turtle.getItemCount(i) ~= 0 then
return false
end
end
return true
end
function Craft.getItemCount(items, item) function Craft.getItemCount(items, item)
if type(item) == 'string' then if type(item) == 'string' then
item = splitKey(item) item = splitKey(item)
@@ -117,7 +125,7 @@ local function machineCraft(recipe, storage, machineName, request, count, item)
end end
local function turtleCraft(recipe, storage, request, count) local function turtleCraft(recipe, storage, request, count)
if not clearGrid(storage) then if not Craft.clearGrid(storage) then
request.status = 'grid in use' request.status = 'grid in use'
request.statusCode = Craft.STATUS_ERROR request.statusCode = Craft.STATUS_ERROR
return return
@@ -142,7 +150,7 @@ local function turtleCraft(recipe, storage, request, count)
request.status = 'Failed to craft' request.status = 'Failed to craft'
request.statusCode = Craft.STATUS_ERROR request.statusCode = Craft.STATUS_ERROR
end end
clearGrid(storage) Craft.clearGrid(storage)
return request.statusCode == Craft.STATUS_SUCCESS return request.statusCode == Craft.STATUS_SUCCESS
end end

View File

@@ -7,20 +7,26 @@ local colors = _G.colors
local device = _G.device local device = _G.device
local turtle = _G.turtle local turtle = _G.turtle
--[[ Configuration Screen ]] --[[ Configuration Screen ]]--
local template =
[[%sBound Manipulator%s
Automatically import items into storage from your ender chest.
]]
local wizardPage = UI.Window { local wizardPage = UI.Window {
title = 'Manipulator', title = 'Manipulator',
index = 2, index = 2,
backgroundColor = colors.cyan, backgroundColor = colors.cyan,
[1] = UI.TextArea { form = UI.Form {
x = 2, ex = -2, y = 2, ey = -2, x = 1, y = 3, ex = -1, ey = -2,
value = string.format(template, Ansi.yellow, Ansi.reset), manualControls = true,
[1] = UI.Checkbox {
formLabel = 'Import', formKey = 'importEnder',
help = 'Locks chest to a single item type',
pruneEmpty = true,
},
[2] = UI.TextArea {
x = 13, ex = -2, y = 2,
value = 'Automatically import the user\'s ender chest contents',
},
},
userInfo = UI.TextArea {
x = 2, ex = -2, y = 2, height = 1,
}, },
} }
@@ -36,8 +42,19 @@ function wizardPage:isValidFor(node)
return node.mtype == 'manipulator' return node.mtype == 'manipulator'
end end
function wizardPage:setNode(node)
self.form:setValues(node)
self.userInfo.value = string.format('%sBound to: %s%s',
Ansi.black, Ansi.yellow, node.adapter.getName())
end
function wizardPage:validate()
return self.form:save()
end
UI:getPage('nodeWizard').wizard:add({ manipulator = wizardPage }) UI:getPage('nodeWizard').wizard:add({ manipulator = wizardPage })
--[[ Task ]]--
local task = { local task = {
name = 'manipulator', name = 'manipulator',
priority = 15, priority = 15,
@@ -45,7 +62,7 @@ local task = {
function task:cycle(context) function task:cycle(context)
local function filter(v) local function filter(v)
return v.adapter.getEnder return v.adapter.getEnder and v.importEnder
end end
for manipulator in context.storage:filterActive('manipulator', filter) do for manipulator in context.storage:filterActive('manipulator', filter) do

View File

@@ -30,6 +30,7 @@ function ReplenishTask:cycle(context)
damage = res.ignoreDamage and 0 or item.damage, damage = res.ignoreDamage and 0 or item.damage,
nbtHash = nbtHash, nbtHash = nbtHash,
requested = res.low - count, requested = res.low - count,
count = count,
name = item.name, name = item.name,
displayName = item.displayName, displayName = item.displayName,
replenish = true, replenish = true,