1
0
mirror of https://github.com/osmarks/random-stuff synced 2024-11-09 22:09:55 +00:00
random-stuff/computercraft/nilaser.lua

41 lines
1.2 KiB
Lua
Raw Normal View History

2023-11-13 19:37:05 +00:00
local ni = peripheral.find "neuralInterface"
if not ni or not ni.fire or not ni.sense then error "Neural interface with laser and entity sensor required!" end
local args = {...}
local power = table.remove(args, 1)
local num_power = tonumber(power)
if num_power then power = num_power end
local follow_mode, stare_mode = power == "follow", power == "stare"
local laser_mode = not (follow_mode or stare_mode)
if #args == 0 or not power then
error([[Usage: nilaser <power> <patterns to match targets...>]])
end
local function calc_yaw_pitch(x, y, z)
local pitch = -math.atan2(y, math.sqrt(x * x + z * z))
local yaw = math.atan2(-x, z)
return math.deg(yaw), math.deg(pitch)
end
local function is_target(entity)
for _, pattern in pairs(args) do
if entity.name:match(pattern) or entity.displayName:match(pattern) then return true end
end
return false
end
while true do
for _, entity in pairs(ni.sense()) do
if is_target(entity) then
print(entity.name, entity.displayName)
local y, p = calc_yaw_pitch(entity.x, entity.y, entity.z)
if laser_mode then
ni.fire(y, p, power)
elseif follow_mode then
ni.launch(y, p, 1)
elseif stare_mode then
ni.look(y, p)
end
end
end
sleep(0.05)
end