upgrade item keys

This commit is contained in:
kepler155c@gmail.com 2017-09-13 20:02:26 -04:00
parent 9aca96cc3e
commit b5ee5db16b
15 changed files with 1076 additions and 1116 deletions

View File

@ -3,7 +3,7 @@ local Util = require('util')
local itemDB = require('itemDB') local itemDB = require('itemDB')
local Peripheral = require('peripheral') local Peripheral = require('peripheral')
local ChestProvider = class() local ChestAdapter = class()
local keys = Util.transpose({ local keys = Util.transpose({
'damage', 'damage',
@ -14,7 +14,7 @@ local keys = Util.transpose({
'nbtHash', 'nbtHash',
}) })
function ChestProvider:init(args) function ChestAdapter:init(args)
local defaults = { local defaults = {
items = { }, items = { },
name = 'chest', name = 'chest',
@ -33,11 +33,11 @@ function ChestProvider:init(args)
end end
end end
function ChestProvider:isValid() function ChestAdapter:isValid()
return not not self.list return not not self.list
end end
function ChestProvider:getCachedItemDetails(item, k) function ChestAdapter:getCachedItemDetails(item, k)
local key = { item.name, item.damage, item.nbtHash } local key = { item.name, item.damage, item.nbtHash }
local detail = itemDB:get(key) local detail = itemDB:get(key)
@ -64,12 +64,12 @@ function ChestProvider:getCachedItemDetails(item, k)
end end
end end
function ChestProvider:refresh(throttle) function ChestAdapter:refresh(throttle)
return self:listItems(throttle) return self:listItems(throttle)
end end
-- provide a consolidated list of items -- provide a consolidated list of items
function ChestProvider:listItems(throttle) function ChestAdapter:listItems(throttle)
self.cache = { } self.cache = { }
local items = { } local items = { }
@ -82,13 +82,7 @@ function ChestProvider:listItems(throttle)
if not entry then if not entry then
entry = self:getCachedItemDetails(v, k) entry = self:getCachedItemDetails(v, k)
if entry then if entry then
entry.dmg = entry.damage
entry.id = entry.name
entry.count = 0 entry.count = 0
entry.display_name = entry.displayName
entry.max_size = entry.maxCount
entry.nbt_hash = entry.nbtHash
entry.lname = entry.displayName:lower()
self.cache[key] = entry self.cache[key] = entry
table.insert(items, entry) table.insert(items, entry)
end end
@ -96,7 +90,6 @@ function ChestProvider:listItems(throttle)
if entry then if entry then
entry.count = entry.count + v.count entry.count = entry.count + v.count
entry.qty = entry.count
end end
throttle() throttle()
end end
@ -106,24 +99,24 @@ function ChestProvider:listItems(throttle)
return items return items
end end
function ChestProvider:getItemInfo(id, dmg, nbtHash) function ChestAdapter:getItemInfo(name, damage, nbtHash)
if not self.cache then if not self.cache then
self:listItems() self:listItems()
end end
local key = table.concat({ id, dmg, nbtHash }, ':') local key = table.concat({ name, damage, nbtHash }, ':')
return self.cache[key] return self.cache[key]
end end
function ChestProvider:craft(id, dmg, qty) function ChestAdapter:craft(name, damage, qty)
end end
function ChestProvider:craftItems(items) function ChestAdapter:craftItems(items)
end end
function ChestProvider:provide(item, qty, slot, direction) function ChestAdapter:provide(item, qty, slot, direction)
local stacks = self.list() local stacks = self.list()
for key,stack in pairs(stacks) do for key,stack in pairs(stacks) do
if stack.name == item.id and stack.damage == item.dmg then if stack.name == item.name and stack.damage == item.damage then
local amount = math.min(qty, stack.count) local amount = math.min(qty, stack.count)
if amount > 0 then if amount > 0 then
self.pushItems(direction or self.direction, key, amount, slot) self.pushItems(direction or self.direction, key, amount, slot)
@ -136,12 +129,12 @@ function ChestProvider:provide(item, qty, slot, direction)
end end
end end
function ChestProvider:extract(slot, qty, toSlot) function ChestAdapter:extract(slot, qty, toSlot)
self.pushItems(self.direction, slot, qty, toSlot) self.pushItems(self.direction, slot, qty, toSlot)
end end
function ChestProvider:insert(slot, qty) function ChestAdapter:insert(slot, qty)
self.pullItems(self.direction, slot, qty) self.pullItems(self.direction, slot, qty)
end end
return ChestProvider return ChestAdapter

View File

@ -1,6 +1,5 @@
local class = require('class') local class = require('class')
local Util = require('util') local Util = require('util')
local Logger = require('logger')
local Peripheral = require('peripheral') local Peripheral = require('peripheral')
local MEProvider = class() local MEProvider = class()
@ -58,11 +57,28 @@ local function safeString(text)
return text return text
end end
local convertNames = {
name = 'id',
damage = 'dmg',
maxCount = 'max_size',
count = 'qty',
displayName = 'display_name',
maxDamage = 'max_dmg',
}
local function convertItem(item)
for k,v in pairs(convertNames) do
item[k] = item[v]
item[v] = nil
end
item.displayName = safeString(item.displayName)
end
function MEProvider:refresh() function MEProvider:refresh()
self.items = self.getAvailableItems('all') self.items = self.getAvailableItems('all')
for _,v in pairs(self.items) do for _,v in pairs(self.items) do
Util.merge(v, v.item) Util.merge(v, v.item)
v.name = safeString(v.display_name) convertItem(v)
end end
return self.items return self.items
end end
@ -72,25 +88,24 @@ function MEProvider:listItems()
return self.items return self.items
end end
function MEProvider:getItemInfo(id, dmg) function MEProvider:getItemInfo(name, damage)
for key,item in pairs(self.items) do for key,item in pairs(self.items) do
if item.id == id and item.dmg == dmg then if item.name == name and item.damage == damage then
return item return item
end end
end end
end end
function MEProvider:craft(id, dmg, qty) function MEProvider:craft(name, damage, count)
self:refresh() self:refresh()
local item = self:getItemInfo(id, dmg) local item = self:getItemInfo(name, damage)
if item and item.is_craftable then if item and item.is_craftable then
Logger.log('MEProvider', 'requested crafting for: ' .. id .. ':' .. dmg .. ' qty: ' .. qty) self.requestCrafting({ id = name, dmg = damage }, count)
self.requestCrafting({ id = id, dmg = dmg }, qty)
return true return true
end end
end end
@ -109,36 +124,32 @@ function MEProvider:craftItems(items)
if count >= #cpus then if count >= #cpus then
break break
end end
if self:craft(item.id, item.dmg, item.qty) then if self:craft(item.name, item.damage, item.count) then
count = count + 1 count = count + 1
end end
end end
end end
function MEProvider:provide(item, qty, slot) function MEProvider:provide(item, count, slot)
return pcall(function() return pcall(function()
self.exportItem({ self.exportItem({
id = item.id, id = item.name,
dmg = item.dmg dmg = item.damage
}, self.oside, qty, slot) }, self.oside, count, slot)
end) end)
end end
function MEProvider:insert(slot, qty) function MEProvider:insert(slot, count)
local s, m = pcall(function() self.pullItem(self.oside, slot, qty) end) local s, m = pcall(function() self.pullItem(self.oside, slot, count) end)
if not s and m then if not s and m then
print('MEProvider:pullItem') print('MEProvider:pullItem')
print(m) print(m)
Logger.log('MEProvider', 'Insert failed, trying again')
sleep(1) sleep(1)
s, m = pcall(function() self.pullItem(self.oside, slot, qty) end) s, m = pcall(function() self.pullItem(self.oside, slot, count) end)
if not s and m then if not s and m then
print('MEProvider:pullItem') print('MEProvider:pullItem')
print(m) print(m)
Logger.log('MEProvider', 'Insert failed again')
read() read()
else
Logger.log('MEProvider', 'Insert successful')
end end
end end
end end

View File

@ -3,7 +3,7 @@ local Util = require('util')
local Peripheral = require('peripheral') local Peripheral = require('peripheral')
local itemDB = require('itemDB') local itemDB = require('itemDB')
local RefinedProvider = class() local RefinedAdapter = class()
local keys = { local keys = {
'damage', 'damage',
@ -14,7 +14,7 @@ local keys = {
'nbtHash', 'nbtHash',
} }
function RefinedProvider:init(args) function RefinedAdapter:init(args)
local defaults = { local defaults = {
items = { }, items = { },
name = 'refinedStorage', name = 'refinedStorage',
@ -28,15 +28,15 @@ function RefinedProvider:init(args)
end end
end end
function RefinedProvider:isValid() function RefinedAdapter:isValid()
return not not self.listAvailableItems return not not self.listAvailableItems
end end
function RefinedProvider:isOnline() function RefinedAdapter:isOnline()
return self.getNetworkEnergyStored() > 0 return self.getNetworkEnergyStored() > 0
end end
function RefinedProvider:getCachedItemDetails(item) function RefinedAdapter:getCachedItemDetails(item)
local key = { item.name, item.damage, item.nbtHash } local key = { item.name, item.damage, item.nbtHash }
local detail = itemDB:get(key) local detail = itemDB:get(key)
@ -49,7 +49,6 @@ function RefinedProvider:getCachedItemDetails(item)
return return
end end
Util.merge(detail, meta) Util.merge(detail, meta)
detail.lname = detail.displayName:lower()
local t = { } local t = { }
for _,k in pairs(keys) do for _,k in pairs(keys) do
@ -65,7 +64,7 @@ function RefinedProvider:getCachedItemDetails(item)
end end
end end
function RefinedProvider:listItems() function RefinedAdapter:listItems()
local items = { } local items = { }
local list local list
@ -80,10 +79,7 @@ function RefinedProvider:listItems()
for _,v in pairs(list) do for _,v in pairs(list) do
local item = self:getCachedItemDetails(v) local item = self:getCachedItemDetails(v)
if item then if item then
item.display_name = item.displayName
item.id = v.name
item.count = v.count item.count = v.count
item.qty = v.count
table.insert(items, item) table.insert(items, item)
end end
throttle() throttle()
@ -94,7 +90,7 @@ function RefinedProvider:listItems()
return items return items
end end
function RefinedProvider:getItemInfo(fingerprint) function RefinedAdapter:getItemInfo(fingerprint)
local key = { fingerprint.name, fingerprint.damage, fingerprint.nbtHash } local key = { fingerprint.name, fingerprint.damage, fingerprint.nbtHash }
@ -106,12 +102,11 @@ function RefinedProvider:getItemInfo(fingerprint)
local detail = self.findItem(item) local detail = self.findItem(item)
if detail then if detail then
item.count = detail.count item.count = detail.count
item.qty = detail.count
return item return item
end end
end end
function RefinedProvider:isCrafting(item) function RefinedAdapter:isCrafting(item)
for _,task in pairs(self.getCraftingTasks()) do for _,task in pairs(self.getCraftingTasks()) do
local output = task.getPattern().outputs[1] local output = task.getPattern().outputs[1]
if output.name == item.name and if output.name == item.name and
@ -123,26 +118,26 @@ function RefinedProvider:isCrafting(item)
return false return false
end end
function RefinedProvider:craft(item, qty) function RefinedAdapter:craft(item, qty)
local detail = self.findItem(item) local detail = self.findItem(item)
if detail then if detail then
return detail.craft(qty) return detail.craft(qty)
end end
end end
function RefinedProvider:craftItems(items) function RefinedAdapter:craftItems(items)
return false return false
end end
function RefinedProvider:provide(item, qty, slot) function RefinedAdapter:provide(item, qty, slot)
end end
function RefinedProvider:extract(slot, qty) function RefinedAdapter:extract(slot, qty)
-- self.pushItems(self.direction, slot, qty) -- self.pushItems(self.direction, slot, qty)
end end
function RefinedProvider:insert(slot, qty) function RefinedAdapter:insert(slot, qty)
-- self.pullItems(self.direction, slot, qty) -- self.pullItems(self.direction, slot, qty)
end end
return RefinedProvider return RefinedAdapter

View File

@ -3,11 +3,11 @@ local Util = require('util')
local Craft = { } local Craft = { }
local function clearGrid(chestProvider) local function clearGrid(chestAdapter)
for i = 1, 16 do for i = 1, 16 do
local count = turtle.getItemCount(i) local count = turtle.getItemCount(i)
if count > 0 then if count > 0 then
chestProvider:insert(i, count) chestAdapter:insert(i, count)
if turtle.getItemCount(i) ~= 0 then if turtle.getItemCount(i) ~= 0 then
return false return false
end end
@ -39,13 +39,13 @@ local function getItemCount(items, key)
return 0 return 0
end end
local function turtleCraft(recipe, qty, chestProvider) local function turtleCraft(recipe, qty, chestAdapter)
clearGrid(chestProvider) clearGrid(chestAdapter)
for k,v in pairs(recipe.ingredients) do for k,v in pairs(recipe.ingredients) do
local item = splitKey(v) local item = splitKey(v)
chestProvider:provide({ id = item.name, dmg = item.damage, nbt_hash = item.nbtHash }, qty, k) chestAdapter:provide(item, qty, k)
if turtle.getItemCount(k) == 0 then -- ~= qty then if turtle.getItemCount(k) == 0 then -- ~= qty then
-- FIX: ingredients cannot be stacked -- FIX: ingredients cannot be stacked
return false return false
@ -55,9 +55,9 @@ local function turtleCraft(recipe, qty, chestProvider)
return turtle.craft() return turtle.craft()
end end
function Craft.craftRecipe(recipe, count, chestProvider) function Craft.craftRecipe(recipe, count, chestAdapter)
local items = chestProvider:listItems() local items = chestAdapter:listItems()
local function sumItems(items) local function sumItems(items)
-- produces { ['minecraft:planks:0'] = 8 } -- produces { ['minecraft:planks:0'] = 8 }
@ -81,7 +81,7 @@ function Craft.craftRecipe(recipe, count, chestProvider)
Util.print('Crafting %d %s', icount * count - itemCount, key) Util.print('Crafting %d %s', icount * count - itemCount, key)
if not Craft.craftRecipe(irecipe, if not Craft.craftRecipe(irecipe,
icount * count - itemCount, icount * count - itemCount,
chestProvider) then chestAdapter) then
turtle.select(1) turtle.select(1)
return return
end end
@ -89,7 +89,7 @@ Util.print('Crafting %d %s', icount * count - itemCount, key)
end end
end end
repeat repeat
if not turtleCraft(recipe, math.min(count, maxCount), chestProvider) then if not turtleCraft(recipe, math.min(count, maxCount), chestAdapter) then
turtle.select(1) turtle.select(1)
return false return false
end end
@ -157,10 +157,10 @@ function Craft.getCraftableAmountTest()
end end
function Craft.craftRecipeTest(name, count) function Craft.craftRecipeTest(name, count)
local ChestProvider = require('chestProvider18') local ChestAdapter = require('chestAdapter18')
local chestProvider = ChestProvider({ wrapSide = 'top', direction = 'down' }) local chestAdapter = ChestAdapter({ wrapSide = 'top', direction = 'down' })
Craft.setRecipes(Util.readTable('usr/etc/recipes.db')) Craft.setRecipes(Util.readTable('usr/etc/recipes.db'))
return { Craft.craftRecipe(Craft.recipes[name], count, chestProvider) } return { Craft.craftRecipe(Craft.recipes[name], count, chestAdapter) }
end end
return Craft return Craft

View File

@ -38,6 +38,7 @@ local function dig(action)
local hi = turtle.getHeadingInfo(direction) local hi = turtle.getHeadingInfo(direction)
local node = { x = turtle.point.x + hi.xd, y = turtle.point.y + hi.yd, z = turtle.point.z + hi.zd } local node = { x = turtle.point.x + hi.xd, y = turtle.point.y + hi.yd, z = turtle.point.z + hi.zd }
if Point.inBox(node, box) then if Point.inBox(node, box) then
local key = toKey(node) local key = toKey(node)
@ -57,8 +58,10 @@ local function move(action)
dig(turtle.getAction('forward')) dig(turtle.getAction('forward'))
elseif action == 'up' then elseif action == 'up' then
dig(turtle.getAction('up')) dig(turtle.getAction('up'))
dig(turtle.getAction('forward'))
elseif action == 'down' then elseif action == 'down' then
dig(turtle.getAction('down')) dig(turtle.getAction('down'))
dig(turtle.getAction('forward'))
elseif action == 'back' then elseif action == 'back' then
dig(turtle.getAction('up')) dig(turtle.getAction('up'))
dig(turtle.getAction('down')) dig(turtle.getAction('down'))
@ -123,7 +126,9 @@ return function(startPt, endPt, firstPt, verbose)
box.ey = math.max(startPt.y, endPt.y) box.ey = math.max(startPt.y, endPt.y)
box.ez = math.max(startPt.z, endPt.z) box.ez = math.max(startPt.z, endPt.z)
turtle.pathfind(firstPt) if not turtle.pathfind(firstPt) then
error('failed to reach starting point')
end
turtle.setPolicy("attack", { dig = dig }, "assuredMove") turtle.setPolicy("attack", { dig = dig }, "assuredMove")

View File

@ -4,20 +4,20 @@ end
requireInjector(getfenv(1)) requireInjector(getfenv(1))
local Blocks = require('blocks') local Blocks = require('blocks')
local class = require('class') local class = require('class')
local Event = require('event') local Event = require('event')
local MEProvider = require('meProvider') local MEAdapter = require('meAdapter')
local Message = require('message') local Message = require('message')
local Point = require('point') local Point = require('point')
local Schematic = require('schematic') local Schematic = require('schematic')
local TableDB = require('tableDB') local TableDB = require('tableDB')
local UI = require('ui') local UI = require('ui')
local Util = require('util') local Util = require('util')
local ChestProvider = require('chestProvider') local ChestAdapter = require('chestAdapter')
if Util.getVersion() == 1.8 then if Util.getVersion() == 1.8 then
ChestProvider = require('chestProvider18') ChestAdapter = require('chestAdapter18')
end end
if not _G.device then if not _G.device then
@ -45,6 +45,40 @@ local Builder = {
local pistonFacings local pistonFacings
-- Temp functions until conversion to new adapters is complete
local function convertSingleForward(item)
item.name = item.id
item.damage = item.dmg
item.count = item.count
item.maxCount = item.max_size
return item
end
local function convertForward(t)
for _,v in pairs(t) do
convertSingleForward(v)
end
return t
end
local function convertSingleBack(item)
if item then
item.id = item.name
item.dmg = item.damage
item.qty = item.count
item.max_size = item.maxCount
item.display_name = item.displayName
end
return item
end
local function convertBack(t)
for _,v in pairs(t) do
convertSingleBack(v)
end
return t
end
--[[-- SubDB --]]-- --[[-- SubDB --]]--
subDB = TableDB({ subDB = TableDB({
fileName = fs.combine(BUILDER_DIR, 'sub.db'), fileName = fs.combine(BUILDER_DIR, 'sub.db'),
@ -397,7 +431,7 @@ function Builder:dumpInventory()
for i = 1, 16 do for i = 1, 16 do
local qty = turtle.getItemCount(i) local qty = turtle.getItemCount(i)
if qty > 0 then if qty > 0 then
self.itemProvider:insert(i, qty) self.itemAdapter:insert(i, qty)
end end
if turtle.getItemCount(i) ~= 0 then if turtle.getItemCount(i) ~= 0 then
success = false success = false
@ -411,7 +445,7 @@ end
function Builder:dumpInventoryWithCheck() function Builder:dumpInventoryWithCheck()
while not self:dumpInventory() do while not self:dumpInventory() do
print('Provider is full or missing - make space or replace') print('Storage is full or missing - make space or replace')
print('Press enter to continue') print('Press enter to continue')
turtle.setHeading(0) turtle.setHeading(0)
read() read()
@ -435,17 +469,17 @@ function Builder:autocraft(supplies)
item.qty = item.qty + (s.need - s.qty) item.qty = item.qty + (s.need - s.qty)
end end
Builder.itemProvider:craftItems(t) Builder.itemAdapter:craftItems(convertForward(t))
end end
function Builder:getSupplies() function Builder:getSupplies()
self.itemProvider:refresh() self.itemAdapter:refresh()
local t = { } local t = { }
for _,s in ipairs(self.slots) do for _,s in ipairs(self.slots) do
if s.need > 0 then if s.need > 0 then
local item = self.itemProvider:getItemInfo(s.id, s.dmg) local item = convertSingleBack(self.itemAdapter:getItemInfo(s.id, s.dmg))
if item then if item then
s.name = item.display_name s.name = item.display_name
@ -459,7 +493,7 @@ function Builder:getSupplies()
s.need = qty s.need = qty
end end
if qty > 0 then if qty > 0 then
self.itemProvider:provide(item, qty, s.index) self.itemAdapter:provide(convertSingleForward(item), qty, s.index)
s.qty = turtle.getItemCount(s.index) s.qty = turtle.getItemCount(s.index)
end end
else else
@ -485,7 +519,7 @@ function Builder:refuel()
local fuel = subDB:getSubstitutedItem(self.fuelItem.id, self.fuelItem.dmg) local fuel = subDB:getSubstitutedItem(self.fuelItem.id, self.fuelItem.dmg)
self.itemProvider:provide(fuel, 64, 1) self.itemAdapter:provide(convertSingleForward(fuel), 64, 1)
if turtle.getItemCount(1) == 0 then if turtle.getItemCount(1) == 0 then
print('Out of fuel, add fuel to chest/ME system') print('Out of fuel, add fuel to chest/ME system')
turtle.setHeading(0) turtle.setHeading(0)
@ -523,23 +557,23 @@ function Builder:inAirDropoff()
turtle.goto(pt.x, pt.z, pt.y) turtle.goto(pt.x, pt.z, pt.y)
os.sleep(.1) -- random computer is not connected error os.sleep(.1) -- random computer is not connected error
local chestProvider = ChestProvider({ direction = 'down', wrapSide = 'top' }) local chestAdapter = ChestAdapter({ direction = 'down', wrapSide = 'top' })
if not chestProvider:isValid() then if not chestAdapter:isValid() then
self:log('Chests above is not valid') self:log('Chests above is not valid')
return false return false
end end
local oldProvider = self.itemProvider local oldAdapter = self.itemAdapter
self.itemProvider = chestProvider self.itemAdapter = chestAdapter
if not self:dumpInventory() then if not self:dumpInventory() then
self:log('Unable to dump inventory') self:log('Unable to dump inventory')
self.itemProvider = oldProvider self.itemAdapter = oldAdapter
return false return false
end end
self.itemProvider = oldProvider self.itemAdapter = oldAdapter
Message.broadcast('thanks', { }) Message.broadcast('thanks', { })
@ -563,7 +597,7 @@ function Builder:inAirResupply()
return false return false
end end
local oldProvider = self.itemProvider local oldAdapter = self.itemAdapter
self:log('Requesting air supply drop for supply #: ' .. self.slotUid) self:log('Requesting air supply drop for supply #: ' .. self.slotUid)
while true do while true do
@ -571,7 +605,7 @@ function Builder:inAirResupply()
local _, id, msg, _ = Message.waitForMessage('gotSupplies', 1) local _, id, msg, _ = Message.waitForMessage('gotSupplies', 1)
if not msg or not msg.contents then if not msg or not msg.contents then
self.itemProvider = oldProvider self.itemAdapter = oldAdapter
return false return false
end end
@ -586,17 +620,17 @@ function Builder:inAirResupply()
turtle.goto(pt.x, pt.z, pt.y) turtle.goto(pt.x, pt.z, pt.y)
os.sleep(.1) -- random computer is not connected error os.sleep(.1) -- random computer is not connected error
local chestProvider = ChestProvider({ direction = 'down', wrapSide = 'top' }) local chestAdapter = ChestAdapter({ direction = 'down', wrapSide = 'top' })
if not chestProvider:isValid() then if not chestAdapter:isValid() then
Util.print('not valid') Util.print('not valid')
read() read()
end end
self.itemProvider = chestProvider self.itemAdapter = chestAdapter
if not self:dumpInventory() then if not self:dumpInventory() then
self.itemProvider = oldProvider self.itemAdapter = oldAdapter
return false return false
end end
self:refuel() self:refuel()
@ -606,7 +640,7 @@ function Builder:inAirResupply()
Message.broadcast('thanks', { }) Message.broadcast('thanks', { })
self.itemProvider = oldProvider self.itemAdapter = oldAdapter
if #supplies == 0 then if #supplies == 0 then
@ -1449,7 +1483,7 @@ end
function substitutionPage:enable() function substitutionPage:enable()
self.allItems = Builder.itemProvider:refresh() self.allItems = convertBack(Builder.itemAdapter:refresh())
self.grid.values = self.allItems self.grid.values = self.allItems
for _,item in pairs(self.grid.values) do for _,item in pairs(self.grid.values) do
item.key = item.id .. ':' .. item.dmg item.key = item.id .. ':' .. item.dmg
@ -1583,7 +1617,7 @@ function supplyPage:eventHandler(event)
if event.type == 'craft' then if event.type == 'craft' then
local s = self.grid:getSelected() local s = self.grid:getSelected()
if Builder.itemProvider:craft(s.id, s.dmg, s.need-s.qty) then if Builder.itemAdapter:craft(s.id, s.dmg, s.need-s.qty) then
local name = s.name or '' local name = s.name or ''
self.statusBar:timedStatus('Requested ' .. s.need-s.qty .. ' ' .. name, 3) self.statusBar:timedStatus('Requested ' .. s.need-s.qty .. ' ' .. name, 3)
else else
@ -1708,12 +1742,12 @@ function listingPage:eventHandler(event)
if event.type == 'craft' then if event.type == 'craft' then
local s = self.grid:getSelected() local s = self.grid:getSelected()
local item = Builder.itemProvider:getItemInfo(s.id, s.dmg) local item = convertSingleBack(Builder.itemAdapter:getItemInfo(s.id, s.dmg))
if item and item.is_craftable then if item and item.is_craftable then
local qty = math.max(0, s.need - item.qty) local qty = math.max(0, s.need - item.qty)
if item then if item then
Builder.itemProvider:craft(s.id, s.dmg, qty) Builder.itemAdapter:craft(s.id, s.dmg, qty)
local name = s.name or s.key local name = s.name or s.key
self.statusBar:timedStatus('Requested ' .. qty .. ' ' .. name, 3) self.statusBar:timedStatus('Requested ' .. qty .. ' ' .. name, 3)
end end
@ -1768,11 +1802,11 @@ function listingPage:refresh(throttle)
local supplyList = Builder:getBlockCounts() local supplyList = Builder:getBlockCounts()
Builder.itemProvider:refresh(throttle) Builder.itemAdapter:refresh(throttle)
for _,b in pairs(supplyList) do for _,b in pairs(supplyList) do
if b.need > 0 then if b.need > 0 then
local item = Builder.itemProvider:getItemInfo(b.id, b.dmg) local item = convertSingleBack(Builder.itemAdapter:getItemInfo(b.id, b.dmg))
if item then if item then
local block = blocks.blockDB:lookup(b.id, b.dmg) local block = blocks.blockDB:lookup(b.id, b.dmg)
@ -2054,10 +2088,10 @@ if #args < 1 then
error('supply file name') error('supply file name')
end end
Builder.itemProvider = MEProvider() Builder.itemAdapter = MEAdapter()
if not Builder.itemProvider:isValid() then if not Builder.itemAdapter:isValid() then
Builder.itemProvider = ChestProvider() Builder.itemAdapter = ChestAdapter()
if not Builder.itemProvider:isValid() then if not Builder.itemAdapter:isValid() then
error('A chest or ME interface must be below turtle') error('A chest or ME interface must be below turtle')
end end
end end

View File

@ -1,15 +1,15 @@
requireInjector(getfenv(1)) requireInjector(getfenv(1))
local ChestProvider = require('chestProvider18') local ChestAdapter = require('chestAdapter18')
local Config = require('config') local Config = require('config')
local Craft = require('turtle.craft') local Craft = require('turtle.craft')
local Event = require('event') local Event = require('event')
local itemDB = require('itemDB') local itemDB = require('itemDB')
local Peripheral = require('peripheral') local Peripheral = require('peripheral')
local RefinedProvider = require('refinedProvider') local RefinedAdapter = require('refinedAdapter')
local Terminal = require('terminal') local Terminal = require('terminal')
local UI = require('ui') local UI = require('ui')
local Util = require('util') local Util = require('util')
multishell.setTitle(multishell.getCurrent(), 'Resource Manager') multishell.setTitle(multishell.getCurrent(), 'Resource Manager')
@ -25,14 +25,14 @@ local config = {
Config.load('resourceManager', config) Config.load('resourceManager', config)
local controller = RefinedProvider() local controller = RefinedAdapter()
if not controller:isValid() then if not controller:isValid() then
-- error('Refined storage controller not found') -- error('Refined storage controller not found')
controller = nil controller = nil
end end
local chestProvider = ChestProvider({ direction = 'west', wrapSide = 'back' }) local chestAdapter = ChestAdapter({ direction = 'west', wrapSide = 'back' })
local turtleChestProvider = ChestProvider({ direction = 'up', wrapSide = 'bottom' }) local turtleChestAdapter = ChestAdapter({ direction = 'up', wrapSide = 'bottom' })
local RESOURCE_FILE = 'usr/etc/resources.db' local RESOURCE_FILE = 'usr/etc/resources.db'
local RECIPES_FILE = 'sys/etc/recipes.db' local RECIPES_FILE = 'sys/etc/recipes.db'
@ -195,7 +195,7 @@ local function clearGrid()
for i = 1, 16 do for i = 1, 16 do
local count = turtle.getItemCount(i) local count = turtle.getItemCount(i)
if count > 0 then if count > 0 then
chestProvider:insert(i, count) chestAdapter:insert(i, count)
if turtle.getItemCount(i) ~= 0 then if turtle.getItemCount(i) ~= 0 then
return false return false
end end
@ -224,9 +224,9 @@ local function craftItem(recipe, items, originalItem, craftList, count)
local toCraft = Craft.getCraftableAmount(recipe, count, items) local toCraft = Craft.getCraftableAmount(recipe, count, items)
if toCraft > 0 then if toCraft > 0 then
Craft.craftRecipe(recipe, toCraft, chestProvider) Craft.craftRecipe(recipe, toCraft, chestAdapter)
clearGrid() clearGrid()
items = chestProvider:listItems() items = chestAdapter:listItems()
end end
count = count - toCraft count = count - toCraft
@ -250,7 +250,7 @@ local function craftItems(craftList, allItems)
local recipe = recipes[key] local recipe = recipes[key]
if recipe then if recipe then
craftItem(recipe, allItems, item, craftList, item.count) craftItem(recipe, allItems, item, craftList, item.count)
allItems = chestProvider:listItems() -- refresh counts allItems = chestAdapter:listItems() -- refresh counts
elseif item.rsControl then elseif item.rsControl then
item.status = 'Activated' item.status = 'Activated'
end end
@ -368,7 +368,7 @@ local function watchResources(items)
end end
if res.limit and item.count > res.limit then if res.limit and item.count > res.limit then
chestProvider:provide({ id = res.name, dmg = res.damage, nbtHash = res.nbt_hash }, item.count - res.limit, nil, config.trashDirection) chestAdapter:provide(res, item.count - res.limit, nil, config.trashDirection)
elseif res.low and item.count < res.low then elseif res.low and item.count < res.low then
if res.ignoreDamage then if res.ignoreDamage then
@ -701,7 +701,7 @@ function listingPage:enable()
end end
function listingPage:refresh() function listingPage:refresh()
self.allItems = chestProvider:listItems() self.allItems = chestAdapter:listItems()
mergeResources(self.allItems) mergeResources(self.allItems)
self:applyFilter() self:applyFilter()
end end
@ -733,10 +733,10 @@ local function getTurtleInventory()
for i = 1,16 do for i = 1,16 do
local qty = turtle.getItemCount(i) local qty = turtle.getItemCount(i)
if qty > 0 then if qty > 0 then
turtleChestProvider:insert(i, qty) turtleChestAdapter:insert(i, qty)
local items = turtleChestProvider:listItems() local items = turtleChestAdapter:listItems()
_, inventory[i] = next(items) _, inventory[i] = next(items)
turtleChestProvider:extract(1, qty, i) turtleChestAdapter:extract(1, qty, i)
end end
end end
return inventory return inventory
@ -759,26 +759,26 @@ local function learnRecipe(page)
if device.workbench and turtle.craft() then if device.workbench and turtle.craft() then
recipe = getTurtleInventory() recipe = getTurtleInventory()
if recipe and recipe[1] then if recipe and recipe[1] then
recipe = recipe[1]
local key = uniqueKey(recipe)
clearGrid() clearGrid()
filter(recipe, { 'name', 'damage', 'nbtHash', 'count', 'maxCount' }) local key = uniqueKey(recipe[1])
local newRecipe = {
for _,ingredient in pairs(ingredients) do count = recipe[1].count,
filter(ingredient, { 'name', 'damage', 'nbtHash', 'count', 'maxCount' }) ingredients = ingredients,
--if ingredient.max_dmg > 0 then -- let's try this... }
-- ingredient.dmg = 0 if recipe[1].maxCount ~= 64 then
--end newRecipe.maxCount = recipe[1].maxCount
end end
recipe.ingredients = ingredients
recipes[key] = recipe for k,ingredient in pairs(ingredients) do
ingredients[k] = uniqueKey(ingredient)
end
recipes[key] = newRecipe
Util.writeTable(RECIPES_FILE, recipes) Util.writeTable(RECIPES_FILE, recipes)
local displayName = getName(recipe) local displayName = getName(recipe[1])
listingPage.statusBar.filter:setValue(displayName) listingPage.statusBar.filter:setValue(displayName)
listingPage.statusBar:timedStatus('Learned: ' .. displayName, 3) listingPage.statusBar:timedStatus('Learned: ' .. displayName, 3)
@ -914,7 +914,7 @@ jobListGrid:sync()
Event.onInterval(5, function() Event.onInterval(5, function()
if not craftingPaused then if not craftingPaused then
local items = chestProvider:listItems() local items = chestAdapter:listItems()
if Util.size(items) == 0 then if Util.size(items) == 0 then
jobListGrid.parent:clear() jobListGrid.parent:clear()
jobListGrid.parent:centeredWrite(math.ceil(jobListGrid.parent.height/2), 'No items in system') jobListGrid.parent:centeredWrite(math.ceil(jobListGrid.parent.height/2), 'No items in system')

View File

@ -1,40 +0,0 @@
requireInjector(getfenv(1))
local RefinedProvider = require('refinedProvider')
local TableDB = require('tableDB')
local controller = RefinedProvider()
if not controller:isValid() then
error('Refined storage controller not found')
end
local itemInfoDB = TableDB({
fileName = 'items.db'
})
itemInfoDB:load()
local items = controller:listItems()
local keys = {
'fields',
'damage',
'displayName',
'maxCount',
'maxDamage',
'name',
'nbtHash',
'rawName',
}
for _, item in pairs(items) do
local t = { }
for _,key in pairs(keys) do
t[key] = item[key]
end
itemInfoDB:add({ item.name, item.damage, item.nbtHash }, t)
end
itemInfoDB:flush()

View File

@ -15,22 +15,22 @@ local script = [[
requireInjector(getfenv(1)) requireInjector(getfenv(1))
local GPS = require('gps') local GPS = require('gps')
local ChestProvider = require('chestProvider18') local ChestAdapter = require('chestAdapter18')
local Point = require('point') local Point = require('point')
local Util = require('util') local Util = require('util')
local itemProvider local itemAdapter
function dumpInventory() function dumpInventory()
for i = 1, 16 do for i = 1, 16 do
local qty = turtle.getItemCount(i) local qty = turtle.getItemCount(i)
if qty > 0 then if qty > 0 then
itemProvider:insert(i, qty) itemAdapter:insert(i, qty)
end end
if turtle.getItemCount(i) ~= 0 then if turtle.getItemCount(i) ~= 0 then
print('Provider is full or missing - make space or replace') print('Adapter is full or missing - make space or replace')
print('Press enter to continue') print('Press enter to continue')
read() read()
end end
@ -43,7 +43,7 @@ local function refuel()
print('Refueling') print('Refueling')
turtle.select(1) turtle.select(1)
itemProvider:provide({ id = 'minecraft:coal', dmg = 0 }, 64, 1) itemAdapter:provide({ name = 'minecraft:coal', damage = 0 }, 64, 1)
if turtle.getItemCount(1) == 0 then if turtle.getItemCount(1) == 0 then
print('Out of fuel, add fuel to chest/ME system') print('Out of fuel, add fuel to chest/ME system')
turtle.status = 'waiting' turtle.status = 'waiting'
@ -73,7 +73,7 @@ local function resupply()
if data.suppliesPt then if data.suppliesPt then
pathTo(data.suppliesPt) pathTo(data.suppliesPt)
itemProvider = ChestProvider({ direction = 'up', wrapSide = 'bottom' }) itemAdapter = ChestAdapter({ direction = 'up', wrapSide = 'bottom' })
dumpInventory() dumpInventory()
refuel() refuel()
end end

View File

@ -1,17 +1,17 @@
requireInjector(getfenv(1)) requireInjector(getfenv(1))
local ChestProvider = require('chestProvider18') local ChestAdapter = require('chestAdapter18')
local Event = require('event') local Event = require('event')
local MEProvider = require('meProvider') local MEAdapter = require('meAdapter')
local RefinedProvider = require('refinedProvider') local RefinedAdapter = require('refinedAdapter')
local UI = require('ui') local UI = require('ui')
local Util = require('util') local Util = require('util')
local storage = RefinedProvider() local storage = RefinedAdapter()
if not storage:isValid() then if not storage:isValid() then
storage = MEProvider() storage = MEAdapter()
if not storage:isValid() then if not storage:isValid() then
storage = ChestProvider() storage = ChestAdapter()
end end
end end
@ -25,11 +25,11 @@ UI:configure('StorageActivity', ...)
local changedPage = UI.Page({ local changedPage = UI.Page({
grid = UI.Grid({ grid = UI.Grid({
columns = { columns = {
{ heading = 'Qty', key = 'qty', width = 5 }, { heading = 'Qty', key = 'count', width = 5 },
{ heading = 'Change', key = 'change', width = 6 }, { heading = 'Change', key = 'change', width = 6 },
{ heading = 'Name', key = 'display_name', width = UI.term.width - 15 }, { heading = 'Name', key = 'displayName', width = UI.term.width - 15 },
}, },
sortColumn = 'display_name', sortColumn = 'displayName',
rey = -6, rey = -6,
}), }),
buttons = UI.Window({ buttons = UI.Window({
@ -77,7 +77,7 @@ function changedPage.grid:getDisplayValues(row)
ind = '' ind = ''
end end
row.change = ind .. Util.toBytes(row.change) row.change = ind .. Util.toBytes(row.change)
row.qty = Util.toBytes(row.qty) row.count = Util.toBytes(row.count)
return row return row
end end
@ -132,9 +132,9 @@ function changedPage:refresh()
found = false found = false
for k2,v2 in pairs(t) do for k2,v2 in pairs(t) do
if uniqueKey(v) == uniqueKey(v2) then if uniqueKey(v) == uniqueKey(v2) then
if v.qty ~= v2.qty then if v.count ~= v2.count then
local c = Util.shallowCopy(v2) local c = Util.shallowCopy(v2)
c.lastQty = v.qty c.lastCount = v.count
table.insert(changedItems, c) table.insert(changedItems, c)
end end
table.remove(t, k2) table.remove(t, k2)
@ -145,19 +145,19 @@ function changedPage:refresh()
-- New item -- New item
if not found then if not found then
local c = Util.shallowCopy(v) local c = Util.shallowCopy(v)
c.lastQty = v.qty c.lastCount = v.count
c.qty = 0 c.count = 0
table.insert(changedItems, c) table.insert(changedItems, c)
end end
end end
-- No items left -- No items left
for k,v in pairs(t) do for k,v in pairs(t) do
v.lastQty = 0 v.lastCount = 0
table.insert(changedItems, v) table.insert(changedItems, v)
end end
for k,v in pairs(changedItems) do for k,v in pairs(changedItems) do
v.change = v.qty - v.lastQty v.change = v.count - v.lastCount
end end
self.grid:setValues(changedItems) self.grid:setValues(changedItems)

View File

@ -4,7 +4,7 @@ requireInjector(getfenv(1))
Requirements: Requirements:
Place turtle against an oak tree or oak sapling Place turtle against an oak tree or oak sapling
Area around turtle must be flat and can only be dirt or grass Area around turtle must be flat and can only be dirt or grass
(9 blocks in each direction from turtle) (10 blocks in each direction from turtle)
Turtle must have: crafting table, chest Turtle must have: crafting table, chest
Turtle must have a pick equipped on the left side Turtle must have a pick equipped on the left side
@ -16,15 +16,15 @@ requireInjector(getfenv(1))
down another sapling in front of the turtle. down another sapling in front of the turtle.
The program will be able to survive server restarts as long as it has The program will be able to survive server restarts as long as it has
created the cobble line. If the program is stopped before that time, created the cobblestone line. If the program is stopped before that time,
place the turtle in the original position before restarting the program. place the turtle in the original position before restarting the program.
]]-- ]]--
local ChestProvider = require('chestProvider18') local ChestAdapter = require('chestAdapter18')
local Craft = require('turtle.craft') local Craft = require('turtle.craft')
local Level = require('turtle.level') local Level = require('turtle.level')
local Point = require('point') local Point = require('point')
local Util = require('util') local Util = require('util')
local FUEL_BASE = 0 local FUEL_BASE = 0
local FUEL_DIRE = FUEL_BASE + 10 local FUEL_DIRE = FUEL_BASE + 10
@ -126,18 +126,18 @@ local function craftItem(item, qty)
if turtle.equip('left', 'minecraft:crafting_table') then if turtle.equip('left', 'minecraft:crafting_table') then
local chestProvider = ChestProvider({ local chestAdapter = ChestAdapter({
wrapSide = 'top', wrapSide = 'top',
direction = 'down', direction = 'down',
}) })
if not chestProvider:isValid() then if not chestAdapter:isValid() then
print('invalid chestProvider') print('invalid chestAdapter')
read() read()
end end
-- turtle.emptyInventory(turtle.dropUp) -- turtle.emptyInventory(turtle.dropUp)
Util.print('Crafting %d %s', (qty or 1), item) Util.print('Crafting %d %s', (qty or 1), item)
success = Craft.craftRecipe(recipes[item], qty or 1, chestProvider) success = Craft.craftRecipe(recipes[item], qty or 1, chestAdapter)
repeat until not turtle.suckUp() repeat until not turtle.suckUp()
end end
@ -265,7 +265,7 @@ local function getCobblestone(count)
turtle.select(1) turtle.select(1)
turtle.digDown() turtle.digDown()
turtle.down() turtle.down()
for i = 1, 3 do for i = 1, 4 do
if inspect(turtle.inspect) == STONE then if inspect(turtle.inspect) == STONE then
turtle.dig() turtle.dig()
end end
@ -353,7 +353,7 @@ local function createChests()
return false return false
end end
if state.perimeter and if state.perimeter and
turtle.getFuelLevel() > FUEL_BASE + 100 and turtle.getFuelLevel() > FUEL_GOOD and
Craft.canCraft(CHEST, 4, turtle.getSummedInventory()) then Craft.canCraft(CHEST, 4, turtle.getSummedInventory()) then
print('Adding storage') print('Adding storage')
@ -428,8 +428,8 @@ local function placeTorches()
return return
end end
if Craft.canCraft(TORCH, 4, turtle.getSummedInventory()) and if turtle.getFuelLevel() > 100 and
turtle.getFuelLevel() > 100 then Craft.canCraft(TORCH, 4, turtle.getSummedInventory()) then
print('Placing torches') print('Placing torches')
@ -525,6 +525,7 @@ local function fell()
fellTree(pt) fellTree(pt)
end end
turtle.placeAt(pt, randomSapling()) turtle.placeAt(pt, randomSapling())
turtle.select(1)
end) end)
print('Used ' .. (fuel - turtle.getFuelLevel()) .. ' fuel') print('Used ' .. (fuel - turtle.getFuelLevel()) .. ' fuel')
@ -635,14 +636,14 @@ local function findHome()
while inspect(turtle.inspectDown) ~= COBBLESTONE do while inspect(turtle.inspectDown) ~= COBBLESTONE do
pt.x = pt.x - 1 pt.x = pt.x - 1
turtle.pathfind(pt) turtle.pathfind(pt)
if pt.x < -16 then if pt.x < -20 then
error('lost') error('lost')
end end
end end
while inspect(turtle.inspectDown) == COBBLESTONE do while inspect(turtle.inspectDown) == COBBLESTONE do
pt.z = pt.z - 1 pt.z = pt.z - 1
turtle.pathfind(pt) turtle.pathfind(pt)
if pt.z < -16 then if pt.z < -20 then
error('lost') error('lost')
end end
end end

File diff suppressed because it is too large Load Diff

View File

@ -430,18 +430,17 @@ function turtle.setHeading(heading)
end end
if heading - turtle.point.heading == 3 then if heading - turtle.point.heading == 3 then
turtle.native.turnLeft() turtle.native.turnLeft()
turtle.point.heading = turtle.point.heading - 1 turtle.point.heading = (turtle.point.heading - 1) % 4
state.moveCallback('turn', turtle.point)
else else
local turns = heading - turtle.point.heading local turns = heading - turtle.point.heading
while turns > 0 do while turns > 0 do
turns = turns - 1 turns = turns - 1
turtle.point.heading = turtle.point.heading + 1
turtle.native.turnRight() turtle.native.turnRight()
turtle.point.heading = (turtle.point.heading + 1) % 4
state.moveCallback('turn', turtle.point)
end end
end end
turtle.point.heading = turtle.point.heading % 4
state.moveCallback('turn', turtle.point)
end end
return turtle.point return turtle.point

View File

@ -6,7 +6,7 @@ if device.wireless_modem then
local config = { } local config = { }
Config.load('gps', config) Config.load('gps', config)
if config.host then if config.host and type(config.host) == 'table' then
multishell.setTitle(multishell.getCurrent(), 'GPS Daemon') multishell.setTitle(multishell.getCurrent(), 'GPS Daemon')