bit of everything

This commit is contained in:
kepler155c@gmail.com
2017-07-23 22:37:07 -04:00
parent 027f386ed1
commit f8bcf90a6b
30 changed files with 866 additions and 502 deletions

View File

@@ -116,23 +116,20 @@ function page.grid:getDisplayValues(row)
return row
end
Event.addThread(function()
while true do
page.grid:update()
page.grid:draw()
page:sync()
os.sleep(1)
end
Event.onInterval(1, function()
page.grid:update()
page.grid:draw()
page:sync()
end)
Event.addHandler('device_attach', function(h, deviceName)
Event.on('device_attach', function(h, deviceName)
if deviceName == 'wireless_modem' then
page.notification:success('Modem connected')
page:sync()
end
end)
Event.addHandler('device_detach', function(h, deviceName)
Event.on('device_detach', function(h, deviceName)
if deviceName == 'wireless_modem' then
page.notification:error('Wireless modem not attached')
page:sync()

View File

@@ -532,34 +532,30 @@ if not fs.exists(GROUPS_PATH) then
fs.makeDir(GROUPS_PATH)
end
Event.addHandler('network_attach', function()
Event.on('network_attach', function()
if mainPage.enabled then
mainPage:draw()
end
end)
Event.addHandler('network_detach', function()
Event.on('network_detach', function()
if mainPage.enabled then
mainPage:draw()
end
end)
function statusUpdate()
while true do
if mainPage.enabled then
local selected = mainPage.computers:getSelected()
if selected then
local computer = _G.network[selected.id]
mainPage.statusBar.values = { computer }
mainPage.statusBar:draw()
mainPage:sync()
end
Event.onInterval(1, function()
if mainPage.enabled then
local selected = mainPage.computers:getSelected()
if selected then
local computer = _G.network[selected.id]
mainPage.statusBar.values = { computer }
mainPage.statusBar:draw()
mainPage:sync()
end
os.sleep(1)
end
end
end)
UI:setPage(mainPage)
Event.pullEvents(statusUpdate)
UI:pullEvents()
UI.term:reset()

View File

@@ -52,7 +52,7 @@ function page.grid:getDisplayValues(row)
if elapsed < 60 then
row.timestamp = string.format("%ds", math.floor(elapsed))
else
row.timestamp = string.format("%fm", math.floor(elapsed/6)/10)
row.timestamp = string.format("%sm", math.floor(elapsed/6)/10)
end
if row.isDead then
row.status = 'error'

View File

@@ -2,7 +2,7 @@ require = requireInjector(getfenv(1))
local UI = require('ui')
local Socket = require('socket')
local Terminal = require('terminal')
local TableDB = require('tableDB')
local itemDB = require('itemDB')
multishell.setTitle(multishell.getCurrent(), 'Turtles')
UI.Button.defaults.focusIndicator = ' '
@@ -31,12 +31,6 @@ local policies = {
{ label = 'turtleSafe' },
}
local itemInfoDB = TableDB({
fileName = 'items.db'
})
itemInfoDB:load()
local page = UI.Page {
--[[
policy = UI.Chooser {
@@ -177,7 +171,7 @@ function page.coords:draw()
self:clear()
self:setCursorPos(1, 1)
local ind = 'GPS'
if t.coordSystem ~= 'GPS' then
if not t.point.gps then
ind = 'REL'
end
self:print(string.format('%s : %d,%d,%d\nFuel: %s\n',
@@ -204,7 +198,7 @@ function page.tabs.inventory:draw()
v.selected = true
end
if v.id then
local item = itemInfoDB:get({ v.id, v.dmg })
local item = itemDB:get({ v.id, v.dmg })
if item then
v.id = item.displayName
else

View File

@@ -41,6 +41,7 @@ local Builder = {
resourceSlots = 14,
facing = 'south',
confirmFacing = false,
wrenchSucks = false,
}
local pistonFacings
@@ -95,6 +96,7 @@ function subDB:seedDB()
[ "minecraft:wall_banner:0" ] = "minecraft:banner:0",
[ "minecraft:standing_banner:0" ] = "minecraft:banner:0",
[ "minecraft:tripwire:0" ] = "minecraft:string:0",
[ "minecraft:pumpkin_stem:0" ] = "minecraft:pumpkin_seeds:0",
}
self.dirty = true
self:flush()
@@ -266,7 +268,7 @@ function Builder:getAirResupplyList(blockIndex)
local fuel = subDB:getSubstitutedItem(Builder.fuelItem.id, Builder.fuelItem.dmg)
slots[15] = {
id = 'ironchest:BlockIronChest', -- 'minecraft:chest',
id = 'minecraft:chest', --'ironchest:BlockIronChest', --
dmg = 0,
qty = 0,
need = 1,
@@ -821,6 +823,14 @@ function Builder:placePiston(b)
return
end
if self.wrenchSucks then
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
end
local success = self:wrenchBlock('forward', 'down', pistonFacings) --wrench piston to point downwards
rs.setOutput('front', true)
@@ -830,6 +840,11 @@ function Builder:placePiston(b)
turtle.select(ps.index)
turtle.dig()
if not success and not self.wrenchSucks then
self.wrenchSucks = true
success = self:placePiston(b)
end
return success
end
@@ -2029,12 +2044,9 @@ UI:setPages({
UI:setPage('start')
turtle.run(function()
local s, m = turtle.run(function()
turtle.setPolicy(turtle.policies.digAttack)
turtle.setPoint({ x = -1, z = -1, y = 0, heading = 0 })
turtle.getState().coordSystem = 'relative'
turtle.saveLocation('supplies')
Event.pullEvents()
UI:pullEvents()
end)
UI.term:reset()

View File

@@ -5,16 +5,18 @@ local ChestProvider = require('chestProvider18')
local RefinedProvider = require('refinedProvider')
local itemDB = require('itemDB')
local Terminal = require('terminal')
local Peripheral = require('peripheral')
multishell.setTitle(multishell.getCurrent(), 'Resource Manager')
-- 3 wide monitor (any side of turtle)
-- Config location is /sys/config/chestManager
-- Config location is /sys/config/resourceManager
-- adjust directions in that file if needed
local config = {
trashDirection = 'up', -- trash /chest in relation to interface
turtleDirection = 'down', -- turtle in relation to interface
noCraftingStorage = 'false' -- no ME crafting (or ability to tell if powered - use with caution)
trashDirection = 'up', -- trash /chest in relation to chest
turtleDirection = 'down', -- turtle in relation to chest
}
Config.load('resourceManager', config)
@@ -26,14 +28,34 @@ if not controller:isValid() then
end
local chestProvider = ChestProvider({ direction = 'west', wrapSide = 'back' })
local turtleChestProvider = ChestProvider({ direction = 'up', wrapSide = 'bottom' })
local RESOURCE_FILE = 'usr/etc/resources.db'
local RECIPES_FILE = 'usr/etc/recipes.db'
local jobListGrid
local craftingPaused = false
local recipes = Util.readTable('recipes') or { }
local recipes = Util.readTable(RECIPES_FILE) or { }
local resources = Util.readTable(RESOURCE_FILE) or { }
multishell.setTitle(multishell.getCurrent(), 'Resource Manager')
for _,r in pairs(resources) do
r.maxDamage = nil
r.displayName = nil
r.count = nil
r.lname = nil
r.has_recipe = nil
function getItem(items, inItem, ignoreDamage)
if not r.ignoreDamage then
r.ignoreDamage = nil
end
if not r.auto then
r.auto = nil
end
end
Util.writeTable(RESOURCE_FILE, resources)
local function getItem(items, inItem, ignoreDamage)
for _,item in pairs(items) do
if item.name == inItem.name then
if ignoreDamage then
@@ -61,7 +83,7 @@ local function getItemDetails(items, item)
cItem = itemDB:get(itemDB:makeKey(item))
if cItem then
return { count = 0, maxCount = cItem.maxCount }
enditemDB:makeKey
end
return { count = 0, maxCount = 64 }
end
@@ -69,7 +91,7 @@ local function uniqueKey(item)
return table.concat({ item.name, item.damage, item.nbtHash }, ':')
end
function getName(item)
local function getName(item)
local detail = itemDB:get(itemDB:makeKey(item))
if detail then
return detail.displayName
@@ -77,60 +99,51 @@ function getName(item)
return item.name .. ':' .. item.damage
end
function mergeResources(t)
local resources = Util.readTable('resource.limits') or { }
local function mergeResources(t)
for _,v in pairs(resources) do
v.low = tonumber(v.low) -- backwards compatibility
local item = getItem(t, v)
if item then
item.low = v.low
item.limit = v.limit
item.auto = v.auto
item.ignoreDamage = v.ignoreDamage
item.rsControl = v.rsControl
item.rsDevice = v.rsDevice
item.rsSide = v.rsSide
Util.merge(item, v)
else
v.count = 0
table.insert(t, v)
item = Util.shallowCopy(v)
item.count = 0
table.insert(t, item)
end
end
for _,v in pairs(recipes) do
local item = getItem(t, v)
if item then
item.has_recipe = true
else
if not item then
item = Util.shallowCopy(v)
item.displayName = getName(item)
item.count = 0
item.has_recipe = true
table.insert(t, item)
end
item.has_recipe = true
end
for _,v in pairs(t) do
if not v.displayName then
v.displayName = getName(v)
end
v.lname = v.displayName:lower()
end
end
function filterItems(t, filter)
local r = {}
local function filterItems(t, filter)
if filter then
local r = {}
filter = filter:lower()
for k,v in pairs(t) do
if string.find(v.lname, filter) then
if string.find(v.lname, filter) then
table.insert(r, v)
end
end
else
return t
return r
end
return r
return t
end
function sumItems3(ingredients, items, summedItems, count)
local function sumItems3(ingredients, items, summedItems, count)
local canCraft = 0
for _,item in pairs(ingredients) do
@@ -179,7 +192,7 @@ local function sumItems2(ingredients, items, summedItems, count)
return canCraft
end
function sumItems(items)
local function sumItems(items)
local t = {}
for _,item in pairs(items) do
@@ -197,7 +210,7 @@ function sumItems(items)
return t
end
function isGridClear()
local function isGridClear()
for i = 1, 16 do
if turtle.getItemCount(i) ~= 0 then
return false
@@ -219,22 +232,11 @@ local function clearGrid()
return true
end
function turtleCraft(recipe, originalItem, qty)
local function turtleCraft(recipe, originalItem, qty)
for k,v in pairs(recipe.ingredients) do
-- ugh
local dmg = v.damage
--FIX - LOOKUP IN ITEMS
if v.max_dmg and v.max_dmg > 0 then
local item = ME.getItemDetail({ id = v.id, nbt_hash = v.nbt_hash }, false)
if item then
dmg = item.dmg
end
end
chestProvider:provide({ id = v.name, dmg = dmg, nbt_hash = v.nbtHash }, v.count * qty, k)
chestProvider:provide({ id = v.name, dmg = v.damage, nbt_hash = v.nbtHash }, v.count * qty, k)
if turtle.getItemCount(k) ~= v.count * qty then
clearGrid()
originalItem.status = v.name .. ' (extract failed)'
@@ -256,7 +258,7 @@ function turtleCraft(recipe, originalItem, qty)
return true
end
function addCraftingRequest(item, craftList, count)
local function addCraftingRequest(item, craftList, count)
local key = uniqueKey(item)
local request = craftList[key]
if not craftList[key] then
@@ -267,9 +269,18 @@ function addCraftingRequest(item, craftList, count)
request.count = request.count + count
end
function craftRecipe(recipe, items, originalItem, count)
local function craftRecipe(recipe, items, originalItem, count)
local maxCount = 64
local maxCount = recipe.maxCount
if not maxCount then -- temporary
local cItem = itemDB:get(itemDB:makeKey(recipe))
if cItem then
maxCount = cItem.maxCount
else
maxCount = 1
end
end
local summedItems = sumItems(recipe.ingredients)
for key,ingredient in pairs(summedItems) do
@@ -293,7 +304,7 @@ function craftRecipe(recipe, items, originalItem, count)
return true
end
function craftItem(recipe, items, originalItem, craftList, count)
local function craftItem(recipe, items, originalItem, craftList, count)
if craftingPaused or not device.workbench or not isGridClear() then
return
@@ -305,12 +316,11 @@ function craftItem(recipe, items, originalItem, craftList, count)
if toCraft > 0 then
craftRecipe(recipe, items, originalItem, toCraft)
items = chestProvider:listItems()
end
count = count - toCraft
items = chestProvider:listItems()
local summedItems = { }
sumItems3(recipe.ingredients, items, summedItems, count)
@@ -321,7 +331,7 @@ function craftItem(recipe, items, originalItem, craftList, count)
end
end
function craftItems(craftList, allItems)
local function craftItems(craftList, allItems)
for _,key in pairs(Util.keys(craftList)) do
local item = craftList[key]
@@ -336,25 +346,29 @@ function craftItems(craftList, allItems)
for key,item in pairs(craftList) do
if controller and not recipes[key] then
if controller:isCrafting(item) then
item.status = '(crafting)'
if not recipes[key] then
if not controller then
item.status = '(no recipe)'
else
if controller:isCrafting(item) then
item.status = '(crafting)'
else
local count = item.count
while count >= 1 do -- try to request smaller quantities until successful
local s, m = pcall(function()
item.status = '(no recipe)'
if not controller:craft(item, count) then
item.status = '(missing ingredients)'
error('failed')
local count = item.count
while count >= 1 do -- try to request smaller quantities until successful
local s, m = pcall(function()
item.status = '(no recipe)'
if not controller:craft(item, count) then
item.status = '(missing ingredients)'
error('failed')
end
item.status = '(crafting)'
end)
if s then
break -- successfully requested crafting
end
item.status = '(crafting)'
end)
if s then
break -- successfully requested crafting
count = math.floor(count / 2)
end
count = math.floor(count / 2)
end
end
end
@@ -363,11 +377,11 @@ end
local function jobMonitor(jobList)
local mon
local mon = Peripheral.getByType('monitor')
if device.monitor then
if mon then
mon = UI.Device({
deviceType = 'monitor',
device = mon,
textScale = .5,
})
else
@@ -387,11 +401,10 @@ local function jobMonitor(jobList)
})
end
function getAutocraftItems()
local t = Util.readTable('resource.limits') or { }
local function getAutocraftItems()
local craftList = { }
for _,res in pairs(t) do
for _,res in pairs(resources) do
if res.auto then
res.count = 4 -- this could be higher to increase autocrafting speed
@@ -426,19 +439,18 @@ local function getItemWithQty(items, res, ignoreDamage)
return item
end
function watchResources(items)
local function watchResources(items)
local craftList = { }
local t = Util.readTable('resource.limits') or { }
for k, res in pairs(t) do
for k, res in pairs(resources) do
local item = getItemWithQty(items, res, res.ignoreDamage)
if not item then
item = {
damage = res.damage,
nbtHash = res.nbtHash,
name = res.name,
displayName = res.displayName,
displayName = getName(res),
count = 0
}
end
@@ -472,7 +484,7 @@ function watchResources(items)
return craftList
end
itemPage = UI.Page {
local itemPage = UI.Page {
backgroundColor = colors.lightGray,
titleBar = UI.TitleBar {
title = 'Limit Resource',
@@ -481,10 +493,10 @@ itemPage = UI.Page {
backgroundColor = colors.green
},
displayName = UI.Window {
x = 5, y = 2, width = UI.term.width - 10, height = 3,
x = 2, y = 2, width = UI.term.width - 4, height = 3,
},
form = UI.Form {
x = 4, y = 4, height = 8, rex = -4,
x = 4, y = 5, height = 8, rex = -4,
[1] = UI.TextEntry {
width = 7,
backgroundColor = colors.gray,
@@ -555,7 +567,7 @@ function itemPage.displayName:draw()
local item = self.parent.item
local str = string.format('Name: %s\nDamage: %d', item.displayName, item.damage)
if item.nbtHash then
str = str .. string.format('\nNBT: %s\n', item.nbtHash)
str = str .. string.format('\n%s', item.nbtHash)
end
self:setCursorPos(1, 1)
self:print(str)
@@ -593,9 +605,8 @@ function itemPage:eventHandler(event)
elseif event.type == 'form_complete' then
local values = self.form.values
local t = Util.readTable('resource.limits') or { }
local keys = { 'name', 'displayName', 'auto', 'low', 'limit', 'damage',
'maxDamage', 'nbtHash', 'ignoreDamage',
local keys = { 'name', 'auto', 'low', 'limit', 'damage',
'nbtHash', 'ignoreDamage',
'rsControl', 'rsDevice', 'rsSide', }
local filtered = { }
@@ -605,16 +616,26 @@ function itemPage:eventHandler(event)
filtered.low = tonumber(filtered.low)
filtered.limit = tonumber(filtered.limit)
filtered.ignoreDamage = filtered.ignoreDamage == true
filtered.auto = filtered.auto == true
filtered.rsControl = filtered.rsControl == true
--filtered.ignoreDamage = filtered.ignoreDamage == true
--filtered.auto = filtered.auto == true
--filtered.rsControl = filtered.rsControl == true
if filtered.ignoreDamage then
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 values.ignoreDamage == true then
filtered.damage = 0
end
t[uniqueKey(filtered)] = filtered
Util.writeTable('resource.limits', t)
resources[uniqueKey(filtered)] = filtered
Util.writeTable(RESOURCE_FILE, resources)
UI:setPreviousPage()
@@ -624,11 +645,12 @@ function itemPage:eventHandler(event)
return true
end
listingPage = UI.Page {
local listingPage = UI.Page {
menuBar = UI.MenuBar {
buttons = {
{ text = 'Learn', event = 'learn' },
{ text = 'Forget', event = 'forget' },
{ text = 'Craft', event = 'craft' },
},
},
grid = UI.Grid {
@@ -720,6 +742,9 @@ function listingPage:eventHandler(event)
self.statusBar.filter:focus()
elseif event.type == 'learn' then
UI:setPage('learn')
elseif event.type == 'craft' then
UI:setPage('craft')
elseif event.type == 'forget' then
@@ -729,16 +754,12 @@ function listingPage:eventHandler(event)
if recipes[key] then
recipes[key] = nil
Util.writeTable('recipes', recipes)
Util.writeTable(RECIPES_FILE, recipes)
end
local resources = Util.readTable('resource.limits') or { }
for k,v in pairs(resources) do
if v.name == item.name and v.damage == item.damage then
resources[k] = nil
Util.writeTable('resource.limits', resources)
break
end
if resources[key] then
resources[key] = nil
Util.writeTable(RESOURCE_FILE, resources)
end
self.statusBar:timedStatus('Forgot: ' .. item.name, 3)
@@ -779,7 +800,7 @@ function listingPage:applyFilter()
end
-- without duck antenna
local function getTurtleInventory()
local function getTurtleInventoryOld()
local inventory = { }
for i = 1,16 do
if turtle.getItemCount(i) > 0 then
@@ -795,6 +816,20 @@ local function getTurtleInventory()
return inventory
end
local function getTurtleInventory()
local inventory = { }
for i = 1,16 do
local qty = turtle.getItemCount(i)
if qty > 0 then
turtleChestProvider:insert(i, qty)
local items = turtleChestProvider:listItems()
_, inventory[i] = next(items)
turtleChestProvider:extract(1, qty, i)
end
end
return inventory
end
local function filter(t, filter)
local keys = Util.keys(t)
for _,key in pairs(keys) do
@@ -817,10 +852,10 @@ local function learnRecipe(page)
clearGrid()
filter(recipe, { 'name', 'damage', 'nbtHash', 'count' })
filter(recipe, { 'name', 'damage', 'nbtHash', 'count', 'maxCount' })
for _,ingredient in pairs(ingredients) do
filter(ingredient, { 'name', 'damage', 'nbtHash', 'count' })
filter(ingredient, { 'name', 'damage', 'nbtHash', 'count', 'maxCount' })
--if ingredient.max_dmg > 0 then -- let's try this...
-- ingredient.dmg = 0
--end
@@ -829,7 +864,7 @@ local function learnRecipe(page)
recipes[key] = recipe
Util.writeTable('recipes', recipes)
Util.writeTable(RECIPES_FILE, recipes)
local displayName = getName(recipe)
@@ -849,13 +884,10 @@ local function learnRecipe(page)
end
end
craftPage = UI.Dialog {
local learnPage = UI.Dialog {
height = 7, width = UI.term.width - 6,
backgroundColor = colors.lightGray,
titleBar = UI.TitleBar {
title = 'Learn Recipe',
previousPage = true,
},
title = 'Learn Recipe',
idField = UI.Text {
x = 5,
y = 3,
@@ -875,6 +907,61 @@ craftPage = UI.Dialog {
}
}
function learnPage:enable()
craftingPaused = true
self:focusFirst()
UI.Dialog.enable(self)
end
function learnPage:disable()
craftingPaused = false
UI.Dialog.disable(self)
end
function learnPage:eventHandler(event)
if event.type == 'cancel' then
UI:setPreviousPage()
elseif event.type == 'accept' then
if learnRecipe(self) then
UI:setPreviousPage()
end
else
return UI.Dialog.eventHandler(self, event)
end
return true
end
local craftPage = UI.Dialog {
height = 6, width = UI.term.width - 10,
backgroundColor = colors.lightGray,
title = 'Enter amount to craft',
idField = UI.TextEntry {
x = 15,
y = 3,
width = 10,
limit = 6,
value = '1',
backgroundColor = colors.black,
backgroundFocusColor = colors.black,
},
accept = UI.Button {
rx = -7, ry = -1,
backgroundColor = colors.green,
text = '+', event = 'accept',
},
cancel = UI.Button {
rx = -3, ry = -1,
backgroundColor = colors.red,
backgroundFocusColor = colors.red,
text = '\215', event = 'cancel'
},
}
function craftPage:draw()
UI.Dialog.draw(self)
self:write(6, 3, 'Quantity')
end
function craftPage:enable()
craftingPaused = true
self:focusFirst()
@@ -890,9 +977,7 @@ function craftPage:eventHandler(event)
if event.type == 'cancel' then
UI:setPreviousPage()
elseif event.type == 'accept' then
if learnRecipe(self) then
UI:setPreviousPage()
end
else
return UI.Dialog.eventHandler(self, event)
end
@@ -902,6 +987,7 @@ end
UI:setPages({
listing = listingPage,
item = itemPage,
learn = learnPage,
craft = craftPage,
})
@@ -913,15 +999,12 @@ jobMonitor()
jobListGrid:draw()
jobListGrid:sync()
function craftingThread()
local function craftingThread()
while true do
os.sleep(5)
if not craftingPaused then
local items = chestProvider:listItems()
if Util.size(items) == 0 then
jobListGrid.parent:clear()
jobListGrid.parent:centeredWrite(math.ceil(jobListGrid.parent.height/2), 'No items in system')
@@ -942,8 +1025,6 @@ function craftingThread()
end
end
end
--craftingThread()
UI:pullEvents(craftingThread)
UI.term:reset()
UI:pullEvents(craftingThread)
jobListGrid.parent:reset()

View File

@@ -61,40 +61,37 @@ function getClient(id)
return ids[id]
end
local function logWriter()
while true do
os.pullEvent('logMessage')
local t = { }
while #messages > 0 do
local msg = messages[1]
table.remove(messages, 1)
local client = getClient(msg.id)
client.scrollingText:appendLine(string.format('%d %s', math.floor(os.clock()), msg.text))
t[msg.id] = client
end
for _,client in pairs(t) do
client.scrollingText:draw()
end
terminal:sync()
Event.on('logMessage', function()
local t = { }
while #messages > 0 do
local msg = messages[1]
table.remove(messages, 1)
local client = getClient(msg.id)
client.scrollingText:appendLine(string.format('%d %s', math.floor(os.clock()), msg.text))
t[msg.id] = client
end
end
for _,client in pairs(t) do
client.scrollingText:draw()
end
terminal:sync()
end)
Message.addHandler('log', function(h, id, msg)
table.insert(messages, { id = id, text = msg.contents })
os.queueEvent('logMessage')
end)
Event.addHandler('monitor_touch', function()
Event.on('monitor_touch', function()
terminal:reset()
ids = { }
end)
Event.addHandler('mouse_click', function()
Event.on('mouse_click', function()
terminal:reset()
ids = { }
end)
Event.addHandler('char', function()
Event.on('char', function()
Event.exitPullEvents()
end)

View File

@@ -2,7 +2,7 @@ require = requireInjector(getfenv(1))
local Socket = require('socket')
local Terminal = require('terminal')
local Logger = require('logger')
local process = require('process')
local Event = require('event')
Logger.setScreenLogging()
@@ -31,7 +31,12 @@ local function wrapTerm(socket)
socket.term[k] = function(...)
if not socket.queue then
socket.queue = { }
os.queueEvent('mirror_flush')
Event.onTimeout(0, function()
if socket.queue then
socket:write(socket.queue)
socket.queue = nil
end
end)
end
table.insert(socket.queue, {
f = k,
@@ -61,17 +66,13 @@ while true do
os.queueEvent('term_resize')
while true do
local e = process:pullEvent('mirror_flush')
local e = Event.pullEvent()
if e == 'terminate' then
break
end
if not socket.connected then
break
end
if socket.queue then
socket:write(socket.queue)
socket.queue = nil
end
end
for k,v in pairs(socket.oldTerm) do

View File

@@ -1,7 +1,7 @@
require = requireInjector(getfenv(1))
local Event = require('event')
local Socket = require('socket')
local Logger = require('logger')
local process = require('process')
Logger.setScreenLogging()
@@ -20,7 +20,7 @@ while true do
print('mirror: connection from ' .. socket.dhost)
local updateThread = process:newThread('updateThread', function()
Event.addRoutine(function()
while true do
local data = socket:read()
if not data then
@@ -33,18 +33,15 @@ while true do
end)
-- ensure socket is connected
process:newThread('pinger', function()
while true do
os.sleep(3)
if not socket:ping() then
break
end
Event.onInterval(3, function(h)
if not socket:ping() then
Event.off(h)
end
end)
while true do
process:pullEvent('modem_message')
if updateThread:isDead() then
Event.pullEvent()
if not socket.connected then
break
end
end

View File

@@ -243,6 +243,159 @@ turtle.run(function()
end)
]]
local levelScript = [[
require = requireInjector(getfenv(1))
local Point = require('point')
local checkedNodes = { }
local nodes = { }
local box = { }
local function inBox(pt, box)
return pt.x >= box.x and
pt.y >= box.y and
pt.z >= box.z and
pt.x <= box.ex and
pt.y <= box.ey and
pt.z <= box.ez
end
local function toKey(pt)
return table.concat({ pt.x, pt.y, pt.z }, ':')
end
local function addNode(node)
for i = 0, 5 do
local hi = turtle.getHeadingInfo(i)
local testNode = { x = node.x + hi.xd, y = node.y + hi.yd, z = node.z + hi.zd }
if inBox(testNode, box) then
local key = toKey(testNode)
if not checkedNodes[key] then
nodes[key] = testNode
end
end
end
end
local function dig(action)
local directions = {
top = 'up',
bottom = 'down',
}
-- convert to up, down, north, south, east, west
local direction = directions[action.side] or
turtle.getHeadingInfo(turtle.point.heading).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 }
if inBox(node, box) then
local key = toKey(node)
checkedNodes[key] = true
nodes[key] = nil
if action.dig() then
addNode(node)
repeat until not action.dig() -- sand, etc
return true
end
end
end
local function move(action)
if action == 'turn' then
dig(turtle.getAction('forward'))
elseif action == 'up' then
dig(turtle.getAction('up'))
elseif action == 'down' then
dig(turtle.getAction('down'))
elseif action == 'back' then
dig(turtle.getAction('up'))
dig(turtle.getAction('down'))
end
end
local function getAdjacentPoint(pt)
local t = { }
table.insert(t, pt)
for i = 0, 5 do
local hi = turtle.getHeadingInfo(i)
local heading
if i < 4 then
heading = (hi.heading + 2) % 4
end
table.insert(t, { x = pt.x + hi.xd, z = pt.z + hi.zd, y = pt.y + hi.yd, heading = heading })
end
return Point.closest2(turtle.getPoint(), t)
end
local function level()
box.x = math.min(data.startPt.x, data.endPt.x)
box.y = math.min(data.startPt.y, data.endPt.y)
box.z = math.min(data.startPt.z, data.endPt.z)
box.ex = math.max(data.startPt.x, data.endPt.x)
box.ey = math.max(data.startPt.y, data.endPt.y)
box.ez = math.max(data.startPt.z, data.endPt.z)
turtle.pathfind(data.firstPt)
turtle.setPolicy("attack", { dig = dig }, "assuredMove")
turtle.setMoveCallback(move)
repeat
local key = toKey(turtle.point)
checkedNodes[key] = true
nodes[key] = nil
dig(turtle.getAction('down'))
dig(turtle.getAction('up'))
dig(turtle.getAction('forward'))
print(string.format('%d nodes remaining', Util.size(nodes)))
if Util.size(nodes) == 0 then
break
end
local node = Point.closest2(turtle.point, nodes)
node = getAdjacentPoint(node)
if not turtle.gotoPoint(node) then
break
end
until turtle.abort
turtle.resetState()
end
local s, m = turtle.run(function()
turtle.status = 'Leveling'
if turtle.enableGPS() then
local pt = Util.shallowCopy(turtle.point)
local s, m = pcall(level)
turtle.pathfind(pt)
if not s and m then
error(m)
end
end
end)
if not s then
error(m)
end
]]
local data = Util.readTable('/usr/config/shapes') or { }
local page = UI.Page {
@@ -251,6 +404,7 @@ local page = UI.Page {
startCoord = UI.Button { x = 2, y = 6, text = 'Start ', event = 'startCoord' },
endCoord = UI.Button { x = 2, y = 8, text = 'End ', event = 'endCoord' },
supplies = UI.Button { x = 2, y = 10, text = 'Supplies', event = 'supplies' },
first = UI.Button { x = 2, y = 11, text = 'First', event = 'firstCoord' },
cancel = UI.Button { rx = 2, ry = -2, text = 'Abort', event = 'cancel' },
begin = UI.Button { rx = -7, ry = -2, text = 'Begin', event = 'begin' },
accelerators = { q = 'quit' },
@@ -286,6 +440,7 @@ end
function page:runFunction(id, script)
Util.writeFile('script.tmp', script)
self.notification:info('Connecting')
local fn, msg = loadstring(script, 'script')
if not fn then
@@ -299,7 +454,6 @@ function page:runFunction(id, script)
self.notification:error('Unable to connect')
return
end
socket:write({ type = 'script', args = script })
socket:close()
@@ -321,6 +475,13 @@ function page:eventHandler(event)
Util.writeTable('/usr/config/shapes', data)
end
self:draw()
elseif event.type == 'firstCoord' then
data.firstPt = self:getPoint()
if data.firstPt then
self.statusBar:setStatus('first point set')
Util.writeTable('/usr/config/shapes', data)
end
self:draw()
elseif event.type == 'supplies' then
data.suppliesPt = self:getPoint()
if data.suppliesPt then
@@ -329,7 +490,7 @@ function page:eventHandler(event)
end
elseif event.type == 'begin' then
if data.startPt and data.endPt then
local s = 'local data = ' .. textutils.serialize(data) .. script
local s = 'local data = ' .. textutils.serialize(data) .. levelScript
self:runFunction(turtleId, s)
else
self.notification:error('Corners not set')

View File

@@ -863,43 +863,37 @@ jobMonitor()
jobListGrid:draw()
jobListGrid:sync()
function craftingThread()
Event.onInterval(5, function()
while true do
os.sleep(5)
if not craftingPaused then
if not craftingPaused then
local items = ME.getAvailableItems()
if Util.size(items) == 0 then
jobListGrid.parent:clear()
jobListGrid.parent:centeredWrite(math.ceil(jobListGrid.parent.height/2), 'No items in system')
jobListGrid:sync()
elseif config.noCraftingStorage ~= 'true' and #ME.getCraftingCPUs() <= 0 then -- only way to determine if AE is online
jobListGrid.parent:clear()
jobListGrid.parent:centeredWrite(math.ceil(jobListGrid.parent.height/2), 'Power failure')
jobListGrid:sync()
local items = ME.getAvailableItems()
if Util.size(items) == 0 then
jobListGrid.parent:clear()
jobListGrid.parent:centeredWrite(math.ceil(jobListGrid.parent.height/2), 'No items in system')
jobListGrid:sync()
elseif config.noCraftingStorage ~= 'true' and #ME.getCraftingCPUs() <= 0 then -- only way to determine if AE is online
jobListGrid.parent:clear()
jobListGrid.parent:centeredWrite(math.ceil(jobListGrid.parent.height/2), 'Power failure')
jobListGrid:sync()
else
local itemList = watchResources(items)
jobListGrid:setValues(itemList)
jobListGrid:draw()
jobListGrid:sync()
craftItems(itemList)
jobListGrid:update()
jobListGrid:draw()
jobListGrid:sync()
else
local itemList = watchResources(items)
jobListGrid:setValues(itemList)
jobListGrid:draw()
jobListGrid:sync()
craftItems(itemList)
jobListGrid:update()
jobListGrid:draw()
jobListGrid:sync()
itemList = getAutocraftItems(items) -- autocrafted items don't show on job monitor
craftItems(itemList)
end
itemList = getAutocraftItems(items) -- autocrafted items don't show on job monitor
craftItems(itemList)
end
end
end
end)
Event.pullEvents(craftingThread)
UI.term:reset()
UI:pullEvents()
jobListGrid.parent:reset()

View File

@@ -429,7 +429,6 @@ end
turtle.run(function()
turtle.setPoint({ x = -1, z = -2, y = -1, heading = 1 })
turtle.getState().coordSystem = 'relative'
turtle.saveLocation('supplies')

View File

@@ -1,5 +1,5 @@
require = requireInjector(getfenv(1))
local process = require('process')
local Event = require('event')
local Socket = require('socket')
local Terminal = require('terminal')
@@ -36,7 +36,7 @@ socket:write({
isColor = ct.isColor(),
})
process:newThread('telnet_read', function()
Event.addRoutine(function()
while true do
local data = socket:read()
if not data then
@@ -57,7 +57,7 @@ local filter = Util.transpose({
})
while true do
local e = { process:pullEvent() }
local e = Event.pullEvent()
local event = e[1]
if not socket.connected then

View File

@@ -1,5 +1,5 @@
require = requireInjector(getfenv(1))
local process = require('process')
local Event = require('event')
local Socket = require('socket')
local Terminal = require('terminal')
@@ -39,7 +39,7 @@ if not ct.isColor() then
Terminal.toGrayscale(ct)
end
process:newThread('vnc_read', function()
Event.addRoutine(function()
while true do
local data = socket:read()
if not data then
@@ -60,7 +60,7 @@ local filter = Util.transpose({
})
while true do
local e = { process:pullEvent() }
local e = Event.pullEvent()
local event = e[1]
if not socket.connected then