From 7549c4e620115fd29c10c1f514b0cf4f9897c8ab Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Tue, 30 Apr 2019 12:20:57 -0400 Subject: [PATCH] neural refactor --- neural/apis/interface.lua | 157 +++++++++++++++++++------------------- neural/mobRancher.lua | 67 +++++++++------- 2 files changed, 118 insertions(+), 106 deletions(-) diff --git a/neural/apis/interface.lua b/neural/apis/interface.lua index 3054651..98865ad 100644 --- a/neural/apis/interface.lua +++ b/neural/apis/interface.lua @@ -1,17 +1,34 @@ -local Interface = { } - local Angle = require('neural.angle') local Util = require('util') local os = _G.os local peripheral = _G.peripheral -local ni = peripheral.find('neuralInterface') or { } -for k,v in pairs(ni) do - Interface[k] = v +local Neural = { } + +function Neural.assertModules(modules) + local all = { + [ 'plethora:glasses' ] = 'Overlay glasses', + [ 'plethora:sensor' ] = 'Entity sensor', + [ 'plethora:scanner' ] = 'Block scanner', + [ 'plethora:introspection' ] = 'Introspection module', + [ 'plethora:kinetic' ] = 'Kinetic augment', + [ 'plethora:laser' ] = 'Laser', + } + + for _, m in pairs(modules) do + if not Neural.hasModule(m) then + print('Required:') + for _, v in pairs(modules) do + print(' * ' .. (modules[v] or v)) + end + print('') + error('Missing: ' .. (all[m] or m)) + end + end end -function Interface.yap(spt, dpt) +function Neural.yap(spt, dpt) local x, y, z = dpt.x - spt.x, dpt.y - spt.y, dpt.z - spt.z local pitch = -math.atan2(y, math.sqrt(x * x + z * z)) local yaw = math.atan2(-x, z) @@ -19,7 +36,7 @@ function Interface.yap(spt, dpt) return math.deg(yaw), math.deg(pitch) end -function Interface.launchTo(pt, strength) +function Neural.launchTo(pt, strength) local yaw = math.deg(math.atan2(pt.x, -pt.z)) if not strength then local dist = math.sqrt( @@ -28,29 +45,34 @@ function Interface.launchTo(pt, strength) strength = math.sqrt(math.max(32, dist) / 3) debug(strength) end - Interface.launch(yaw, 225, strength or 1) + Neural.launch(yaw, 225, strength or 1) end -function Interface.dropArmor() +function Neural.dropArmor() for i = 3, 5 do - Interface.unequip(i) + Neural.unequip(i) end end -function Interface.walkTo(pt) - local s, m = ni.walk(pt.x, pt.y, pt.z) - if not s then - _G.printError(m) - end - os.sleep(.05) - while ni.isWalking() do - os.sleep(0) - end +function Neural.walkTo(pt, speed) + Neural.walk(pt.x, pt.y, pt.z, speed) + os.sleep(1) + repeat until not Neural.isWalking() +end + +function Neural.walkAgainst(pt, radius, speed) + local angle = math.atan2(pt.x, pt.z) + local x = pt.x - ((radius or 1) * math.sin(angle)) + local z = pt.z - ((radius or 1) * math.cos(angle)) + + Neural.walk(x, 0, z, speed) + os.sleep(1) + repeat until not Neural.isWalking() end -- flatten equipment functions -function Interface.getEquipmentList() - local l = Interface.getEquipment and Interface.getEquipment().list() or { } +function Neural.getEquipmentList() + local l = Neural.getEquipment and Neural.getEquipment().list() or { } for k, v in pairs(l) do v.slot = k @@ -59,81 +81,56 @@ function Interface.getEquipmentList() return l end -function Interface.equip(slot) - return Interface.getEquipment and Interface.getEquipment().suck(slot) or 0 +function Neural.equip(slot) + return Neural.getEquipment and Neural.getEquipment().suck(slot) or 0 end -function Interface.unequip(slot) - return Interface.getEquipment and Interface.getEquipment().drop(slot) +function Neural.unequip(slot) + return Neural.getEquipment and Neural.getEquipment().drop(slot) end -function Interface.getUniqueNames() +function Neural.getUniqueNames() local t = { } - for _,v in pairs(Interface.sense()) do + for _,v in pairs(Neural.sense()) do t[v.name] = v.name end return Util.transpose(t) end -function Interface.lookAt(pt) - local yaw, pitch = Angle.towards(pt.x - .5, pt.y, pt.z - .5) - return Interface.look(yaw, pitch) +function Neural.lookAt(pt) + if pt then + local yaw, pitch = Angle.towards(pt.x, pt.y, pt.z) + return Neural.look(yaw, pitch) + end end -function Interface.shootAt(entity, strength) - Interface.lookAt(entity) - return Interface.shoot(strength or 1) +function Neural.fireAt(pt, strength) + local yaw, pitch = Angle.towards(pt.x, pt.y, pt.z) + return Neural.fire(yaw, pitch, strength or .5) end -function Interface.shootAt2(entity, strength) - local x, z = entity.x - .5, entity.z - .5 - - local function quad(a, b, c) - if math.abs(a) < 1e-6 then - if math.abs(b) < 1e-6 then - return math.abs(c) < 1e-6 and 0, 0 - else - return -c/b, -c/b - end - else - local disc = b*b - 4*a*c - if disc >= 0 then - disc = math.sqrt(disc) - a = 2*a - return (-b-disc)/a, (-b+disc)/a - end - end +function Neural.shootAt(pt, strength) + if Neural.fire then + return Neural.fireAt(pt, strength) + else + Neural.lookAt(pt) + return Neural.shoot(strength or 1) end - - local v = .025 -- velocity of arrow - - local tvx = entity.motionX - local tvz = entity.motionZ - local a = tvx*tvx + tvz*tvz - v*v - local b = 2 * (tvx * x + tvz * z) - local c = x * x + z * z - local t0, t1 = quad(a, b, c) - if t0 then - local t = math.min(t0, t1) - if t < 0 then - t = math.max(t0, t1) - end - if t > 0 then - --Util.print({ x, t, tvx, x + tvx * t }) - x = x + tvx * t - z = z + tvz * t - end - end - - local yaw = math.deg(math.atan2(-(x - .5), z - .5)) - local pitch = -math.deg(math.atan2(entity.y, math.sqrt(x * x + z * z))) - - Interface.look(yaw, pitch) -- pitch is broken - return Interface.shoot(strength or 1) end -function Interface.setStatus(s) - ni.status = s +function Neural.setStatus(s) + Neural.status = s end -return Interface +function Neural.reload() + return setmetatable(Neural, { + __index = peripheral.find('neuralInterface') + }) +end + +function Neural.testWalk() + local e = Neural.getMetaByName('kepler155c') + Neural.walkAgainst(e) +end + +return Neural.reload() diff --git a/neural/mobRancher.lua b/neural/mobRancher.lua index 91fb25b..177d98a 100644 --- a/neural/mobRancher.lua +++ b/neural/mobRancher.lua @@ -1,61 +1,76 @@ -local Kinetic = require('neural.kinetic') +local neural = require('neural.interface') local Sound = require('sound') local Util = require('util') -local device = _G.device local os = _G.os -local sensor = device['plethora:sensor'] -local scanner = device['plethora:scanner'] +local WALK_SPEED = 1.5 +local MAX_COWS = 12 + +neural.assertModules({ + 'plethora:sensor', + 'plethora:scanner', + 'plethora:laser', + 'plethora:kinetic', + 'plethora:introspection', +}) local fed = { } local function resupply() - local slot = sensor.getEquipment().list()[1] + local slot = neural.getEquipment().list()[1] if slot and slot.count > 32 then return end print('resupplying') - for _ = 1, 8 do - local dispenser = Util.find(scanner.scan(), 'name', 'minecraft:dispenser') - if dispenser and math.abs(dispenser.x) <= 1 and math.abs(dispenser.z) <= 1 then - Kinetic.lookAt(dispenser) - Kinetic.use(0, 'off') + for _ = 1, 2 do + local dispenser = Util.find(neural.scan(), 'name', 'minecraft:dispenser') + if not dispenser then + print('dispenser not found') + break + end + if math.abs(dispenser.x) <= 1 and math.abs(dispenser.z) <= 1 then + neural.lookAt(dispenser) + for _ = 1, 8 do + neural.use(0, 'off') os.sleep(.2) - Kinetic.getEquipment().suck(1, 64) - elseif dispenser then - Kinetic.walkTo(dispenser) + neural.getEquipment().suck(1, 64) + end + break + else + neural.walkTo({ x = dispenser.x, y = 0, z = dispenser.z }, WALK_SPEED) end end end -local function feed(entity) - print('feeding') +local function breed(entity) + print('breeding') entity.lastFed = os.clock() fed[entity.id] = entity - Kinetic.walkAgainst(entity) - entity = sensor.getMetaByID(entity.id) + neural.walkAgainst(entity, 1, WALK_SPEED) + entity = neural.getMetaByID(entity.id) if entity then - Kinetic.lookAt(entity) - Kinetic.use(1) + neural.lookAt(entity) + neural.use(1) os.sleep(.1) end end local function kill(entity) print('killing') - Kinetic.walkAgainst(entity, 2) - entity = sensor.getMetaByID(entity.id) + neural.walkAgainst(entity, 2.5, WALK_SPEED) + entity = neural.getMetaByID(entity.id) if entity then - Kinetic.lookAt(entity) - Kinetic.fireAt({ x = entity.x, y = 0, z = entity.z }) + neural.lookAt(entity) + neural.fireAt({ x = entity.x, y = 0, z = entity.z }) Sound.play('entity.firework.launch') + os.sleep(.2) end end local function getEntities() - return Util.filter(sensor.sense(), function(entity) + return Util.filter(neural.sense(), function(entity) if entity.name == 'Cow' and entity.y > -.5 then return true end @@ -86,12 +101,12 @@ while true do local entities = getEntities() - if Util.size(entities) > 10 then + if Util.size(entities) > MAX_COWS then kill(randomEntity(entities)) else local entity = getHungry(entities) if entity then - feed(entity) + breed(entity) else os.sleep(5) end