1
0
mirror of https://github.com/kepler155c/opus synced 2024-12-25 16:10:26 +00:00

api cleanup

This commit is contained in:
kepler155c@gmail.com 2017-11-15 00:08:42 -05:00
parent f533e42c0c
commit a8a4ceb85d
7 changed files with 119 additions and 96 deletions

View File

@ -9,6 +9,10 @@ function Security.verifyPassword(password)
return config.password and password == config.password return config.password and password == config.password
end end
function Security.hasPassword()
return not not config.password
end
function Security.getSecretKey() function Security.getSecretKey()
Config.load('os', config) Config.load('os', config)
if not config.secretKey then if not config.secretKey then

View File

@ -157,6 +157,11 @@ local function trusted(msg, port)
return true return true
end end
if not Security.hasPassword() then
-- no password has been set on this computer
return true
end
local trustList = Util.readTable('usr/.known_hosts') or { } local trustList = Util.readTable('usr/.known_hosts') or { }
local pubKey = trustList[msg.shost] local pubKey = trustList[msg.shost]

View File

@ -149,12 +149,11 @@ local function pathTo(dest, options)
-- Define start and goal locations coordinates -- Define start and goal locations coordinates
local startPt = turtle.point local startPt = turtle.point
local endPt = dest
-- Calculates the path, and its length -- Calculates the path, and its length
local path = finder:getPath( local path = finder:getPath(
startPt.x, startPt.y, startPt.z, turtle.point.heading, startPt.x, startPt.y, startPt.z, turtle.point.heading,
endPt.x, endPt.y, endPt.z, dest.heading) dest.x, dest.y, dest.z, dest.heading)
if not path then if not path then
Util.removeByValue(dests, dest) Util.removeByValue(dests, dest)
@ -176,7 +175,7 @@ local function pathTo(dest, options)
-- use single turn method so the turtle doesn't turn around -- use single turn method so the turtle doesn't turn around
-- when encountering obstacles -- when encountering obstacles
if not turtle.gotoSingleTurn(pt.x, pt.z, pt.y, pt.heading) then if not turtle.gotoSingleTurn(pt.x, pt.y, pt.z, pt.heading) then
local bpt = Point.nearestTo(turtle.point, pt) local bpt = Point.nearestTo(turtle.point, pt)
table.insert(blocks, bpt) table.insert(blocks, bpt)

View File

