robot wars
This commit is contained in:
46
apps/neural.lua
Normal file
46
apps/neural.lua
Normal file
@@ -0,0 +1,46 @@
|
||||
_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
|
||||
35
apps/robotWars.lua
Normal file
35
apps/robotWars.lua
Normal file
@@ -0,0 +1,35 @@
|
||||
_G.requireInjector(_ENV)
|
||||
|
||||
local Event = require('event')
|
||||
local Util = require('util')
|
||||
|
||||
local os = _G.os
|
||||
local peripheral = _G.peripheral
|
||||
|
||||
local ni = peripheral.find("neuralInterface")
|
||||
if not ni then
|
||||
error("Cannot find neuralInterface")
|
||||
end
|
||||
|
||||
local TARGET = 'joebodo'
|
||||
|
||||
local function look(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)
|
||||
|
||||
ni.look(math.deg(yaw), math.deg(pitch))
|
||||
end
|
||||
|
||||
Event.addRoutine(function()
|
||||
while true do
|
||||
local target = Util.find(ni.sense(), 'name', TARGET)
|
||||
if target then
|
||||
look(target)
|
||||
ni.shoot()
|
||||
end
|
||||
os.sleep(0)
|
||||
end
|
||||
end)
|
||||
|
||||
Event.pullEvents()
|
||||
Reference in New Issue
Block a user