diff --git a/apps/Turtles.lua b/apps/Turtles.lua new file mode 100644 index 0000000..45ce8c1 --- /dev/null +++ b/apps/Turtles.lua @@ -0,0 +1,288 @@ +require = requireInjector(getfenv(1)) +local Event = require('event') +local UI = require('ui') +local Socket = require('socket') +local Terminal = require('terminal') + +multishell.setTitle(multishell.getCurrent(), 'Turtles') +UI.Button.defaults.focusIndicator = ' ' +UI:configure('Turtles', ...) + +local SCRIPTS_PATH = '/apps/scripts' + +local ct = term.current() +local nullTerm = Terminal.getNullTerm(term.current()) +local turtles = { } +local policies = { + { label = 'none' }, + { label = 'digOnly' }, + { label = 'attackOnly' }, + { label = 'digAttack' }, + { label = 'turtleSafe' }, +} + +local page = UI.Page { + 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', + }, +--[[ + policy = UI.Chooser { + x = 2, y = 8, + choices = { + { name = ' None ', value = 'none' }, + { name = ' Safe ', value = 'turtleSafe' }, + }, + }, +]] + coords = UI.Window { + x = 16, y = 2, height = 4, width = 9, + }, + tabs = UI.Tabs { + x = 1, y = 8, rey = -2, + scripts = UI.Grid { + tabTitle = 'Run', + columns = { + { heading = '', key = 'label' }, + }, + disableHeader = true, + sortColumn = 'label', + autospace = true, + }, + turtles = UI.Grid { + tabTitle = 'Sel', + 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 { + tabTitle = 'Inv', + columns = { + { heading = '', key = 'qty', width = 2 }, + { heading = 'Inventory', key = 'id', width = 13 }, + }, + disableHeader = true, + sortColumn = 'index', + }, + policy = UI.Grid { + tabTitle = 'Mod', + columns = { + { heading = 'label', key = 'label' }, + }, + values = policies, + disableHeader = true, + sortColumn = 'label', + autospace = true, + }, + }, + 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) + + local socket = Socket.connect(self.turtle.id, 161) + if not socket then + self.notification:error('Unable to connect') + return + end + + if not nowrap then + script = 'turtle.run(' .. script .. ')' + end + socket:write({ type = 'script', args = script }) + socket:close() +end + +function page:runScript(scriptName) + 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) +end + +function page.coords:draw() + local t = self.parent.turtle + if t then + self:clear() + self:setCursorPos(1, 1) + self:print(string.format('%s\nx: %d\ny: %d\nz: %d\n', t.coordSystem, t.point.x, t.point.y, t.point.z)) + 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 + v.id = v.id:gsub('.*:(.*)', '%1') + 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() + Util.clear(self.values) + local files = fs.list(SCRIPTS_PATH) + for _,f in pairs(files) do + table.insert(self.values, { label = f, path = fs.combine(SCRIPTS_PATH, f) }) + 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 + 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 + UI:setPreviousPage() + 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 + +local function updateThread() + + while true do + if page.turtle then + local t = _G.network[page.turtle.id] + page.turtle = t + page:draw() + page:sync() + end + + os.sleep(1) + end +end + +UI:setPage(page) + +Event.pullEvents(updateThread) +UI.term:reset() diff --git a/apps/simpleMiner.lua b/apps/simpleMiner.lua index 99c7545..8f8457d 100644 --- a/apps/simpleMiner.lua +++ b/apps/simpleMiner.lua @@ -8,20 +8,30 @@ end local args = { ... } local options = { - chunks = { arg = 'c', type = 'number', value = -1, + chunks = { arg = 'c', type = 'number', value = -1, desc = 'Number of chunks to mine' }, - depth = { arg = 'd', type = 'number', value = 9000, + depth = { arg = 'd', type = 'number', value = 9000, desc = 'Mining depth' }, -- enderChest = { arg = 'e', type = 'flag', value = false, -- desc = 'Use ender chest' }, - resume = { arg = 'r', type = 'flag', value = false, + resume = { arg = 'r', type = 'flag', value = false, desc = 'Resume mining' }, - setTrash = { arg = 's', type = 'flag', value = false, + fortunePick = { arg = 'p', type = 'string', value = nil, + desc = 'Pick to use with CCTweaks toolhost' }, + setTrash = { arg = 's', type = 'flag', value = false, desc = 'Set trash items' }, - help = { arg = 'h', type = 'flag', value = false, + help = { arg = 'h', type = 'flag', value = false, desc = 'Displays the options' }, } +local fortuneBlocks = { + [ 'minecraft:redstone_ore' ] = true, + [ 'minecraft:lapis_ore' ] = true, + [ 'minecraft:coal_ore' ] = true, + [ 'minecraft:diamond_ore' ] = true, + [ 'minecraft:emerald_ore' ] = true, +} + local MIN_FUEL = 7500 local LOW_FUEL = 1500 local MAX_FUEL = 100000 @@ -161,10 +171,10 @@ function addTrash() local slots = turtle.getFilledSlots() for k,slot in pairs(slots) do - if slot.iddmg ~= 'minecraft:bucket:0' then - trash[slot.iddmg] = true - end + trash[slot.iddmg] = true end + + trash['minecraft:bucket:0'] = nil Util.writeTable('mining.trash', trash) end @@ -278,9 +288,14 @@ function normalChestUnload() end local slots = turtle.getFilledSlots() for _,slot in pairs(slots) do - if not trash[slot.iddmg] and slot.iddmg ~= 'minecraft:bucket:0' then - turtle.select(slot.index) - turtle.dropUp(64) + if not trash[slot.iddmg] and + slot.iddmg ~= 'minecraft:bucket:0' and + slot.id ~= 'minecraft:diamond_pickaxe' and + slot.id ~= 'cctweaks:toolHost' then + if slot.id ~= options.fortunePick.value then + turtle.select(slot.index) + turtle.dropUp(64) + end end end turtle.select(1) @@ -336,17 +351,36 @@ function mineable(action) end if action.side == 'bottom' then - return true + return block.name end - return not trash[block.name .. ':0'] + if trash[block.name .. ':0'] then + return false + end + + return block.name +end + +function fortuneDig(action, blockName) + if options.fortunePick.value and fortuneBlocks[blockName] then + turtle.selectSlot('cctweaks:toolHost') + turtle.equipRight() + turtle.selectSlot(options.fortunePick.value) + repeat until not turtle.dig() + turtle.selectSlot('minecraft:diamond_pickaxe') + turtle.equipRight() + return true + end end function mine(action) - if mineable(action) then + local blockName = mineable(action) + if blockName then checkSpace() --collectDrops(action.suck) - action.dig() + if not fortuneDig(action, blockName) then + action.dig() + end end end @@ -377,9 +411,11 @@ function bore() break end - mine(turtle.getAction('forward')) - turtle.turnRight() - mine(turtle.getAction('forward')) + if loc.y < level - 1 then + mine(turtle.getAction('forward')) + turtle.turnRight() + mine(turtle.getAction('forward')) + end end boreDirection = 'up' @@ -470,9 +506,7 @@ function Point.compare(pta, ptb) end function inspect(action, name) - local r, block = action.inspect() - if r and block.name == name then return true end @@ -549,6 +583,21 @@ if not turtle.getSlot('minecraft:bucket:0') or read() end +if options.fortunePick.value then + local s = turtle.getSlot(options.fortunePick.value) + if not s then + error('fortunePick not found: ' .. options.fortunePick.value) + end + if not turtle.getSlot('cctweaks:toolHost:0') then + error('CCTweaks tool host not found') + end + trash[s.iddmg] = nil + trash['minecraft:diamond_pickaxe:0'] = nil + trash['cctweaks:toolHost:0'] = nil +end + +_G._p = trash + local function main() repeat while #mining.locations > 0 do