@ -682,7 +682,7 @@ end
function UI.Window:centeredWrite(y, text, bg, fg) function UI.Window:centeredWrite(y, text, bg, fg)
if #text >= self.width then if #text >= self.width then
self:write(1, y, text, bg) self:write(1, y, text, bg, fg)
else else
local space = math.floor((self.width-#text) / 2) local space = math.floor((self.width-#text) / 2)
local filler = _rep(' ', space + 1) local filler = _rep(' ', space + 1)
@ -1847,11 +1847,11 @@ UI.Button = class(UI.Window)
UI.Button.defaults = { UI.Button.defaults = {
UIElement = 'Button', UIElement = 'Button',
text = 'button', text = 'button',
backgroundColor = colors.gray, backgroundColor = colors.lightGray,
backgroundFocusColor = colors.lightGray, backgroundFocusColor = colors.gray,
textFocusColor = colors.white, textFocusColor = colors.white,
textInactiveColor = colors.gray, textInactiveColor = colors.gray,
textColor = colors.white, textColor = colors.black,
centered = true, centered = true,
height = 1, height = 1,
focusIndicator = ' ', focusIndicator = ' ',
@ -1998,7 +1998,7 @@ UI.DropMenuItem.defaults = {
UIElement = 'DropMenuItem', UIElement = 'DropMenuItem',
textColor = colors.black, textColor = colors.black,
backgroundColor = colors.white, backgroundColor = colors.white,
textFocusColor = colors.black, textFocusColor = colors.white,
textInactiveColor = colors.lightGray, textInactiveColor = colors.lightGray,
backgroundFocusColor = colors.lightGray, backgroundFocusColor = colors.lightGray,
} }

View File

@ -240,7 +240,9 @@ function page.container:setCategory(categoryName, animate)
y = 4, y = 4,
text = title, text = title,
backgroundColor = self.backgroundColor, backgroundColor = self.backgroundColor,
--backgroundFocusColor = colors.gray, backgroundFocusColor = colors.gray,
textColor = colors.white,
textFocusColor = colors.white,
width = #title + 2, width = #title + 2,
event = 'button', event = 'button',
app = program, app = program,

View File

@ -11,6 +11,7 @@ local multishell = _ENV.multishell
local os = _G.os local os = _G.os
local settings = _G.settings local settings = _G.settings
local shell = _ENV.shell local shell = _ENV.shell
local turtle = _G.turtle
multishell.setTitle(multishell.getCurrent(), 'System') multishell.setTitle(multishell.getCurrent(), 'System')
UI:configure('System', ...) UI:configure('System', ...)
@ -44,7 +45,7 @@ local systemPage = UI.Page {
}, },
aliasTab = UI.Window { aliasTab = UI.Window {
tabTitle = 'Aliases', tabTitle = 'Alias',
alias = UI.TextEntry { alias = UI.TextEntry {
x = 2, y = 2, ex = -2, x = 2, y = 2, ex = -2,
limit = 32, limit = 32,
@ -141,6 +142,64 @@ local systemPage = UI.Page {
}, },
} }
if turtle then
local Home = require('turtle.home')
local values = { }
Config.load('gps', values.home or { })
systemPage.tabs:add({
gpsTab = UI.Window {
tabTitle = 'GPS',
labelText = UI.Text {
x = 3, y = 2,
value = 'On restart, return to this location'
},
grid = UI.Grid {
x = 3, ex = -3, y = 4,
height = 2,
values = values,
inactive = true,
columns = {
{ heading = 'x', key = 'x' },
{ heading = 'y', key = 'y' },
{ heading = 'z', key = 'z' },
},
},
button1 = UI.Button {
x = 3, y = 7,
text = 'Set home',
event = 'gps_set',
},
button2 = UI.Button {
ex = -3, y = 7, width = 7,
text = 'Clear',
event = 'gps_clear',
},
},
})
function systemPage.tabs.gpsTab:eventHandler(event)
if event.type == 'gps_set' then
systemPage.notification:info('Determining location', 10)
systemPage:sync()
if Home.set() then
Config.load('gps', values)
self.grid:setValues(values.home or { })
self.grid:draw()
systemPage.notification:success('Location set')
else
systemPage.notification:error('Unable to determine location')
end
return true
elseif event.type == 'gps_clear' then
fs.delete('usr/config/gps')
self.grid:setValues({ })
self.grid:draw()
return true
end
end
end
if settings then if settings then
local values = { } local values = { }
for _,v in pairs(settings.getNames()) do for _,v in pairs(settings.getNames()) do
@ -162,8 +221,6 @@ if settings then
{ heading = 'Setting', key = 'name' }, { heading = 'Setting', key = 'name' },
{ heading = 'Value', key = 'value' }, { heading = 'Value', key = 'value' },
}, },
accelerators = {
},
}, },
} }
}) })

View File

