From b1c709795fba43977fa285654bc72e248e35da95 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Tue, 19 Mar 2019 01:41:32 -0400 Subject: [PATCH] neural cleanup --- neural/Sensor.lua | 53 ++++++++++++++++++----------------------- neural/apis/kinetic.lua | 16 +++++++++++++ neural/neuralLook.lua | 45 ++++++++++++++++------------------ neural/neuralSword.lua | 4 +--- neural/shootMob.lua | 31 ------------------------ 5 files changed, 61 insertions(+), 88 deletions(-) create mode 100644 neural/apis/kinetic.lua delete mode 100644 neural/shootMob.lua diff --git a/neural/Sensor.lua b/neural/Sensor.lua index 0ba3f97..f2d0db5 100644 --- a/neural/Sensor.lua +++ b/neural/Sensor.lua @@ -4,27 +4,18 @@ local Project = require('neural.project') local UI = require('ui') local Util = require('util') -local peripheral = _G.peripheral -local turtle = _G.turtle +local device = _G.device -local function equip(side, rawName) - return turtle and turtle.equip(side, rawName) and peripheral.wrap(side) -end - -local target = nil -local ni = peripheral.find('neuralInterface') -local sensor = ni or - peripheral.find('plethora:sensor') or - peripheral.find('manipulator') or - equip('left', 'plethora:module:3') - -if not sensor or not sensor.sense then +local glasses = device['plethora:glasses'] +local intro = device['plethora:introspection'] +local sensor = device['plethora:sensor'] or error('Plethora sensor must be equipped') -end -UI:configure('Entities', ...) +UI:configure('Sensor', ...) -local config = Config.load('Entities', { +local target + +local config = Config.load('Sensor', { ignore = { } }) if not config.ignore then @@ -127,7 +118,7 @@ function page:eventHandler(event) elseif event.type == 'totals' then config.totals = not config.totals - Config.update('Entities', config) + Config.update('Sensor', config) elseif event.type == 'detail' or event.type == 'grid_select' then local selected = self.grid:getSelected() @@ -144,20 +135,22 @@ function page:eventHandler(event) if selected then config.ignore[selected.name] = true end - Config.update('Entities', config) + Config.update('Sensor', config) elseif event.type == 'project' or event.type == 'project-target' then if event.type == 'project' then target = nil end - config.projecting = not config.projecting - if config.projecting then - Project:init(ni.canvas()) - else - Project.canvas:clear() + if glasses then + config.projecting = not config.projecting + if config.projecting then + Project:init(glasses.canvas()) + else + Project.canvas:clear() + end end - Config.update('Entities', config) + Config.update('Sensor', config) end UI.Page.eventHandler(self, event) @@ -167,8 +160,8 @@ Event.onInterval(.5, function() local entities = sensor.sense() Util.filterInplace(entities, function(e) return not config.ignore[e.name] end) - if config.projecting then - local meta = ni.getMetaOwner() + if config.projecting and glasses and intro then + local meta = intro.getMetaOwner() Project.canvas:clear() local t = entities if target then @@ -194,13 +187,13 @@ Event.onInterval(.5, function() page:sync() end) -if config.projecting then - Project:init(ni.canvas()) +if config.projecting and glasses then + Project:init(glasses.canvas()) end UI:setPage(page) UI:pullEvents() -if config.projecting then +if config.projecting and glasses then Project.canvas:clear() end diff --git a/neural/apis/kinetic.lua b/neural/apis/kinetic.lua new file mode 100644 index 0000000..a3a90eb --- /dev/null +++ b/neural/apis/kinetic.lua @@ -0,0 +1,16 @@ +local Angle = require('neural.angle') +local Util = require('util') + +local device = _G.device + +local module = device['plethora:kinetic'] or error('Missing kinetic') + +local Kinetic = Util.shallowCopy(module) + +function Kinetic.lookAt(pt) + --local x = pt.x < 0 and pt.x + .5 or pt.x - .5 + local yaw, pitch = Angle.towards(pt.x, pt.y, pt.z) + return Kinetic.look(yaw, pitch) +end + +return Kinetic diff --git a/neural/neuralLook.lua b/neural/neuralLook.lua index 051de04..22d674e 100644 --- a/neural/neuralLook.lua +++ b/neural/neuralLook.lua @@ -1,17 +1,19 @@ -local ni = require('neural.interface') -local Point = require('point') -local Util = require('util') +local Array = require('array') +local kinetic = require('plethora.kinetic') +local Point = require('point') -local os = _G.os +local device = _G.device +local os = _G.os local pos = { x = 0, y = 0, z = 0 } -local meta = ni.getMetaOwner() +local sensor = device['plethora:sensor'] or error('Missing sensor') +local intro = device['plethora:introspection'] or error('Missing introspection module') + +local ID = intro.getMetaOwner() local function findTargets() - local l = ni.sense() - - Util.filterInplace(l, function(a) - return math.abs(a.motionY) > 0 and meta.id ~= a.id + local l = Array.filter(sensor.sense(), function(a) + return math.abs(a.motionY) > 0 and ID ~= a.id end) table.sort(l, function(e1, e2) return Point.distance(e1, pos) < Point.distance(e2, pos) @@ -26,24 +28,19 @@ local count = 0 while true do local target = findTargets() if target and (not last or Point.distance(last, target) > .2) then --- last = target -if last then print(Point.distance(last, target)) end -last = target ---print(target.x, target.y, target.z, count) - ni.lookAt(target) - count = 0 - os.sleep(0) --- elseif count < 10 then --- count = count + 1 --- os.sleep(.1) --- end + last = target + kinetic.lookAt(target) + count = 0 + os.sleep(0) else count = count + 1 if count > 50 or not target then - ni.lookAt({ x = math.random(-10, 10), - y = math.random(-10, 10), - z = math.random(-10, 10) }) - os.sleep(3) + kinetic.lookAt({ + x = math.random(-10, 10), + y = math.random(-10, 10), + z = math.random(-10, 10) + }) + os.sleep(3) else os.sleep(.1) end diff --git a/neural/neuralSword.lua b/neural/neuralSword.lua index c269c62..6c0e202 100644 --- a/neural/neuralSword.lua +++ b/neural/neuralSword.lua @@ -1,12 +1,10 @@ -_G.requireInjector(_ENV) - local ni = require('neural.interface') local Util = require('util') local os = _G.os while true do - local target = Util.find(ni.sense(), 'name', 'joebodo') + local target = Util.find(ni.sense(), 'name', '///////') if target then if math.abs(target.x) < 2 and math.abs(target.z) < 2 then diff --git a/neural/shootMob.lua b/neural/shootMob.lua deleted file mode 100644 index 02b4815..0000000 --- a/neural/shootMob.lua +++ /dev/null @@ -1,31 +0,0 @@ -_G.requireInjector(_ENV) - -local ni = require('neural.interface') -local uid = ni.getID and ni.getID() or error('Introspection module is required') - -local os = _G.os - -local args = { ... } - -local function findEntity(name) - for _,v in pairs(ni.sense()) do - if v.id ~= uid and v.name == name then - return v - end - end -end - -print('Targets:') -for _,v in pairs(ni.sense()) do - print(v.name) -end - -local target = args[1] or error('specify target name') - -repeat - local entity = findEntity(target) - if entity then - ni.shootAt(entity, 1) - end - os.sleep(.5) -until not entity