1
0
mirror of https://github.com/kepler155c/opus synced 2024-12-24 15:40: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
end
function Security.hasPassword()
return not not config.password
end
function Security.getSecretKey()
Config.load('os', config)
if not config.secretKey then

View File

@ -157,6 +157,11 @@ local function trusted(msg, port)
return true
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 pubKey = trustList[msg.shost]

View File

@ -149,12 +149,11 @@ local function pathTo(dest, options)
-- Define start and goal locations coordinates
local startPt = turtle.point
local endPt = dest
-- Calculates the path, and its length
local path = finder:getPath(
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
Util.removeByValue(dests, dest)
@ -176,7 +175,7 @@ local function pathTo(dest, options)
-- use single turn method so the turtle doesn't turn around
-- 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)
table.insert(blocks, bpt)

View File

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

View File

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

View File

@ -11,6 +11,7 @@ local multishell = _ENV.multishell
local os = _G.os
local settings = _G.settings
local shell = _ENV.shell
local turtle = _G.turtle
multishell.setTitle(multishell.getCurrent(), 'System')
UI:configure('System', ...)
@ -44,7 +45,7 @@ local systemPage = UI.Page {
},
aliasTab = UI.Window {
tabTitle = 'Aliases',
tabTitle = 'Alias',
alias = UI.TextEntry {
x = 2, y = 2, ex = -2,
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
local values = { }
for _,v in pairs(settings.getNames()) do
@ -162,8 +221,6 @@ if settings then
{ heading = 'Setting', key = 'name' },
{ heading = 'Value', key = 'value' },
},
accelerators = {
},
},
}
})

View File

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