@ -4,10 +4,11 @@ end
_G.requireInjector() _G.requireInjector()
local Pathing = require('turtle.pathfind')
local GPS = require('gps')
local Point = require('point') local Point = require('point')
local synchronized = require('sync') local synchronized = require('sync')
local Util = require('util') local Util = require('util')
local Pathing = require('turtle.pathfind')
local os = _G.os local os = _G.os
local peripheral = _G.peripheral local peripheral = _G.peripheral
@ -15,10 +16,7 @@ local turtle = _G.turtle
local function noop() end local function noop() end
local headings = Point.headings local headings = Point.headings
local state = { local state = { }
status = 'idle',
abort = false,
}
turtle.pathfind = Pathing.pathfind turtle.pathfind = Pathing.pathfind
turtle.point = { x = 0, y = 0, z = 0, heading = 0 } turtle.point = { x = 0, y = 0, z = 0, heading = 0 }
@ -155,6 +153,7 @@ local function inventoryAction(fn, name, qty)
return s return s
end end
-- [[ Attack ]] --
local function _attack(action) local function _attack(action)
if action.attack() then if action.attack() then
repeat until not action.attack() repeat until not action.attack()
@ -163,10 +162,21 @@ local function _attack(action)
return false return false
end end
turtle.attackPolicies = {
none = noop,
attack = function(action)
return _attack(action)
end,
}
function turtle.attack() return _attack(actions.forward) end function turtle.attack() return _attack(actions.forward) end
function turtle.attackUp() return _attack(actions.up) end function turtle.attackUp() return _attack(actions.up) end
function turtle.attackDown() return _attack(actions.down) end function turtle.attackDown() return _attack(actions.down) end
function turtle.setAttackPolicy(policy) state.attackPolicy = policy end
-- [[ Place ]] --
local function _place(action, indexOrId) local function _place(action, indexOrId)
local slot local slot
@ -219,25 +229,11 @@ function turtle.refuel(qtyOrName, qty)
return inventoryAction(turtle.native.refuel, qtyOrName, qty or 64) return inventoryAction(turtle.native.refuel, qtyOrName, qty or 64)
end end
--[[
function turtle.dig() return state.dig(actions.forward) end
function turtle.digUp() return state.dig(actions.up) end
function turtle.digDown() return state.dig(actions.down) end
--]]
function turtle.isTurtleAtSide(side) function turtle.isTurtleAtSide(side)
local sideType = peripheral.getType(side) local sideType = peripheral.getType(side)
return sideType and sideType == 'turtle' return sideType and sideType == 'turtle'
end end
turtle.attackPolicies = {
none = noop,
attack = function(action)
return _attack(action)
end,
}
turtle.digPolicies = { turtle.digPolicies = {
none = noop, none = noop,
@ -332,7 +328,6 @@ function turtle.setPolicy(...)
end end
function turtle.setDigPolicy(policy) state.digPolicy = policy end function turtle.setDigPolicy(policy) state.digPolicy = policy end
function turtle.setAttackPolicy(policy) state.attackPolicy = policy end
function turtle.setMoveCallback(cb) state.moveCallback = cb end function turtle.setMoveCallback(cb) state.moveCallback = cb end
function turtle.clearMoveCallback() state.moveCallback = noop end function turtle.clearMoveCallback() state.moveCallback = noop end
function turtle.getMoveCallback() return state.moveCallback end function turtle.getMoveCallback() return state.moveCallback end
@ -455,7 +450,7 @@ function turtle.back()
end end
end end
function turtle.moveTowardsX(dx) local function moveTowardsX(dx)
local direction = dx - turtle.point.x local direction = dx - turtle.point.x
local move local move
@ -478,7 +473,7 @@ function turtle.moveTowardsX(dx)
return true return true
end end
function turtle.moveTowardsZ(dz) local function moveTowardsZ(dz)
local direction = dz - turtle.point.z local direction = dz - turtle.point.z
local move local move
@ -503,12 +498,14 @@ end
-- [[ go ]] -- -- [[ go ]] --
-- 1 turn goto (going backwards if possible) -- 1 turn goto (going backwards if possible)
function turtle.gotoSingleTurn(dx, dz, dy, dh) function turtle.gotoSingleTurn(dx, dy, dz, dh)
dx = dx or turtle.point.x
dy = dy or turtle.point.y dy = dy or turtle.point.y
dz = dz or turtle.point.z
local function gx() local function gx()
if turtle.point.x ~= dx then if turtle.point.x ~= dx then
turtle.moveTowardsX(dx) moveTowardsX(dx)
end end
if turtle.point.z ~= dz then if turtle.point.z ~= dz then
if dh and dh % 2 == 1 then if dh and dh % 2 == 1 then
@ -521,7 +518,7 @@ function turtle.gotoSingleTurn(dx, dz, dy, dh)
local function gz() local function gz()
if turtle.point.z ~= dz then if turtle.point.z ~= dz then
turtle.moveTowardsZ(dz) moveTowardsZ(dz)
end end
if turtle.point.x ~= dx then if turtle.point.x ~= dx then
if dh and dh % 2 == 0 then if dh and dh % 2 == 0 then
@ -561,7 +558,7 @@ function turtle.gotoSingleTurn(dx, dz, dy, dh)
return false return false
end end
local function gotoEx(dx, dz, dy) local function gotoEx(dx, dy, dz)
-- determine the heading to ensure the least amount of turns -- determine the heading to ensure the least amount of turns
-- first check is 1 turn needed - remaining require 2 turns -- first check is 1 turn needed - remaining require 2 turns
if turtle.point.heading == 0 and turtle.point.x <= dx or if turtle.point.heading == 0 and turtle.point.x <= dx or
@ -595,8 +592,8 @@ local function gotoEx(dx, dz, dy)
end end
-- fallback goto - will turn around if was previously moving backwards -- fallback goto - will turn around if was previously moving backwards
local function gotoMultiTurn(dx, dz, dy) local function gotoMultiTurn(dx, dy, dz)
if gotoEx(dx, dz, dy) then if gotoEx(dx, dy, dz) then
return true return true
end end
@ -625,19 +622,20 @@ local function gotoMultiTurn(dx, dz, dy)
return false return false
end end
function turtle.gotoPoint(pt)
return turtle._goto(pt.x, pt.z, pt.y, pt.heading)
end
-- go backwards - turning around if necessary to fight mobs / break blocks -- go backwards - turning around if necessary to fight mobs / break blocks
function turtle.goback() function turtle.goback()
local hi = headings[turtle.point.heading] local hi = headings[turtle.point.heading]
return turtle._goto(turtle.point.x - hi.xd, turtle.point.z - hi.zd, turtle.point.y, turtle.point.heading) return turtle._goto({
x = turtle.point.x - hi.xd,
y = turtle.point.y,
z = turtle.point.z - hi.zd,
heading = turtle.point.heading,
})
end end
function turtle.gotoYfirst(pt) function turtle.gotoYfirst(pt)
if turtle._gotoY(pt.y) then if turtle._gotoY(pt.y) then
if turtle._goto(pt.x, pt.z, nil, pt.heading) then if turtle._goto(pt) then
turtle.setHeading(pt.heading) turtle.setHeading(pt.heading)
return true return true
end end
@ -645,7 +643,7 @@ function turtle.gotoYfirst(pt)
end end
function turtle.gotoYlast(pt) function turtle.gotoYlast(pt)
if turtle._goto(pt.x, pt.z, nil, pt.heading) then if turtle._goto({ x = pt.x, z = pt.z, heading = pt.heading }) then
if turtle.gotoY(pt.y) then if turtle.gotoY(pt.y) then
turtle.setHeading(pt.heading) turtle.setHeading(pt.heading)
return true return true
@ -653,9 +651,10 @@ function turtle.gotoYlast(pt)
end end
end end
function turtle._goto(dx, dz, dy, dh) function turtle._goto(pt)
if not turtle.gotoSingleTurn(dx, dz, dy, dh) then local dx, dy, dz, dh = pt.x, pt.y, pt.z, pt.heading
if not gotoMultiTurn(dx, dz, dy) then if not turtle.gotoSingleTurn(dx, dy, dz, dh) then
if not gotoMultiTurn(dx, dy, dz) then
return false return false
end end
end end
@ -1058,10 +1057,12 @@ local actionsAt = {
-- ex: place a block at the point from above facing east -- ex: place a block at the point from above facing east
local function _actionAt(action, pt, ...) local function _actionAt(action, pt, ...)
if not pt.heading and not pt.direction then if not pt.heading and not pt.direction then
pt = turtle.moveAgainst(pt) local msg
pt, msg = turtle.moveAgainst(pt)
if pt then if pt then
return action[pt.direction](...) return action[pt.direction](...)
end end
return pt, msg
end end
local reversed = local reversed =
@ -1148,14 +1149,7 @@ function turtle.inspectForwardAt(pt) return _actionForwardAt(actionsAt.inspe
function turtle.inspectUpAt(pt) return _actionUpAt(actionsAt.inspect, pt) end function turtle.inspectUpAt(pt) return _actionUpAt(actionsAt.inspect, pt) end
-- [[ GPS ]] -- -- [[ GPS ]] --
local GPS = require('gps')
local Config = require('config')
function turtle.enableGPS(timeout) function turtle.enableGPS(timeout)
--if turtle.point.gps then
-- return turtle.point
--end
local pt = GPS.getPointAndHeading(timeout) local pt = GPS.getPointAndHeading(timeout)
if pt then if pt then
turtle.setPoint(pt, true) turtle.setPoint(pt, true)
@ -1163,44 +1157,6 @@ function turtle.enableGPS(timeout)
end end
end end
function turtle.gotoGPSHome()
local config = { }
Config.load('gps', config)
if config.home then
if turtle.enableGPS() then
turtle.pathfind(config.home)
end
end
end
function turtle.setGPSHome()
local config = { }
Config.load('gps', config)
if turtle.point.gps then
config.home = turtle.point
Config.update('gps', config)
else
local pt = GPS.getPoint()
if pt then
local originalHeading = turtle.point.heading
local heading = GPS.getHeading()
if heading then
local turns = (turtle.point.heading - originalHeading) % 4
pt.heading = (heading - turns) % 4
config.home = pt
Config.update('gps', config)
pt = GPS.getPoint()
pt.heading = heading
turtle.setPoint(pt, true)
turtle.gotoPoint(config.home)
end
end
end
end
function turtle.addFeatures(...) function turtle.addFeatures(...)
for _,feature in pairs({ ... }) do for _,feature in pairs({ ... }) do
require('turtle.' .. feature) require('turtle.' .. feature)