error handling
This commit is contained in:
@@ -1,46 +0,0 @@
|
|||||||
_G.requireInjector(_ENV)
|
|
||||||
|
|
||||||
local GPS = require('gps')
|
|
||||||
local Util = require('util')
|
|
||||||
local Peripheral = require('peripheral')
|
|
||||||
local Point = require('point')
|
|
||||||
|
|
||||||
local p = Peripheral.lookup('594://name/neuralInterface')
|
|
||||||
|
|
||||||
_G._p = p
|
|
||||||
if not p 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 = p.goTo(gpt.x, gpt.y + 1, gpt.z)
|
|
||||||
if not s then
|
|
||||||
print(m)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
os.sleep(.5)
|
|
||||||
end
|
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
local os = _G.os
|
|
||||||
local peripheral = _G.peripheral
|
|
||||||
|
|
||||||
local ni = peripheral.find("neuralInterface")
|
|
||||||
if not ni then
|
|
||||||
error("Cannot find neuralInterface")
|
|
||||||
end
|
|
||||||
|
|
||||||
local RADIUS = 6
|
|
||||||
local ROTATION = math.pi / 16
|
|
||||||
local args = { ... }
|
|
||||||
|
|
||||||
local TARGET = args[1] or error('Syntax: robotWars <targetName>')
|
|
||||||
|
|
||||||
local function yap(entity)
|
|
||||||
local x, y, z = entity.x, entity.y + 1, entity.z
|
|
||||||
local pitch = -math.atan2(y, math.sqrt(x * x + z * z))
|
|
||||||
local yaw = math.atan2(-(x - .5), z - .5)
|
|
||||||
|
|
||||||
return math.deg(yaw), math.deg(pitch)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function getUid()
|
|
||||||
for _,v in pairs(ni.sense()) do
|
|
||||||
if math.floor(v.x) == 0 and
|
|
||||||
math.floor(v.z) == 0 then
|
|
||||||
return v.id
|
|
||||||
end
|
|
||||||
end
|
|
||||||
error('Could not find myself')
|
|
||||||
end
|
|
||||||
|
|
||||||
local uid = getUid()
|
|
||||||
|
|
||||||
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(name)
|
|
||||||
local target = findTarget(name)
|
|
||||||
if not target then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local yaw, pitch = yap(target)
|
|
||||||
debug('look: ' .. yaw)
|
|
||||||
ni.look(yaw, pitch)
|
|
||||||
debug('shoot')
|
|
||||||
pcall(ni.shoot, 1)
|
|
||||||
end
|
|
||||||
|
|
||||||
while true do
|
|
||||||
local target = findTarget(TARGET)
|
|
||||||
if not target then
|
|
||||||
print('Won??')
|
|
||||||
break
|
|
||||||
end
|
|
||||||
local angle = math.atan2(-target.x, -target.z) + ROTATION
|
|
||||||
debug('walk: ' .. angle)
|
|
||||||
|
|
||||||
ni.walk(
|
|
||||||
target.x + RADIUS * math.sin(angle),
|
|
||||||
0,
|
|
||||||
target.z + RADIUS * math.cos(angle))
|
|
||||||
os.sleep(.2)
|
|
||||||
repeat
|
|
||||||
os.sleep(0)
|
|
||||||
until not ni.isWalking()
|
|
||||||
|
|
||||||
shootAt(TARGET)
|
|
||||||
end
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
_G.requireInjector(_ENV)
|
|
||||||
|
|
||||||
local GPS = require('gps')
|
|
||||||
|
|
||||||
local device = _G.device
|
|
||||||
|
|
||||||
if device.neuralInterface then
|
|
||||||
function device.neuralInterface.goTo(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 device.neuralInterface.walk(gpt.x, gpt.y, gpt.z)
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
return false, 'No GPS'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
Reference in New Issue
Block a user