robot wars
This commit is contained in:
@@ -1,8 +1,3 @@
|
|||||||
_G.requireInjector(_ENV)
|
|
||||||
|
|
||||||
local Event = require('event')
|
|
||||||
local Util = require('util')
|
|
||||||
|
|
||||||
local os = _G.os
|
local os = _G.os
|
||||||
local peripheral = _G.peripheral
|
local peripheral = _G.peripheral
|
||||||
|
|
||||||
@@ -11,25 +6,69 @@ if not ni then
|
|||||||
error("Cannot find neuralInterface")
|
error("Cannot find neuralInterface")
|
||||||
end
|
end
|
||||||
|
|
||||||
local TARGET = 'joebodo'
|
local RADIUS = 6
|
||||||
|
local ROTATION = math.pi / 16
|
||||||
|
local args = { ... }
|
||||||
|
|
||||||
local function look(entity)
|
local TARGET = args[1] or error('Syntax: robotWars <targetName>')
|
||||||
local x, y, z = entity.x, entity.y, entity.z
|
|
||||||
|
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 pitch = -math.atan2(y, math.sqrt(x * x + z * z))
|
||||||
local yaw = math.atan2(-x, z)
|
local yaw = math.atan2(-(x - .5), z - .5)
|
||||||
|
|
||||||
ni.look(math.deg(yaw), math.deg(pitch))
|
return math.deg(yaw), math.deg(pitch)
|
||||||
end
|
end
|
||||||
|
|
||||||
Event.addRoutine(function()
|
local function getUid()
|
||||||
while true do
|
for _,v in pairs(ni.sense()) do
|
||||||
local target = Util.find(ni.sense(), 'name', TARGET)
|
if math.floor(v.x) == 0 and
|
||||||
if target then
|
math.floor(v.z) == 0 then
|
||||||
look(target)
|
return v.id
|
||||||
ni.shoot()
|
end
|
||||||
end
|
end
|
||||||
os.sleep(0)
|
error('Could not find myself')
|
||||||
end
|
end
|
||||||
end)
|
|
||||||
|
|
||||||
Event.pullEvents()
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user