Milo: remote learn + remove logger

This commit is contained in:
kepler155c@gmail.com
2019-01-22 15:50:37 -05:00
parent e4cfba5750
commit 4276ea533f
13 changed files with 363 additions and 282 deletions

View File

@@ -75,7 +75,7 @@ local function client(socket)
if not data then
break
end
--_G._debug(data)
_G._debug(data)
socket.co = coroutine.running()
if data.request == 'scan' then -- full scan of all inventories
@@ -163,6 +163,12 @@ local function client(socket)
craft = request.craft,
})
end
else
for _,v in pairs(context.plugins.remoteHandler or { }) do
if v.messages and v.messages[data.request] then
v.callback(user, data, socket)
end
end
end
until not socket.connected
@@ -191,7 +197,7 @@ Event.on({ 'device_attach', 'device_detach' }, function(_, name)
if handler then
handler:terminate()
handler = nil
_debug('REMOTE: wireless modem disconnected')
_G._debug('REMOTE: wireless modem disconnected')
else
listen()
end

View File

@@ -0,0 +1,26 @@
local Sound = require('sound')
local args = { ... }
local context = args[1]
local function learn()
context:sendRequest({
request = 'craft',
slot = 15,
})
end
context.responseHandlers['craft'] = function(response)
if response.success then
Sound.play('entity.item.pickup')
else
Sound.play('entity.villager.no')
end
end
return {
menuItem = 'Learn Recipe',
callback = function()
learn()
end,
}

View File

@@ -0,0 +1,45 @@
local Event = require('event')
local device = _G.device
local os = _G.os
local args = { ... }
local context = args[1]
local SHIELD_SLOT = 2
Event.addRoutine(function()
local lastTransfer
while true do
local sleepTime = 1.5
if lastTransfer and os.clock() - lastTransfer < 3 then
sleepTime = .25
end
os.sleep(context.socket and sleepTime or 5)
if context.state.deposit then
local neural = device.neuralInterface
local inv = context.state.useShield and 'getEquipment' or 'getInventory'
if not neural or not neural[inv] then
_G._debug('missing Introspection module')
elseif context.state.server and (context.state.useShield or context.state.slot) then
local s, m = pcall(function()
local method = neural[inv]
local item = method and method().list()[context.state.useShield and SHIELD_SLOT or context.state.slot]
if item then
if context:sendRequest({
request = 'deposit',
slot = context.state.useShield and 'shield' or context.state.slot,
count = item.count,
}) then
lastTransfer = os.clock()
end
end
end)
if not s and m then
_G._debug(m)
end
end
end
end
end)

View File

@@ -81,7 +81,7 @@ function page:depositAll()
for slot, item in pairs(inv) do
if (context.state.depositAll.includeHotbar or slot > 9) and item.name ~= 'plethora:neuralconnector' then
context.page:sendRequest({
context:sendRequest({
request = 'deposit',
slot = slot,
count = item.count,
@@ -94,12 +94,15 @@ function page:eventHandler(event)
if event.type == 'checkbox_change' and event.element.formKey == 'includeHotbar' then
context.state.depositAll.includeHotbar = event.checked
page:updateInventoryList()
elseif event.type == 'form_complete' then
Config.update('miloRemote', context.state)
page:depositAll()
UI:setPreviousPage()
elseif event.type == 'form_cancel' then
UI:setPreviousPage()
else
return UI.Page.eventHandler(self, event)
end

View File

@@ -0,0 +1,57 @@
local itemDB = require('itemDB')
local Milo = require('milo')
local context = Milo:getContext()
local device = _G.device
local function craftHandler(user, message, socket)
local function makeNode()
local devName = user .. ':inventory'
local adapter = device[devName]
if adapter then
return {
adapter = adapter,
name = devName,
}
end
end
local function craft()
local slots = {
[1] = 1, [2] = 2, [3] = 3,
[5] = 10, [6] = 11, [7] = 12,
[9] = 19, [10] = 20, [11] = 21,
}
local node = makeNode()
if node then
for k, v in pairs(slots) do
node.adapter.pushItems(context.turtleInventory.name, v + message.slot - 1, 1, k)
end
local recipe, msg = Milo:learnRecipe()
if recipe then
socket:write({
type = 'craft',
msg = 'Learned: ' .. itemDB:getName(recipe),
success = true,
})
for k,v in pairs(context.turtleInventory.adapter.list()) do
node.adapter.pullItems(context.turtleInventory.name, k, v.count)
end
else
socket:write({
type = 'craft',
msg = msg,
})
for k, v in pairs(slots) do
node.adapter.pullItems(context.turtleInventory.name, k, 1, v + message.slot - 1)
end
end
end
end
Milo:queueRequest({ }, craft)
end
return {
remoteHandler = { callback = craftHandler, messages = { craft = true } }
}

View File

@@ -1,102 +1,10 @@
local itemDB = require('itemDB')
local Milo = require('milo')
local UI = require('ui')
local Util = require('util')
local colors = _G.colors
local turtle = _G.turtle
local function learnRecipe()
local ingredients = Milo: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 = Milo: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[itemDB:makeKey(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
return false, 'Unknown error'
end
newRecipe.count = recipe.count
local key = itemDB:makeKey(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] = itemDB:makeKey(ingredient)
end
Milo:updateRecipe(key, newRecipe)
turtle.emptyInventory()
return recipe
end
local pages = {
turtleCraft = UI.Window {
index = 2,
@@ -110,11 +18,13 @@ local pages = {
}
function pages.turtleCraft:validate()
local recipe, msg = learnRecipe()
local recipe, msg = Milo:learnRecipe()
if recipe then
local displayName = itemDB:getName(recipe)
turtle.emptyInventory()
UI:setPage('listing', {
filter = displayName,
message = 'Learned: ' .. displayName,