From 9f009c164a04bd11a5b4538965f94eaa8ff16ac4 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Wed, 20 Feb 2019 01:26:08 -0500 Subject: [PATCH] launch control for elytraFly + mirror fixes --- miners/findSwarm.lua | 1 - monitor/mirror.lua | 52 +++++++++++++++++++++++++++++++++---------- neural/elytraFly.lua | 44 +++++++++++++++++++++++++++--------- neural/etc/apps.db | 6 ++--- turtle/apis/swarm.lua | 8 ++++++- 5 files changed, 83 insertions(+), 28 deletions(-) diff --git a/miners/findSwarm.lua b/miners/findSwarm.lua index e79434f..a0667ce 100644 --- a/miners/findSwarm.lua +++ b/miners/findSwarm.lua @@ -1,4 +1,3 @@ - local Event = require('event') local GPS = require('gps') local Point = require('point') diff --git a/monitor/mirror.lua b/monitor/mirror.lua index a6dd561..edf26f3 100644 --- a/monitor/mirror.lua +++ b/monitor/mirror.lua @@ -1,8 +1,13 @@ local Terminal = require('terminal') local Util = require('util') -local shell = _ENV.shell -local term = _G.term +local device = _G.device +local os = _G.os +local parallel = _G.parallel +local shell = _ENV.shell +local term = _G.term + +-- Example usage: mirror -r -e "vnc 1" local options = { scale = { arg = 's', type = 'flag', value = false, @@ -22,29 +27,52 @@ if not Util.getOptions(options, args) then return end -local mon = _G.device[options.monitor.value] +local mon +for k,v in pairs(device) do + if k == options.monitor.value or v.side == options.monitor.value then + mon = v + break + end +end + if not mon then error('mirror: Invalid device') end mon.clear() -if options.scale.value then - mon.setTextScale(.5) -end +mon.setTextScale(options.scale.value and .5 or 1) mon.setCursorPos(1, 1) -local oterm = Terminal.copy(term.current()) -Terminal.mirror(term.current(), mon) +local oterm = term.current() if options.resize.value then - term.current().getSize = mon.getSize + oterm.reposition(1, 1, mon.getSize()) end +local mirror = Terminal.mirror(term.current(), mon) + +term.redirect(mirror) + if options.execute.value then - -- TODO: allow args to be passed - shell.run(options.execute.value) -- unpack(args)) - Terminal.copy(oterm, term.current()) + parallel.waitForAny( + function() + shell.run(options.execute.value) + end, + + function() + while true do + local event, side, x, y = os.pullEventRaw('monitor_touch') + + if event == 'monitor_touch' and side == mon.side then + os.queueEvent('mouse_click', 1, x, y + 1) + os.queueEvent('mouse_up', 1, x, y + 1) + end + end + end + ) + + term.redirect(oterm) mon.setCursorBlink(false) end diff --git a/neural/elytraFly.lua b/neural/elytraFly.lua index 9dd4a02..31797cf 100644 --- a/neural/elytraFly.lua +++ b/neural/elytraFly.lua @@ -8,11 +8,13 @@ local parallel = _G.parallel if not modules.launch or not modules.getMetaOwner then error([[Required: -* kinetic augment is required') -* error('introspection module is required]]) +* Kinetic augment +* Introspection module]]) end local function run() + local launchCounter = 0 + while true do local meta = modules.getMetaOwner() @@ -20,21 +22,38 @@ local function run() if meta.pitch < 0 then -- looking up modules.launch(meta.yaw, meta.pitch, -meta.pitch / 22.5) - Sound.play('entity.bobber.throw') + Sound.play('entity.bobber.throw', .6) + os.sleep(0.1) elseif meta.motionY < -0.5 then -- falling fast - modules.launch(0, 270, 2) + modules.launch(0, 270, -meta.motionY + 1) Sound.play('entity.bat.takeoff') + os.sleep(0) + + else + os.sleep(0.1) + end + + elseif meta.isSneaking and not meta.isElytraFlying and meta.pitch == -90 then + if launchCounter < 2 then + launchCounter = launchCounter + 1 + Sound.play('block.note.pling', .5) + os.sleep(0.5) + + else + Sound.play('entity.bobber.throw', 1) + modules.launch(0, 270, 4) + os.sleep(0.2) end - os.sleep(0.1) elseif not meta.isSneaking and meta.motionY < -0.8 then print('falling...') - modules.launch(0, 270, 2) + modules.launch(0, 270, -meta.motionY + 1) Sound.play('entity.bat.takeoff') - os.sleep(0.1) + os.sleep(0) else + launchCounter = 0 os.sleep(0.4) end end @@ -42,14 +61,17 @@ end parallel.waitForAny( function() - print('press any key to exit') + print('\nFlight control initialized') + print('\nSneak and look straight up for launch') + print('\nPress any key to exit') os.pullEvent('char') end, function() while true do - print('Starting') - local s, m = pcall(run) - print(m) + local _, m = pcall(run) + if m then + print(m) + end print('Waiting for 5 seconds before restarting') os.sleep(5) end diff --git a/neural/etc/apps.db b/neural/etc/apps.db index 5a77795..679261d 100644 --- a/neural/etc/apps.db +++ b/neural/etc/apps.db @@ -9,10 +9,10 @@ category = "Neural", run = "neuralFight.lua", }, - [ "neuralFly" ] = { - title = "Fly", + [ "elytraFly" ] = { + title = "ElytraFly", category = "Neural", - run = "neuralFly.lua", + run = "elytraFly.lua", }, [ "neuralRemote" ] = { title = "Remote", diff --git a/turtle/apis/swarm.lua b/turtle/apis/swarm.lua index cee1976..57475fa 100644 --- a/turtle/apis/swarm.lua +++ b/turtle/apis/swarm.lua @@ -1,3 +1,4 @@ +local class = require('class') local Event = require('event') local Socket = require('socket') local Util = require('util') @@ -26,17 +27,19 @@ local function hijackTurtle(remoteId) return hijack, socket end -local class = require('class') + local Swarm = class() function Swarm:init(args) self.pool = { } Util.merge(self, args) end + function Swarm:add(id, args) local member = Util.shallowCopy(args) member.id = id self.pool[id] = member end + function Swarm:run(fn) for id, member in pairs(self.pool) do Event.addRoutine(function() @@ -54,6 +57,7 @@ function Swarm:run(fn) end) end end + function Swarm:shutdown() for _, member in pairs(self.pool) do if member.socket then @@ -62,6 +66,8 @@ function Swarm:shutdown() end end end + +-- Override function Swarm:onRemove(member, success, msg) print('removed from pool: ' .. member.id) if not success then