From a0a92818d6d9e4c3552f381ea2ea35b62db37582 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Wed, 13 Mar 2019 17:53:49 -0400 Subject: [PATCH] neural look --- milo/MiloLocal.lua | 4 ++++ neural/apis/angle.lua | 2 +- neural/apis/interface.lua | 2 +- neural/neuralLook.lua | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 neural/neuralLook.lua diff --git a/milo/MiloLocal.lua b/milo/MiloLocal.lua index 23b4e19..79f6b97 100644 --- a/milo/MiloLocal.lua +++ b/milo/MiloLocal.lua @@ -162,6 +162,10 @@ Event.on({ 'milo_cycle', 'milo_queue' }, function(e) context.taskTimer = context.taskTimer + taskTimer() context.taskCounter = context.taskCounter + 1 end + + if context.storage:isOnline() and #context.queue > 0 then + os.queueEvent('milo_cycle') + end end) Event.on('turtle_inventory', function() diff --git a/neural/apis/angle.lua b/neural/apis/angle.lua index f378a31..20fd424 100644 --- a/neural/apis/angle.lua +++ b/neural/apis/angle.lua @@ -1,7 +1,7 @@ local Angle = { } function Angle.towards(x, y, z) - return math.deg(math.atan2(-x, z)), 0 + return math.deg(math.atan2(-x, z)), math.deg(-math.atan2(y, math.sqrt(x * x + z * z))) end function Angle.away(x, y, z) diff --git a/neural/apis/interface.lua b/neural/apis/interface.lua index 5989696..3054651 100644 --- a/neural/apis/interface.lua +++ b/neural/apis/interface.lua @@ -76,7 +76,7 @@ function Interface.getUniqueNames() end function Interface.lookAt(pt) - local yaw, pitch = Angle.towards(pt.x - .5, pt.y + 1, pt.z - .5) + local yaw, pitch = Angle.towards(pt.x - .5, pt.y, pt.z - .5) return Interface.look(yaw, pitch) end diff --git a/neural/neuralLook.lua b/neural/neuralLook.lua new file mode 100644 index 0000000..d590b65 --- /dev/null +++ b/neural/neuralLook.lua @@ -0,0 +1,32 @@ +local ni = require('neural.interface') +local Point = require('point') +local Util = require('util') + +local os = _G.os + +local pos = { x = 0, y = 0, z = 0 } +local meta = ni.getMetaOwner() + +local function findTargets() + local l = ni.sense() + + Util.filterInplace(l, function(a) + return math.abs(a.motionY) > 0 and meta.id ~= a.id + end) + table.sort(l, function(e1, e2) + return Point.distance(e1, pos) < Point.distance(e2, pos) + end) + + return l[1] +end + +while true do + local target = findTargets() + if target then +print('looking at ' .. target.name) + ni.lookAt(target) + os.sleep(0) + else + os.sleep(3) + end +end