Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local sensor = peripheral.wrap "left"
- local laser = peripheral.wrap "right"
- local function fire(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)
- laser.fire(math.deg(yaw), math.deg(pitch), 5)
- sleep(0.2)
- end
- local mob_names = { "Creeper", "Zombie", "Skeleton", "Blaze"}
- local mob_lookup = {}
- for _, mob in pairs(mob_names) do
- mob_lookup[mob] = true
- end
- local function distance(entity)
- return math.sqrt(entity.x * entity.x + entity.y * entity.y + entity.z * entity.z)
- end
- while true do
- local mobs = sensor.sense()
- local nearest
- for _, mob in pairs(mobs) do
- if mob_lookup[mob.name] then
- mob.distance = distance(mob)
- if nearest == nil or mob.distance < nearest.distance then
- nearest = mob
- end
- end
- end
- if nearest then
- fire(entity)
- else
- sleep(0.5)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement