package management

This commit is contained in:
kepler155c
2018-11-03 18:14:11 -04:00
parent aa66b1c663
commit 1f7ef4a483
124 changed files with 1274 additions and 9 deletions

9
neural/.package Normal file
View File

@@ -0,0 +1,9 @@
{
required = {
'opus-develop-1.8',
},
title = 'Programs for the neural interface',
repository = 'kepler155c/opus-apps/develop1.8/neural',
description = [[ ... ]],
licence = 'MIT',
}

View File

@@ -0,0 +1,11 @@
local Angle = { }
function Angle.towards(x, y, z)
return math.deg(math.atan2(-x, z)), 0
end
function Angle.away(x, y, z)
return math.deg(math.atan2(x, -z)), 0
end
return Angle

View File

@@ -0,0 +1,139 @@
local Interface = { }
local Angle = require('neural.angle')
local Util = require('util')
local device = _G.device
local os = _G.os
local ni = device.neuralInterface or { }
for k,v in pairs(ni) do
Interface[k] = v
end
local function yap(pt)
local x, y, z = pt.x, pt.y + 1, pt.z
local pitch = -math.atan2(y, math.atan2(-(x - .5), z - .5))
local yaw = math.deg(math.atan2(-(x - .5), z - .5))
return math.deg(yaw), math.deg(pitch)
end
function Interface.launchTo(pt, strength)
local yaw = math.deg(math.atan2(pt.x, -pt.z))
if not strength then
local dist = math.sqrt(
math.pow(pt.x, 2) +
math.pow(pt.z, 2))
strength = math.sqrt(math.max(32, dist) / 3)
debug(strength)
end
Interface.launch(yaw, 225, strength or 1)
end
function Interface.dropArmor()
for i = 3, 5 do
Interface.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
end
-- flatten equipment functions
function Interface.getEquipmentList()
local l = Interface.getEquipment and Interface.getEquipment().list() or { }
for k, v in pairs(l) do
v.slot = k
end
return l
end
function Interface.equip(slot)
return Interface.getEquipment and Interface.getEquipment().suck(slot) or 0
end
function Interface.unequip(slot)
return Interface.getEquipment and Interface.getEquipment().drop(slot)
end
function Interface.getUniqueNames()
local t = { }
for _,v in pairs(Interface.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 + 1, pt.z - .5)
return Interface.look(yaw, pitch)
end
function Interface.shootAt(entity, strength)
Interface.lookAt(entity)
return Interface.shoot(strength or 1)
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
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
end
return Interface

View File

@@ -0,0 +1,22 @@
local Mobs = { }
local hostiles = {
ancient_golem = true,
BabySkeleton = true,
BabyZombie = true,
Bat = true,
Creeper = true,
Husk = true,
Skeleton = true,
Slime = true,
Spider = true,
Witch = true,
Zombie = true,
ZombieVillager = true,
}
function Mobs.getNames()
return hostiles
end
return Mobs

View File

@@ -0,0 +1,24 @@
_G.requireInjector(_ENV)
local GPS = require('gps')
local kernel = _G.kernel
kernel.onDeviceAttach('neuralInterface', function(dev)
dev.goTo = function(x, _, z)
local pt = GPS.locate(2)
if pt then
return pcall(function()
local gpt = {
x = x - pt.x,
y = 0,
z = z - pt.z,
}
gpt.x = math.min(math.max(gpt.x, -15), 15)
gpt.z = math.min(math.max(gpt.z, -15), 15)
return dev.walk(gpt.x, gpt.y, gpt.z)
end)
end
return false, 'No GPS'
end
end)

View File

@@ -0,0 +1,22 @@
{
[ "shootingGallery" ] = {
title = "Gallery",
category = "Neural",
run = "shootingGallery.lua",
},
[ "neuralFight" ] = {
title = "Fight",
category = "Neural",
run = "neuralFight.lua",
},
[ "neuralFly" ] = {
title = "Fly",
category = "Neural",
run = "neuralFly.lua",
},
[ "neuralRemote" ] = {
title = "Remote",
category = "Neural",
run = "neuralRemote.lua",
},
}

49
neural/mobfollow.lua Normal file
View File

@@ -0,0 +1,49 @@
_G.requireInjector(_ENV)
local GPS = require('gps')
local Util = require('util')
local Peripheral = require('peripheral')
local Point = require('point')
local os = _G.os
local args = { ... }
local remoteId = args[1] or error('mobFollow <remote id>')
local ni = Peripheral.lookup(remoteId .. '://name/neuralInterface')
if not ni then
error('failed to connect')
end
local lpt = nil
while true do
local pt = GPS.locate(2)
if not pt then
print('No GPS')
else
local gpt = Util.shallowCopy(pt)
if pt and lpt and Point.same(pt, lpt) then
-- havent moved
print('no move')
else
if not lpt then
gpt.x = gpt.x - 2
else
local dx = lpt.x - pt.x
local dz = lpt.z - pt.z
local angle = math.atan2(dx, dz)
gpt.x = pt.x + 2.5 * math.sin(angle)
gpt.z = pt.z + 2.5 * math.cos(angle)
end
lpt = pt
local s, m = ni.goTo(gpt.x, gpt.y + 1, gpt.z)
if not s then
print(m)
end
end
end
os.sleep(.5)
end

98
neural/neuralFight.lua Normal file
View File

@@ -0,0 +1,98 @@
_G.requireInjector()
local Angle = require('neural.angle')
local GPS = require('gps')
local Mobs = require('neural.mobs')
local ni = require('neural.interface')
local Point = require('point')
local Util = require('util')
local os = _G.os
local RADIUS = 13
local ROTATION = math.pi / 16
local uid = ni.getID and ni.getID() or error('Introspection module is required')
local pos = { x = 0, y = 0, z = 0 }
local function findTargets()
local l = ni.sense()
table.sort(l, function(e1, e2)
return Point.distance(e1, pos) < Point.distance(e2, pos)
end)
local targets = { }
for _,v in ipairs(l) do
if v.id ~= uid and Mobs.getNames()[v.name] then
if math.abs(v.y) < 2 and Point.distance(v, pos) < 16 then -- pitch is broken
table.insert(targets, v)
end
end
end
return #targets > 0 and targets
end
local function shootAt(targets)
for _,target in ipairs(targets) do
target = ni.getMetaByID(target.id)
if target and target.isAlive and Point.distance(target, pos) < 14 then
ni.shootAt(target)
end
end
end
local potions = Util.filter(
ni.getEquipmentList(),
function(a)
return a.name == 'minecraft:splash_potion'
end)
local function heal(target)
local hands = { 'main', 'off' }
if #potions > 0 and ni.getMetaOwner().health < 10 then
local yaw, pitch = Angle.away(target.x - .5, 0, target.z - .5)
ni.look(yaw, pitch)
ni.use(.01, hands[potions[1].slot])
ni.launch(yaw, pitch, 1)
table.remove(potions, 1)
end
end
local pt = GPS.locate()
while true do
local targets = findTargets()
if not targets then
local cpt = GPS.locate()
if Point.distance(pt, cpt) > 2 then
print('walking to starting point')
local s, m = ni.goTo(pt.x, pt.y, pt.z)
Util.print({ s, m })
os.sleep(.05)
while ni.isWalking() do
os.sleep(0)
end
Util.print('done walking')
end
os.sleep(1)
else
local target = targets[1]
local angle = math.atan2(-target.x, -target.z) + ROTATION
ni.launchTo({
x = target.x + RADIUS * math.sin(angle),
y = 0,
z = target.z + RADIUS * math.cos(angle)
}, 1)
os.sleep(.2)
shootAt(targets)
heal(target)
if math.random(1, 3) == 3 then
ROTATION = -ROTATION
end
end
end

78
neural/neuralFly.lua Normal file
View File

@@ -0,0 +1,78 @@
_G.requireInjector(_ENV)
local ni = require('neural.interface')
local GPS = require('gps')
local strength = .315
local delay = .1
while ni.getMetaOwner().health < 26 do
print('health: ' .. ni.getMetaOwner().health)
os.sleep(1)
end
ni.launch(0, 270, 1.5)
os.sleep(.25)
local pt
local function fly()
for i = 1, 100 do
os.sleep(1)
if pt then
print(pt.y)
print(strength)
end
end
end
local function gps()
local lastY = 12
while true do
pt = GPS.locate()
if pt then
local d = math.abs(lastY - pt.y)
-- force required to get to lvl 12
local motionY = ni.getMetaOwner().motionY
-- print('y: ' .. pt.y)
if pt.y < 12 then
if pt.y > lastY then
--strength = strength + .001
else
strength = strength + .02 * d
end
elseif pt.y > 12 then
if pt.y > lastY then
strength = strength - .02 * d
else
--strength = strength - .001
end
end
lastY = pt.y
-- force required to offset motion
local om = (motionY - 0.138) / 0.8
ni.launch(0, 270, strength-motionY)
-- print('strength: ' .. strength)
os.sleep(delay)
end
end
end
parallel.waitForAny(fly, gps)
repeat
ni.launch(0, 270, .25)
os.sleep(.1)
until not ni.getMetaOwner().isAirborne
print('descending')
for i = 1, 50 do
ni.launch(0, 270, .2)
os.sleep(.1)
end
ni.look(180, 0)

88
neural/neuralRecorder.lua Normal file
View File

@@ -0,0 +1,88 @@
_G.requireInjector(_ENV)
local GPS = require('gps')
local Point = require('point')
local Util = require('util')
local os = _G.os
local parallel = _G.parallel
local t = { }
local ni = _G.device.neuralInterface or error('Neural Interface not found')
if not ni.getID then
error('Missing Introspection Module')
end
local uid = ni.getID()
local c = os.clock()
local pt = GPS.locate(3) or error('GPS failed')
local lpt
local me = Util.find(ni.sense(), 'id', uid)
local function gps()
while true do
me = Util.find(ni.sense(), 'id', uid)
pt = GPS.locate(3) or error('GPS failed')
os.sleep(.3)
print('got gps')
end
end
local function record()
local timerId = os.startTimer(.1)
repeat
local event, ch = os.pullEvent()
local v
local delay = os.clock() - c
c = os.clock()
--print(event .. ' ' .. tostring(ch))
if event == 'char' then
print('char ' .. ch)
if ch == ' ' then
v = {
action = 'walk',
x = pt.x,
y = pt.y,
z = pt.z,
pitch = me.pitch,
yaw = me.yaw,
delay = delay,
}
elseif ch == 'u' then
v = {
action = 'use',
x = pt.x,
y = pt.y,
z = pt.z,
pitch = me.pitch,
yaw = me.yaw,
delay = delay,
}
end
elseif event == 'timer' and ch == timerId then
if not lpt or not Point.same(pt, lpt) then
v = {
action = 'walk',
x = pt.x,
y = pt.y,
z = pt.z,
pitch = me.pitch,
yaw = me.yaw,
delay = delay,
}
lpt = pt
end
timerId = os.startTimer(.2)
end
if v then
Util.print(v)
table.insert(t, v)
end
until event == 'char' and ch == 'q'
end
parallel.waitForAny(gps, record)
Util.writeTable('neural.tbl', t)

56
neural/neuralRemote.lua Normal file
View File

