View difference between Paste ID: g7hhvWvg and rsMaWzk4
SHOW: | | - or go back to the newest paste.
1
local sensor = peripheral.wrap "left"
2
local laser = peripheral.wrap "right"
3
4
local function fire(entity)
5
	local x, y, z = entity.x, entity.y, entity.z
6
	local pitch = -math.atan2(y, math.sqrt(x * x + z * z))
7
	local yaw = math.atan2(-x, z)
8
9
	laser.fire(math.deg(yaw), math.deg(pitch), 5)
10
	sleep(0.2)
11
end
12
13-
local mobNames = { "Creeper", "Zombie", "Skeleton", "Blaze"}
13+
local mob_names = { "Creeper", "Zombie", "Skeleton", "Blaze"}
14-
local mobLookup = {}
14+
local mob_lookup = {}
15-
for i = 1, #mobNames do
15+
for _, mob in pairs(mob_names) do
16-
	mobLookup[mobNames[i]] = true
16+
	mob_lookup[mob] = true
17
end
18
19
local function distance(entity)
20
	return math.sqrt(entity.x * entity.x + entity.y * entity.y + entity.z * entity.z)
21-
	local candidates = {}
21+
22-
	for i = 1, #mobs do
22+
23-
		local mob = mobs[i]
23+
24-
		if mobLookup[mob.name] then
24+
25-
			candidates[#candidates + 1] = mob
25+
	local nearest
26
	for _, mob in pairs(mobs) do
27
		if mob_lookup[mob.name] then
28-
	if #candidates > 0 then
28+
			mob.distance = distance(mob)
29-
		local mob = candidates[math.random(1, #candidates)]
29+
			if nearest == nil or mob.distance < nearest.distance then
30-
		fire(mob)
30+
				nearest = mob
31
			end
32
		end
33
	end
34
	if nearest then
35
		fire(entity)
36
	else
37
		sleep(0.5)
38
	end
39
end