1
0
mirror of https://github.com/kepler155c/opus synced 2025-10-20 02:07:39 +00:00
This commit is contained in:
kepler155c@gmail.com
2017-10-08 17:45:01 -04:00
parent 98ec840db1
commit a625b52bad
46 changed files with 334 additions and 319 deletions

View File

@@ -1,5 +1,5 @@
local Ansi = setmetatable({ }, {
__call = function(self, ...)
__call = function(_, ...)
local str = '\027['
for k,v in ipairs({ ...}) do
if k == 1 then

View File

@@ -1,5 +1,7 @@
local Util = require('util')
local fs = _G.fs
local Config = { }
Config.load = function(fname, data)

View File

@@ -1,3 +1,5 @@
local os = _G.os
local Event = {
uid = 1, -- unique id for handlers
routines = { }, -- coroutines
@@ -123,13 +125,13 @@ function Event.waitForEvent(event, timeout)
local e = { os.pullEvent() }
if e[1] == event then
return table.unpack(e)
end
end
until e[1] == 'timer' and e[2] == timerId
end
function Event.addRoutine(fn)
local r = {
co = coroutine.create(fn),
co = coroutine.create(fn),
uid = nextUID()
}
setmetatable(r, { __index = Routine })

View File

@@ -1,5 +1,7 @@
local git = require('git')
local fs = _G.fs
local gitfs = { }
function gitfs.mount(dir, repo)

View File

@@ -1,6 +1,8 @@
local fs = _G.fs
local linkfs = { }
local methods = { 'exists', 'getFreeSpace', 'getSize',
local methods = { 'exists', 'getFreeSpace', 'getSize',
'isDir', 'isReadOnly', 'list', 'listEx', 'makeDir', 'open', 'getDrive' }
for _,m in pairs(methods) do
@@ -10,7 +12,7 @@ for _,m in pairs(methods) do
end
end
function linkfs.mount(dir, source)
function linkfs.mount(_, source)
if not source then
error('Source is required')
end
@@ -23,7 +25,7 @@ function linkfs.mount(dir, source)
end
return {
source = source
}
}
end
function linkfs.copy(node, s, t)

View File

@@ -1,11 +1,13 @@
local Socket = require('socket')
local Socket = require('socket')
local synchronized = require('sync')
local fs = _G.fs
local netfs = { }
local function remoteCommand(node, msg)
for i = 1, 2 do
for _ = 1, 2 do
if not node.socket then
node.socket = Socket.connect(node.id, 139)
end
@@ -49,7 +51,7 @@ for _,m in pairs(methods) do
end
end
function netfs.mount(dir, id, directory)
function netfs.mount(_, id, directory)
if not id or not tonumber(id) then
error('ramfs syntax: computerId [directory]')
end

View File

@@ -1,8 +1,10 @@
local Util = require('util')
local fs = _G.fs
local ramfs = { }
function ramfs.mount(dir, nodeType)
function ramfs.mount(_, nodeType)
if nodeType == 'directory' then
return {
nodes = { },
@@ -34,7 +36,7 @@ function ramfs.isReadOnly()
return false
end
function ramfs.makeDir(node, dir)
function ramfs.makeDir(_, dir)
fs.mount(dir, 'ramfs', 'directory')
end
@@ -46,10 +48,10 @@ function ramfs.getDrive()
return 'ram'
end
function ramfs.list(node, dir, full)
function ramfs.list(node, dir)
if node.nodes and node.mountPoint == dir then
local files = { }
for k,v in pairs(node.nodes) do
for k in pairs(node.nodes) do
table.insert(files, k)
end
return files

View File

@@ -1,9 +1,11 @@
local synchronized = require('sync')
local Util = require('util')
local fs = _G.fs
local urlfs = { }
function urlfs.mount(dir, url)
function urlfs.mount(_, url)
if not url then
error('URL is required')
end
@@ -12,7 +14,7 @@ function urlfs.mount(dir, url)
}
end
function urlfs.delete(node, dir)
function urlfs.delete(_, dir)
fs.unmount(dir)
end

View File

@@ -6,9 +6,9 @@ local FILE_URL = 'https://raw.githubusercontent.com/%s/%s/%s/%s'
local git = { }
function git.list(repo)
function git.list(repository)
local t = Util.split(repo, '(.-)/')
local t = Util.split(repository, '(.-)/')
local user = t[1]
local repo = t[2]
@@ -33,7 +33,7 @@ function git.list(repo)
local list = { }
for k,v in pairs(data.tree) do
for _,v in pairs(data.tree) do
if v.type == "blob" then
v.path = v.path:gsub("%s","%%20")
list[v.path] = {

View File

@@ -1,5 +1,9 @@
local GPS = { }
local device = _G.device
local gps = _G.gps
local turtle = _G.turtle
function GPS.locate(timeout, debug)
local pt = { }
timeout = timeout or 10
@@ -14,7 +18,6 @@ function GPS.isAvailable()
end
function GPS.getPoint(timeout, debug)
local pt = GPS.locate(timeout, debug)
if not pt then
return
@@ -24,7 +27,7 @@ function GPS.getPoint(timeout, debug)
pt.y = math.floor(pt.y)
pt.z = math.floor(pt.z)
if pocket then
if _G.pocket then
pt.y = pt.y - 1
end
@@ -47,7 +50,7 @@ function GPS.getHeading(timeout)
while not turtle.forward() do
turtle.turnRight()
if turtle.getHeading() == heading then
printError('GPS.getPoint: Unable to move forward')
_G.printError('GPS.getPoint: Unable to move forward')
return
end
end
@@ -79,13 +82,13 @@ function GPS.getPointAndHeading(timeout)
end
-- from stock gps API
local function trilaterate( A, B, C )
local function trilaterate(A, B, C)
local a2b = B.position - A.position
local a2c = C.position - A.position
if math.abs( a2b:normalize():dot( a2c:normalize() ) ) > 0.999 then
return nil
end
return
end
local d = a2b:length()
local ex = a2b:normalize( )
@@ -100,22 +103,22 @@ local function trilaterate( A, B, C )
local x = (r1*r1 - r2*r2 + d*d) / (2*d)
local y = (r1*r1 - r3*r3 - x*x + (x-i)*(x-i) + j*j) / (2*j)
local result = A.position + (ex * x) + (ey * y)
local zSquared = r1*r1 - x*x - y*y
local zSquared = r1*r1 - x*x - y*y
if zSquared > 0 then
local z = math.sqrt( zSquared )
local result1 = result + (ez * z)
local result2 = result - (ez * z)
local rounded1, rounded2 = result1:round(), result2:round()
if rounded1.x ~= rounded2.x or rounded1.y ~= rounded2.y or rounded1.z ~= rounded2.z then
return rounded1, rounded2
else
return rounded1
end
end
end
end
return result:round()
end
@@ -129,7 +132,7 @@ local function narrow( p1, p2, fix )
return p1:round()
else
return p2:round()
end
end
end
-- end stock gps api

View File

@@ -2,6 +2,10 @@ local DEFAULT_UPATH = 'https://raw.githubusercontent.com/kepler155c/opus/develop
local PASTEBIN_URL = 'http://pastebin.com/raw'
local GIT_URL = 'https://raw.githubusercontent.com'
local fs = _G.fs
local http = _G.http
local os = _G.os
-- fix broken http get
local syncLocks = { }
@@ -44,7 +48,7 @@ end
local function requireWrapper(env)
local function standardSearcher(modname, env, shell)
local function standardSearcher(modname)
if package.loaded[modname] then
return function()
return package.loaded[modname]
@@ -52,18 +56,18 @@ local function requireWrapper(env)
end
end
local function shellSearcher(modname, env, shell)
local function shellSearcher(modname)
local fname = modname:gsub('%.', '/') .. '.lua'
if shell and type(shell.dir) == 'function' then
local path = shell.resolve(fname)
if env.shell and type(env.shell.dir) == 'function' then
local path = env.shell.resolve(fname)
if fs.exists(path) and not fs.isDir(path) then
return loadfile(path, env)
end
end
end
local function pathSearcher(modname, env, shell)
local function pathSearcher(modname)
local fname = modname:gsub('%.', '/') .. '.lua'
for dir in string.gmatch(package.path, "[^:]+") do
@@ -75,7 +79,7 @@ local function requireWrapper(env)
end
-- require('BniCQPVf')
local function pastebinSearcher(modname, env, shell)
local function pastebinSearcher(modname)
if #modname == 8 and not modname:match('%W') then
local url = PASTEBIN_URL .. '/' .. modname
local c = loadUrl(url)
@@ -86,7 +90,7 @@ local function requireWrapper(env)
end
-- require('kepler155c.opus.master.sys.apis.util')
local function gitSearcher(modname, env, shell)
local function gitSearcher(modname)
local fname = modname:gsub('%.', '/') .. '.lua'
local _, count = fname:gsub("/", "")
if count >= 3 then
@@ -98,7 +102,7 @@ local function requireWrapper(env)
end
end
local function urlSearcher(modname, env, shell)
local function urlSearcher(modname)
local fname = modname:gsub('%.', '/') .. '.lua'
if fname:sub(1, 1) ~= '/' then
@@ -113,7 +117,7 @@ local function requireWrapper(env)
end
-- place package and require function into env
package = {
env.package = {
path = LUA_PATH or 'sys/apis',
upath = LUA_UPATH or DEFAULT_UPATH,
config = '/\n:\n?\n!\n-',
@@ -134,14 +138,14 @@ local function requireWrapper(env)
}
}
function require(modname)
function env.require(modname)
for _,searcher in ipairs(package.loaders) do
local fn, msg = searcher(modname, env, shell)
local fn, msg = searcher(modname)
if fn then
local module, msg = fn(modname, env)
local module, msg2 = fn(modname, env)
if not module then
error(msg or (modname .. ' module returned nil'), 2)
error(msg2 or (modname .. ' module returned nil'), 2)
end
package.loaded[modname] = module
return module
@@ -153,10 +157,11 @@ local function requireWrapper(env)
error('Unable to find module ' .. modname)
end
return require -- backwards compatible
return env.require -- backwards compatible
end
return function(env)
env = env or getfenv(2)
setfenv(requireWrapper, env)
return requireWrapper(env)
end

View File

@@ -189,7 +189,7 @@ function json.parseObject(str)
local val = {}
while str:sub(1, 1) ~= "}" do
local k, v = nil, nil
local k, v
k, v, str = json.parseMember(str)
val[k] = v
str = removeWhite(str)

View File

@@ -21,7 +21,6 @@ function NFT.parse(imageText)
}
local num = 1
local index = 1
for _,sLine in ipairs(Util.split(imageText)) do
table.insert(image.fg, { })
table.insert(image.bg, { })
@@ -47,7 +46,7 @@ function NFT.parse(imageText)
fgNext = false
else
if nextChar ~= " " and currFG == nil then
currFG = colours.white
currFG = _G.colors.white
end
image.bg[num][writeIndex] = currBG
image.fg[num][writeIndex] = currFG

View File

@@ -1,3 +1,9 @@
local colors = _G.colors
local fs = _G.fs
local os = _G.os
--local shell = _ENV.shell
local term = _G.term
local Opus = { }
local function runDir(directory, open)
@@ -27,7 +33,7 @@ local function runDir(directory, open)
term.setTextColor(colors.white)
term.write(fs.combine(directory, file))
if err then
printError(err)
_G.printError(err)
end
success = false
end

View File

@@ -1,16 +1,14 @@
local Util = require('util')
local Peripheral = { }
local function getDeviceList()
local Peripheral = Util.shallowCopy(_G.peripheral)
function Peripheral.getList()
if _G.device then
return _G.device
end
local deviceList = { }
for _,side in pairs(peripheral.getNames()) do
for _,side in pairs(Peripheral.getNames()) do
Peripheral.addDevice(deviceList, side)
end
@@ -19,14 +17,14 @@ end
function Peripheral.addDevice(deviceList, side)
local name = side
local ptype = peripheral.getType(side)
local ptype = Peripheral.getType(side)
if not ptype then
return
end
if ptype == 'modem' then
if peripheral.call(name, 'isWireless') then
if Peripheral.call(name, 'isWireless') then
ptype = 'wireless_modem'
else
ptype = 'wired_modem'
@@ -52,10 +50,10 @@ function Peripheral.addDevice(deviceList, side)
name = uniqueName
end
local s, m pcall(function() deviceList[name] = peripheral.wrap(side) end)
local s, m = pcall(function() deviceList[name] = Peripheral.wrap(side) end)
if not s and m then
printError('wrap failed')
printError(m)
_G.printError('wrap failed')
_G.printError(m)
end
if deviceList[name] then
@@ -70,15 +68,15 @@ function Peripheral.addDevice(deviceList, side)
end
function Peripheral.getBySide(side)
return Util.find(getDeviceList(), 'side', side)
return Util.find(Peripheral.getList(), 'side', side)
end
function Peripheral.getByType(typeName)
return Util.find(getDeviceList(), 'type', typeName)
return Util.find(Peripheral.getList(), 'type', typeName)
end
function Peripheral.getByMethod(method)
for _,p in pairs(getDeviceList()) do
for _,p in pairs(Peripheral.getList()) do
if p[method] then
return p
end
@@ -92,8 +90,6 @@ function Peripheral.get(args)
args = { type = args }
end
args = args or { type = pType }
if args.type then
local p = Peripheral.getByType(args.type)
if p then

View File

@@ -37,7 +37,7 @@ end
-- turtle distance (manhattan)
function Point.turtleDistance(a, b)
if a.y and b.y then
return math.abs(a.x - b.x) +
return math.abs(a.x - b.x) +
math.abs(a.y - b.y) +
math.abs(a.z - b.z)
else
@@ -116,7 +116,7 @@ function Point.calculateMoves(pta, ptb, distance)
heading = ptb.heading
end
end
return moves, heading
end
@@ -149,7 +149,7 @@ end
function Point.adjacentPoints(pt)
local pts = { }
for _, hi in pairs(turtle.getHeadings()) do
for _, hi in pairs(_G.turtle.getHeadings()) do
table.insert(pts, { x = pt.x + hi.xd, y = pt.y + hi.yd, z = pt.z + hi.zd })
end

View File

@@ -28,7 +28,7 @@ function Security.getPublicKey()
local function modexp(base, exponent, modulo)
local remainder = base
for i = 1, exponent-1 do
for _ = 1, exponent-1 do
remainder = remainder * remainder
if remainder >= modulo then
remainder = remainder % modulo

View File

@@ -3,6 +3,10 @@ local Logger = require('logger')
local Security = require('security')
local Util = require('util')
local device = _G.device
local os = _G.os
local transport = _G.transport
local socketClass = { }
function socketClass:read(timeout)
@@ -165,7 +169,7 @@ function Socket.server(port)
Logger.log('socket', 'Waiting for connections on port ' .. port)
while true do
local e, _, sport, dport, msg = os.pullEvent('modem_message')
local _, _, sport, dport, msg = os.pullEvent('modem_message')
if sport == port and
msg and

View File

@@ -1,5 +1,7 @@
local syncLocks = { }
local os = _G.os
return function(obj, fn)
local key = tostring(obj)
if syncLocks[key] then

View File

@@ -1,14 +1,15 @@
local Util = require('util')
local colors = _G.colors
local term = _G.term
local _gsub = string.gsub
local Terminal = { }
local _sgsub = string.gsub
function Terminal.scrollable(ct, size)
local size = size or 25
local w, h = ct.getSize()
local win = window.create(ct, 1, 1, w, h + size, true)
local win = _G.window.create(ct, 1, 1, w, h + size, true)
local oldWin = Util.shallowCopy(win)
local scrollPos = 0
@@ -87,7 +88,7 @@ function Terminal.toGrayscale(ct)
local methods = { 'setBackgroundColor', 'setBackgroundColour',
'setTextColor', 'setTextColour' }
for _,v in pairs(methods) do
local fn = ct[v]
local fn = ct[v]
ct[v] = function(c)
fn(scolors[c])
end
@@ -110,7 +111,7 @@ function Terminal.toGrayscale(ct)
local function translate(s)
if s then
s = _sgsub(s, "%w", bcolors)
s = _gsub(s, "%w", bcolors)
end
return s
end
@@ -136,9 +137,9 @@ end
function Terminal.copy(it, ot)
ot = ot or { }
for k,v in pairs(it) do
if type(v) == 'function' then
ot[k] = v
end
if type(v) == 'function' then
ot[k] = v
end
end
return ot
end
@@ -162,7 +163,7 @@ function Terminal.readPassword(prompt)
local fn = term.current().write
term.current().write = function() end
local s
pcall(function() s = read(prompt) end)
pcall(function() s = _G.read(prompt) end)
term.current().write = fn
if s == '' then

View File

@@ -1,19 +1,21 @@
requireInjector(getfenv(1))
_G.requireInjector()
local Grid = require ("jumper.grid")
local Pathfinder = require ("jumper.pathfinder")
local Point = require('point')
local Util = require('util')
local turtle = _G.turtle
local WALKABLE = 0
local function createMap(dim)
local map = { }
for z = 1, dim.ez do
for _ = 1, dim.ez do
local row = {}
for x = 1, dim.ex do
for _ = 1, dim.ex do
local col = { }
for y = 1, dim.ey do
for _ = 1, dim.ey do
table.insert(col, WALKABLE)
end
table.insert(row, col)
@@ -80,10 +82,10 @@ local function mapDimensions(dest, blocks, boundingBox)
return {
ex = ex - sx + 1,
ez = ez - sz + 1,
ey = ey - sy + 1,
ox = -sx + 1,
oz = -sz + 1,
ez = ez - sz + 1,
ey = ey - sy + 1,
ox = -sx + 1,
oz = -sz + 1,
oy = -sy + 1
}
end
@@ -149,7 +151,7 @@ local function selectDestination(pts, box, map, dim)
map[pt.z + dim.oz][pt.x + dim.ox][pt.y + dim.oy] == 1 then
Util.removeByValue(pts, pt)
else
return pt
return pt
end
end
end
@@ -169,7 +171,7 @@ local function pathTo(dest, options)
end
-- Creates a pathfinder object
local myFinder = Pathfinder(grid, 'ASTAR', walkable)
local myFinder = Pathfinder(grid, 'ASTAR', WALKABLE)
myFinder:setMode('ORTHOGONAL')
myFinder:setHeuristic(heuristic)
@@ -208,12 +210,14 @@ local function pathTo(dest, options)
local endPt = pointToMap(dim, dest)
-- Calculates the path, and its length
local path = myFinder:getPath(startPt.x, startPt.y, startPt.z, turtle.point.heading, endPt.x, endPt.y, endPt.z, dest.heading)
local path = myFinder:getPath(
startPt.x, startPt.y, startPt.z, turtle.point.heading,
endPt.x, endPt.y, endPt.z, dest.heading)
if not path then
Util.removeByValue(dests, dest)
else
for node, count in path:nodes() do
for node in path:nodes() do
local pt = nodeToPoint(dim, node)
if turtle.abort then
@@ -223,10 +227,10 @@ local function pathTo(dest, options)
-- use single turn method so the turtle doesn't turn around
-- when encountering obstacles -- IS THIS RIGHT ??
if not turtle.gotoSingleTurn(pt.x, pt.z, pt.y, node.heading) then
table.insert(blocks, pt)
--if device.turtlesensorenvironment then
-- addSensorBlocks(blocks, device.turtlesensorenvironment.sonicScan())
--end
table.insert(blocks, pt)
--if device.turtlesensorenvironment then
-- addSensorBlocks(blocks, device.turtlesensorenvironment.sonicScan())
--end
break
end
end

View File

@@ -1320,7 +1320,7 @@ end
function UI.Grid:adjustWidth()
local t = { } -- cols without width
local w = self.width - #self.columns - 1 - self.marginRight -- width remaing
local w = self.width - #self.columns - 1 - self.marginRight -- width remaining
for _,c in pairs(self.columns) do
if c.width then

View File

@@ -1,6 +1,9 @@
local UI = require('ui')
local Util = require('util')
local colors = _G.colors
local fs = _G.fs
return function(args)
local columns = {
@@ -86,7 +89,7 @@ return function(args)
return row
end
function selectFile.grid:getRowTextColor(file, selected)
function selectFile.grid:getRowTextColor(file)
if file.isDir then
return colors.cyan
end

View File

@@ -162,7 +162,7 @@ end
function Util.findAll(t, name, value)
local rt = { }
for k,v in pairs(t) do
for _,v in pairs(t) do
if v[name] == value then
table.insert(rt, v)
end
@@ -253,7 +253,7 @@ function Util.spairs(t, order)
local keys = Util.keys(t)
-- if order function given, sort by it by passing the table and keys a, b,
-- otherwise just sort the keys
-- otherwise just sort the keys
if order then
table.sort(keys, function(a,b) return order(t[a], t[b]) end)
else
@@ -317,7 +317,7 @@ function Util.writeLines(fname, lines)
local file = fs.open(fname, 'w')
if file then
for _,line in ipairs(lines) do
line = file.writeLine(line)
file.writeLine(line)
end
file.close()
return true
@@ -458,12 +458,12 @@ end
function Util.trim(s)
return s:find'^%s*$' and '' or s:match'^%s*(.*%S)'
end
-- trim whitespace from left end of string
function Util.triml(s)
return s:match'^%s*(.*)'
end
-- trim whitespace from right end of string
function Util.trimr(s)
return s:find'^%s*$' and '' or s:match'^(.*%S)'
@@ -548,7 +548,7 @@ end
function Util.showOptions(options)
print('Arguments: ')
for k, v in pairs(options) do
for _, v in pairs(options) do
print(string.format('-%s %s', v.arg, v.desc))
end
end
@@ -561,7 +561,6 @@ function Util.getOptions(options, args, ignoreInvalid)
end
end
local rawOptions = getopt(args, argLetters)
local argCount = 0
for k,ro in pairs(rawOptions) do
local found = false