begin moving turtle specific code from opus into turtle package

This commit is contained in:
kepler155c@gmail.com 2019-02-18 02:56:28 -05:00
parent a36051bf78
commit 440b829f62
7 changed files with 33 additions and 162 deletions

View File

@ -51,6 +51,9 @@ local function trim_traceback(target, marker)
t_len = t_len - 1
end
ttarget[#ttarget] = nil -- remove 2 calls added by the added xpcall
ttarget[#ttarget] = nil
return ttarget
end
@ -83,14 +86,14 @@ return function (fn, ...)
-- If this traceback is more than 15 elements long, keep the first 9, last 5
-- and put an ellipsis between the rest
local max = 12
local max = 10
if trace_starts and #trace - trace_starts > max then
local keep_starts = trace_starts + 9
local keep_starts = trace_starts + 7
for i = #trace - trace_starts - max, 0, -1 do table.remove(trace, keep_starts + i) end
table.insert(trace, keep_starts, " ...")
end
return false, table.concat(trace, "\n")
return false, table.remove(trace, 1), table.concat(trace, "\n")
end
return table.unpack(res, 1, res.n)

View File

@ -1,42 +0,0 @@
local Config = require('config')
local GPS = require('gps')
local turtle = _G.turtle
local Home = { }
function Home.go()
local config = { }
Config.load('gps', config)
if config.home then
if turtle.enableGPS() then
return turtle.pathfind(config.home)
end
end
end
function Home.set()
local config = { }
Config.load('gps', config)
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._goto(config.home)
return config.home
end
end
end
return Home

View File

@ -1,62 +0,0 @@
local Config = require('config')
local UI = require('ui')
local fs = _G.fs
local turtle = _G.turtle
if turtle then
local Home = require('turtle.home')
local values = { }
Config.load('gps', values.home and { values.home } or { })
local gpsTab = UI.Tab {
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 gpsTab:eventHandler(event)
if event.type == 'gps_set' then
self:emit({ type = 'info_message', message = 'Determining location' })
self:sync()
if Home.set() then
Config.load('gps', values)
self.grid:setValues(values.home and { values.home } or { })
self.grid:draw()
self:emit({ type = 'success_message', message = 'Location set' })
else
self:emit({ type = 'error_message', message = '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
return gpsTab
end

View File

@ -1,43 +0,0 @@
local modem = _G.device.wireless_modem
local turtle = _G.turtle
if turtle and modem then
local s, m = turtle.run(function()
_G.requireInjector(_ENV)
local Config = require('config')
local config = {
destructive = false,
}
Config.load('gps', config)
if config.home then
local s = turtle.enableGPS(2)
if not s then
s = turtle.enableGPS(2)
end
if not s and config.destructive then
turtle.set({ attackPolicy = 'attack', digPolicy = 'turtleSafe' })
s = turtle.enableGPS(2)
end
if not s then
error('Unable to get GPS position')
end
if config.destructive then
turtle.set({ attackPolicy = 'attack', digPolicy = 'turtleSafe' })
end
if not turtle.pathfind(config.home) then
error('Failed to return home')
end
end
end)
if not s and m then
error(m)
end
end

View File

@ -2,7 +2,7 @@ if not _G.turtle then
return
end
local Pathing = require('turtle.pathfind')
local Pathing = require('pathfind')
local GPS = require('gps')
local Point = require('point')
local synchronized = require('sync').sync
@ -661,7 +661,7 @@ end
-- go backwards - turning around if necessary to fight mobs / break blocks
function turtle.goback()
local hi = headings[turtle.point.heading]
return turtle._goto({
return turtle.go({
x = turtle.point.x - hi.xd,
y = turtle.point.y,
z = turtle.point.z - hi.zd,
@ -670,8 +670,8 @@ function turtle.goback()
end
function turtle.gotoYfirst(pt)
if turtle._gotoY(pt.y) then
if turtle._goto(pt) then
if turtle.gotoY(pt.y) then
if turtle.go(pt) then
turtle.setHeading(pt.heading)
return true
end
@ -679,7 +679,7 @@ function turtle.gotoYfirst(pt)
end
function turtle.gotoYlast(pt)
if turtle._goto({ x = pt.x, z = pt.z, heading = pt.heading }) then
if turtle.go({ x = pt.x, z = pt.z, heading = pt.heading }) then
if turtle.gotoY(pt.y) then
turtle.setHeading(pt.heading)
return true
@ -687,8 +687,18 @@ function turtle.gotoYlast(pt)
end
end
function turtle._goto(pt)
local dx, dy, dz, dh = pt.x, pt.y, pt.z, pt.heading
function turtle.go(pt)
if not pt.x and not pt.z and pt.y then
if turtle.gotoY(pt.y) then
turtle.setHeading(pt.heading)
return true
end
return false, 'Failed to reach location'
end
local dx = pt.x or turtle.point.x
local dz = pt.z or turtle.point.z
local dy, dh = pt.y, pt.heading
if not turtle.gotoSingleTurn(dx, dy, dz, dh) then
if not gotoMultiTurn(dx, dy, dz) then
return false, 'Failed to reach location'
@ -699,7 +709,9 @@ function turtle._goto(pt)
end
-- avoid lint errors
turtle['goto'] = turtle._goto
-- deprecated
turtle['goto'] = turtle.go
turtle['_goto'] = turtle.go
function turtle.gotoX(dx)
turtle.headTowardsX(dx)
@ -1026,7 +1038,7 @@ function turtle.setMovementStrategy(strategy)
if strategy == 'pathing' then
movementStrategy = turtle.pathfind
elseif strategy == 'goto' then
movementStrategy = turtle._goto
movementStrategy = turtle.go
else
error('Invalid movement strategy')
end

View File

@ -109,7 +109,7 @@ local function xprun(env, path, ...)
setmetatable(env, { __index = _G })
local fn, m = loadfile(path, env)
if fn then
return pcall(fn, ...)
return trace(fn, ...)
end
return fn, m
end
@ -125,12 +125,12 @@ function multishell.openTab(tab)
local routine = kernel.newRoutine(tab)
routine.co = coroutine.create(function()
local result, err
local result, err, stacktrace
if tab.fn then
result, err = Util.runFunction(routine.env, tab.fn, table.unpack(tab.args or { } ))
elseif tab.path then
result, err = xprun(routine.env, tab.path, table.unpack(tab.args or { } ))
result, err, stacktrace = xprun(routine.env, tab.path, table.unpack(tab.args or { } ))
else
err = 'multishell: invalid tab'
end
@ -138,6 +138,9 @@ function multishell.openTab(tab)
if not result and err and err ~= 'Terminated' then
if err then
printError(tostring(err))
if stacktrace then -- alternatively log stack to _debug
print(stacktrace)
end
end
print('\nPress enter to close')
routine.isDead = true