1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-05 08:26:16 +00:00
opus/sys/apps/Turtles.lua

352 lines
8.1 KiB
Lua
Raw Normal View History

2017-04-15 09:51:44 +00:00
require = requireInjector(getfenv(1))
2017-07-28 23:01:59 +00:00
local Event = require('event')
2017-04-15 09:51:44 +00:00
local UI = require('ui')
local Socket = require('socket')
local Terminal = require('terminal')
2017-07-24 02:37:07 +00:00
local itemDB = require('itemDB')
2017-04-15 09:51:44 +00:00
multishell.setTitle(multishell.getCurrent(), 'Turtles')
UI.Button.defaults.focusIndicator = ' '
UI:configure('Turtles', ...)
2017-04-16 03:39:15 +00:00
local options = {
turtle = { arg = 'i', type = 'number', value = -1,
desc = 'Turtle ID' },
2017-05-26 01:06:17 +00:00
tab = { arg = 's', type = 'string', value = 'turtles',
2017-04-16 03:39:15 +00:00
desc = 'Selected tab to display' },
help = { arg = 'h', type = 'flag', value = false,
desc = 'Displays the options' },
}
2017-05-20 23:05:00 +00:00
local USR_SCRIPTS_PATH = 'usr/scripts'
local SYS_SCRIPTS_PATH = 'sys/etc/scripts'
2017-04-15 09:51:44 +00:00
local nullTerm = Terminal.getNullTerm(term.current())
local turtles = { }
2017-05-26 01:06:17 +00:00
local socket
2017-04-15 09:51:44 +00:00
local policies = {
{ label = 'none' },
{ label = 'digOnly' },
{ label = 'attackOnly' },
{ label = 'digAttack' },
{ label = 'turtleSafe' },
}
local page = UI.Page {
--[[
policy = UI.Chooser {
x = 2, y = 8,
choices = {
{ name = ' None ', value = 'none' },
{ name = ' Safe ', value = 'turtleSafe' },
},
},
]]
coords = UI.Window {
2017-05-26 01:06:17 +00:00
x = 2, y = 2, height = 3, rex = -2,
2017-04-15 09:51:44 +00:00
},
tabs = UI.Tabs {
2017-05-26 01:06:17 +00:00
x = 1, y = 5, rey = -2,
2017-04-15 09:51:44 +00:00
scripts = UI.Grid {
tabTitle = 'Run',
2017-04-20 11:33:36 +00:00
backgroundColor = UI.TabBar.defaults.selectedBackgroundColor,
2017-04-15 09:51:44 +00:00
columns = {
2017-04-20 11:33:36 +00:00
{ heading = '', key = 'label' },
2017-04-15 09:51:44 +00:00
},
disableHeader = true,
sortColumn = 'label',
autospace = true,
},
turtles = UI.Grid {
tabTitle = 'Sel',
2017-04-20 11:33:36 +00:00
backgroundColor = UI.TabBar.defaults.selectedBackgroundColor,
2017-04-15 09:51:44 +00:00
columns = {
{ heading = 'label', key = 'label' },
{ heading = 'Dist', key = 'distance' },
{ heading = 'Status', key = 'status' },
{ heading = 'Fuel', key = 'fuel' },
},
disableHeader = true,
sortColumn = 'label',
autospace = true,
},
inventory = UI.Grid {
2017-04-20 11:33:36 +00:00
backgroundColor = UI.TabBar.defaults.selectedBackgroundColor,
2017-04-15 09:51:44 +00:00
tabTitle = 'Inv',
columns = {
2017-05-26 01:06:17 +00:00
{ heading = '', key = 'index', width = 2 },
2017-04-20 11:33:36 +00:00
{ heading = '', key = 'qty', width = 2 },
2017-05-26 01:06:17 +00:00
{ heading = 'Inventory', key = 'id', width = UI.term.width - 7 },
2017-04-15 09:51:44 +00:00
},
disableHeader = true,
sortColumn = 'index',
},
policy = UI.Grid {
tabTitle = 'Mod',
2017-04-20 11:33:36 +00:00
backgroundColor = UI.TabBar.defaults.selectedBackgroundColor,
2017-04-15 09:51:44 +00:00
columns = {
2017-04-20 11:33:36 +00:00
{ heading = 'label', key = 'label' },
2017-04-15 09:51:44 +00:00
},
values = policies,
disableHeader = true,
sortColumn = 'label',
autospace = true,
},
2017-05-26 01:06:17 +00:00
action = UI.Window {
tabTitle = 'Act',
moveUp = UI.Button {
x = 5, y = 2,
text = '/\\',
fn = 'turtle.up',
},
moveDown = UI.Button {
x = 5, y = 4,
text = '\\/',
fn = 'turtle.down',
},
moveForward = UI.Button {
x = 9, y = 3,
text = '>',
fn = 'turtle.forward',
},
moveBack = UI.Button {
x = 2, y = 3,
text = '<',
fn = 'turtle.back',
},
turnLeft = UI.Button {
x = 2, y = 6,
text = '<-',
fn = 'turtle.turnLeft',
},
turnRight = UI.Button {
x = 8, y = 6,
text = '->',
fn = 'turtle.turnRight',
},
},
2017-04-15 09:51:44 +00:00
},
statusBar = UI.StatusBar(),
notification = UI.Notification(),
accelerators = {
q = 'quit',
},
}
function page:enable(turtle)
self.turtle = turtle
UI.Page.enable(self)
end
function page:runFunction(script, nowrap)
if not socket then
2017-05-26 01:06:17 +00:00
socket = Socket.connect(self.turtle.id, 161)
if not socket then
self.notification:error('Unable to connect')
return
end
2017-04-15 09:51:44 +00:00
end
if not nowrap then
script = 'turtle.run(' .. script .. ')'
end
socket:write({ type = 'script', args = script })
end
function page:runScript(scriptName)
2017-05-20 23:05:00 +00:00
if self.turtle then
2017-06-23 06:04:56 +00:00
self.notification:info('Connecting')
self:sync()
2017-05-20 23:05:00 +00:00
local cmd = string.format('Script %d %s', self.turtle.id, scriptName)
local ot = term.redirect(nullTerm)
pcall(function() shell.run(cmd) end)
term.redirect(ot)
2017-06-23 06:04:56 +00:00
self.notification:success('Sent')
2017-05-20 23:05:00 +00:00
end
2017-04-15 09:51:44 +00:00
end
function page.coords:draw()
local t = self.parent.turtle
if t then
self:clear()
self:setCursorPos(1, 1)
2017-05-26 01:06:17 +00:00
local ind = 'GPS'
2017-07-24 02:37:07 +00:00
if not t.point.gps then
2017-05-26 01:06:17 +00:00
ind = 'REL'
end
self:print(string.format('%s : %d,%d,%d\nFuel: %s\n',
ind, t.point.x, t.point.y, t.point.z, Util.toBytes(t.fuel)))
2017-04-15 09:51:44 +00:00
end
end
--[[ Inventory Tab ]]--
function page.tabs.inventory:getRowTextColor(row, selected)
if page.turtle and row.selected then
return colors.yellow
end
return UI.Grid.getRowTextColor(self, row, selected)
end
function page.tabs.inventory:draw()
local t = page.turtle
Util.clear(self.values)
if t then
for _,v in ipairs(t.inventory) do
if v.qty > 0 then
table.insert(self.values, v)
if v.index == t.slotIndex then
v.selected = true
end
if v.id then
2017-07-24 02:37:07 +00:00
local item = itemDB:get({ v.id, v.dmg })
2017-04-17 08:16:12 +00:00
if item then
v.id = item.displayName
else
v.id = v.id:gsub('.*:(.*)', '%1')
end
2017-04-15 09:51:44 +00:00
end
end
end
end
self:adjustWidth()
self:update()
UI.Grid.draw(self)
end
function page.tabs.inventory:eventHandler(event)
if event.type == 'grid_select' then
local fn = string.format('turtle.select(%d)', event.selected.index)
page:runFunction(fn)
else
return UI.Grid.eventHandler(self, event)
end
return true
end
function page.tabs.scripts:draw()
2017-05-20 23:05:00 +00:00
local function combineDirs(...)
local list = { }
for _,path in pairs({...}) do
if fs.exists(path) then
local files = fs.list(path)
for _,f in pairs(files) do
list[f] = fs.combine(path, f)
end
end
end
return list
end
2017-04-15 09:51:44 +00:00
Util.clear(self.values)
2017-05-20 23:05:00 +00:00
local files = combineDirs(SYS_SCRIPTS_PATH, USR_SCRIPTS_PATH)
for f,path in pairs(files) do
table.insert(self.values, { label = f, path = path })
2017-04-15 09:51:44 +00:00
end
self:update()
UI.Grid.draw(self)
end
function page.tabs.scripts:eventHandler(event)
if event.type == 'grid_select' then
page:runScript(event.selected.label)
else
return UI.Grid.eventHandler(self, event)
end
return true
end
function page.tabs.turtles:getDisplayValues(row)
row = Util.shallowCopy(row)
if row.fuel then
row.fuel = Util.toBytes(row.fuel)
end
if row.distance then
row.distance = Util.round(row.distance, 1)
end
return row
end
function page.tabs.turtles:draw()
Util.clear(self.values)
for _,v in pairs(network) do
if v.fuel then
table.insert(self.values, v)
end
end
self:update()
UI.Grid.draw(self)
end
function page.tabs.turtles:eventHandler(event)
if event.type == 'grid_select' then
page.turtle = event.selected
2017-05-26 01:06:17 +00:00
if socket then
socket:close()
socket = nil
end
2017-04-15 09:51:44 +00:00
else
return UI.Grid.eventHandler(self, event)
end
return true
end
function page.statusBar:draw()
local t = self.parent.turtle
if t then
local status = string.format('%s [ %s ]', t.status, Util.round(t.distance, 2))
self:setStatus(status, true)
end
UI.StatusBar.draw(self)
end
function page:eventHandler(event)
if event.type == 'quit' then
2017-04-20 11:33:36 +00:00
UI:exitPullEvents()
2017-04-15 09:51:44 +00:00
elseif event.type == 'button_press' then
if event.button.fn then
self:runFunction(event.button.fn, event.button.nowrap)
elseif event.button.script then
self:runScript(event.button.script)
end
else
return UI.Page.eventHandler(self, event)
end
return true
end
function page:enable()
UI.Page.enable(self)
-- self.tabs:activateTab(page.tabs.turtles)
end
2017-04-16 03:39:15 +00:00
if not Util.getOptions(options, { ... }, true) then
return
end
2017-04-17 07:13:00 +00:00
if options.turtle.value >= 0 then
for i = 1, 10 do
page.turtle = _G.network[options.turtle.value]
if page.turtle then
break
end
os.sleep(1)
end
2017-04-16 03:39:15 +00:00
end
2017-07-28 23:01:59 +00:00
Event.onInterval(1, function()
if page.turtle then
local t = _G.network[page.turtle.id]
page.turtle = t
page:draw()
page:sync()
end
end)
2017-04-15 09:51:44 +00:00
UI:setPage(page)
2017-04-16 03:39:15 +00:00
page.tabs:activateTab(page.tabs[options.tab.value])
2017-07-28 23:01:59 +00:00
UI:pullEvents()