@@ -0,0 +1,56 @@
rednet.open("right")
local sensor = peripheral.wrap("back")
local modules = peripheral.wrap("back")
local Ka = peripheral.find("neuralInterface")
local function fire(entity)
local x, y, z = entity.x, entity.y, entity.z
local pitch = -math.atan2(y, math.sqrt(x * x + z * z))
local yaw = math.atan2(-x, z)
Ka.look(math.deg(yaw), math.deg(pitch), 5)
Ka.shoot(1)
sleep(0.2)
end
local mobNames = {"Skeleton"}
local mobLookup = {}
for i = 1, #mobNames do
mobLookup[mobNames[i]] = true
end
function SkeletonShoot()
local mobs = sensor.sense()
local candidates = {}
for i = 1, #mobs do
local mob = mobs[i]
if mobLookup[mob.name] then
candidates[#candidates + 1] = mob
end
end
if #candidates > 0 then
local mob = candidates[math.random(1, #candidates)]
fire(mob)
else
sleep(.1)
end
end
while true do
local id,message = rednet.receive()
print(tostring(id)..message)
if id == 582 then
if message == "forward" then --W
Ka.walk(1,0,0)
elseif message == "back" then --S
Ka.walk(-1,0,0)
elseif message == "turnLeft" then--A
Ka.walk(0,0,-1)
elseif message == "turnRight" then--D
Ka.walk(0,0,1)
elseif message == "shoot" then--Starts fell program
SkeletonShoot()
end
else
write(" Denied!")
end
end

52
neural/neuralReplay.lua Normal file
View File

@@ -0,0 +1,52 @@
_G.requireInjector(_ENV)
local GPS = require('gps')
local Util = require('util')
local os = _G.os
local shell = _ENV.shell
local args = { ... }
local fileName = args[1] or 'neural.tbl'
local t = Util.readTable(shell.resolve(fileName)) or error('Unable to read ' .. fileName)
local ni = _G.device.neuralInterface
local function walkTo(x, y, z)
local pt = GPS.locate(2)
if pt then
local s, m, m2 = pcall(function()
local gpt = {
x = x - pt.x,
y = math.floor(y) - math.floor(pt.y),
z = z - pt.z,
}
gpt.x = math.min(math.max(gpt.x, -30), 30)
gpt.z = math.min(math.max(gpt.z, -30), 30)
return ni.walk(gpt.x, gpt.y, gpt.z)
end)
if not s or not m then
_G.printError(m2 or m)
end
end
os.sleep(.5)
while ni.isWalking() do
os.sleep(0)
end
end
for _,v in pairs(t) do
Util.print(v)
--if v.action == 'walk' then
walkTo(v.x, v.y, v.z)
--end
ni.look(v.yaw, v.pitch)
if v.action == 'use' then
ni.use()
os.sleep(2)
end
-- os.sleep(v.delay)
-- os.sleep(2)
-- read()
end

28
neural/neuralSword.lua Normal file
View File

@@ -0,0 +1,28 @@
_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')
if target then
if math.abs(target.x) < 2 and
math.abs(target.z) < 2 then
ni.lookAt(target)
ni.swing()
os.sleep(.5)
else
local angle = math.atan2(-(target.x - .5), target.z - .5)
ni.walkTo({
x = target.x + 1.5 * math.sin(angle),
y = 0,
z = target.z - 1.5 * math.cos(angle)
}, 1)
end
else
print('no target')
os.sleep(1)
end
end

83
neural/robotWars.lua Normal file
View File

@@ -0,0 +1,83 @@
_G.requireInjector()
local Angle = require('neural.angle')
local ni = require('neural.interface')
local Util = require('util')
local os = _G.os
local RADIUS = 13
local ROTATION = math.pi / 16
local args = { ... }
local TARGET = args[1] or error('Syntax: robotWars <targetName>')
local uid = ni.getID and ni.getID() or error('Introspection module is required')
local function findTarget(name)
for _, v in pairs(ni.sense()) do
if v.name == name and v.id ~= uid then
return v
end
end
end
local function shootAt(entity)
local target = ni.getMetaByID(entity.id)
if target then
ni.shootAt(target)
end
end
local enemy = findTarget(TARGET)
local potions = Util.filter(
ni.getEquipmentList(),
function(a)
return a.name == 'minecraft:splash_potion'
end)
if not enemy then
print('Current enemies:')
for _,v in pairs(ni.getUniqueNames()) do
print(v)
end
print()
error('Invalid enemy')
end
local function heal(target)
local hands = { 'main', 'off' }
if #potions > 0 and ni.getMetaOwner().health < 10 then
local yaw, pitch = Angle.away({ x = target.x, y = 0, z = target.z })
ni.look(yaw, pitch)
ni.use(.01, hands[potions[1].slot])
ni.launch(yaw, pitch, 1)
table.remove(potions, 1)
end
end
repeat
local target = ni.getMetaByID(enemy.id)
if not target then
print('lost target')
break
end
local angle = math.atan2(-target.x, -target.z) + ROTATION
ni.launchTo({
x = target.x + RADIUS * math.sin(angle),
y = 0,
z = target.z + RADIUS * math.cos(angle)
}, 1)
os.sleep(.2)
shootAt(enemy)
heal(enemy)
if math.random(1, 3) == 3 then
ROTATION = -ROTATION
end
until not target.isAlive
print('Won !')

31
neural/shootMob.lua Normal file
View File

@@ -0,0 +1,31 @@
_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

View File

@@ -0,0 +1,48 @@
_G.requireInjector(_ENV)
local Mobs = require('neural.mobs')
local ni = require('neural.interface')
local Point = require('point')
local Util = require('util')
local os = _G.os
if not ni.look then
error('neuralInterface required')
end
local uid = ni.getID and ni.getID() or error('Introspection module is required')
local function findTargets()
local pos = { x = 0, y = 0, z = 0 }
local l = ni.sense()
table.sort(l, function(e1, e2)
return Point.distance(e1, pos) < Point.distance(e2, pos)
end)
local targets = { }
for _,v in ipairs(l) do
if v.id ~= uid and Mobs.getNames()[v.name] then
if math.abs(v.y) < 2 then -- pitch is broken
table.insert(targets, v)
end
end
end
return #targets > 0 and targets
end
print('Targets:')
for _,v in pairs(ni.sense()) do
print(v.name)
end
while true do
local targets = findTargets()
if targets then
for _, entity in ipairs(targets) do
Util.print(entity)
ni.shootAt(entity, 1)
end
end
os.sleep(.5)
end