neural refactor
This commit is contained in:
@@ -1,17 +1,34 @@
|
|||||||
local Interface = { }
|
|
||||||
|
|
||||||
local Angle = require('neural.angle')
|
local Angle = require('neural.angle')
|
||||||
local Util = require('util')
|
local Util = require('util')
|
||||||
|
|
||||||
local os = _G.os
|
local os = _G.os
|
||||||
local peripheral = _G.peripheral
|
local peripheral = _G.peripheral
|
||||||
|
|
||||||
local ni = peripheral.find('neuralInterface') or { }
|
local Neural = { }
|
||||||
for k,v in pairs(ni) do
|
|
||||||
Interface[k] = v
|
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
|
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 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 pitch = -math.atan2(y, math.sqrt(x * x + z * z))
|
||||||
local yaw = math.atan2(-x, z)
|
local yaw = math.atan2(-x, z)
|
||||||
@@ -19,7 +36,7 @@ function Interface.yap(spt, dpt)
|
|||||||
return math.deg(yaw), math.deg(pitch)
|
return math.deg(yaw), math.deg(pitch)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Interface.launchTo(pt, strength)
|
function Neural.launchTo(pt, strength)
|
||||||
local yaw = math.deg(math.atan2(pt.x, -pt.z))
|
local yaw = math.deg(math.atan2(pt.x, -pt.z))
|
||||||
if not strength then
|
if not strength then
|
||||||
local dist = math.sqrt(
|
local dist = math.sqrt(
|
||||||
@@ -28,29 +45,34 @@ function Interface.launchTo(pt, strength)
|
|||||||
strength = math.sqrt(math.max(32, dist) / 3)
|
strength = math.sqrt(math.max(32, dist) / 3)
|
||||||
debug(strength)
|
debug(strength)
|
||||||
end
|
end
|
||||||
Interface.launch(yaw, 225, strength or 1)
|
Neural.launch(yaw, 225, strength or 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Interface.dropArmor()
|
function Neural.dropArmor()
|
||||||
for i = 3, 5 do
|
for i = 3, 5 do
|
||||||
Interface.unequip(i)
|
Neural.unequip(i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Interface.walkTo(pt)
|
function Neural.walkTo(pt, speed)
|
||||||
local s, m = ni.walk(pt.x, pt.y, pt.z)
|
Neural.walk(pt.x, pt.y, pt.z, speed)
|
||||||
if not s then
|
os.sleep(1)
|
||||||
_G.printError(m)
|
repeat until not Neural.isWalking()
|
||||||
end
|
end
|
||||||
os.sleep(.05)
|
|
||||||
while ni.isWalking() do
|
function Neural.walkAgainst(pt, radius, speed)
|
||||||
os.sleep(0)
|
local angle = math.atan2(pt.x, pt.z)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
-- flatten equipment functions
|
-- flatten equipment functions
|
||||||
function Interface.getEquipmentList()
|
function Neural.getEquipmentList()
|
||||||
local l = Interface.getEquipment and Interface.getEquipment().list() or { }
|
local l = Neural.getEquipment and Neural.getEquipment().list() or { }
|
||||||
|
|
||||||
for k, v in pairs(l) do
|
for k, v in pairs(l) do
|
||||||
v.slot = k
|
v.slot = k
|
||||||
@@ -59,81 +81,56 @@ function Interface.getEquipmentList()
|
|||||||
return l
|
return l
|
||||||
end
|
end
|
||||||
|
|
||||||
function Interface.equip(slot)
|
function Neural.equip(slot)
|
||||||
return Interface.getEquipment and Interface.getEquipment().suck(slot) or 0
|
return Neural.getEquipment and Neural.getEquipment().suck(slot) or 0
|
||||||
end
|
end
|
||||||
|
|
||||||
function Interface.unequip(slot)
|
function Neural.unequip(slot)
|
||||||
return Interface.getEquipment and Interface.getEquipment().drop(slot)
|
return Neural.getEquipment and Neural.getEquipment().drop(slot)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Interface.getUniqueNames()
|
function Neural.getUniqueNames()
|
||||||
local t = { }
|
local t = { }
|
||||||
for _,v in pairs(Interface.sense()) do
|
for _,v in pairs(Neural.sense()) do
|
||||||
t[v.name] = v.name
|
t[v.name] = v.name
|
||||||
end
|
end
|
||||||
return Util.transpose(t)
|
return Util.transpose(t)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Interface.lookAt(pt)
|
function Neural.lookAt(pt)
|
||||||
local yaw, pitch = Angle.towards(pt.x - .5, pt.y, pt.z - .5)
|
if pt then
|
||||||
return Interface.look(yaw, pitch)
|
local yaw, pitch = Angle.towards(pt.x, pt.y, pt.z)
|
||||||
|
return Neural.look(yaw, pitch)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Interface.shootAt(entity, strength)
|
function Neural.fireAt(pt, strength)
|
||||||
Interface.lookAt(entity)
|
local yaw, pitch = Angle.towards(pt.x, pt.y, pt.z)
|
||||||
return Interface.shoot(strength or 1)
|
return Neural.fire(yaw, pitch, strength or .5)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Interface.shootAt2(entity, strength)
|
function Neural.shootAt(pt, strength)
|
||||||
local x, z = entity.x - .5, entity.z - .5
|
if Neural.fire then
|
||||||
|
return Neural.fireAt(pt, strength)
|
||||||
local function quad(a, b, c)
|
else
|
||||||
if math.abs(a) < 1e-6 then
|
Neural.lookAt(pt)
|
||||||
if math.abs(b) < 1e-6 then
|
return Neural.shoot(strength or 1)
|
||||||
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
|
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
function Interface.setStatus(s)
|
function Neural.setStatus(s)
|
||||||
ni.status = s
|
Neural.status = s
|
||||||
end
|
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()
|
||||||
|
|||||||
@@ -1,61 +1,76 @@
|
|||||||
local Kinetic = require('neural.kinetic')
|
local neural = require('neural.interface')
|
||||||
local Sound = require('sound')
|
local Sound = require('sound')
|
||||||
local Util = require('util')
|
local Util = require('util')
|
||||||
|
|
||||||
local device = _G.device
|
|
||||||
local os = _G.os
|
local os = _G.os
|
||||||
|
|
||||||
local sensor = device['plethora:sensor']
|
local WALK_SPEED = 1.5
|
||||||
local scanner = device['plethora:scanner']
|
local MAX_COWS = 12
|
||||||
|
|
||||||
|
neural.assertModules({
|
||||||
|
'plethora:sensor',
|
||||||
|
'plethora:scanner',
|
||||||
|
'plethora:laser',
|
||||||
|
'plethora:kinetic',
|
||||||
|
'plethora:introspection',
|
||||||
|
})
|
||||||
|
|
||||||
local fed = { }
|
local fed = { }
|
||||||
|
|
||||||
local function resupply()
|
local function resupply()
|
||||||
local slot = sensor.getEquipment().list()[1]
|
local slot = neural.getEquipment().list()[1]
|
||||||
if slot and slot.count > 32 then
|
if slot and slot.count > 32 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
print('resupplying')
|
print('resupplying')
|
||||||
for _ = 1, 8 do
|
for _ = 1, 2 do
|
||||||
local dispenser = Util.find(scanner.scan(), 'name', 'minecraft:dispenser')
|
local dispenser = Util.find(neural.scan(), 'name', 'minecraft:dispenser')
|
||||||
if dispenser and math.abs(dispenser.x) <= 1 and math.abs(dispenser.z) <= 1 then
|
if not dispenser then
|
||||||
Kinetic.lookAt(dispenser)
|
print('dispenser not found')
|
||||||
Kinetic.use(0, 'off')
|
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)
|
os.sleep(.2)
|
||||||
Kinetic.getEquipment().suck(1, 64)
|
neural.getEquipment().suck(1, 64)
|
||||||
elseif dispenser then
|
end
|
||||||
Kinetic.walkTo(dispenser)
|
break
|
||||||
|
else
|
||||||
|
neural.walkTo({ x = dispenser.x, y = 0, z = dispenser.z }, WALK_SPEED)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function feed(entity)
|
local function breed(entity)
|
||||||
print('feeding')
|
print('breeding')
|
||||||
entity.lastFed = os.clock()
|
entity.lastFed = os.clock()
|
||||||
fed[entity.id] = entity
|
fed[entity.id] = entity
|
||||||
|
|
||||||
Kinetic.walkAgainst(entity)
|
neural.walkAgainst(entity, 1, WALK_SPEED)
|
||||||
entity = sensor.getMetaByID(entity.id)
|
entity = neural.getMetaByID(entity.id)
|
||||||
if entity then
|
if entity then
|
||||||
Kinetic.lookAt(entity)
|
neural.lookAt(entity)
|
||||||
Kinetic.use(1)
|
neural.use(1)
|
||||||
os.sleep(.1)
|
os.sleep(.1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function kill(entity)
|
local function kill(entity)
|
||||||
print('killing')
|
print('killing')
|
||||||
Kinetic.walkAgainst(entity, 2)
|
neural.walkAgainst(entity, 2.5, WALK_SPEED)
|
||||||
entity = sensor.getMetaByID(entity.id)
|
entity = neural.getMetaByID(entity.id)
|
||||||
if entity then
|
if entity then
|
||||||
Kinetic.lookAt(entity)
|
neural.lookAt(entity)
|
||||||
Kinetic.fireAt({ x = entity.x, y = 0, z = entity.z })
|
neural.fireAt({ x = entity.x, y = 0, z = entity.z })
|
||||||
Sound.play('entity.firework.launch')
|
Sound.play('entity.firework.launch')
|
||||||
|
os.sleep(.2)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function getEntities()
|
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
|
if entity.name == 'Cow' and entity.y > -.5 then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@@ -86,12 +101,12 @@ while true do
|
|||||||
|
|
||||||
local entities = getEntities()
|
local entities = getEntities()
|
||||||
|
|
||||||
if Util.size(entities) > 10 then
|
if Util.size(entities) > MAX_COWS then
|
||||||
kill(randomEntity(entities))
|
kill(randomEntity(entities))
|
||||||
else
|
else
|
||||||
local entity = getHungry(entities)
|
local entity = getHungry(entities)
|
||||||
if entity then
|
if entity then
|
||||||
feed(entity)
|
breed(entity)
|
||||||
else
|
else
|
||||||
os.sleep(5)
|
os.sleep(5)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user